diff --git a/src/main/java/app/updates/OnlineUpdater.java b/src/main/java/app/updates/OnlineUpdater.java new file mode 100644 index 0000000..ad1dfc5 --- /dev/null +++ b/src/main/java/app/updates/OnlineUpdater.java @@ -0,0 +1,56 @@ +package app.updates; + +import app.entities.Stats; +import jakarta.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +import java.time.Instant; + +@Component +public class OnlineUpdater extends BaseUpdater { + + private Stats stats; + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Autowired + @Qualifier("jt_rw") + private JdbcTemplate jdbcTemplate; + private final static int sleep_time = 30 * 1000; + + @Autowired + public OnlineUpdater(Stats stats) { + this.stats = stats; + } + + @PostConstruct + public void AfterStart() { + logger.warn("Per server online updater enabled"); + stats.getServers().forEach((server_name, server) -> { + logger.info("{} created online updater", server_name); + CreateTaskUpdater(() -> AppendOnlineStats(server_name), sleep_time); + }); + } + + public boolean AppendOnlineStats(String server_id) { + long utime = Instant.now().getEpochSecond(); + int player_count = stats.getServers().get(server_id).getPlayer_count(); + int player_max = stats.getServers().get(server_id).getMax_players(); + jdbcTemplate.update("INSERT INTO `servers_online` (`utime`, `player_count`, `max_players`, `server_id`) VALUES (?, ?, ?, ?)", + utime, player_count, player_max, server_id); + return true; + } + + //CREATE TABLE `tf2.facti13`.`servers_online` (`id` INT NOT NULL AUTO_INCREMENT , `utime` INT NOT NULL , `player_count` INT NOT NULL , `max_players` INT NOT NULL , `server_id` VARCHAR(32) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB; + public class ServerOnline { + private int id; + private long utime; + private int player_count; + private int max_players; + private String server_id; + } +}