From 49ba57324a5b2bf44744e804a99c0f71f810e633 Mon Sep 17 00:00:00 2001 From: gsd Date: Wed, 15 Mar 2023 20:44:59 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BD=D0=B5=20=D0=BD=D1=83=20=D0=B2=D1=81?= =?UTF-8?q?=D0=B5=20=D0=BB=D1=8E=D0=B1=D1=8F=D1=82=20=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D1=82=D0=B8=D1=81=D1=82=D0=B8=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/controllers/admin/VIPController.java | 58 +++++++++++++++++++ src/main/java/app/entities/VipGiveMethod.java | 2 +- .../app/services/db/PermitionService.java | 7 ++- src/main/java/app/services/db/VIPService.java | 41 ++++++++++++- 4 files changed, 102 insertions(+), 6 deletions(-) create mode 100644 src/main/java/app/controllers/admin/VIPController.java diff --git a/src/main/java/app/controllers/admin/VIPController.java b/src/main/java/app/controllers/admin/VIPController.java new file mode 100644 index 0000000..d96966a --- /dev/null +++ b/src/main/java/app/controllers/admin/VIPController.java @@ -0,0 +1,58 @@ +package app.controllers.admin; + +import app.annotations.interfaces.CheckPermitionFlag; +import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.WaitAfterNext; +import app.entities.VipGiveMethod; +import app.services.db.VIPService; +import app.utils.SteamIDConverter; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("api/admin/vip") +public class VIPController { + VIPService vipService; + + @Autowired + public VIPController(VIPService vipService) { + this.vipService = vipService; + } + + @PostMapping + @CheckWebAccess + @CheckPermitionFlag(flag = "z") + @WaitAfterNext + public ResponseEntity giveVIP( + HttpServletRequest request, + @CookieValue(value = "steam64") String admin_steam64, + @RequestParam(value = "steam64") String user_steam64, + @RequestParam int amount + ) { + return new ResponseEntity(vipService.addVIP( + SteamIDConverter.getSteamID(user_steam64), + amount, + VipGiveMethod.MANUAL, + SteamIDConverter.getSteamID(admin_steam64).steam2 + ), HttpStatus.OK); + } + + @DeleteMapping + @CheckWebAccess + @CheckPermitionFlag(flag = "z") + @WaitAfterNext + public ResponseEntity removeVIP( + HttpServletRequest request, + @CookieValue(value = "steam64") String admin_steam64, + @RequestParam(value = "steam64") String user_steam64 + ) { + return new ResponseEntity(vipService.removeVIP( + SteamIDConverter.getSteamID(user_steam64), + SteamIDConverter.getSteamID(admin_steam64), + VipGiveMethod.MANUAL + ), HttpStatus.OK); + } +} diff --git a/src/main/java/app/entities/VipGiveMethod.java b/src/main/java/app/entities/VipGiveMethod.java index 4451fdf..4ac84d9 100644 --- a/src/main/java/app/entities/VipGiveMethod.java +++ b/src/main/java/app/entities/VipGiveMethod.java @@ -1,5 +1,5 @@ package app.entities; public enum VipGiveMethod { - FREE, STEAM, QIWI, MANUAL + FREE, STEAM, QIWI, MANUAL, AFTERTIME } diff --git a/src/main/java/app/services/db/PermitionService.java b/src/main/java/app/services/db/PermitionService.java index 2e5d149..1430bcb 100644 --- a/src/main/java/app/services/db/PermitionService.java +++ b/src/main/java/app/services/db/PermitionService.java @@ -70,8 +70,11 @@ public class PermitionService { } 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"); + //лишняя проверка не будет лишним + Permition permition = getPermition(steamID); + if (permition == null) return addPermition(steamID, amount, "oa", 20, "VIP", "f13bot.User"); + else if (permition.getStatus().equals("VIP")) return -1 * extendPermition(steamID, amount, "VIP"); + else return 0; } 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 47efae1..bf9b377 100644 --- a/src/main/java/app/services/db/VIPService.java +++ b/src/main/java/app/services/db/VIPService.java @@ -4,6 +4,7 @@ import app.entities.VipGiveMethod; import app.entities.db.Permition; import app.entities.other.SteamID; import app.services.ServerService; +import app.utils.SteamIDConverter; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.beans.factory.annotation.Autowired; @@ -37,6 +38,7 @@ public class VIPService { this.serverService = serverService; } + // Список ид из дискорда кто имеет платную випку public List getUsersDiscordWithActiveVIP() { return entityManager.createNativeQuery("SELECT `discord_id` FROM `sm_admins` INNER JOIN `steam2discord` ON `sm_admins`.`status` LIKE ?1 AND (`sm_admins`.`comment` LIKE ?2 OR `sm_admins`.`comment` LIKE ?3) AND `sm_admins`.`identity` = `steam2discord`.`steam_id`") .setParameter(1, "VIP") @@ -45,6 +47,7 @@ public class VIPService { .getResultList(); } + // Список ид из дискорда кто имеет бесплатную випку public List getUsersDiscordWithActiveFreeVIP() { return entityManager.createNativeQuery("SELECT `discord_id` FROM `sm_admins` INNER JOIN `steam2discord` ON `sm_admins`.`status` LIKE ?1 AND `sm_admins`.`comment` LIKE ?2 AND `sm_admins`.`identity` = `steam2discord`.`steam_id`") .setParameter(1, "VIP") @@ -52,13 +55,25 @@ public class VIPService { .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`)") + List steamid2remove = entityManager.createNativeQuery("SELECT `identity` 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") + .getResultStream().map(identity -> SteamIDConverter.getSteamID((String) identity)).toList(); + + int removed_vips = 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(); + + steamid2remove.forEach(steamid -> { + regesterGiveVIP(steamid, -1, VipGiveMethod.AFTERTIME, "check service", false); + }); + + return removed_vips; } + // Транзакция, добавление статистики @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)") @@ -70,15 +85,20 @@ public class VIPService { .executeUpdate(); } + // Проверка наличия прав для дочерних сервисов public Permition CheckCurrentPermition(SteamID steamID) { return permitionService.getPermition(steamID); } + // Добавить права + // 0 - если не было ничего добавлено + // >0 - если випка была добавлена + // <0 - если випка была продленна public int addVIP(SteamID steamID, int amount, VipGiveMethod vipGiveMethod, String extra) { - int result = 1; + int result = 0; switch (vipGiveMethod){ case FREE -> { - permitionService.addFreeVIP(steamID, amount); + result = permitionService.addFreeVIP(steamID, amount); regesterGiveVIP(steamID, amount, vipGiveMethod, extra, false); publishWebhook(steamID, amount, "Free", true); } @@ -102,6 +122,21 @@ public class VIPService { return result; } + // -1 если прав и так нет + // 1 если випка убрана + // 0 если была попытка снести права которые не випка + public int removeVIP(SteamID user_steam, SteamID admin_steam, VipGiveMethod vipGiveMethod) { + Permition permition = permitionService.getPermition(user_steam); + if (permition == null) return -1; + boolean result = permitionService.removeVIP(user_steam); + if (result) { + regesterGiveVIP(user_steam, -1, vipGiveMethod, admin_steam != null? admin_steam.steam2 : "check service", false); + serverService.executeRCONOnAllServers("sm_reloadadmins"); + return 1; + } else return 0; + } + + // Публикация в дискорд вебхука о покупке public void publishWebhook(SteamID steamID, int amount, String status, boolean free) { HashMap payload = new HashMap<>(); //МДА БЛЯТЬ НЕ СМОТРИТЕ ЧТО НИЖЕ