From a432fc344a06948340ca7e2948b5485e293d833b Mon Sep 17 00:00:00 2001 From: gsd Date: Mon, 30 Oct 2023 00:34:50 +0300 Subject: [PATCH] usertime upd --- .../controllers/user/DetailController.java | 18 ++++++- src/main/java/app/entities/db/Gametime.java | 52 +++++++++++++++++++ .../java/app/services/ProfileService.java | 2 +- .../java/app/services/db/UsertimeService.java | 23 ++++++++ 4 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/main/java/app/entities/db/Gametime.java diff --git a/src/main/java/app/controllers/user/DetailController.java b/src/main/java/app/controllers/user/DetailController.java index ef0b280..7155ac3 100644 --- a/src/main/java/app/controllers/user/DetailController.java +++ b/src/main/java/app/controllers/user/DetailController.java @@ -6,6 +6,7 @@ import app.annotations.interfaces.CheckWebAccess; import app.annotations.interfaces.WaitAfterNext; import app.entities.other.SteamID; import app.services.ProfileService; +import app.utils.SteamIDConverter; import jakarta.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -74,5 +75,20 @@ public class DetailController { return new ResponseEntity<>(profileService.GetSteamIDFromAnyData(container.get("any")), HttpStatus.OK); } - + @GetMapping("/gametime/web") + @CheckWebAccess(auth_method = AuthMethod.STEAM64) + @WaitAfterNext(order = "gametime") + public ResponseEntity GetGametime4Web(HttpServletRequest request, + @RequestParam String steam64, + @RequestParam String srv, + @RequestParam(required = false, defaultValue = "0") Integer offset, + @RequestParam(required = false, defaultValue = "20") Integer limit) { + SteamID steamID = SteamIDConverter.getSteamID(steam64); + if (steamID == null) return new ResponseEntity(HttpStatus.NOT_FOUND); + if (limit > 20) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); + return new ResponseEntity<>(new HashMap<>(){{ + put("array", profileService.usertimeService.getGametimeOnServer(steamID, srv, limit, offset)); + put("total", profileService.usertimeService.getTotalGametimeOnServer(steamID, srv)); + }}, HttpStatus.OK); + } } diff --git a/src/main/java/app/entities/db/Gametime.java b/src/main/java/app/entities/db/Gametime.java new file mode 100644 index 0000000..327097f --- /dev/null +++ b/src/main/java/app/entities/db/Gametime.java @@ -0,0 +1,52 @@ +package app.entities.db; + +import com.fasterxml.jackson.annotation.JsonGetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; + +import java.sql.Timestamp; + +@Data +public class Gametime { + + @JsonIgnore + int id; + String player_name; + @JsonIgnore + String steam_id; + @JsonIgnore + String connect_ip; + @JsonIgnore + long account_id; + @JsonIgnore + Timestamp timestamp; + @JsonIgnore + String map; + @JsonIgnore + String connection_type; + int connect_duration; + String reason; + + public Gametime(Object[] obj) { + id = (int) obj[0]; + player_name = (String) obj[1]; + steam_id = (String) obj[2]; + connect_ip = (String) obj[3]; + account_id = (long) obj[4]; + timestamp = (Timestamp) obj[5]; + map = (String) obj[6]; + connection_type = (String) obj[7]; + connect_duration = (int) obj[8]; + reason = (String) obj[9]; + } + + @JsonGetter + public String getMap() { + return map.replace("workshop/","").split(".ugc", 2)[0]; + } + + @JsonGetter + public Long getUtime() { + return timestamp.toInstant().getEpochSecond(); + } +} diff --git a/src/main/java/app/services/ProfileService.java b/src/main/java/app/services/ProfileService.java index 6c391c7..4021c82 100644 --- a/src/main/java/app/services/ProfileService.java +++ b/src/main/java/app/services/ProfileService.java @@ -18,7 +18,7 @@ import java.util.List; @Service public class ProfileService { SteamWebApi steamWebApi; - UsertimeService usertimeService; + public UsertimeService usertimeService; PermitionService permitionService; StatsService statsService; BanService banService; diff --git a/src/main/java/app/services/db/UsertimeService.java b/src/main/java/app/services/db/UsertimeService.java index 8dfb952..04c84d1 100644 --- a/src/main/java/app/services/db/UsertimeService.java +++ b/src/main/java/app/services/db/UsertimeService.java @@ -1,5 +1,6 @@ package app.services.db; +import app.entities.db.Gametime; import app.entities.other.SteamID; import app.entities.server.Server; import app.entities.Stats; @@ -14,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.sql.Timestamp; import java.util.*; +import java.util.stream.Collectors; @Service @Transactional("RoTransactionManager") @@ -125,4 +127,25 @@ public class UsertimeService { } return null; } + + public List getGametimeOnServer(SteamID steamID, String server, Integer limit, Integer offset) { + String db = stats.getServers().entrySet().stream().filter(s -> s.getKey().equals(server)).map(s -> s.getValue().getDb()).findFirst().orElse(null); + if (db == null) return new ArrayList<>(); + return (List) entityManager.createNativeQuery("SELECT * FROM `"+db+"`.`user_connections` WHERE account_id = ?1 AND connection_type LIKE ?2 ORDER BY `"+db+"`.`user_connections`.`id` DESC LIMIT ?3 OFFSET ?4") + .setParameter(1, steamID.account_id) + .setParameter(2, "disconnect") + .setParameter(3, limit) + .setParameter(4, offset) + .getResultStream().map(obj -> new Gametime((Object[]) obj)).collect(Collectors.toList()); + + } + + public Long getTotalGametimeOnServer(SteamID steamID, String server) { + String db = stats.getServers().entrySet().stream().filter(s -> s.getKey().equals(server)).map(s -> s.getValue().getDb()).findFirst().orElse(null); + if (db == null) return 0L; + return (Long) entityManager.createNativeQuery("SELECT count(id) FROM `"+db+"`.`user_connections` WHERE account_id = ?1 AND connection_type LIKE ?2") + .setParameter(1, steamID.account_id) + .setParameter(2, "disconnect") + .getSingleResult(); + } }