From b3bc004de38b55b88aca578e69cc243a554389d8 Mon Sep 17 00:00:00 2001 From: gsd Date: Sun, 12 Mar 2023 13:18:13 +0300 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B0=D0=BA=20=D0=B1=D1=83=D0=B4=D0=B5?= =?UTF-8?q?=D1=82=20=D0=BF=D0=BE=20=D1=83=D0=BC=D1=83,=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=BB=D0=BE=D0=B6=D0=B8=D0=BB=20=D0=B2=20=D0=B1?= =?UTF-8?q?=D0=B5=D0=BA=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D0=BA=D1=83?= =?UTF-8?q?=20=D0=BE=D0=BA=D0=BE=D0=BD=D1=87=D0=B0=D0=BD=D0=B8=D1=8F=20?= =?UTF-8?q?=D0=B2=D0=B8=D0=BF=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/app/services/ServerService.java | 34 ++++++++++++++++--- src/main/java/app/services/StatsService.java | 6 +--- src/main/java/app/services/db/VIPService.java | 7 ++++ .../java/app/updates/VipCountUpdater.java | 22 +++++++++++- 4 files changed, 59 insertions(+), 10 deletions(-) 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; + } }