Browse Source

FreeVIP start

master
gsd 2 years ago
parent
commit
b6b47aadad
  1. 11
      src/main/java/app/entities/SocialAuth.java
  2. 21
      src/main/java/app/entities/db/freevip/FreeVIP.java
  3. 95
      src/main/java/app/services/db/FreeVIPService.java
  4. 45
      src/main/java/app/services/db/PermitionService.java

11
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;
}

21
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];
}
}

95
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<FreeVIP> 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<Integer, Long> 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<String, HashMap<String, BigDecimal>> stringServerEntry : usertimeService.getPlayerUsertime(steamID).entrySet()) {
for (Map.Entry<String, BigDecimal> 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);
}
}

45
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");
}
}

Loading…
Cancel
Save