Browse Source

не ну все любят статистику

master
gsd 2 years ago
parent
commit
49ba57324a
  1. 58
      src/main/java/app/controllers/admin/VIPController.java
  2. 2
      src/main/java/app/entities/VipGiveMethod.java
  3. 7
      src/main/java/app/services/db/PermitionService.java
  4. 41
      src/main/java/app/services/db/VIPService.java

58
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);
}
}

2
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
}

7
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) {

41
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<Long> 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<Long> 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<SteamID> 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<String, Object> payload = new HashMap<>();
//МДА БЛЯТЬ НЕ СМОТРИТЕ ЧТО НИЖЕ

Loading…
Cancel
Save