diff --git a/src/main/java/app/controllers/user/KillFeedController.java b/src/main/java/app/controllers/user/KillFeedController.java index 49958a2..246a89c 100644 --- a/src/main/java/app/controllers/user/KillFeedController.java +++ b/src/main/java/app/controllers/user/KillFeedController.java @@ -60,11 +60,12 @@ public class KillFeedController { @RequestParam(required = false) String steam64, @RequestParam(required = false) String srv, @RequestParam(required = false, defaultValue = "0") Integer offset, - @RequestParam(required = false, defaultValue = "20") Integer limit) { + @RequestParam(required = false, defaultValue = "20") Integer limit, + @RequestParam(required = false, defaultValue = "false") boolean current) { SteamID steamID = steam64 == null || steam64.isEmpty() ? SteamIDConverter.getSteamID(mysteam64) : SteamIDConverter.getSteamID(steam64); if (steamID == null) return new ResponseEntity(HttpStatus.NOT_FOUND); if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); - return new ResponseEntity(killfeedService.getKills(steamID, srv, offset, limit), HttpStatus.OK); + return new ResponseEntity(killfeedService.getKills(steamID, srv, offset, limit, current), HttpStatus.OK); } @PutMapping @@ -76,11 +77,12 @@ public class KillFeedController { @RequestParam(required = false) String steam64, @RequestParam(required = false) String srv, @RequestParam(required = false, defaultValue = "0") Integer offset, - @RequestParam(required = false, defaultValue = "20") Integer limit) { + @RequestParam(required = false, defaultValue = "20") Integer limit, + @RequestParam(required = false, defaultValue = "false") boolean current) { SteamID steamID = steam64 == null || steam64.isEmpty() ? SteamIDConverter.getSteamID(mysteam64) : SteamIDConverter.getSteamID(steam64); if (steamID == null) return new ResponseEntity(HttpStatus.NOT_FOUND); if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); - return new ResponseEntity(killfeedService.getAssists(steamID, srv, offset, limit), HttpStatus.OK); + return new ResponseEntity(killfeedService.getAssists(steamID, srv, offset, limit, current), HttpStatus.OK); } @DeleteMapping @@ -92,11 +94,12 @@ public class KillFeedController { @RequestParam(required = false) String steam64, @RequestParam(required = false) String srv, @RequestParam(required = false, defaultValue = "0") Integer offset, - @RequestParam(required = false, defaultValue = "20") Integer limit) { + @RequestParam(required = false, defaultValue = "20") Integer limit, + @RequestParam(required = false, defaultValue = "false") boolean current) { SteamID steamID = steam64 == null || steam64.isEmpty() ? SteamIDConverter.getSteamID(mysteam64) : SteamIDConverter.getSteamID(steam64); if (steamID == null) return new ResponseEntity(HttpStatus.NOT_FOUND); if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); - return new ResponseEntity(killfeedService.getDeads(steamID, srv, offset, limit), HttpStatus.OK); + return new ResponseEntity(killfeedService.getDeads(steamID, srv, offset, limit, current), HttpStatus.OK); } @DeleteMapping("/top") diff --git a/src/main/java/app/entities/PlayerProfile.java b/src/main/java/app/entities/PlayerProfile.java index 51c5826..82c2a5c 100644 --- a/src/main/java/app/entities/PlayerProfile.java +++ b/src/main/java/app/entities/PlayerProfile.java @@ -30,6 +30,7 @@ public class PlayerProfile { List donates; List ban_list; Killfeed killfeed; + Killfeed killfeed_current; Long messages; ReportCount reports; } diff --git a/src/main/java/app/services/ProfileService.java b/src/main/java/app/services/ProfileService.java index 95e5300..bd58b49 100644 --- a/src/main/java/app/services/ProfileService.java +++ b/src/main/java/app/services/ProfileService.java @@ -213,22 +213,42 @@ public class ProfileService { }); } - if(requests.contains("killfeed")){ + if(requests.contains("killfeed")) { callables.add(() -> { try { long start_time = Instant.now().toEpochMilli(); profile.setKillfeed((new Killfeed()) - .setKills(killfeedService.getKills(steamID, null)) - .setDeads(killfeedService.getDeads(steamID, null)) - .setAssists(killfeedService.getAssists(steamID, null)) - .setSuicides(killfeedService.getSuicides(steamID, null))); + .setKills(killfeedService.getKills(steamID, null, null)) + .setDeads(killfeedService.getDeads(steamID, null, null)) + .setAssists(killfeedService.getAssists(steamID, null, null)) + .setSuicides(killfeedService.getSuicides(steamID, null, null))); long end_time = Instant.now().toEpochMilli() - start_time; profile.getResponse_time().put("killfeed", (double) end_time / 1000); } catch (Exception err) { err.printStackTrace(); } return null; - }); + }); + + callables.add(() -> { + try { + long start_time = Instant.now().toEpochMilli(); + Long currentPlayerTimeSession = statsService.searchCurrentPlayerStartTime(steamID); + if (currentPlayerTimeSession != null) { + Long ses_start = Instant.now().getEpochSecond() - currentPlayerTimeSession; + profile.setKillfeed_current((new Killfeed()) + .setKills(killfeedService.getKills(steamID, null, ses_start)) + .setDeads(killfeedService.getDeads(steamID, null, ses_start)) + .setAssists(killfeedService.getAssists(steamID, null, ses_start)) + .setSuicides(killfeedService.getSuicides(steamID, null, ses_start))); + } + long end_time = Instant.now().toEpochMilli() - start_time; + profile.getResponse_time().put("killfeed_current", (double) end_time / 1000); + } catch (Exception err) { + err.printStackTrace(); + } + return null; + }); } if(requests.contains("reports")){ diff --git a/src/main/java/app/services/StatsService.java b/src/main/java/app/services/StatsService.java index 7b6e084..75a59ed 100644 --- a/src/main/java/app/services/StatsService.java +++ b/src/main/java/app/services/StatsService.java @@ -6,6 +6,7 @@ import app.entities.other.SteamID; import app.entities.server.PlayOn; import app.entities.server.Server; import app.entities.server.players.RCONPlayer; +import app.entities.server.request.PlayerOnServer; import app.services.io.readers.GeoIP; import app.utils.CryptedCookie; import org.springframework.beans.factory.annotation.Autowired; @@ -39,6 +40,21 @@ public class StatsService { return null; } + private PlayerOnServer searchCurrentPlayer(SteamID steamID) { + for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { + RCONPlayer player = stringServerEntry.getValue().searchPlayer(steamID); + if (player != null) return (PlayerOnServer) player; + } + return null; + } + + public Long searchCurrentPlayerStartTime(SteamID steamID) { + PlayerOnServer player = searchCurrentPlayer(steamID); + if (player != null) { + return Integer.valueOf(player.getDuration_seconds()).longValue(); + } else return null; + } + public SteamID searchPlayer(String player_nickname){ for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { SteamID steamID = stringServerEntry.getValue().searchPlayer(player_nickname); diff --git a/src/main/java/app/services/db/KillfeedService.java b/src/main/java/app/services/db/KillfeedService.java index 3cc69ec..730d925 100644 --- a/src/main/java/app/services/db/KillfeedService.java +++ b/src/main/java/app/services/db/KillfeedService.java @@ -2,6 +2,7 @@ package app.services.db; import app.entities.Stats; import app.entities.other.SteamID; +import app.services.StatsService; import lombok.Data; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; @@ -11,6 +12,7 @@ import org.springframework.stereotype.Service; import java.sql.ResultSet; import java.sql.SQLException; +import java.time.Instant; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -23,15 +25,18 @@ public class KillfeedService { private JdbcTemplate jdbcTemplate; private Stats stats; private NicknameService nicknameService; + private StatsService statsService; - KillfeedService(Stats stats, NicknameService nicknameService) { + @Autowired + KillfeedService(Stats stats, NicknameService nicknameService, StatsService statsService) { this.stats = stats; this.nicknameService = nicknameService; + this.statsService = statsService; } - public Long getKills(SteamID steamID, String server_id) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `attacker_id` = ? AND `attacker_id` != `victim_id` AND `server_id` like ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + public Long getKills(SteamID steamID, String server_id, Long utime) { + return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `utime` > ? AND `attacker_id` = ? AND `attacker_id` != `victim_id` AND `server_id` like ?", + new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } @@ -42,9 +47,9 @@ public class KillfeedService { (rs, n) -> new TopInFeed(rs).setNicknames(nicknameService)); } - public Long getDeads(SteamID steamID, String server_id) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `victim_id` = ? AND `attacker_id` != `victim_id` AND `server_id` like ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + public Long getDeads(SteamID steamID, String server_id, Long utime) { + return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `utime` > ? AND `victim_id` = ? AND `attacker_id` != `victim_id` AND `server_id` like ?", + new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } @@ -55,16 +60,16 @@ public class KillfeedService { (rs, n) -> new TopInFeed(rs).setNicknames(nicknameService)); } - public Long getSuicides(SteamID steamID, String server_id) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `victim_id` = ? AND `attacker_id` = `victim_id` AND `server_id` like ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + public Long getSuicides(SteamID steamID, String server_id, Long utime) { + return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `utime` > ? AND `victim_id` = ? AND `attacker_id` = `victim_id` AND `server_id` like ?", + new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } - public Long getAssists(SteamID steamID, String server_id) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `assister_id` = ? AND `server_id` like ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id}, + public Long getAssists(SteamID steamID, String server_id, Long utime) { + return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `utime` > ? AND `assister_id` = ? AND `server_id` like ?", + new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id}, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } @@ -81,38 +86,59 @@ public class KillfeedService { return Map.of("result", result, "count", -1); } - public Map getKills(SteamID steamID, String server_id, int offset, int limit) { - List result = jdbcTemplate.query("SELECT -1, u.victim_id, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.`attacker_id` = ? AND u.`attacker_id` != u.`victim_id` AND u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", - new Object[]{steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, + public Map getKills(SteamID steamID, String server_id, int offset, int limit, boolean current) { + long current_utime = 0L; + if (current) { + Long currentPlayerSesTime = statsService.searchCurrentPlayerStartTime(steamID); + if (currentPlayerSesTime != null) current_utime = Instant.now().getEpochSecond() - currentPlayerSesTime; + else current_utime = Instant.now().getEpochSecond(); + } + + List result = jdbcTemplate.query("SELECT -1, u.victim_id, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.utime > ? AND u.`attacker_id` = ? AND u.`attacker_id` != u.`victim_id` AND u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", + new Object[]{ current_utime, steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, (rs, n) -> new KillsInFeed(rs).setNicknames(nicknameService)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `attacker_id` = ? AND `attacker_id` != `victim_id` AND `server_id` like ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE utime > ? AND `attacker_id` = ? AND `attacker_id` != `victim_id` AND `server_id` like ?", + new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); return Map.of("result", result, "count", count, "type", FeedType.KILL.ordinal()); } - public Map getDeads(SteamID steamID, String server_id, int offset, int limit) { - List result = jdbcTemplate.query("SELECT u.attacker_id, -1, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.`victim_id` = ? AND u.`attacker_id` != u.`victim_id` AND u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", - new Object[]{steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, + public Map getDeads(SteamID steamID, String server_id, int offset, int limit, boolean current) { + long current_utime = 0L; + if (current) { + Long currentPlayerSesTime = statsService.searchCurrentPlayerStartTime(steamID); + if (currentPlayerSesTime != null) current_utime = Instant.now().getEpochSecond() - currentPlayerSesTime; + else current_utime = Instant.now().getEpochSecond(); + } + + List result = jdbcTemplate.query("SELECT u.attacker_id, -1, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.utime > ? AND u.`victim_id` = ? AND u.`attacker_id` != u.`victim_id` AND u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", + new Object[]{ current_utime, steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, (rs, n) -> new KillsInFeed(rs).setNicknames(nicknameService)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `victim_id` = ? AND `attacker_id` != `victim_id` AND `server_id` like ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE utime > ? AND `victim_id` = ? AND `attacker_id` != `victim_id` AND `server_id` like ?", + new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")).stream().findFirst().orElse(0L); return Map.of("result", result, "count", count, "type", FeedType.DEAD.ordinal()); } - public Map getAssists(SteamID steamID, String server_id, int offset, int limit) { - List result = jdbcTemplate.query("SELECT u.attacker_id, u.victim_id, -1, u.utime, i.name, u.server_id, u.weapon_classname FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.`assister_id` = ? AND u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", - new Object[]{steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, + public Map getAssists(SteamID steamID, String server_id, int offset, int limit, boolean current) { + long current_utime = 0L; + if (current) { + Long currentPlayerSesTime = statsService.searchCurrentPlayerStartTime(steamID); + if (currentPlayerSesTime != null) current_utime = Instant.now().getEpochSecond() - currentPlayerSesTime; + else current_utime = Instant.now().getEpochSecond(); + } + + List result = jdbcTemplate.query("SELECT u.attacker_id, u.victim_id, -1, u.utime, i.name, u.server_id, u.weapon_classname FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.utime > ? AND u.`assister_id` = ? AND u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", + new Object[]{ current_utime, steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, limit, offset}, (rs, n) -> new KillsInFeed(rs).setNicknames(nicknameService)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE `assister_id` = ? AND `server_id` like ?", - new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, + Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_killfeed` WHERE utime > ? AND `assister_id` = ? AND `server_id` like ?", + new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L);