Browse Source

lastplay and gametime

master
gsd 2 years ago
parent
commit
5e5bf1b5b1
  1. 5
      src/main/java/app/entities/PlayerProfile.java
  2. 17
      src/main/java/app/services/ProfileService.java
  3. 112
      src/main/java/app/services/UsertimeService.java

5
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<String, HashMap<String, Long>> gametime;
HashMap<String, HashMap<String, BigDecimal>> gametime;
HashMap<String, HashMap<String, Long>> lastplay;
Object permition;
HashMap<String, Long> response_time;
SteamData steam_data;

17
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<String> 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(",")));
}
}

112
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<String, HashMap<String, Long>> getPlayerLastplay(SteamID steamID) {
List<String> maps;
HashMap<String, HashMap<String, Long>> result = new HashMap<>();
for (Map.Entry<String, Server> 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<String, HashMap<String, BigDecimal>> getPlayerUsertime(SteamID steamID) {
List<String> maps;
HashMap<String, HashMap<String, BigDecimal>> result = new HashMap<>();
for (Map.Entry<String, Server> 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<Timestamp> 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<BigDecimal> 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<List> 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<List> getMaps(String db) {
return Optional.ofNullable(entityManager.createNativeQuery("select distinct `map` from `"+db+"`.`user_connections`")
//.setParameter(1, db)
.getResultList());
}
}
Loading…
Cancel
Save