From 808b8b2c221fa7b1a544e98b33ba8f65d88cd6b4 Mon Sep 17 00:00:00 2001 From: gsd Date: Tue, 30 May 2023 22:55:36 +0300 Subject: [PATCH] =?UTF-8?q?mute=20unmute=20kick=20=D0=BF=D0=BE=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D1=8C=D1=88=D0=B5=20=D0=B3=D0=BE=D0=B2=D0=BD=D0=BE=D0=BA?= =?UTF-8?q?=D0=BE=D0=B4=D0=B0=20=D0=B8=20=D0=BD=D0=BE=D0=B2=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=88=D1=82=D1=83=D0=BA=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/controllers/admin/KickController.java | 22 +----- .../app/controllers/admin/MuteContoller.java | 73 +++++++++++++++++++ .../handler/GlobalExceptionAdvice.java | 16 ++++ .../java/app/services/ProfileService.java | 19 +++++ src/main/java/app/services/ServerService.java | 6 ++ 5 files changed, 118 insertions(+), 18 deletions(-) create mode 100644 src/main/java/app/controllers/admin/MuteContoller.java diff --git a/src/main/java/app/controllers/admin/KickController.java b/src/main/java/app/controllers/admin/KickController.java index 1b74300..ef7a131 100644 --- a/src/main/java/app/controllers/admin/KickController.java +++ b/src/main/java/app/controllers/admin/KickController.java @@ -45,24 +45,10 @@ public class KickController { @RequestParam(value = "player_name", required = false, defaultValue = "") String player_name, @RequestParam(value = "reason", required = false, defaultValue = "kicked from backend") String reason ) { - if(kicked_steam64.isEmpty() && player_name.isEmpty()) return new ResponseEntity<>(HttpStatus.NO_CONTENT); + PlayerProfile profile = profileService.prepareProfileToAction(steam64, kicked_steam64, player_name); + boolean result = serverService.kickPlayer(profile, reason); - boolean result = false; - PlayerProfile profile = null; - if(!kicked_steam64.isEmpty()) { - profile = profileService.GetProfile(kicked_steam64, List.of()); - } else if (!player_name.isEmpty()) { - profile = profileService.GetProfileOnPlayerOnServers(player_name, List.of()); - } - - if (profile == null) return new ResponseEntity(HttpStatus.NOT_FOUND); - if (!permitionService.CheckMorePowerfull(SteamIDConverter.getSteamID(steam64), profile.getSteamids())) return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); - result = serverService.kickPlayer(profile, reason); - - if(result){ - return new ResponseEntity(HttpStatus.OK); - } else { - return new ResponseEntity(HttpStatus.NOT_FOUND); - } + if (result) return new ResponseEntity(HttpStatus.OK); + else return new ResponseEntity(HttpStatus.NOT_FOUND); } } diff --git a/src/main/java/app/controllers/admin/MuteContoller.java b/src/main/java/app/controllers/admin/MuteContoller.java new file mode 100644 index 0000000..9810fe7 --- /dev/null +++ b/src/main/java/app/controllers/admin/MuteContoller.java @@ -0,0 +1,73 @@ +package app.controllers.admin; + +import app.annotations.interfaces.BurstUpdatePlayers; +import app.annotations.interfaces.CheckPermitionFlag; +import app.annotations.interfaces.CheckWebAccess; +import app.annotations.interfaces.WaitAfterNext; +import app.entities.PlayerProfile; +import app.services.ProfileService; +import app.services.ServerService; +import app.services.db.PermitionService; +import app.utils.SteamIDConverter; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("api/admin/mute") +public class MuteContoller { + ServerService serverService; + ProfileService profileService; + PermitionService permitionService; + + @Autowired + public MuteContoller(ServerService serverService, + ProfileService profileService, + PermitionService permitionService) { + this.serverService = serverService; + this.profileService = profileService; + this.permitionService = permitionService; + } + + @PostMapping + @CheckWebAccess + @CheckPermitionFlag(flag = "c") + @BurstUpdatePlayers + @WaitAfterNext + public ResponseEntity mutePlayer( + HttpServletRequest request, + @CookieValue(value = "steam64") String steam64, + @RequestParam(value = "steam64", required = false, defaultValue = "") String kicked_steam64, + @RequestParam(value = "player_name", required = false, defaultValue = "") String player_name + ) { + PlayerProfile profile = profileService.prepareProfileToAction(steam64, kicked_steam64, player_name); + boolean result = serverService.mutePlayer(profile, true); + + if (result) return new ResponseEntity(HttpStatus.OK); + else return new ResponseEntity(HttpStatus.NOT_FOUND); + } + + @DeleteMapping + @CheckWebAccess + @CheckPermitionFlag(flag = "c") + @BurstUpdatePlayers + @WaitAfterNext + public ResponseEntity unmutePlayer( + HttpServletRequest request, + @CookieValue(value = "steam64") String steam64, + @RequestParam(value = "steam64", required = false, defaultValue = "") String kicked_steam64, + @RequestParam(value = "player_name", required = false, defaultValue = "") String player_name + ) { + PlayerProfile profile = profileService.prepareProfileToAction(steam64, kicked_steam64, player_name); + boolean result = serverService.mutePlayer(profile, false); + + if (result) return new ResponseEntity(HttpStatus.OK); + else return new ResponseEntity(HttpStatus.NOT_FOUND); + } + + +} diff --git a/src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java b/src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java index 5148744..233e87c 100644 --- a/src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java +++ b/src/main/java/app/exceptions/handler/GlobalExceptionAdvice.java @@ -2,6 +2,7 @@ package app.exceptions.handler; import app.annotations.exceptions.*; import app.exceptions.steam.InvalidSteamID; +import app.services.ProfileService; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -44,4 +45,19 @@ public class GlobalExceptionAdvice { public ResponseEntity handWaitRateLimit(){ return new ResponseEntity<>("wait current before call this", HttpStatus.TOO_MANY_REQUESTS); } + + @ExceptionHandler(ProfileService.EmptyRequestPlayer.class) + public ResponseEntity handEmptyRequestPlayer(){ + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + + @ExceptionHandler(ProfileService.LowAdminPermition.class) + public ResponseEntity handLowAdminPermition(){ + return new ResponseEntity(HttpStatus.NOT_ACCEPTABLE); + } + + @ExceptionHandler(ProfileService.NotFoundPlayer.class) + public ResponseEntity handNotFoundPlayer(){ + return new ResponseEntity(HttpStatus.NOT_FOUND); + } } diff --git a/src/main/java/app/services/ProfileService.java b/src/main/java/app/services/ProfileService.java index c002c3d..dafb497 100644 --- a/src/main/java/app/services/ProfileService.java +++ b/src/main/java/app/services/ProfileService.java @@ -152,4 +152,23 @@ public class ProfileService { result = usertimeService.getSteamOnUsername(any); return result; } + + public PlayerProfile prepareProfileToAction(String steam64, String kicked_steam64, String player_name) throws EmptyRequestPlayer, LowAdminPermition, NotFoundPlayer { + if(kicked_steam64.isEmpty() && player_name.isEmpty()) throw new EmptyRequestPlayer(); + + PlayerProfile profile = null; + if(!kicked_steam64.isEmpty()) { + profile = GetProfile(kicked_steam64, List.of()); + } else if (!player_name.isEmpty()) { + profile = GetProfileOnPlayerOnServers(player_name, List.of()); + } + + if (profile == null) throw new NotFoundPlayer(); + if (!permitionService.CheckMorePowerfull(SteamIDConverter.getSteamID(steam64), profile.getSteamids())) throw new LowAdminPermition(); + return profile; + } + + public class EmptyRequestPlayer extends RuntimeException {} + public class LowAdminPermition extends RuntimeException {} + public class NotFoundPlayer extends RuntimeException {} } diff --git a/src/main/java/app/services/ServerService.java b/src/main/java/app/services/ServerService.java index 8bfc334..7b257d5 100644 --- a/src/main/java/app/services/ServerService.java +++ b/src/main/java/app/services/ServerService.java @@ -33,6 +33,12 @@ public class ServerService { .ExecuteRCON("sm_kick #%d %s".formatted(playerProfile.getPlay_on().getPlayer_id(), reason)).contains("kicked"); } + public boolean mutePlayer(PlayerProfile playerProfile, boolean mute) { + if (playerProfile == null || playerProfile.getPlay_on() == null) return false; + return stats.getServers().get(playerProfile.getPlay_on().getServer_id()) + .ExecuteRCON("sm_%s #%d".formatted(mute ? "mute" : "unmute",playerProfile.getPlay_on().getPlayer_id())).contains("микрофон"); + } + public void executeRCONOnAllServers(String command) { ExecutorService executor = Executors.newCachedThreadPool(); List tasks = new ArrayList<>();