package app.entities; import app.entities.server.Server; import com.fasterxml.jackson.annotation.JsonGetter; import lombok.Data; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.time.Instant; import java.time.LocalDate; import java.util.HashMap; @Data @Component @Scope(value = "singleton") public class Stats { long ban_count = 0; int discord_users = 0; long freevip_players = 0; long server_uptime = Instant.now().getEpochSecond(); long vip_players = 0; int vk_users = 0; HashMap countries = new HashMap<>(); HashMap servers = new HashMap<>(); HashMap uniq = new HashMap<>(); HashMap updates = new HashMap<>(); Statistic statistic = new Statistic(); String builddate = ""; HashMap donate = new HashMap<>(); public Stats(){ try { builddate = System.getenv("BUILDDATE"); } catch (Exception err) { builddate = "0"; } } @JsonGetter public Statistic getStatistic() { statistic.setTotal_servers(servers.size()); statistic.setWorking_servers((int) servers.entrySet().stream().filter(x -> x.getValue().isStatus() == true).count()); statistic.setPlayer_now(servers.entrySet().stream().mapToInt(x -> x.getValue().getPlayer_count()).sum()); if (LocalDate.now().getDayOfMonth() != statistic.getCurrent_day()) { statistic.setCurrent_day(LocalDate.now().getDayOfMonth()); statistic.setPlayer_max(0); } if (statistic.getPlayer_max() < statistic.getPlayer_now()) { statistic.setPlayer_max(statistic.getPlayer_now()); } return statistic; } @JsonGetter public long getServer_uptime() { return Instant.now().getEpochSecond() - server_uptime; } public void UpdateUniq(String key, Long value) { uniq.merge(key, value, (x, y) -> y); } public HashMap getOnlyThis(String value) { switch (value) { case "statistic": return new HashMap<>(){{put(value, getStatistic());}}; case "servers": return new HashMap<>(){{put(value, getServers());}}; case "uniq": return new HashMap<>(){{put(value, getUniq());}}; case "donate": return new HashMap<>(){{ put(value, getDonate()); put("freevip_players", getFreevip_players()); put("vip_players", getVip_players()); }}; case "updates": return new HashMap<>(){{put(value, getUpdates());}}; case "countries": return new HashMap<>(){{put(value, getCountries());}}; case "social": return new HashMap<>(){{ put("discord_users", getDiscord_users()); put("vk_users", getVk_users()); }}; case "other": return new HashMap<>(){{ put("ban_count", getBan_count()); put("server_uptime", getServer_uptime()); put("builddate", getBuilddate()); put("updates", getUpdates()); }}; default: return new HashMap<>(); } } }