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.
 
 
 
 

109 lines
5.1 KiB

package app.services.db;
import app.entities.PlayerProfile;
import app.entities.db.Ban;
import app.entities.other.SteamID;
import app.services.ProfileService;
import app.services.ServerService;
import app.utils.CryptedCookie;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.sql.Timestamp;
import java.util.List;
@Service
@Transactional(value = "RwTransactionManager")
public class BanService {
EntityManager entityManager;
ServerService serverService;
CryptedCookie cryptedCookie;
@Autowired
public BanService(ServerService serverService, @Qualifier(value = "RwEntityManager") EntityManager entityManager, CryptedCookie cryptedCookie) {
this.entityManager = entityManager;
this.serverService = serverService;
this.cryptedCookie = cryptedCookie;
}
public Ban getBan(SteamID steamID) {
List<Object[]> result = entityManager.createNativeQuery("SELECT * FROM light_bans WHERE account_id = ?1 AND active = 1")
.setParameter(1, steamID.account_id)
.getResultList();
return result.stream().map(Ban::new).peek(ban -> ban.cryptIP(cryptedCookie)).findFirst().orElse(null);
}
public Ban getBan(int ban_id) {
List<Object[]> result = entityManager.createNativeQuery("SELECT * FROM light_bans WHERE id = ?1")
.setParameter(1, ban_id)
.getResultList();
return result.stream().map(Ban::new).peek(ban -> ban.cryptIP(cryptedCookie)).findFirst().orElse(null);
}
public List<Ban> getBans(SteamID steamID) {
List<Object[]> result = entityManager.createNativeQuery("SELECT * FROM light_bans WHERE account_id = ?1 AND active = 0")
.setParameter(1, steamID.account_id)
.getResultList();
return result.stream().map(Ban::new).peek(ban -> ban.cryptIP(cryptedCookie)).toList();
}
public List<Ban> getLastBans(Integer limit) {
return ((List<Ban>) entityManager.createNativeQuery("SELECT * FROM `light_bans` WHERE `active` = ?1 ORDER BY `light_bans`.`id` DESC LIMIT ?2")
.setParameter(1, 1)
.setParameter(2, limit)
.getResultStream().map(obj -> new Ban((Object[]) obj)).toList()).stream().peek(ban -> ban.cryptIP(cryptedCookie)).toList();
//этот каст конечно пиздец, но он работает
}
public Long getBansCount(SteamID steamID) {
return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM light_bans WHERE account_id = ?1 AND active = 0").getSingleResult();
}
@Transactional(value = "RwTransactionManager")
public boolean removeBan(PlayerProfile user, PlayerProfile admin) {
Ban ban = getBan(user.getSteamids());
if (ban == null) return false;
return entityManager.createNativeQuery("UPDATE light_bans SET active=?1, unbanned_by_id = ?2, unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = ?3")
.setParameter(1, 0)
.setParameter(2, admin.getSteamids().steam2)
.setParameter(3, ban.getId())
.executeUpdate() > 0;
}
@Transactional(value = "RwTransactionManager")
public int addBan(PlayerProfile user, PlayerProfile admin, int ban_length, String ban_reason) {
Ban ban = getBan(user.getSteamids());
if (ban != null) return -1 * ban.getId();
int result = entityManager.createNativeQuery("INSERT INTO light_bans (steam_id, account_id, ban_length, ban_reason, banned_by_id, active, ip, player_name, banned_by) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)")
.setParameter(1, user.getSteamids().steam2)
.setParameter(2, user.getSteamids().account_id)
.setParameter(3, ban_length)
.setParameter(4, ban_reason)
.setParameter(5, admin.getSteamids().steam2)
.setParameter(6, 1)
.setParameter(7, user.getPlay_on() != null ? user.getPlay_on().getIp(cryptedCookie) : "")
.setParameter(8, user.getPlay_on() != null ? user.getPlay_on().getName() : user.getSteam_data().getNickname())
.setParameter(9, admin.getSteam_data().getNickname())
.executeUpdate();
serverService.kickPlayer(user, "banned");
return getBan(user.getSteamids()).getId();
}
public List<Long> getUsersDiscordWithBanOnServers() {
return entityManager.createNativeQuery("SELECT `discord_id` FROM `light_bans` INNER JOIN `steam2discord` ON `light_bans`.`active` = ?1 AND `light_bans`.`steam_id` COLLATE utf8mb4_unicode_ci LIKE `steam2discord`.`steam_id`")
.setParameter(1,1)
.getResultList();
}
public String deCrypt(String crypted) {
try {
return cryptedCookie.ReadCh(crypted);
} catch (Exception e) {
return crypted;
}
}
}