package app.services.db; import app.entities.PlayerProfile; import app.entities.db.Ban; import app.entities.other.SteamID; import app.services.ServerService; import app.utils.CryptedCookie; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Service; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @Service public class BanService { @Autowired @Qualifier("jt_rw") JdbcTemplate jdbcTemplate; ServerService serverService; CryptedCookie cryptedCookie; PermitionService permitionService; @Autowired public BanService(ServerService serverService, CryptedCookie cryptedCookie, PermitionService permitionService) { this.serverService = serverService; this.cryptedCookie = cryptedCookie; this.permitionService = permitionService; } public Ban getBan(SteamID steamID) { return jdbcTemplate.query("SELECT * FROM light_bans WHERE account_id = ? AND active = true", new Object[]{ steamID.account_id }, (rs, n) -> new Ban(rs)) .stream() .peek(ban -> ban.cryptIP(cryptedCookie)) .peek(ban -> ban.fillAdmins(permitionService)) .findFirst() .orElse(null); } public Ban getBan(int ban_id) { return jdbcTemplate.query("SELECT * FROM light_bans WHERE id = ?", new Object[]{ ban_id }, (rs, n) -> new Ban(rs)) .stream() .peek(ban -> ban.cryptIP(cryptedCookie)) .peek(ban -> ban.fillAdmins(permitionService)) .findFirst() .orElse(null); } public List getBans(SteamID steamID) { return jdbcTemplate.query("SELECT * FROM light_bans WHERE account_id = ? AND active = false", new Object[]{steamID.account_id}, (rs, n) -> new Ban(rs)) .stream() .peek(ban -> ban.cryptIP(cryptedCookie)) .peek(ban -> ban.fillAdmins(permitionService)) .toList(); } public List getLastBans(Integer limit, Integer offset) { return jdbcTemplate.query("SELECT * FROM light_bans WHERE active = ? ORDER BY light_bans.id DESC LIMIT ? OFFSET ?", new Object[]{true, limit, offset}, (rs, n) -> new Ban(rs)) .stream() .peek(ban -> ban.cryptIP(cryptedCookie)) .peek(ban -> ban.fillAdmins(permitionService)) .toList(); } public Long getBansCount() { return jdbcTemplate.query("SELECT COUNT(*) as c FROM light_bans WHERE active = true", (rs, n) -> rs.getLong("c")).stream().findFirst().orElse(0L); } public boolean removeBan(PlayerProfile user, PlayerProfile admin) { Ban ban = getBan(user.getSteamids()); if (ban == null) return false; return jdbcTemplate.update("UPDATE light_bans SET active = ?, unbanned_by_id = ?, unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = ?", false, admin.getSteamids().steam2, ban.getId()) > 0; } 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(); jdbcTemplate.update("INSERT INTO light_bans (steam_id, account_id, ban_length, ban_reason, banned_by_id, active, ip, player_name, banned_by) VALUES (?,?,?,?,?,?,?,?,?)", user.getSteamids().steam2, user.getSteamids().account_id, ban_length, ban_reason, admin.getSteamids().steam2, true, user.getPlay_on() != null ? user.getPlay_on().getIp(cryptedCookie) : "", user.getPlay_on() != null ? user.getPlay_on().getName() : user.getSteam_data().getNickname(), admin.getSteam_data().getNickname()); serverService.kickPlayer(user, "banned"); return getBan(user.getSteamids()).getId(); } public List getUsersDiscordWithBanOnServers() { return jdbcTemplate.query("SELECT discord_id FROM light_bans INNER JOIN steam2discord ON light_bans.active = ? AND light_bans.steam_id LIKE steam2discord.steam_id", new Object[]{true}, (rs, n) -> rs.getLong("discord_id")); } public String deCrypt(String crypted) { try { return cryptedCookie.ReadCh(crypted); } catch (Exception e) { return crypted; } } }