package app.updates; import app.entities.Stats; import app.services.ServerService; import app.services.db.VIPService; 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.beans.factory.annotation.Value; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; import java.sql.ResultSet; import java.sql.SQLException; import java.time.Instant; @Component public class VipCountUpdater extends BaseUpdater{ private Stats stats; @Autowired @Qualifier("jt_ro") private JdbcTemplate jdbcTemplate; private VIPService vipService; private ServerService serverService; private final Logger logger = LoggerFactory.getLogger(VipCountUpdater.class); @Value("${backend.updates.vip_count}") private boolean update = false; private int timeout = 5 * 60 * 1000; @Autowired public VipCountUpdater(Stats stats, VIPService vipService, ServerService serverService) { this.stats = stats; this.vipService = vipService; this.serverService = serverService; } @PostConstruct public void SetUpdater(){ if (update) { logger.warn("Updater enabled"); CreateTaskUpdater(this::UpdateVIPCount, timeout, getClass().getName() + "_count"); CreateTaskUpdater(this::UpdateFreeVIPCount, timeout, getClass().getName() + "_freecount"); CreateTaskUpdater(this::CheckEndedVIPs, 60 * 1000, getClass().getName() + "_ended"); } } public boolean UpdateVIPCount() { logger.info("Update current active VIP profiles"); Long count = jdbcTemplate.query("SELECT COUNT(*) as count FROM sm_admins WHERE status LIKE 'VIP' AND (comment LIKE 'Donate.User' OR comment LIKE 'f13bot.User')", (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L); stats.setVip_players(count); stats.getUpdates().merge("vip_count", Instant.now().getEpochSecond(), (x, y) -> y); return true; } public boolean UpdateFreeVIPCount() { logger.info("Update current active FreeVIP profiles"); Long count = jdbcTemplate.query("SELECT COUNT(*) as count FROM sm_admins WHERE status LIKE 'VIP' AND comment LIKE 'f13bot.FreeVIP'", (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L); stats.setFreevip_players(count); stats.getUpdates().merge("freevip_count", Instant.now().getEpochSecond(), (x, y) -> y); return true; } public boolean CheckEndedVIPs() { int count = vipService.removeEndedVIPs(); if (count > 0) { logger.info("Found {} ended vips", count); serverService.executeRCONOnAllServers("sm_reloadadmins"); return true; } return false; } }