diff --git a/src/main/java/app/controllers/user/KillFeedController.java b/src/main/java/app/controllers/user/KillFeedController.java new file mode 100644 index 0000000..b4bae68 --- /dev/null +++ b/src/main/java/app/controllers/user/KillFeedController.java @@ -0,0 +1,54 @@ +package app.controllers.user; + +import app.annotations.enums.AuthMethod; +import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.WaitAfterNext; +import app.entities.other.SteamID; +import app.services.db.KillfeedService; +import app.utils.SteamIDConverter; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("api/profile/killfeed") +public class KillFeedController { + private KillfeedService killfeedService; + + @Autowired + KillFeedController(KillfeedService killfeedService) { + this.killfeedService = killfeedService; + } + + @GetMapping("/weapons") + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "weapons") + public ResponseEntity getPopulateWeapons(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.getPopulateWeapons(steamID, srv, offset, limit), HttpStatus.OK); + } + + @GetMapping + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "kills") + public ResponseEntity getKills(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.getKills(steamID, srv, offset, limit), HttpStatus.OK); + } +} diff --git a/src/main/java/app/controllers/user/MessagesController.java b/src/main/java/app/controllers/user/MessagesController.java new file mode 100644 index 0000000..4953b9a --- /dev/null +++ b/src/main/java/app/controllers/user/MessagesController.java @@ -0,0 +1,52 @@ +package app.controllers.user; + +import app.annotations.enums.AuthMethod; +import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.WaitAfterNext; +import app.entities.other.SteamID; +import app.services.db.MessageService; +import app.utils.SteamIDConverter; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/profile/messages") +public class MessagesController { + private MessageService messageService; + + @Autowired + MessagesController(MessageService messageService) { + this.messageService = messageService; + } + + @PostMapping + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "messages") + public ResponseEntity getMessages(HttpServletRequest request, + @RequestBody(required = false) String filter, + @RequestParam(required = false) String srv, + @RequestParam(required = false, defaultValue = "0") Integer offset, + @RequestParam(required = false, defaultValue = "20") Integer limit) { + if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); + return new ResponseEntity(messageService.getAllMessages(srv, filter, offset, limit), HttpStatus.OK); + } + + @PostMapping("/account") + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "messages") + public ResponseEntity getAccountMessages(HttpServletRequest request, + @CookieValue(value = "steam64", defaultValue = "") String mysteam64, + @RequestParam(required = false) String steam64, + @RequestBody(required = false) String filter, + @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(messageService.getAccountMessages(steamID, srv, filter, offset, limit), HttpStatus.OK); + } +} diff --git a/src/main/java/app/entities/db/Killfeed.java b/src/main/java/app/entities/db/Killfeed.java index f83399e..ced5191 100644 --- a/src/main/java/app/entities/db/Killfeed.java +++ b/src/main/java/app/entities/db/Killfeed.java @@ -7,8 +7,10 @@ public class Killfeed { long kills; long deads; long assists; + long suicides; public Killfeed setKills(Long count) {this.kills = count; return this;} public Killfeed setDeads(Long count) {this.deads = count; return this;} public Killfeed setAssists(Long count) {this.assists = count; return this;} + public Killfeed setSuicides(Long count) {this.suicides = count; return this;} } \ No newline at end of file diff --git a/src/main/java/app/services/ProfileService.java b/src/main/java/app/services/ProfileService.java index a90914c..d76588d 100644 --- a/src/main/java/app/services/ProfileService.java +++ b/src/main/java/app/services/ProfileService.java @@ -140,7 +140,8 @@ public class ProfileService { profile.setKillfeed((new Killfeed()) .setKills(killfeedService.getKills(steamID, null)) .setDeads(killfeedService.getDeads(steamID, null)) - .setAssists(killfeedService.getAssists(steamID, null))); + .setAssists(killfeedService.getAssists(steamID, null)) + .setSuicides(killfeedService.getSuicides(steamID, null))); end_time = Instant.now().toEpochMilli() - start_time; profile.getResponse_time().put("killfeed", Double.valueOf(end_time) / 1000); //} diff --git a/src/main/java/app/services/db/KillfeedService.java b/src/main/java/app/services/db/KillfeedService.java index e27b033..6e2f60b 100644 --- a/src/main/java/app/services/db/KillfeedService.java +++ b/src/main/java/app/services/db/KillfeedService.java @@ -12,7 +12,9 @@ import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.util.HashMap; import java.util.List; +import java.util.Map; @Service @Transactional("RoTransactionManager") @@ -28,44 +30,60 @@ public class KillfeedService { this.nicknameService = nicknameService; } - // - public List getPopulalatedWeapons(SteamID steamID, String server_id) { - return entityManager.createNativeQuery("SELECT COUNT(u.`weapon_index`) as c, i.`name` 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.server_id like ?2 GROUP BY u.`weapon_index` DESC ORDER BY `c` DESC") - .setParameter(1, steamID.account_id) - .setParameter(2, server_id==null?'%':server_id) - .getResultStream().map(obj -> new HypeWeapons(obj)).toList(); - } - public Long getKills(SteamID steamID, String server_id) { return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_killfeed` WHERE `attacker_id` = ?1 AND `attacker_id` != `victim_id` AND `server_id` like ?2") .setParameter(1, steamID.account_id) - .setParameter(2, server_id==null?'%':server_id) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) .getSingleResult(); } public Long getDeads(SteamID steamID, String server_id) { return (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) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) + .getSingleResult(); + } + + public Long getSuicides(SteamID steamID, String server_id) { + return (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(); } public Long getAssists(SteamID steamID, String server_id) { return (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) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) + .getSingleResult(); + } + + public Map getPopulateWeapons(SteamID steamID, String server_id, int offset, int limit) { + List result = entityManager.createNativeQuery("SELECT COUNT(u.`weapon_index`) as c, i.`name` FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.`attacker_id` = ?1 AND `attacker_id` != `victim_id` AND u.server_id like ?2 GROUP BY u.`weapon_index` DESC ORDER BY `c` 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 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`") + .setParameter(1, steamID.account_id) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) .getSingleResult(); + return Map.of("result", result, "count", count); } - //; - public List getKills(SteamID steamID, String server_id, int offset, int limit) { + 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") .setParameter(1, steamID.account_id) - .setParameter(2, server_id==null?'%':server_id) + .setParameter(2, server_id==null||server_id.isEmpty()?'%':server_id) .setParameter(3, limit) .setParameter(4, offset) .getResultStream().map(obj -> new KillsInFeed(obj)).toList(); - return result.stream().peek(kif -> kif.setNicknames(nicknameService)).toList(); + Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_killfeed` WHERE `attacker_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); } @Data diff --git a/src/main/java/app/services/db/MessageService.java b/src/main/java/app/services/db/MessageService.java index 9fe6742..d9a3ff9 100644 --- a/src/main/java/app/services/db/MessageService.java +++ b/src/main/java/app/services/db/MessageService.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.Map; @Service @Transactional("RoTransactionManager") @@ -24,19 +25,40 @@ public class MessageService { this.nicknameService = nicknameService; } - public List getMessages(String server_id, int offset, int limit) { - List result = entityManager.createNativeQuery("SELECT `account_id`,`utime`,`message`,`server_id` FROM `user_messages` WHERE `server_id` like ?1 ORDER BY `user_messages`.`id` DESC LIMIT ?2 OFFSET ?3") - .setParameter(1, server_id==null?'%':server_id) - .setParameter(2, limit) - .setParameter(3, offset) + public Map getAllMessages(String server_id, String filter, int offset, int limit) { + List result = entityManager.createNativeQuery("SELECT `account_id`,`utime`,`message`,`server_id` FROM `user_messages` WHERE `server_id` like ?1 AND `message` LIKE ?2 ORDER BY `user_messages`.`id` DESC LIMIT ?3 OFFSET ?4") + .setParameter(1, server_id==null||server_id.isEmpty()?"%":server_id) + .setParameter(2, filter==null||filter.isEmpty()?"%":"%" + filter + "%") + .setParameter(3, limit) + .setParameter(4, offset) .getResultStream().map(obj -> new Message(obj)).toList(); - return result.stream().peek(message -> message.setAccount_name(nicknameService)).toList(); + Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_messages` WHERE `server_id` like ?1 AND `message` LIKE ?2") + .setParameter(1, server_id==null||server_id.isEmpty()?"%":server_id) + .setParameter(2, filter==null||filter.isEmpty()?"%":"%" + filter + "%") + .getSingleResult(); + return Map.of("result", result.stream().peek(message -> message.setAccount_name(nicknameService)).toList(), "count", count); + } + + public Map getAccountMessages(SteamID steamID, String server_id, String filter, int offset, int limit) { + List result = entityManager.createNativeQuery("SELECT `account_id`,`utime`,`message`,`server_id` FROM `user_messages` WHERE `account_id` = ?1 AND `server_id` like ?2 AND `message` LIKE ?3 ORDER BY `user_messages`.`id` DESC LIMIT ?4 OFFSET ?5") + .setParameter(1, steamID.account_id) + .setParameter(2, server_id==null||server_id.isEmpty()?"%":server_id) + .setParameter(3, filter==null||filter.isEmpty()?"%":"%" + filter + "%") + .setParameter(4, limit) + .setParameter(5, offset) + .getResultStream().map(obj -> new Message(obj)).toList(); + Long count = (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_messages` WHERE `account_id` = ?1 AND `server_id` like ?2 AND `message` LIKE ?3") + .setParameter(1, steamID.account_id) + .setParameter(2, server_id==null||server_id.isEmpty()?"%":server_id) + .setParameter(3, filter==null||filter.isEmpty()?"%":"%" + filter + "%") + .getSingleResult(); + return Map.of("result", result.stream().peek(message -> message.setAccount_name(nicknameService)).toList(), "count", count); } public Long getMessageCount(SteamID steamID, String server_id) { return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_messages` WHERE `account_id` = ?1 AND `server_id` like ?2") .setParameter(1, steamID.account_id) - .setParameter(2, server_id==null?"%":server_id) + .setParameter(2, server_id==null||server_id.isEmpty()?"%":server_id) .getSingleResult(); }