package app.updates; import app.entities.Stats; import app.services.ServerService; import app.services.db.VIPService; import jakarta.annotation.PostConstruct; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; 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.stereotype.Component; import java.time.Instant; import java.util.concurrent.CompletableFuture; @Component public class VipCountUpdater extends BaseUpdater{ private Stats stats; EntityManager entityManager; 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, @Qualifier(value = "RoEntityManager") EntityManager entityManager) { this.stats = stats; this.vipService = vipService; this.serverService = serverService; this.entityManager = entityManager; } @PostConstruct public void SetUpdater(){ if (update) { logger.warn("Updater enabled"); CreateTaskUpdater(this::UpdateVIPCount, timeout); CreateTaskUpdater(this::UpdateFreeVIPCount, timeout); CreateTaskUpdater(this::CheckEndedVIPs, 60 * 1000); } } public boolean UpdateVIPCount() { logger.info("Update current active VIP profiles"); stats.setVip_players( (Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND (`comment` LIKE 'Donate.User' OR `comment` LIKE 'f13bot.User')") .getSingleResult()); stats.getUpdates().merge("vip_count", Instant.now().getEpochSecond(), (x, y) -> y); return true; } public boolean UpdateFreeVIPCount() { logger.info("Update current active FreeVIP profiles"); stats.setFreevip_players( (Long) entityManager.createNativeQuery("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND `comment` LIKE 'f13bot.FreeVIP'") .getSingleResult()); 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; } }