Skip to content

Commit

Permalink
⚡ PlayerListMapper parses UUIDs
Browse files Browse the repository at this point in the history
  • Loading branch information
MrGraversen committed Jan 22, 2020
1 parent 76aecb3 commit ba7afcb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 7 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@
<artifactId>tinylog-impl</artifactId>
<version>2.0.1</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.4.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.util.List;

public class PlayerList {
private final List<String> playerNames;
private final List<String> playerUuids;

PlayerList(List<String> playerNames) {
this.playerNames = playerNames;
PlayerList(List<String> playerUuids) {
this.playerUuids = playerUuids;
}

public List<String> getPlayerNames() {
return playerNames;
public List<String> getPlayerUuids() {
return playerUuids;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,34 @@

import io.graversen.minecraft.rcon.RconResponse;
import io.graversen.minecraft.rcon.query.IRconResponseMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class PlayerListMapper implements IRconResponseMapper<PlayerList> {
static final Pattern PATTERN_INITIAL = Pattern.compile(":");
static final Pattern PATTERN_PLAYERS = Pattern.compile(",");

@Override
public PlayerList apply(RconResponse rconResponse) {
final String[] players = rconResponse.getResponseString().split(":");
final String[] players = rconResponse.getResponseString().split(PATTERN_INITIAL.pattern());

if (players.length == 2) {
return new PlayerList(List.of(players[1].split(",")));
return extractPlayerUuids(players[1]);
} else {
return new PlayerList(List.of());
}
}

private PlayerList extractPlayerUuids(String players) {
final var playerUuids = Arrays.stream(players.split(PATTERN_PLAYERS.pattern()))
.map(String::trim)
.map(player -> StringUtils.substringBetween(player, "(", ")"))
.collect(Collectors.toUnmodifiableList());

return new PlayerList(playerUuids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.graversen.minecraft.rcon.query.playerlist;

import io.graversen.minecraft.rcon.RconResponse;
import org.junit.jupiter.api.Test;

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

class PlayerListMapperTest {
private final PlayerListMapper playerListMapper = new PlayerListMapper();

@Test
void apply_playersOnline() {
final var testRconResponse =
new RconResponse(0, 0, 0, 0, "There are 2 of a max 20 players online: MrSkurk (ab9b6457-e657-4a9c-ace6-22a291f92035), test (bb9b6457-e657-4a9c-ace6-22a291f92035)");

final var playerList = playerListMapper.apply(testRconResponse);

assertNotNull(playerList);
assertEquals(2, playerList.getPlayerUuids().size());
assertEquals("ab9b6457-e657-4a9c-ace6-22a291f92035", playerList.getPlayerUuids().get(0));
assertEquals("bb9b6457-e657-4a9c-ace6-22a291f92035", playerList.getPlayerUuids().get(1));
}

@Test
void apply_noPlayersOnline() {
final var testRconResponse =
new RconResponse(0, 0, 0, 0, "There are 0 of a max 20 players online:");

final var playerList = playerListMapper.apply(testRconResponse);

assertNotNull(playerList);
assertTrue(playerList.getPlayerUuids().isEmpty());
}
}

0 comments on commit ba7afcb

Please sign in to comment.