Skip to content

Commit

Permalink
✨ Get sleeping players
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGraversen committed Sep 30, 2023
1 parent af34895 commit bda5623
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 27 deletions.
16 changes: 14 additions & 2 deletions src/main/java/io/graversen/rust/rcon/DefaultRustRconService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import io.graversen.rust.rcon.protocol.dto.ServerInfoDTO;
import io.graversen.rust.rcon.protocol.oxide.DefaultOxideManagement;
import io.graversen.rust.rcon.protocol.oxide.OxideManagement;
import io.graversen.rust.rcon.protocol.player.DefaultPlayerManagement;
import io.graversen.rust.rcon.protocol.player.PlayerManagement;
import io.graversen.rust.rcon.tasks.RconTask;
import io.graversen.rust.rcon.tasks.RustPlayersEmitTask;
import io.graversen.rust.rcon.tasks.ServerInfoEmitTask;
Expand Down Expand Up @@ -39,7 +41,7 @@
public class DefaultRustRconService implements RustRconService {
private final AtomicBoolean isRconLogEnabled = new AtomicBoolean(false);
private final AtomicReference<RustDiagnostics> diagnostics = new AtomicReference<>(null);
private final AtomicReference<List<RustPlayer>> rustPlayers = new AtomicReference<>(List.of());
private final AtomicReference<List<FullRustPlayer>> rustPlayers = new AtomicReference<>(List.of());

private final @NonNull RustRconConfiguration configuration;

Expand All @@ -52,6 +54,7 @@ public class DefaultRustRconService implements RustRconService {
private final Lazy<RustRconRouter> rustRconRouter = Lazy.of(this::createRustRconRouter);
private final Lazy<Codec> codec = Lazy.of(this::createCodec);
private final Lazy<OxideManagement> oxideManagement = Lazy.of(this::createOxideManagement);
private final Lazy<PlayerManagement> playerManagement = Lazy.of(this::createPlayerManagement);

@Override
public Codec codec() {
Expand Down Expand Up @@ -86,6 +89,11 @@ public OxideManagement oxideManagement() {
return oxideManagement.get();
}

@Override
public PlayerManagement playerManagement() {
return playerManagement.get();
}

@Override
public void schedule(@NonNull RconTask task, @NonNull Duration fixedDelay, @Nullable Duration initialDelay) {
final var wrappedTask = wrapRconTask(task);
Expand All @@ -103,7 +111,7 @@ public Optional<RustDiagnostics> diagnostics() {
}

@Override
public List<RustPlayer> rustPlayers() {
public List<FullRustPlayer> players() {
return rustPlayers.get();
}

Expand Down Expand Up @@ -153,6 +161,10 @@ protected OxideManagement createOxideManagement() {
return new DefaultOxideManagement(codec().oxide());
}

protected PlayerManagement createPlayerManagement() {
return new DefaultPlayerManagement(codec().admin());
}

protected RustWebSocketClient createWebSocketClient() {
return new ReconnectingRustWebSocketClient(
configuration.getHostname(),
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/io/graversen/rust/rcon/FullRustPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.graversen.rust.rcon;

import io.graversen.rust.rcon.protocol.util.PlayerName;
import io.graversen.rust.rcon.protocol.util.SteamId64;
import io.graversen.rust.rcon.util.CommonUtils;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.ZonedDateTime;

@Getter
@EqualsAndHashCode(callSuper = true)
public class FullRustPlayer extends RustPlayer {
private final @NonNull String ping;
private final @NonNull String ipAddress;
private final @NonNull Duration connectedDuration;
private final @NonNull BigDecimal health;

public FullRustPlayer(
@NonNull SteamId64 steamId,
@NonNull PlayerName playerName,
@NonNull String ping,
@NonNull String ipAddress,
@NonNull Duration connectedDuration,
@NonNull BigDecimal health
) {
super(steamId, playerName);
this.ping = ping;
this.ipAddress = ipAddress;
this.connectedDuration = connectedDuration;
this.health = health;
}

public ZonedDateTime connectedAt() {
return CommonUtils.now().minus(connectedDuration);
}
}
26 changes: 7 additions & 19 deletions src/main/java/io/graversen/rust/rcon/RustPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,15 @@

import io.graversen.rust.rcon.protocol.util.PlayerName;
import io.graversen.rust.rcon.protocol.util.SteamId64;
import io.graversen.rust.rcon.util.CommonUtils;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Value;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.ZonedDateTime;

@Value
@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
@Getter
@EqualsAndHashCode
@RequiredArgsConstructor
public class RustPlayer {
@NonNull SteamId64 steamId;
@NonNull PlayerName playerName;
@NonNull String ping;
@NonNull String ipAddress;
@NonNull Duration connectedDuration;
@NonNull BigDecimal health;

public ZonedDateTime connectedAt() {
return CommonUtils.now().minus(connectedDuration);
}
private final @NonNull SteamId64 steamId;
private final @NonNull PlayerName playerName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

@RequiredArgsConstructor(access = AccessLevel.PACKAGE)
public class RustPlayerEventListener {
private final @NonNull Consumer<List<RustPlayer>> rustPlayersConsumer;
private final @NonNull Consumer<List<FullRustPlayer>> rustPlayersConsumer;

@Subscribe
public void onServerInfo(RustPlayersEvent rustPlayersEvent) {
final var rustPlayers = mapRustPlayers().apply(rustPlayersEvent);
rustPlayersConsumer.accept(rustPlayers);
}

Function<RustPlayersEvent, List<RustPlayer>> mapRustPlayers() {
Function<RustPlayersEvent, List<FullRustPlayer>> mapRustPlayers() {
return rustPlayersEvent -> {
final var rustPlayers = rustPlayersEvent.getRustPlayers();
return rustPlayers.stream()
Expand All @@ -34,8 +34,8 @@ Function<RustPlayersEvent, List<RustPlayer>> mapRustPlayers() {
};
}

Function<RustPlayerDTO, RustPlayer> mapRustPlayer() {
return rustPlayerDTO -> new RustPlayer(
Function<RustPlayerDTO, FullRustPlayer> mapRustPlayer() {
return rustPlayerDTO -> new FullRustPlayer(
SteamId64.parseOrFail(rustPlayerDTO.getSteamId()),
new PlayerName(rustPlayerDTO.getPlayerName()),
rustPlayerDTO.getPing(),
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/io/graversen/rust/rcon/RustRconService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.graversen.rust.rcon.protocol.Codec;
import io.graversen.rust.rcon.protocol.dto.ServerInfoDTO;
import io.graversen.rust.rcon.protocol.oxide.OxideManagement;
import io.graversen.rust.rcon.protocol.player.PlayerManagement;
import io.graversen.rust.rcon.tasks.RconTask;
import io.graversen.rust.rcon.util.EventEmitter;
import lombok.NonNull;
Expand All @@ -24,9 +25,11 @@ public interface RustRconService extends EventEmitter {

OxideManagement oxideManagement();

PlayerManagement playerManagement();

void schedule(@NonNull RconTask task, @NonNull Duration fixedDelay, @Nullable Duration initialDelay);

Optional<RustDiagnostics> diagnostics();

List<RustPlayer> rustPlayers();
List<FullRustPlayer> players();
}
2 changes: 2 additions & 0 deletions src/main/java/io/graversen/rust/rcon/protocol/AdminCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public interface AdminCodec {
CompletableFuture<RustRconResponse> serverInfo();

CompletableFuture<RustRconResponse> playerList();

CompletableFuture<RustRconResponse> sleepingPlayers();
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,10 @@ public CompletableFuture<RustRconResponse> playerList() {
final var rconMessage = compile(PLAYER_LIST);
return send(rconMessage);
}

@Override
public CompletableFuture<RustRconResponse> sleepingPlayers() {
final var rconMessage = compile(SLEEPING_PLAYERS);
return send(rconMessage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static class AdminProtocol {
public static final String UNMUTE_PLAYER = "global.unmute \"" + STEAM_ID_64 + "\"";
public static final String SERVER_INFO = "global.serverinfo";
public static final String PLAYER_LIST = "playerlist";
public static final String SLEEPING_PLAYERS = "global.sleepingusers";
}

public static class OxideProtocol {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class DefaultOxideManagement implements OxideManagement {
private static final String PLUGINS_REGEX_STRING = "\\d+\\s+\"(.*?)\"\\s+\\((.*?)\\)\\s+by\\s+(.*?)\\s+\\(.*?\\)\\s+-\\s+(.*?)$";
private static final Pattern PLUGINS_REGEX = Pattern.compile(PLUGINS_REGEX_STRING, Pattern.MULTILINE);


private final @NonNull OxideCodec oxideCodec;

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package io.graversen.rust.rcon.protocol.player;

import io.graversen.rust.rcon.RustPlayer;
import io.graversen.rust.rcon.RustRconResponse;
import io.graversen.rust.rcon.protocol.AdminCodec;
import io.graversen.rust.rcon.protocol.util.PlayerName;
import io.graversen.rust.rcon.protocol.util.SteamId64;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;

@Slf4j
@RequiredArgsConstructor
public class DefaultPlayerManagement implements PlayerManagement {
private final @NonNull AdminCodec adminCodec;

@Override
public CompletableFuture<List<RustPlayer>> sleepingPlayers() {
return adminCodec.sleepingPlayers().thenApply(mapSleepingPlayers());
}

Function<RustRconResponse, List<RustPlayer>> mapSleepingPlayers() {
return rconResponse -> {
try {
final var message = rconResponse.getMessage();
return Arrays.stream(message.split("\n"))
.filter(line -> !line.isBlank())
.filter(line -> !line.contains("sleeping users"))
.map(line -> line.split(":"))
.map(parts -> new RustPlayer(SteamId64.parseOrFail(parts[0].trim()), new PlayerName(parts[1].trim())))
.toList();
} catch (Exception e) {
log.error(e.getMessage(), e);
return List.of();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package io.graversen.rust.rcon.protocol.player;

import io.graversen.rust.rcon.RustPlayer;

import java.util.List;
import java.util.concurrent.CompletableFuture;

public interface PlayerManagement {
CompletableFuture<List<RustPlayer>> sleepingPlayers();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package io.graversen.rust.rcon.protocol.player;

import io.graversen.rust.rcon.RustPlayer;
import io.graversen.rust.rcon.TestRustRconResponse;
import io.graversen.rust.rcon.protocol.AdminCodec;
import io.graversen.rust.rcon.protocol.util.PlayerName;
import io.graversen.rust.rcon.protocol.util.SteamId64;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
class DefaultPlayerManagementTest {
@Mock
private AdminCodec adminCodec;

@InjectMocks
private DefaultPlayerManagement defaultPlayerManagement;

@Test
void mapSleepingPlayers() {
final var sleepingPlayersOutput = "\n" +
"76561198127947780:DKautobahnTV\n" +
"76561198813879070:MrCool88\n" +
"76561198072271313:Trey\n" +
"76561198203638647:cuzzy\n" +
"76561198088326077:Hirabayashi\n" +
"76561198436002452:Arska\n" +
"76561198973297741:Nikke\n" +
"76561198251899848:Klarius\n" +
"76561198307063094:IX\n" +
"76561198046357656:DarkDouchebag\n" +
"76561198075300933:[RTA]PenetrationsKonsulten\n" +
"76561198154164007:Hubbi3000\n" +
"76561198201936914:Bosthief\n" +
"13 sleeping users\n";

final var sleepingPlayers = defaultPlayerManagement.mapSleepingPlayers().apply(new TestRustRconResponse(sleepingPlayersOutput));
assertFalse(sleepingPlayers.isEmpty());
assertEquals(13, sleepingPlayers.size());
assertEquals(new RustPlayer(SteamId64.parseOrFail("76561198127947780"), new PlayerName("DKautobahnTV")), sleepingPlayers.get(0));
assertEquals(new RustPlayer(SteamId64.parseOrFail("76561198201936914"), new PlayerName("Bosthief")), sleepingPlayers.get(12));
}
}

0 comments on commit bda5623

Please sign in to comment.