diff --git a/src/main/java/app/services/ServerService.java b/src/main/java/app/services/ServerService.java index d68a5d4..8bfc334 100644 --- a/src/main/java/app/services/ServerService.java +++ b/src/main/java/app/services/ServerService.java @@ -3,20 +3,28 @@ package app.services; import app.entities.PlayerProfile; import app.entities.Stats; import app.entities.server.PlayOn; +import app.updates.SocialUpdater; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + @Service public class ServerService { Stats stats; - ApplicationContext applicationContext; + + private final Logger logger = LoggerFactory.getLogger(ServerService.class); @Autowired - public ServerService(Stats stats, - ApplicationContext applicationContext) { + public ServerService(Stats stats) { this.stats = stats; - this.applicationContext = applicationContext; } public boolean kickPlayer(PlayerProfile playerProfile, String reason){ @@ -24,4 +32,22 @@ public class ServerService { return stats.getServers().get(playerProfile.getPlay_on().getServer_id()) .ExecuteRCON("sm_kick #%d %s".formatted(playerProfile.getPlay_on().getPlayer_id(), reason)).contains("kicked"); } + + public void executeRCONOnAllServers(String command) { + ExecutorService executor = Executors.newCachedThreadPool(); + List tasks = new ArrayList<>(); + stats.getServers().forEach((server_name, server) -> { + tasks.add((Callable) () -> { + server.ExecuteRCON(command); + return null; + }); + }); + + try { + executor.invokeAll(tasks); + executor.shutdown(); + } catch (InterruptedException err) { + logger.error("Cancel burst execute rcon command: {}", command); + } + } } diff --git a/src/main/java/app/services/StatsService.java b/src/main/java/app/services/StatsService.java index ef257c2..7b101ad 100644 --- a/src/main/java/app/services/StatsService.java +++ b/src/main/java/app/services/StatsService.java @@ -15,13 +15,9 @@ import java.util.Map; public class StatsService { Stats stats; - ApplicationContext applicationContext; - @Autowired - public StatsService (Stats stats, - ApplicationContext applicationContext) { + public StatsService (Stats stats) { this.stats = stats; - this.applicationContext = applicationContext; } public PlayOn searchPlayer(SteamID steamID){ diff --git a/src/main/java/app/services/db/VIPService.java b/src/main/java/app/services/db/VIPService.java index b7cee46..f2c2db7 100644 --- a/src/main/java/app/services/db/VIPService.java +++ b/src/main/java/app/services/db/VIPService.java @@ -27,4 +27,11 @@ public class VIPService { .setParameter(2, "f13bot.FreeVIP") .getResultList(); } + + @Transactional + public int removeEndedVIPs() { + return entityManager.createNativeQuery("DELETE FROM `sm_admins` WHERE NOT UNIX_TIMESTAMP(`reg_date`) LIKE 0 AND `amount` NOT LIKE 0 AND `status` LIKE ?1 AND ((unix_timestamp(now()) - UNIX_TIMESTAMP(`reg_date`)) > `amount`)") + .setParameter(1, "VIP") + .executeUpdate(); + } } diff --git a/src/main/java/app/updates/VipCountUpdater.java b/src/main/java/app/updates/VipCountUpdater.java index dce0f25..f1384a1 100644 --- a/src/main/java/app/updates/VipCountUpdater.java +++ b/src/main/java/app/updates/VipCountUpdater.java @@ -1,6 +1,8 @@ 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; @@ -19,6 +21,9 @@ public class VipCountUpdater extends BaseUpdater{ @PersistenceContext EntityManager entityManager; + private VIPService vipService; + private ServerService serverService; + private final Logger logger = LoggerFactory.getLogger(VipCountUpdater.class); @Value("${backend.updates.vip_count}") @@ -26,8 +31,12 @@ public class VipCountUpdater extends BaseUpdater{ private int timeout = 5 * 60 * 1000; @Autowired - public VipCountUpdater(Stats stats) { + public VipCountUpdater(Stats stats, + VIPService vipService, + ServerService serverService) { this.stats = stats; + this.vipService = vipService; + this.serverService = serverService; } @PostConstruct @@ -36,6 +45,7 @@ public class VipCountUpdater extends BaseUpdater{ logger.warn("Updater enabled"); CreateTaskUpdater(this::UpdateVIPCount, timeout); CreateTaskUpdater(this::UpdateFreeVIPCount, timeout); + CreateTaskUpdater(this::CheckEndedVIPs, 60 * 1000); } } @@ -56,4 +66,14 @@ public class VipCountUpdater extends BaseUpdater{ 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; + } }