-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
af34895
commit bda5623
Showing
12 changed files
with
179 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/java/io/graversen/rust/rcon/protocol/player/DefaultPlayerManagement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/io/graversen/rust/rcon/protocol/player/PlayerManagement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
48 changes: 48 additions & 0 deletions
48
src/test/java/io/graversen/rust/rcon/protocol/player/DefaultPlayerManagementTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |