-
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.
✨ Periodically query current players
- Loading branch information
1 parent
c3d6b2a
commit 4631e08
Showing
13 changed files
with
208 additions
and
0 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,28 @@ | ||
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.AccessLevel; | ||
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) | ||
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); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/io/graversen/rust/rcon/RustPlayerEventListener.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,47 @@ | ||
package io.graversen.rust.rcon; | ||
|
||
import com.google.common.eventbus.Subscribe; | ||
import io.graversen.rust.rcon.event.server.RustPlayersEvent; | ||
import io.graversen.rust.rcon.protocol.dto.RustPlayerDTO; | ||
import io.graversen.rust.rcon.protocol.util.PlayerName; | ||
import io.graversen.rust.rcon.protocol.util.SteamId64; | ||
import lombok.AccessLevel; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.math.BigDecimal; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
|
||
@RequiredArgsConstructor(access = AccessLevel.PACKAGE) | ||
public class RustPlayerEventListener { | ||
private final @NonNull Consumer<List<RustPlayer>> rustPlayersConsumer; | ||
|
||
@Subscribe | ||
public void onServerInfo(RustPlayersEvent rustPlayersEvent) { | ||
final var rustPlayers = mapRustPlayers().apply(rustPlayersEvent); | ||
rustPlayersConsumer.accept(rustPlayers); | ||
} | ||
|
||
Function<RustPlayersEvent, List<RustPlayer>> mapRustPlayers() { | ||
return rustPlayersEvent -> { | ||
final var rustPlayers = rustPlayersEvent.getRustPlayers(); | ||
return rustPlayers.stream() | ||
.map(mapRustPlayer()) | ||
.toList(); | ||
}; | ||
} | ||
|
||
Function<RustPlayerDTO, RustPlayer> mapRustPlayer() { | ||
return rustPlayerDTO -> new RustPlayer( | ||
SteamId64.parseOrFail(rustPlayerDTO.getSteamId()), | ||
new PlayerName(rustPlayerDTO.getPlayerName()), | ||
rustPlayerDTO.getPing(), | ||
rustPlayerDTO.getIpAddress().split(":")[0], | ||
Duration.ofSeconds(rustPlayerDTO.getConnectedSeconds()), | ||
BigDecimal.valueOf(rustPlayerDTO.getHealth()) | ||
); | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
src/main/java/io/graversen/rust/rcon/event/server/RustPlayersEvent.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,20 @@ | ||
package io.graversen.rust.rcon.event.server; | ||
|
||
import io.graversen.rust.rcon.RustServer; | ||
import io.graversen.rust.rcon.protocol.dto.RustPlayerDTO; | ||
import lombok.Getter; | ||
import lombok.NonNull; | ||
import lombok.ToString; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@ToString(callSuper = true) | ||
public class RustPlayersEvent extends ServerEvent { | ||
private final @NonNull List<RustPlayerDTO> rustPlayers; | ||
|
||
public RustPlayersEvent(@NonNull RustServer server, @NonNull List<RustPlayerDTO> rustPlayers) { | ||
super(server); | ||
this.rustPlayers = rustPlayers; | ||
} | ||
} |
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
30 changes: 30 additions & 0 deletions
30
src/main/java/io/graversen/rust/rcon/protocol/dto/RustPlayerDTO.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,30 @@ | ||
package io.graversen.rust.rcon.protocol.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE) | ||
public class RustPlayerDTO { | ||
@JsonProperty("SteamID") | ||
private final String steamId; | ||
|
||
@JsonProperty("DisplayName") | ||
private final String playerName; | ||
|
||
@JsonProperty("Ping") | ||
private final String ping; | ||
|
||
@JsonProperty("Address") | ||
private final String ipAddress; | ||
|
||
@JsonProperty("ConnectedSeconds") | ||
private final Integer connectedSeconds; | ||
|
||
@JsonProperty("Health") | ||
private final Double health; | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/io/graversen/rust/rcon/tasks/RustPlayersEmitTask.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,34 @@ | ||
package io.graversen.rust.rcon.tasks; | ||
|
||
import io.graversen.rust.rcon.RustServer; | ||
import io.graversen.rust.rcon.event.server.RustPlayersEvent; | ||
import io.graversen.rust.rcon.protocol.dto.RustPlayerDTO; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class RustPlayersEmitTask implements RconTask { | ||
private final @NonNull RustServer server; | ||
private final @NonNull Supplier<CompletableFuture<List<RustPlayerDTO>>> rustPlayersGetter; | ||
private final @NonNull Consumer<RustPlayersEvent> rustPlayersEventEmitter; | ||
|
||
@Override | ||
public void run() { | ||
rustPlayersGetter.get() | ||
.thenApply(rustPlayersEventMapper()) | ||
.thenAccept(rustPlayersEventEmitter); | ||
} | ||
|
||
Function<List<RustPlayerDTO>, RustPlayersEvent> rustPlayersEventMapper() { | ||
return rustPlayers -> new RustPlayersEvent(server, rustPlayers); | ||
} | ||
|
||
} |
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