Skip to content

Commit

Permalink
Minor changes to CommandTicTacToeAccept for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
lscgh committed Feb 8, 2024
1 parent 1d8980d commit 382b16d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class CommandTicTacToeAccept implements CommandExecutor, TabCompleter {
public static String COMMAND_NAME = "tictactoeaccept";
public static int ARG_COUNT = 1;
public static int PLAYER_NAME_ARG_INDEX = 0;
public static String ERROR_EXECUTION_IS_ONLY_ALLOWED_BY_PLAYERS = ChatColor.RED + "this command may only be executed by players" + ChatColor.RESET;

public CommandTicTacToeAccept(Plugin plugin) {
plugin.getCommand(CommandTicTacToeAccept.COMMAND_NAME).setExecutor(this);
Expand All @@ -30,64 +31,65 @@ public CommandTicTacToeAccept(Plugin plugin) {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

if(!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "this command may only be executed by players" + ChatColor.RESET);
sender.sendMessage(CommandTicTacToeAccept.ERROR_EXECUTION_IS_ONLY_ALLOWED_BY_PLAYERS);
return true;
}

if(args.length != CommandTicTacToeAccept.ARG_COUNT) return false;

String playerName = args[CommandTicTacToeAccept.PLAYER_NAME_ARG_INDEX];

Game targetGame = null;
Game targetGame = this.getQueuedGameWithPlayers(playerName, (Player)sender);

for(Game queuedGame: Game.queuedGames.values()) {
if(queuedGame.config.opponentPlayer != (Player)sender) continue;
if(queuedGame.config.mainPlayer.getName().equals(playerName)) {
targetGame = queuedGame;
break;
}
}

boolean validUUIDIsGiven = false;

if(targetGame == null) {
try {
UUID gameUUID = UUID.fromString(playerName);
validUUIDIsGiven = true;
targetGame = Game.queuedGames.get(gameUUID);
} catch(IllegalArgumentException e) {}
}

if(targetGame == null) {
if(validUUIDIsGiven) {
sender.sendMessage(ChatColor.RED + "This game is not available anymore." + ChatColor.RESET);
} else {
targetGame = this.getQueuedGameByUUID(args[CommandTicTacToeAccept.PLAYER_NAME_ARG_INDEX]);

if(targetGame == null) {
sender.sendMessage(ChatColor.RED + "This game is not available anymore." + ChatColor.RESET);
return true;
}

} catch(IllegalArgumentException e) {
sender.sendMessage(ChatColor.RED + "'" + playerName + "' hasn't sent any game request to you!" + ChatColor.RESET);
return true;
}
return true;
}

targetGame.start();

return true;
}

private Game getQueuedGameWithPlayers(String mainPlayerName, Player opponentPlayer) {
for(Game queuedGame: Game.queuedGames.values()) {
if(queuedGame.config.opponentPlayer != opponentPlayer) continue;
if(queuedGame.config.mainPlayer.getName().equals(mainPlayerName)) {
return queuedGame;
}
}

return null;
}

private Game getQueuedGameByUUID(String uuidString) throws IllegalArgumentException {
UUID gameUUID = UUID.fromString(uuidString);
return Game.queuedGames.get(gameUUID);
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {

if(!(sender instanceof Player)) return new ArrayList<String>();

ArrayList<String> argList = new ArrayList<String>();
for(String arg: args) argList.add(arg);
argList.removeIf((arg) -> arg.isEmpty() && !CommandTicTacToeAccept.listContainsNonEmptyString(argList.subList(0, Math.max(0, argList.indexOf(arg) - 1))));
ArrayList<String> argList = CommandTicTacToeAccept.removeEmptyStringsBeforeStringFromList(args);

if(argList.size() >= CommandTicTacToeAccept.ARG_COUNT) return new ArrayList<String>();

ArrayList<String> completions = new ArrayList<String>();
for(Game queuedGame: Game.queuedGames.values()) {
if(queuedGame.config.opponentPlayer == (Player)sender) {
completions.add(queuedGame.config.mainPlayer.getName());
}
for(Game queuedGame: Game.getRequestsTo((Player)sender)) {
completions.add(queuedGame.config.mainPlayer.getName());
}

ArrayList<String> filteredCompletions = new ArrayList<String>();
Expand All @@ -96,6 +98,14 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
return filteredCompletions;
}

public static ArrayList<String> removeEmptyStringsBeforeStringFromList(String[] list) {
ArrayList<String> newList = new ArrayList<String>();
for(String arg: list) newList.add(arg);

newList.removeIf((item) -> item.isEmpty() && !CommandTicTacToeAccept.listContainsNonEmptyString(newList.subList(0, Math.max(0, newList.indexOf(item) - 1))));

return newList;
}

public static boolean listContainsNonEmptyString(List<String> list) {
for(String string: list) {
Expand Down
13 changes: 13 additions & 0 deletions mavenmcserver/src/main/java/mavenmcserver/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.UUID;

Expand Down Expand Up @@ -47,6 +48,18 @@ public class Game {
/// Contains all queued games that still have to be accepted / rejected
public static HashMap<UUID, Game> queuedGames = new HashMap<UUID, Game>();

public static List<Game> getRequestsTo(Player opponentPlayer) {
ArrayList<Game> result = new ArrayList<Game>();

for(Game queuedGame: Game.queuedGames.values()) {
if(queuedGame.config.opponentPlayer == opponentPlayer) {
result.add(queuedGame);
}
}

return result;
}

/// Contains all games that are currently running in connection to their players (every game is in this map twice!)
public static HashMap<Player, Game> runningGames = new HashMap<Player, Game>();

Expand Down

0 comments on commit 382b16d

Please sign in to comment.