diff --git a/src/main/java/app/controllers/user/KillFeedController.java b/src/main/java/app/controllers/user/KillFeedController.java index b4bae68..d79181a 100644 --- a/src/main/java/app/controllers/user/KillFeedController.java +++ b/src/main/java/app/controllers/user/KillFeedController.java @@ -51,4 +51,34 @@ public class KillFeedController { if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); return new ResponseEntity(killfeedService.getKills(steamID, srv, offset, limit), HttpStatus.OK); } + + @PutMapping + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "assists") + public ResponseEntity getAssists(HttpServletRequest request, + @CookieValue(value = "steam64", defaultValue = "") String mysteam64, + @RequestParam(required = false) String steam64, + @RequestParam(required = false) String srv, + @RequestParam(required = false, defaultValue = "0") Integer offset, + @RequestParam(required = false, defaultValue = "20") Integer limit) { + 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); + } + + @DeleteMapping + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "deads") + public ResponseEntity getDeads(HttpServletRequest request, + @CookieValue(value = "steam64", defaultValue = "") String mysteam64, + @RequestParam(required = false) String steam64, + @RequestParam(required = false) String srv, + @RequestParam(required = false, defaultValue = "0") Integer offset, + @RequestParam(required = false, defaultValue = "20") Integer limit) { + 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); + } } diff --git a/src/main/java/app/services/db/KillfeedService.java b/src/main/java/app/services/db/KillfeedService.java index 6e2f60b..63fcdfb 100644 --- a/src/main/java/app/services/db/KillfeedService.java +++ b/src/main/java/app/services/db/KillfeedService.java @@ -65,15 +65,15 @@ public class KillfeedService { .setParameter(3, limit) .setParameter(4, offset) .getResultStream().map(obj -> new HypeWeapons(obj)).toList(); - Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(`weapon_index`) FROM `user_killfeed` WHERE `attacker_id` = ?1 AND server_id like ?2 GROUP BY `weapon_index`") + /*Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(`weapon_index`) FROM `user_killfeed` WHERE `attacker_id` = ?1 AND server_id like ?2 GROUP BY `weapon_index`") .setParameter(1, steamID.account_id) .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) - .getSingleResult(); - return Map.of("result", result, "count", count); + .getSingleResult();*/ + return Map.of("result", result, "count", -1); } public Map getKills(SteamID steamID, String server_id, int offset, int limit) { - List result = entityManager.createNativeQuery("SELECT u.victim_id, u.assister_id, u.utime, i.name, u.server_id FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE `attacker_id` = ?1 AND `attacker_id` != `victim_id` AND `server_id` like ?2 ORDER BY u.`id` DESC LIMIT ?3 OFFSET ?4") + List result = entityManager.createNativeQuery("SELECT -1, u.victim_id, u.assister_id, u.utime, i.name, u.server_id FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.`attacker_id` = ?1 AND u.`attacker_id` != u.`victim_id` AND u.`server_id` like ?2 ORDER BY u.`id` DESC LIMIT ?3 OFFSET ?4") .setParameter(1, steamID.account_id) .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) .setParameter(3, limit) @@ -83,7 +83,35 @@ public class KillfeedService { .setParameter(1, steamID.account_id) .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) .getSingleResult(); - return Map.of("result", result.stream().peek(kif -> kif.setNicknames(nicknameService)).toList(), "count", count); + return Map.of("result", result.stream().peek(kif -> kif.setNicknames(nicknameService)).toList(), "count", count, "type", FeedType.KILL.ordinal()); + } + + public Map getDeads(SteamID steamID, String server_id, int offset, int limit) { + List result = entityManager.createNativeQuery("SELECT u.attacker_id, -1, u.assister_id, u.utime, i.name, u.server_id FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.`victim_id` = ?1 AND u.`attacker_id` != u.`victim_id` AND u.`server_id` like ?2 ORDER BY u.`id` DESC LIMIT ?3 OFFSET ?4") + .setParameter(1, steamID.account_id) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) + .setParameter(3, limit) + .setParameter(4, offset) + .getResultStream().map(obj -> new KillsInFeed(obj)).toList(); + Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_killfeed` WHERE `victim_id` = ?1 AND `attacker_id` != `victim_id` AND `server_id` like ?2") + .setParameter(1, steamID.account_id) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) + .getSingleResult(); + return Map.of("result", result.stream().peek(kif -> kif.setNicknames(nicknameService)).toList(), "count", count, "type", FeedType.DEAD.ordinal()); + } + + public Map getAssists(SteamID steamID, String server_id, int offset, int limit) { + List result = entityManager.createNativeQuery("SELECT u.attacker_id, u.victim_id, -1, u.utime, i.name, u.server_id FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.`assister_id` = ?1 AND u.`server_id` like ?2 ORDER BY u.`id` DESC LIMIT ?3 OFFSET ?4") + .setParameter(1, steamID.account_id) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) + .setParameter(3, limit) + .setParameter(4, offset) + .getResultStream().map(obj -> new KillsInFeed(obj)).toList(); + Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_killfeed` WHERE `assister_id` = ?1 AND `server_id` like ?2") + .setParameter(1, steamID.account_id) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) + .getSingleResult(); + return Map.of("result", result.stream().peek(kif -> kif.setNicknames(nicknameService)).toList(), "count", count, "type", FeedType.ASSIST.ordinal()); } @Data @@ -96,38 +124,34 @@ public class KillfeedService { } } + enum FeedType { + KILL, DEAD, ASSIST + } + @Data class KillsInFeed { + int attacker_id; + String attacker_name; int victim_id; String victim_name; int assister_id; String assister_name; long utime; String weapon_name; - private String server_id; + String server_id; KillsInFeed(Object obj) { - this.victim_id = (int) ((Object[]) obj)[0]; - this.assister_id = (int) ((Object[]) obj)[1]; - this.utime = (long) ((Object[]) obj)[2]; - this.weapon_name = (String) ((Object[]) obj)[3]; - this.server_id = (String) ((Object[]) obj)[4]; + this.attacker_id = (int) ((Object[]) obj)[0]; + this.victim_id = (int) ((Object[]) obj)[1]; + this.assister_id = (int) ((Object[]) obj)[2]; + this.utime = (long) ((Object[]) obj)[3]; + this.weapon_name = (String) ((Object[]) obj)[4]; + this.server_id = (String) ((Object[]) obj)[5]; } public void setNicknames(NicknameService nicknameService) { - this.victim_name = nicknameService.grabNickname(victim_id, server_id); + this.attacker_name = attacker_id == -1?"":nicknameService.grabNickname(attacker_id, server_id); + this.victim_name = victim_id == -1?"":nicknameService.grabNickname(victim_id, server_id); this.assister_name = assister_id == -1?"":nicknameService.grabNickname(assister_id, server_id); } - - @JsonValue - @JsonSerialize(using = ToStringSerializer.class) - public long victim_steam64() { - return SteamIDConverter.getSteamID(victim_id).steam64; - } - - @JsonValue - @JsonSerialize(using = ToStringSerializer.class) - public long assister_steam64() { - return assister_id == -1?-1:SteamIDConverter.getSteamID(assister_id).steam64; - } } }