From b6b47aadad17a04d43556b9b4baf3d678de5a52a Mon Sep 17 00:00:00 2001 From: gsd Date: Sun, 12 Feb 2023 21:33:59 +0300 Subject: [PATCH] FreeVIP start --- src/main/java/app/entities/SocialAuth.java | 11 +++ .../java/app/entities/db/freevip/FreeVIP.java | 21 ++++ .../java/app/services/db/FreeVIPService.java | 95 +++++++++++++++++++ .../app/services/db/PermitionService.java | 45 +++++++++ 4 files changed, 172 insertions(+) create mode 100644 src/main/java/app/entities/SocialAuth.java create mode 100644 src/main/java/app/entities/db/freevip/FreeVIP.java create mode 100644 src/main/java/app/services/db/FreeVIPService.java diff --git a/src/main/java/app/entities/SocialAuth.java b/src/main/java/app/entities/SocialAuth.java new file mode 100644 index 0000000..261619f --- /dev/null +++ b/src/main/java/app/entities/SocialAuth.java @@ -0,0 +1,11 @@ +package app.entities; + +import app.entities.other.SteamID; +import com.fasterxml.jackson.annotation.JsonIgnore; +import lombok.Data; + +@Data +public class SocialAuth { + protected long vk_id = 0; + protected long discord_id = 0; +} diff --git a/src/main/java/app/entities/db/freevip/FreeVIP.java b/src/main/java/app/entities/db/freevip/FreeVIP.java new file mode 100644 index 0000000..b6b164a --- /dev/null +++ b/src/main/java/app/entities/db/freevip/FreeVIP.java @@ -0,0 +1,21 @@ +package app.entities.db.freevip; + +import app.entities.SocialAuth; +import lombok.Data; + +import java.sql.Timestamp; + +@Data +public class FreeVIP extends SocialAuth { + int id; + String steam3; + Timestamp timestamp; + + public FreeVIP(Object[] obj){ + id = (int) obj[0]; + steam3 = (String) obj[1]; + vk_id = (long) obj[2]; + discord_id = (long) obj[3]; + timestamp = (Timestamp) obj[4]; + } +} diff --git a/src/main/java/app/services/db/FreeVIPService.java b/src/main/java/app/services/db/FreeVIPService.java new file mode 100644 index 0000000..612dee8 --- /dev/null +++ b/src/main/java/app/services/db/FreeVIPService.java @@ -0,0 +1,95 @@ +package app.services.db; + +import app.entities.SocialAuth; +import app.entities.db.freevip.FreeVIP; +import app.entities.other.SteamID; +import app.entities.server.Server; +import jakarta.persistence.EntityManager; +import jakarta.persistence.PersistenceContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Service +public class FreeVIPService { + @PersistenceContext + EntityManager entityManager; + + PermitionService permitionService; + UsertimeService usertimeService; + + @Autowired + public FreeVIPService(PermitionService permitionService, + UsertimeService usertimeService) { + this.permitionService = permitionService; + this.usertimeService = usertimeService; + } + + public Long getDBServerUTime() { + return (Long) entityManager.createNativeQuery("SELECT UNIX_TIMESTAMP(NOW())") + .getSingleResult(); + } + + public List getSocial(SteamID steamID) { + return entityManager.createNativeQuery("SELECT id, steam3, vk_id, discord_id, date FROM free_vip WHERE steam3 = ?1 ORDER BY `free_vip`.`date` DESC") + .setParameter(1, steamID.steam3) + .getResultList(); + } + + public Long getLastGivedFreeVIP(SteamID steamID) { + return (Long) entityManager.createNativeQuery("SELECT UNIX_TIMESTAMP(`date`) as u_time FROM free_vip WHERE steam3 LIKE ?1 ORDER BY `date` DESC") + .setParameter(1, steamID.steam3) + .getResultStream().map(u_time -> Long.valueOf((String) u_time)).findFirst().orElse(0L); + } + + public Long getLastGivedFreeVIP(SocialAuth socialAuth) { + if (socialAuth.getVk_id() != 0) { + return (Long) entityManager.createNativeQuery("SELECT UNIX_TIMESTAMP(`date`) as u_time FROM free_vip WHERE vk_id = ?1 ORDER BY `date` DESC") + .setParameter(1, socialAuth.getVk_id()) + .getResultStream().map(u_time -> Long.valueOf((String) u_time)).findFirst().orElse(0L); + } else if (socialAuth.getDiscord_id() != 0) { + return (Long) entityManager.createNativeQuery("SELECT UNIX_TIMESTAMP(`date`) as u_time FROM free_vip WHERE discord_id = ?1 ORDER BY `date` DESC") + .setParameter(1, socialAuth.getDiscord_id()) + .getResultStream().map(u_time -> Long.valueOf((String) u_time)).findFirst().orElse(0L); + } else { + return 0L; + } + } + + public Map addFreeVIP(SteamID steamID, SocialAuth socialAuth) { + //1.имеются ли уже права + if (permitionService.getPermition(steamID) != null) return Map.of(1, 0L); + long current_server_time = getDBServerUTime(); + long timeout = 60 * 60 * 24 * 30; + long need_usertime = 60 * 60 * 24 * 2; + //////////////////////////////////////////////////////////////// + long last_free_vip_gived = getLastGivedFreeVIP(socialAuth); + if (current_server_time - last_free_vip_gived < timeout) { + //2.Права из под социального аккаунта уже имеются + return Map.of(2, timeout - (current_server_time - last_free_vip_gived)); + } + //////////////////////////////////////////////////////////////// + last_free_vip_gived = getLastGivedFreeVIP(steamID); + if (current_server_time - last_free_vip_gived < timeout) { + //3.Права из под steam аккаунта уже имеются + return Map.of(3, timeout - (current_server_time - last_free_vip_gived)); + } + //////////////////////////////////////////////////////////////// + BigDecimal total_usertime = BigDecimal.valueOf(0); + for (Map.Entry> stringServerEntry : usertimeService.getPlayerUsertime(steamID).entrySet()) { + for (Map.Entry stringBigDecimalEntry : stringServerEntry.getValue().entrySet()) { + total_usertime.add(stringBigDecimalEntry.getValue()); + } + } + if (total_usertime.longValue() < need_usertime) { + //4.недостаточно наиграл + return Map.of(4, need_usertime - total_usertime.longValue()); + } + permitionService.addFreeVIP(steamID, 86400); + return Map.of(0, 0L); + } +} diff --git a/src/main/java/app/services/db/PermitionService.java b/src/main/java/app/services/db/PermitionService.java index be2694d..1a8dc13 100644 --- a/src/main/java/app/services/db/PermitionService.java +++ b/src/main/java/app/services/db/PermitionService.java @@ -6,6 +6,7 @@ import app.entities.other.SteamID; import jakarta.persistence.EntityManager; import jakarta.persistence.PersistenceContext; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -20,4 +21,48 @@ public class PermitionService { .getResultList(); return result.stream().map(Permition::new).findFirst().orElse(null); } + + public boolean CheckMorePowerfull(SteamID user_1, SteamID user_2) { + Permition permition_1 = getPermition(user_1); + if (permition_1 == null) return false; + Permition permition_2 = getPermition(user_2); + if (permition_2 == null) return true; + return permition_1.getImmunity() > permition_2.getImmunity(); + } + + @Transactional + public boolean addPermition(SteamID steamID, Integer amount, String flags, Integer immunity_level, String status, String comment) { + if (getPermition(steamID) != null) return true; + 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) + .setParameter(2, flags) + .setParameter(3, immunity_level) + .setParameter(4, comment) + .setParameter(5, status) + .setParameter(6, immunity_level) + .executeUpdate() > 0; + } + + @Transactional + public boolean removePermition(SteamID steamID, String status) { + if (getPermition(steamID) == null) return true; + return entityManager.createNativeQuery("DELETE FROM sm_admins WHERE identity = ?1 AND status LIKE ?2") + .setParameter(1, steamID.steam2) + .setParameter(2, status) + .executeUpdate() > 0; + } + + public boolean 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 boolean removeVIP(SteamID steamID) { + if (getPermition(steamID) == null) return true; + return removePermition(steamID, "VIP"); + } }