package app.entities.a2s.external; import app.entities.a2s.requests.A2SRequest; import app.entities.a2s.requests.RCONRequest; import app.entities.server.players.RCONPlayer; import app.utils.SteamIDConverter; import com.fasterxml.jackson.annotation.JsonIgnore; import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.client.RestTemplate; import java.util.*; public abstract class ExternalValveClient { @JsonIgnore RestTemplate restTemplate; @JsonIgnore boolean enabled = true; @JsonIgnore public String gateway = System.getenv("A2S_BACKEND_URL"); public ExternalValveClient(){ restTemplate = new RestTemplate(); CheckApi(); } public void CheckApi(){ System.out.printf("Check status: %s/api/ping\n", gateway); try { enabled = restTemplate.getForEntity("%s/api/ping".formatted(gateway), HashMap.class).getBody().containsKey("pong"); } catch (Exception err) { System.out.print("A2S Backend not respond\n"); } } @JsonIgnore public String ExecuteRCON(RCONRequest request){ if(!enabled) { System.out.printf("External client not enabled, cannot get rcon on %s\n", gateway); return "not enabled"; } try { return restTemplate.postForEntity("%s/api/rcon".formatted(gateway), request, String.class).getBody(); } catch (Exception err) { return "backend error"; } } @JsonIgnore public HashMap GetA2SInfo(A2SRequest request){ if(!enabled) { System.out.printf("External client not enabled, cannot get a2s on %s\n", gateway); return null; } try { return restTemplate.postForEntity("%s/api/a2s/info".formatted(gateway), request, HashMap.class).getBody(); } catch (Exception err) { return null; } } public ArrayList GetRCONPlayers(RCONRequest request){ if(!enabled) { System.out.printf("External client not enabled, cannot get rcon players on %s\n", gateway); return null; } try { return new ArrayList<>(Arrays.asList(restTemplate.postForEntity("%s/api/players".formatted(gateway), request, RCONPlayer[].class).getBody())); } catch (Exception err) { err.printStackTrace(); return new ArrayList<>(); } } }