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.
205 lines
10 KiB
205 lines
10 KiB
package app.services.db;
|
|
|
|
import app.entities.PlayerProfile;
|
|
import app.entities.Stats;
|
|
import app.entities.db.Permition;
|
|
import app.entities.other.SteamID;
|
|
import app.entities.report.Report;
|
|
import app.entities.report.ReportPermition;
|
|
import app.entities.report.ReportType;
|
|
import app.entities.server.players.RCONPlayer;
|
|
import app.entities.server.request.PlayerOnServer;
|
|
import app.services.ProfileService;
|
|
import app.services.StatsService;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import java.sql.Timestamp;
|
|
import java.time.Instant;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class ReportService {
|
|
|
|
@Value("${backend.report.webhook_url}")
|
|
String webhook_url;
|
|
@Value("${backend.report.kd}")
|
|
long kd;
|
|
HashMap<Long, Long> userKD;
|
|
Stats stats;
|
|
StatsService statsService;
|
|
String tf2_icon = "https://steamcdn-a.akamaihd.net/steamcommunity/public/images/apps/440/e3f595a92552da3d664ad00277fad2107345f743.jpg";
|
|
RestTemplate restTemplate;
|
|
|
|
@Autowired
|
|
@Qualifier("jt_rw")
|
|
private JdbcTemplate jdbcTemplate_rw;
|
|
|
|
@Autowired
|
|
@Qualifier("jt_ro")
|
|
private JdbcTemplate jdbcTemplate_ro;
|
|
|
|
@Autowired
|
|
public ReportService(Stats stats,
|
|
StatsService statsService) {
|
|
this.stats = stats;
|
|
this.statsService = statsService;
|
|
userKD = new HashMap<>();
|
|
restTemplate = new RestTemplate();
|
|
}
|
|
|
|
private long addReport(PlayerProfile current_user, PlayerProfile reported_user, String text, ReportType reportType) {
|
|
PlayerOnServer current_player = (PlayerOnServer) statsService.searchPlayer(current_user);
|
|
PlayerOnServer reported_player = (PlayerOnServer) statsService.searchPlayer(reported_user);
|
|
|
|
long utime = Instant.now().getEpochSecond();
|
|
int player_count = stats.getServers().get(reported_user==null?current_user.getPlay_on().getServer_id():reported_user.getPlay_on().getServer_id()).getPlayer_count();
|
|
String server_id = reported_user==null?current_user.getPlay_on().getServer_id():reported_user.getPlay_on().getServer_id();
|
|
|
|
jdbcTemplate_rw.update("INSERT INTO user_reports (a_nickname, a_steam2, a_permition, a_kills, a_deads, a_seconds, " +
|
|
"r_nickname, r_steam2, r_permition, r_kills, r_deads, r_seconds, " +
|
|
"reasons, utime, srv, online, type) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
|
|
current_player == null ? current_user.getSteam_data().getNickname() : current_player.getName(), current_user.getSteamids().steam2, Permition2Prefix(current_user.getPermition()).ordinal(),
|
|
current_player == null?null:current_player.getScore(), current_player == null?null:current_player.getDeads(),current_player == null?null:current_player.getDuration_seconds(),
|
|
|
|
reported_player==null?null:reported_player.getName(),
|
|
reported_player==null?null:reported_user.getSteamids().steam2,
|
|
reported_player==null?null:Permition2Prefix(reported_user.getPermition()).ordinal(),
|
|
reported_player==null?null:reported_player.getScore(),
|
|
reported_player==null?null:reported_player.getDeads(),
|
|
reported_player==null?null:reported_player.getDuration_seconds(),
|
|
|
|
text, utime, server_id,
|
|
player_count,
|
|
reportType.ordinal());
|
|
|
|
return jdbcTemplate_rw.query("SELECT id FROM user_reports WHERE utime = ? AND srv LIKE ? AND online = ?", new Object[]{utime, server_id, player_count}, (rs, n) -> rs.getLong("id")).get(0);
|
|
}
|
|
|
|
public int addReportAction(int report_id, String action) {
|
|
return jdbcTemplate_rw.update("INSERT INTO user_reports_action (report_id, action) VALUES (?, ?)", report_id, action);
|
|
}
|
|
|
|
public long getCreatedReportsCount(SteamID steamID) {
|
|
return jdbcTemplate_ro.query("SELECT COUNT(*) as c FROM user_reports WHERE a_steam2 LIKE ?", new Object[]{steamID.steam2}, (rs,n) -> rs.getLong("c")).get(0);
|
|
}
|
|
|
|
public long getReportsCount(SteamID steamID) {
|
|
return jdbcTemplate_ro.query("SELECT COUNT(*) as c FROM user_reports WHERE r_steam2 LIKE ?", new Object[]{steamID.steam2}, (rs,n) -> rs.getLong("c")).get(0);
|
|
}
|
|
|
|
public long getCreatedSelfReportsCount(SteamID steamID) {
|
|
return jdbcTemplate_ro.query("SELECT COUNT(*) as c FROM user_reports WHERE a_steam2 LIKE ? AND r_steam2 IS NULL", new Object[]{steamID.steam2}, (rs,n) -> rs.getLong("c")).get(0);
|
|
}
|
|
|
|
public List<Report> getCreatedReports(SteamID steamID) {
|
|
return jdbcTemplate_ro.query("SELECT * FROM user_reports WHERE a_steam2 LIKE ?", new Object[]{steamID.steam2}, (rs,n) -> new Report(rs));
|
|
}
|
|
|
|
public List<Report> getReports(SteamID steamID) {
|
|
return jdbcTemplate_ro.query("SELECT * FROM user_reports WHERE r_steam2 LIKE ?", new Object[]{steamID.steam2}, (rs,n) -> new Report(rs));
|
|
}
|
|
|
|
public long createReport(ReportType reportType, PlayerProfile current_user, PlayerProfile reported_user, String text, String cluster) {
|
|
switch (reportType) {
|
|
case WEBSITE -> {
|
|
if (reported_user.getPlay_on() == null) return 0L;
|
|
if (userKD.containsKey(current_user.getSteamids().steam64) &&
|
|
(Instant.now().getEpochSecond() - userKD.get(current_user.getSteamids().steam64)) < kd) {
|
|
return (Instant.now().getEpochSecond() - userKD.get(current_user.getSteamids().steam64)) - kd;
|
|
}
|
|
|
|
if (!cluster.isEmpty()) {
|
|
userKD.merge(current_user.getSteamids().steam64, Instant.now().getEpochSecond(), (x,y) -> y);
|
|
return userKD.get(current_user.getSteamids().steam64);
|
|
}
|
|
|
|
long report_id = addReport(current_user, reported_user, text, reportType);
|
|
long res = createReportToDiscord(current_user, reported_user, text, report_id);
|
|
if (res == 0) return 0;
|
|
|
|
userKD.merge(current_user.getSteamids().steam64, res, (x,y) -> y);
|
|
return res;
|
|
}
|
|
case IN_GAME -> {
|
|
long report_id = addReport(current_user, reported_user, text, reportType);
|
|
return createReportToDiscord(current_user, reported_user, text, report_id);
|
|
}
|
|
default -> {
|
|
return -1;
|
|
}
|
|
}
|
|
}
|
|
|
|
private long createReportToDiscord(PlayerProfile current_user, PlayerProfile reported_user, String text, long report_id) {
|
|
PlayerOnServer current_player = (PlayerOnServer) statsService.searchPlayer(current_user);
|
|
String author = "%s | %s%s | %s".formatted(
|
|
current_player == null ? current_user.getSteam_data().getNickname() : current_player.getName(),
|
|
current_user.getSteamids().steam2,
|
|
Permition2PrefixToDiscord(current_user.getPermition()),
|
|
current_player == null?"WEBSITE":"%d/%d %s".formatted(
|
|
current_player.getScore(), current_player.getDeads(), current_player.getDuration())
|
|
);
|
|
|
|
String reported = "";
|
|
if (reported_user != null) {
|
|
PlayerOnServer reported_player = (PlayerOnServer) statsService.searchPlayer(reported_user);
|
|
if (reported_player == null) return 0L;
|
|
|
|
reported = "%s | %s%s | %d/%d %s".formatted(
|
|
reported_player.getName(),
|
|
reported_user.getSteamids().steam2,
|
|
Permition2PrefixToDiscord(reported_user.getPermition()),
|
|
reported_player.getScore(),
|
|
reported_player.getDeads(),
|
|
reported_player.getDuration()
|
|
);
|
|
}
|
|
|
|
//Create Embed
|
|
HashMap<String, Object> embed = new HashMap<>();
|
|
embed.put("author", Map.of("name", author, "url", current_user.getSteamids().community_url));
|
|
|
|
if (reported_user != null) {
|
|
embed.put("title", reported);
|
|
embed.put("url", reported_user.getSteamids().community_url);
|
|
}
|
|
embed.put("description", text);
|
|
embed.put("color", report_id); // 255 * 255 * 255 | black
|
|
embed.put("footer", Map.of(
|
|
"text", "%s • %d/%d ".formatted(
|
|
stats.getServers().get(reported_user==null?current_user.getPlay_on().getServer_id():reported_user.getPlay_on().getServer_id()).getName(),
|
|
stats.getServers().get(reported_user==null?current_user.getPlay_on().getServer_id():reported_user.getPlay_on().getServer_id()).getPlayer_count(),
|
|
stats.getServers().get(reported_user==null?current_user.getPlay_on().getServer_id():reported_user.getPlay_on().getServer_id()).getMax_players()),
|
|
"icon_url", tf2_icon
|
|
));
|
|
embed.put("timestamp", Instant.now().toString());
|
|
|
|
HashMap<String, Object> payload = new HashMap<>();
|
|
payload.put("embeds", List.of(embed));
|
|
restTemplate.postForEntity(webhook_url, (Object) payload, String.class);
|
|
return Instant.now().getEpochSecond();
|
|
}
|
|
|
|
private String Permition2PrefixToDiscord(Permition permition) {
|
|
return Permition2Prefix(permition).getPrefixToDiscord();
|
|
}
|
|
|
|
private ReportPermition Permition2Prefix(Permition permition) {
|
|
if (permition == null) return ReportPermition.DONT_HAVE;
|
|
if (permition.getFlags().contains("z")) return ReportPermition.ROOT;
|
|
if (permition.getFlags().contains("b")) return ReportPermition.ADMIN;
|
|
if (permition.getFlags().contains("d")) return ReportPermition.MODERATOR;
|
|
if (permition.getFlags().contains("t")) return ReportPermition.FREE;
|
|
if (permition.getFlags().contains("a")) return ReportPermition.VIP;
|
|
return ReportPermition.UNKNOWN;
|
|
}
|
|
}
|
|
|