package app.services.db; import app.entities.other.SteamID; import app.entities.server.Server; import app.entities.Stats; import app.utils.SteamIDConverter; 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()); } public SteamID getSteamOnUsername(String username) { SteamID steamID; for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { steamID = (SteamID) entityManager.createNativeQuery("SELECT account_id FROM `"+stringServerEntry.getValue().getDb()+"`.`user_connections` WHERE player_name LIKE ?1 ORDER BY `id` DESC LIMIT 1") .setParameter(1, username) .getResultStream().map(x -> SteamIDConverter.getSteamID("[U:1:%d]".formatted(x))).findFirst().orElse(null); if (steamID != null) return steamID; } return null; } }