From 5e5bf1b5b115c7cdebeaa1701cbb106c82382286 Mon Sep 17 00:00:00 2001 From: gsd Date: Tue, 7 Feb 2023 20:20:04 +0300 Subject: [PATCH] lastplay and gametime --- src/main/java/app/entities/PlayerProfile.java | 5 +- .../java/app/services/ProfileService.java | 17 ++- .../java/app/services/UsertimeService.java | 112 ++++++++++++++++++ 3 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 src/main/java/app/services/UsertimeService.java diff --git a/src/main/java/app/entities/PlayerProfile.java b/src/main/java/app/entities/PlayerProfile.java index 5f6bc73..3d2453f 100644 --- a/src/main/java/app/entities/PlayerProfile.java +++ b/src/main/java/app/entities/PlayerProfile.java @@ -2,14 +2,17 @@ package app.entities; import app.entities.other.SteamID; import app.entities.steam.SteamData; +import com.fasterxml.jackson.annotation.JsonInclude; import lombok.Data; +import java.math.BigDecimal; import java.util.HashMap; @Data public class PlayerProfile { Object ban; - HashMap> gametime; + HashMap> gametime; + HashMap> lastplay; Object permition; HashMap response_time; SteamData steam_data; diff --git a/src/main/java/app/services/ProfileService.java b/src/main/java/app/services/ProfileService.java index e75536e..35ac4d7 100644 --- a/src/main/java/app/services/ProfileService.java +++ b/src/main/java/app/services/ProfileService.java @@ -14,10 +14,12 @@ import java.util.List; @Service public class ProfileService { SteamWebApi steamWebApi; - + UsertimeService usertimeService; @Autowired - public ProfileService(SteamWebApi steamWebApi) { + public ProfileService(SteamWebApi steamWebApi, + UsertimeService usertimeService) { this.steamWebApi = steamWebApi; + this.usertimeService = usertimeService; } public PlayerProfile GetProfile(String steam64, List requests) { @@ -28,10 +30,19 @@ public class ProfileService { if(requests.contains("steam_data")){ profile.setSteam_data(steamWebApi.getSteamData(steamID.steam64)); } + + if(requests.contains("lastplay")){ + profile.setLastplay(usertimeService.getPlayerLastplay(profile.getSteamids())); + } + + if(requests.contains("usertime")){ + profile.setGametime(usertimeService.getPlayerUsertime(profile.getSteamids())); + } + return profile; } public PlayerProfile GetProfile(String steam64) { - return GetProfile(steam64, List.of("steam_data".split(","))); + return GetProfile(steam64, List.of("steam_data,lastplay,usertime".split(","))); } } diff --git a/src/main/java/app/services/UsertimeService.java b/src/main/java/app/services/UsertimeService.java new file mode 100644 index 0000000..428e634 --- /dev/null +++ b/src/main/java/app/services/UsertimeService.java @@ -0,0 +1,112 @@ +package app.services; + +import app.entities.other.SteamID; +import app.entities.server.Server; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.sql.Timestamp; +import java.util.*; + +@Service +public class UsertimeService { + + @PersistenceContext + EntityManager entityManager; + + //Лучше это всегда держать в true + boolean last_map = true; + Stats stats; + + @Autowired + public UsertimeService(Stats stats) { + this.stats = stats; + } + + public HashMap> getPlayerLastplay(SteamID steamID) { + List maps; + HashMap> result = new HashMap<>(); + + for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { + if(last_map) { + maps = getMap(stringServerEntry.getValue().getDb()).orElse(List.of()); + } else { + maps = getMaps(stringServerEntry.getValue().getDb()).orElse(List.of()); + } + + if(maps.size() == 0) {continue;} + + for(String map: maps){ + Timestamp lastplay = getLastplay(stringServerEntry.getValue().getDb(), steamID, map); + if(lastplay != null) { + if(!result.containsKey(stringServerEntry.getKey())){ + result.put(stringServerEntry.getKey(), new HashMap<>()); + } + result.get(stringServerEntry.getKey()).put(map, lastplay.getTime()/1000); + } + } + } + return result; + } + + public HashMap> getPlayerUsertime(SteamID steamID) { + List maps; + HashMap> result = new HashMap<>(); + + for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { + if(last_map) { + maps = getMap(stringServerEntry.getValue().getDb()).orElse(List.of()); + } else { + maps = getMaps(stringServerEntry.getValue().getDb()).orElse(List.of()); + } + + if(maps.size() == 0) {continue;} + + for(String map: maps){ + BigDecimal usertime = getTotalPlaytime(stringServerEntry.getValue().getDb(), steamID, map); + if(usertime != null) { + if(!result.containsKey(stringServerEntry.getKey())){ + result.put(stringServerEntry.getKey(), new HashMap<>()); + } + result.get(stringServerEntry.getKey()).put(map, usertime); + } + } + } + return result; + } + + public Timestamp getLastplay(String db, SteamID steamID, String map) { + List response = entityManager.createNativeQuery("select timestamp from `"+db+"`.`user_connections` WHERE map LIKE ?3 and connection_type LIKE ?1 AND account_id = ?2 ORDER BY `user_connections`.`id` DESC LIMIT 1") + //.setParameter(1, db) + .setParameter(1,"disconnect") + .setParameter(2, steamID.account_id) + .setParameter(3, map) + .getResultList(); + return response.size() > 0 ? response.get(0) : null; + } + + public BigDecimal getTotalPlaytime(String db, SteamID steamID, String map) { + List response = entityManager.createNativeQuery("select sum(connect_duration) total from `"+db+"`.`user_connections` WHERE map LIKE ?1 and connection_type LIKE ?2 AND account_id = ?3") + //.setParameter(1, db) + .setParameter(1, map) + .setParameter(2, "disconnect") + .setParameter(3, steamID.account_id) + .getResultList(); + return response.size() > 0 ? response.get(0) : null; + } + + public Optional getMap(String db) { + return Optional.ofNullable(entityManager.createNativeQuery("select `map` from `"+db+"`.`user_connections` WHERE 1 ORDER BY `user_connections`.`id` DESC LIMIT 1") + //.setParameter(1, db) + .getResultList()); + } + + public Optional getMaps(String db) { + return Optional.ofNullable(entityManager.createNativeQuery("select distinct `map` from `"+db+"`.`user_connections`") + //.setParameter(1, db) + .getResultList()); + } +}