diff --git a/src/main/java/app/services/db/FreeVIPService.java b/src/main/java/app/services/db/FreeVIPService.java index adb3edf..b704abb 100644 --- a/src/main/java/app/services/db/FreeVIPService.java +++ b/src/main/java/app/services/db/FreeVIPService.java @@ -125,7 +125,7 @@ public class FreeVIPService{ if (test != true) { setGivedFreeVip(steamID, socialAuth); - vipService.addVIP(steamID, 86400, VipGiveMethod.FREE); + vipService.addVIP(steamID, 86400, VipGiveMethod.FREE, null); } return 1L; } diff --git a/src/main/java/app/services/db/PermitionService.java b/src/main/java/app/services/db/PermitionService.java index 785af94..2e5d149 100644 --- a/src/main/java/app/services/db/PermitionService.java +++ b/src/main/java/app/services/db/PermitionService.java @@ -34,8 +34,8 @@ public class PermitionService { } @Transactional - public boolean addPermition(SteamID steamID, Integer amount, String flags, Integer immunity_level, String status, String comment) { - if (getPermition(steamID) != null) return true; + public int addPermition(SteamID steamID, Integer amount, String flags, Integer immunity_level, String status, String comment) { + if (getPermition(steamID) != null) return 0; return entityManager.createNativeQuery("INSERT INTO `sm_admins` (`authtype`, `identity`, `password`, `flags`, `name`, `immunity`, `comment`, `status`, `reg_date`, `amount`)" + " VALUES ('steam', ?1, NULL, ?2, '', ?3, ?4, ?5, CURRENT_TIMESTAMP, ?6)") .setParameter(1, steamID.steam2) @@ -44,7 +44,7 @@ public class PermitionService { .setParameter(4, comment) .setParameter(5, status) .setParameter(6, amount) - .executeUpdate() > 0; + .executeUpdate(); } @Transactional @@ -56,12 +56,22 @@ public class PermitionService { .executeUpdate() > 0; } - public boolean addFreeVIP(SteamID steamID, Integer amount) { + @Transactional + public int extendPermition(SteamID steamID, Integer amount, String status) { + return entityManager.createNativeQuery("UPDATE `sm_admins` SET `amount`=`amount`+?1, `reg_date`=`reg_date` WHERE `identity` LIKE ?2 AND `status` LIKE ?3") + .setParameter(1, amount) + .setParameter(2, steamID.steam2) + .setParameter(3, status) + .executeUpdate(); + } + + public int addFreeVIP(SteamID steamID, Integer amount) { return addPermition(steamID, amount, "oat", 20, "VIP", "f13bot.FreeVIP"); } - public boolean addVIP(SteamID steamID, Integer amount) { - return addPermition(steamID, amount, "oa", 20, "VIP", "f13bot.User"); + public int addVIP(SteamID steamID, Integer amount) { + if (getPermition(steamID) == null) return addPermition(steamID, amount, "oa", 20, "VIP", "f13bot.User"); + else return -1 * extendPermition(steamID, amount, "VIP"); } public boolean removeVIP(SteamID steamID) { diff --git a/src/main/java/app/services/db/VIPService.java b/src/main/java/app/services/db/VIPService.java index fdee169..ef19516 100644 --- a/src/main/java/app/services/db/VIPService.java +++ b/src/main/java/app/services/db/VIPService.java @@ -3,6 +3,7 @@ package app.services.db; import app.entities.VipGiveMethod; import app.entities.db.Permition; import app.entities.other.SteamID; +import app.services.ServerService; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; @@ -27,11 +28,13 @@ public class VIPService { String webhook_url; PermitionService permitionService; - + ServerService serverService; @Autowired - public VIPService(PermitionService permitionService) { + public VIPService(PermitionService permitionService, + ServerService serverService) { this.restTemplate = new RestTemplate(); this.permitionService = permitionService; + this.serverService = serverService; } public List getUsersDiscordWithActiveVIP() { @@ -56,25 +59,46 @@ public class VIPService { .executeUpdate(); } + @Transactional + public int regesterGiveVIP(SteamID steamID, int amount, VipGiveMethod vipGiveMethod, String extra, boolean extend) { + return entityManager.createNativeQuery("INSERT INTO `gived_vip` (`id`, `steam2`, `amount`, `timestamp`, `givemethod`, `reserved`, `extend`) VALUES (NULL, ?1, ?2, current_timestamp(), ?3, ?4, ?5)") + .setParameter(1, steamID.steam2) + .setParameter(2, amount) + .setParameter(3, vipGiveMethod.ordinal()) + .setParameter(4, extra) + .setParameter(5, extend) + .executeUpdate(); + } + public Permition CheckCurrentPermition(SteamID steamID) { return permitionService.getPermition(steamID); } - public void addVIP(SteamID steamID, int amount, VipGiveMethod vipGiveMethod) { + public void addVIP(SteamID steamID, int amount, VipGiveMethod vipGiveMethod, String extra) { + int result = 0; switch (vipGiveMethod){ case FREE -> { permitionService.addFreeVIP(steamID, amount); + regesterGiveVIP(steamID, amount, vipGiveMethod, extra, false); publishWebhook(steamID, amount, "Free", true); } case QIWI -> { - permitionService.addVIP(steamID, amount); + result = permitionService.addVIP(steamID, amount); + regesterGiveVIP(steamID, amount, vipGiveMethod, extra, result < 0); publishWebhook(steamID, amount, "Qiwi", false); } case STEAM -> { - permitionService.addVIP(steamID, amount); + result = permitionService.addVIP(steamID, amount); + regesterGiveVIP(steamID, amount, vipGiveMethod, extra, result < 0); publishWebhook(steamID, amount, "Steam", false); } + case MANUAL -> { + result = permitionService.addVIP(steamID, amount); + regesterGiveVIP(steamID, amount, vipGiveMethod, extra, result < 0); + publishWebhook(steamID, amount, "Admin", false); + } } + serverService.executeRCONOnAllServers("sm_reloadadmins"); } public void publishWebhook(SteamID steamID, int amount, String status, boolean free) {