package app.entities.server; import app.entities.a2s.requests.A2SRequest; import app.entities.other.SteamID; import app.entities.a2s.external.ExternalValveClient; import app.entities.a2s.requests.RCONRequest; import app.entities.server.players.RCONPlayer; import app.entities.server.request.ServerRequestBody; import com.fasterxml.jackson.annotation.*; import lombok.Data; import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.List; @Data public class Server extends ExternalValveClient { String name; String description; String address; String preview; int player_count; int max_players; boolean status; String map; String color; String workshop; List naming; HashMap uniq = new HashMap<>(); List players = new ArrayList<>(); long last_update = 0; DockerStats dockerStats = new DockerStats(); String ip; @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String db; @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String dc; @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String rcon_password; @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private int grab_maps_limit = 1; double[] city_pos = {0.0, 0.0}; public Server() {} @JsonIgnore public String getMapCleared() { return map == null || map.isEmpty() ? "" : map.replace("workshop/","").split(".ugc")[0]; } @Override public String toString() { return "SERVER> %s | %s\nSTATS > %d/%d | %s\nCONFIG> grab limit: %d".formatted( name, address, player_count, max_players, map, grab_maps_limit ); } public void UpdateUniq(String key, Long count) { uniq.merge(key, count, (x,y) -> y); } public void RefreshServerA2SData() { UpdateStatusFromA2S(); /////////////////////////////////////////// if (!isStatus() || getPlayer_count() < 1) { return; } /////////////////////////////////////////// UpdatePlayers(); last_update = Instant.now().getEpochSecond(); } public boolean RefreshLastCheck(int sec) { if (Instant.now().getEpochSecond() - last_update > sec) { SetDownStatus(); return true; } return false; } public void RefreshServerFromRequest(ServerRequestBody serverRequestBody) { SetDownStatus(); if (serverRequestBody != null) { setMax_players(serverRequestBody.getMax_players()); setPlayer_count(serverRequestBody.getPlayer_count()); setMap(serverRequestBody.getMap_name()); setStatus(serverRequestBody.isStatus()); // players.clear(); players = new ArrayList<>(List.of(serverRequestBody.getPlayers())); } last_update = Instant.now().getEpochSecond(); } public void UpdateStatusFromA2S(){ SetDownStatus(); HashMap response = super.GetA2SInfo(new A2SRequest(getAddress())); if (response != null) { setMax_players((int) response.get("max_players")); setPlayer_count((int) response.get("player_count")); setMap((String) response.get("map_name")); setStatus(true); } } public void UpdatePlayers(){ players.clear(); players = GetRCONPlayers( new RCONRequest(getAddress(), getRcon_password(), "status") ); } public String ExecuteRCON(String command){ return super.ExecuteRCON(new RCONRequest(getAddress(), getRcon_password(), command)); } public void SetDownStatus() { setStatus(false); setMax_players(0); setPlayer_count(0); players.clear(); setMap(""); } @JsonIgnore public RCONPlayer searchPlayer(SteamID steamID){ return players.stream().filter(player -> player.getSteam().is(steamID)).findFirst().orElse(null); } @JsonIgnore public SteamID searchPlayer(String player_nickname){ return players.stream().filter(player -> player.getName().equals(player_nickname)).map(player -> player.getSteam()).findFirst().orElse(null); } }