Browse Source

usertime upd

master
gsd 1 year ago
parent
commit
a432fc344a
  1. 18
      src/main/java/app/controllers/user/DetailController.java
  2. 52
      src/main/java/app/entities/db/Gametime.java
  3. 2
      src/main/java/app/services/ProfileService.java
  4. 23
      src/main/java/app/services/db/UsertimeService.java

18
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);
}
}

52
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();
}
}

2
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;

23
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<Gametime> 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<Gametime>) 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();
}
}

Loading…
Cancel
Save