You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

120 lines
5.6 KiB

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.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
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;
@Value("${backend.vip.discord}")
String webhook_url;
RestTemplate restTemplate;
@Autowired
public FreeVIPService(PermitionService permitionService,
UsertimeService usertimeService) {
this.permitionService = permitionService;
this.usertimeService = usertimeService;
this.restTemplate = new RestTemplate();
}
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 Long addFreeVIP(SteamID steamID, SocialAuth socialAuth) {
/*
0 - права уже есть
1 - права выданы
> 1 - стоит подождать пока можно получить новые
< 0 - недостаточно наиграно на сервере времени
*/
//1.имеются ли уже права
if (permitionService.getPermition(steamID) != null) return 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 (last_free_vip_gived != 0L && current_server_time - last_free_vip_gived < timeout) {
//2.Права из под социального аккаунта уже имеются
return timeout - (current_server_time - last_free_vip_gived);
}
////////////////////////////////////////////////////////////////
last_free_vip_gived = getLastGivedFreeVIP(steamID);
if (last_free_vip_gived != 0L && current_server_time - last_free_vip_gived < timeout) {
//3.Права из под steam аккаунта уже имеются
return 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 -1 * (need_usertime - total_usertime.longValue());
}
permitionService.addFreeVIP(steamID, 86400);
publishWebhook(steamID, 86400, "Free");
return 1L;
}
public void publishWebhook(SteamID steamID, int amount, String status) {
HashMap<String, Object> payload = new HashMap<>();
payload.put("content","Схватил бесплатно!!!");
payload.put("embeds", List.of(Map.of("fields", List.of(
Map.of("name", "Ссылка на игрока:", "value", steamID.community_url, "inline", true),
Map.of("name", "Количество дней", "value", "%d".formatted(amount/60/60/24), "inline", true),
Map.of("name", "Оплата", "value", status, "inline", true)
))));
restTemplate.postForEntity(webhook_url, payload, String.class);
}
}