Skip to content

Commit

Permalink
Merge pull request #2 from MrGraversen/feature/player-list
Browse files Browse the repository at this point in the history
Feature/player list
  • Loading branch information
MrGraversen authored Oct 4, 2020
2 parents d06db7f + 1a220dd commit 5c24655
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
package io.graversen.minecraft.rcon.commands;

import io.graversen.minecraft.rcon.commands.base.ICommand;
import org.apache.commons.text.StringSubstitutor;

import java.util.Map;

public class PlayerListCommand implements ICommand {
private final boolean uuids;

private PlayerListCommand(boolean uuids) {
this.uuids = uuids;
}

public static PlayerListCommand uuids() {
return new PlayerListCommand(true);
}

public static PlayerListCommand names() {
return new PlayerListCommand(false);
}

@Override
public String command() {
return "list uuids";
return StringSubstitutor.replace("list ${option}", Map.of("option", uuids ? "uuids" : "")).trim();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.graversen.minecraft.rcon.query.playerlist;

import java.util.List;

public class PlayerNames {
private final List<String> playerNames;

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

public List<String> getPlayerNames() {
return playerNames;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package io.graversen.minecraft.rcon.query.playerlist;

import io.graversen.minecraft.rcon.RconResponse;
import io.graversen.minecraft.rcon.query.IRconResponseMapper;

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

public class PlayerNamesMapper implements IRconResponseMapper<PlayerNames> {
static final Pattern PATTERN_INSIDE_PARENTHESIS = Pattern.compile("\\(.*\\)");
static final Pattern PATTERN_INITIAL = Pattern.compile(":");
static final Pattern PATTERN_PLAYERS = Pattern.compile(",");

@Override
public PlayerNames apply(RconResponse rconResponse) {
if (rconResponse.getResponseString() != null) {
final String responseString = rconResponse.getResponseString().trim();
final String[] players = responseString.split(PATTERN_INITIAL.pattern());

if (players.length == 2) {
return extractPlayerList(players[1]);
} else {
return new PlayerNames(List.of());
}
}

return new PlayerNames(List.of());

}

private PlayerNames extractPlayerList(String playersRaw) {
final var players = Arrays.stream(playersRaw.split(PATTERN_PLAYERS.pattern()))
.map(playerRaw -> playerRaw.replaceAll(PATTERN_INSIDE_PARENTHESIS.pattern(), ""))
.map(String::trim)
.collect(Collectors.toUnmodifiableList());

return new PlayerNames(players);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.util.List;

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

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

@Override
public PlayerList apply(RconResponse rconResponse) {
public PlayerUuids apply(RconResponse rconResponse) {
if (rconResponse.getResponseString() != null) {
final String responseString = rconResponse.getResponseString().trim();
final String[] players = responseString.split(PATTERN_INITIAL.pattern());

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

return new PlayerList(List.of());
return new PlayerUuids(List.of());
}

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

return new PlayerList(playerUuids);
return new PlayerUuids(players);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.graversen.minecraft.rcon.commands;

import org.junit.jupiter.api.Test;

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

class PlayerUuidsCommandTest {
@Test
void playerListCommand_uuids() {
final var command = PlayerListCommand.uuids();
assertEquals("list uuids", command.command());
}

@Test
void playerListCommand_names() {
final var command = PlayerListCommand.names();
assertEquals("list", command.command());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 PlayerNamesMapperTest {
private final PlayerNamesMapper playerNamesMapper = new PlayerNamesMapper();

@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 = playerNamesMapper.apply(testRconResponse);

assertNotNull(playerList);
assertEquals(2, playerList.getPlayerNames().size());
assertEquals("MrSkurk", playerList.getPlayerNames().get(0));
assertEquals("test", playerList.getPlayerNames().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 = playerNamesMapper.apply(testRconResponse);

assertNotNull(playerList);
assertTrue(playerList.getPlayerNames().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

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

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

@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);
final var playerList = playerUuidsMapper.apply(testRconResponse);

assertNotNull(playerList);
assertEquals(2, playerList.getPlayerUuids().size());
Expand All @@ -26,7 +26,7 @@ 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);
final var playerList = playerUuidsMapper.apply(testRconResponse);

assertNotNull(playerList);
assertTrue(playerList.getPlayerUuids().isEmpty());
Expand Down

0 comments on commit 5c24655

Please sign in to comment.