Browse Source

impl discord report system

master
gsd 2 years ago
parent
commit
a900608fbe
  1. 25
      src/main/java/app/controllers/user/ProfileController.java
  2. 9
      src/main/java/app/entities/other/SteamID.java
  3. 2
      src/main/java/app/entities/server/players/DefaultPlayer.java
  4. 7
      src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java
  5. 4
      src/main/java/app/exceptions/steam/InvalidSteamID.java
  6. 95
      src/main/java/app/services/ReportService.java
  7. 5
      src/main/java/app/services/StatsService.java
  8. 3
      src/main/resources/application.yaml

25
src/main/java/app/controllers/user/ProfileController.java

@ -5,6 +5,7 @@ import app.annotations.interfaces.NeedValidCookie;
import app.entities.SocialAuth;
import app.entities.other.SteamID;
import app.services.ProfileService;
import app.services.ReportService;
import app.services.db.FreeVIPService;
import app.utils.SaltedCookie;
import app.utils.SteamIDConverter;
@ -15,19 +16,22 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("api/profile")
@RequestMapping("api/profile/current")
public class ProfileController {
private ProfileService profileService;
private FreeVIPService freeVIPService;
private ReportService reportService;
@Autowired
public ProfileController(ProfileService profileService,
FreeVIPService freeVIPService) {
FreeVIPService freeVIPService,
ReportService reportService) {
this.profileService = profileService;
this.freeVIPService = freeVIPService;
this.reportService = reportService;
}
@GetMapping("/current")
@GetMapping
@NeedValidCookie
public ResponseEntity GetCurrentUser(HttpServletRequest request,
@CookieValue(value = "steam64", defaultValue = "") String steam64,
@ -40,7 +44,7 @@ public class ProfileController {
}
}
@PostMapping("/current/freevip")
@PostMapping("/freevip")
@NeedValidCookie
public ResponseEntity GetFreeVIP(HttpServletRequest request,
@CookieValue(value = "steam64", defaultValue = "") String steam64,
@ -51,4 +55,17 @@ public class ProfileController {
//3.так уж и быть випка без анального зонда
return new ResponseEntity(freeVIPService.addFreeVIP(SteamIDConverter.getSteamID(steam64), socialAuth), HttpStatus.OK);
}
@GetMapping("/report")
@NeedValidCookie
public ResponseEntity<Long> ReportUser(HttpServletRequest request,
@CookieValue(value = "steam64", defaultValue = "") String steam64,
@RequestParam(value = "steam64", defaultValue = "") String reported_steam64,
@RequestParam(value = "text", defaultValue = "") String text) {
return new ResponseEntity(reportService.createReport(
profileService.GetProfile(steam64, "permition,steam_data"),
profileService.GetProfile(reported_steam64, "permition"),
text
), HttpStatus.OK);
}
}

9
src/main/java/app/entities/other/SteamID.java

@ -1,5 +1,6 @@
package app.entities.other;
import app.exceptions.steam.InvalidSteamID;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@JsonSerialize
@ -28,7 +29,11 @@ public class SteamID {
}
public boolean is(SteamID steamID){
if (steam64 == steamID.steam64) return true;
else return false;
try {
if (steam64 == steamID.steam64) return true;
else return false;
} catch (NullPointerException err) {
throw new InvalidSteamID();
}
}
}

2
src/main/java/app/entities/server/players/DefaultPlayer.java

@ -7,6 +7,6 @@ import lombok.Data;
public class DefaultPlayer {
String name;
int score;
int id = 0;
SteamID steam = new SteamID();
}

7
src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java

@ -3,6 +3,8 @@ package app.exceptions.handler;
import app.annotations.exceptions.InvalidCookie;
import app.annotations.exceptions.LowPermition;
import app.annotations.exceptions.NeedCookie;
import app.exceptions.steam.InvalidSteamID;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
@ -24,4 +26,9 @@ public class GlobalExceptionAdvice {
public ResponseEntity handLowPermition(){
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
@ExceptionHandler(InvalidSteamID.class)
public ResponseEntity handInvalidSteamID() {
return new ResponseEntity(HttpStatus.NOT_FOUND);
}
}

4
src/main/java/app/exceptions/steam/InvalidSteamID.java

@ -0,0 +1,4 @@
package app.exceptions.steam;
public class InvalidSteamID extends RuntimeException{
}

95
src/main/java/app/services/ReportService.java

@ -0,0 +1,95 @@
package app.services;
import app.entities.PlayerProfile;
import app.entities.Stats;
import app.entities.db.Permition;
import app.entities.server.players.RCONPlayer;
import com.fasterxml.jackson.databind.ObjectMapper;
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.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
public ReportService(Stats stats,
StatsService statsService) {
this.stats = stats;
this.statsService = statsService;
userKD = new HashMap<>();
restTemplate = new RestTemplate();
}
public long createReport(PlayerProfile current_user, PlayerProfile reported_user, String text) {
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;
}
String author = "%s | %s%s | WEBSITE".formatted(
current_user.getSteam_data().getNickname(),
current_user.getSteamids().steam2,
Permition2Prefix(current_user.getPermition())
);
RCONPlayer reported_player = statsService.searchPlayer(reported_user.getPlay_on());
String reported = "%s | %s%s | %d/? %s".formatted(
reported_player.getName(),
reported_user.getSteamids().steam2,
Permition2Prefix(reported_user.getPermition()),
reported_player.getScore(),
reported_player.getDuration()
);
//Create Embed
HashMap<String, Object> embed = new HashMap<>();
embed.put("author", Map.of("name", author, "url", current_user.getSteamids().community_url));
embed.put("title", reported);
embed.put("url", reported_user.getSteamids().community_url);
embed.put("description", text);
embed.put("color", 1488);
embed.put("footer", Map.of(
"text", "%s • %d/%d".formatted(
stats.getServers().get(reported_user.getPlay_on().getServer_id()).getName(),
stats.getServers().get(reported_user.getPlay_on().getServer_id()).getPlayer_count(),
stats.getServers().get(reported_user.getPlay_on().getServer_id()).getMax_players()),
"icon_url", tf2_icon
));
HashMap<String, Object> payload = new HashMap<>();
payload.put("embeds", List.of(embed));
restTemplate.postForEntity(webhook_url, (Object) payload, String.class);
userKD.merge(current_user.getSteamids().steam64, Instant.now().getEpochSecond(), (x,y) -> y);
return userKD.get(current_user.getSteamids().steam64);
}
public String Permition2Prefix(Permition permition) {
if (permition == null) return "";
if (permition.getFlags().contains("z")) return " | [ROOT]";
if (permition.getFlags().contains("b")) return " | [ADMIN]";
if (permition.getFlags().contains("d")) return " | [MODERATOR]";
if (permition.getFlags().contains("t")) return " | [FREE]";
if (permition.getFlags().contains("a")) return " | [VIP]";
return "";
}
}

5
src/main/java/app/services/StatsService.java

@ -44,6 +44,11 @@ public class StatsService {
return null;
}
public RCONPlayer searchPlayer(PlayOn playOn) {
return (RCONPlayer) stats.getServers().get(playOn.getServer_id()).getPlayers().stream()
.filter(player -> player.getId() != 0 && player.getId() == playOn.getPlayer_id()).findFirst().orElse(null);
}
public String rconExecute(String server_name, String command) {
if (!stats.getServers().containsKey(server_name)) return "Invalid server name";
return stats.getServers().get(server_name).ExecuteRCON(applicationContext, command);

3
src/main/resources/application.yaml

@ -38,6 +38,9 @@ backend:
social:
discord: ${DISCORD_INVITE_URL}
vk: ${VK_URL}
report:
webhook_url: ${REPORT_DISCORD_WEBHOOK}
kd: 300
logging:
level:

Loading…
Cancel
Save