cheats: fixed AI naming, improved cheat commands compatibility:

- added additional aliases for Human (me) and Computer (opponent, ai);
- now same cheat command will be work in real, test, duel or multiplayer games (Computer/Human);
- now same cheat command will be work in human only games;
- fixed wrong/random AI opponent selection in multiplayer games;
- fixed wrong opponent selection after first opponent's loose in multiplayer games;
This commit is contained in:
Oleg Agafonov 2025-11-02 01:07:22 +04:00
parent 064c102590
commit a0bdfda912
2 changed files with 53 additions and 17 deletions

View file

@ -1700,7 +1700,7 @@ public class TablesPanel extends javax.swing.JPanel {
MatchOptions options = new MatchOptions(gameName, gameType, multiPlayer); MatchOptions options = new MatchOptions(gameName, gameType, multiPlayer);
options.getPlayerTypes().add(PlayerType.HUMAN); options.getPlayerTypes().add(PlayerType.HUMAN);
options.getPlayerTypes().add(aiType); options.getPlayerTypes().add(aiType);
for (int i=2 ; i < numPlayers ; i++) { for (int i = 2; i < numPlayers; i++) {
options.getPlayerTypes().add(aiType); options.getPlayerTypes().add(aiType);
} }
options.setDeckType("Variant Magic - Freeform Commander"); options.setDeckType("Variant Magic - Freeform Commander");
@ -1719,9 +1719,9 @@ public class TablesPanel extends javax.swing.JPanel {
table = SessionHandler.createTable(roomId, options); table = SessionHandler.createTable(roomId, options);
SessionHandler.joinTable(roomId, table.getTableId(), "Human", PlayerType.HUMAN, 1, testDeck, ""); SessionHandler.joinTable(roomId, table.getTableId(), "Human", PlayerType.HUMAN, 1, testDeck, "");
SessionHandler.joinTable(roomId, table.getTableId(), "Computer", aiType, 1, testDeck, ""); SessionHandler.joinTable(roomId, table.getTableId(), "Computer" + (multiPlayer ? " 2" : ""), aiType, 1, testDeck, "");
for (int i=2 ; i < numPlayers ; i++) { for (int i = 2; i < numPlayers; i++) {
SessionHandler.joinTable(roomId, table.getTableId(), "Computer" + i, aiType, 1, testDeck, ""); SessionHandler.joinTable(roomId, table.getTableId(), "Computer " + (i + 1), aiType, 1, testDeck, "");
} }
SessionHandler.startMatch(roomId, table.getTableId()); SessionHandler.startMatch(roomId, table.getTableId());
} catch (HeadlessException ex) { } catch (HeadlessException ex) {

View file

@ -601,14 +601,13 @@ public final class SystemUtil {
continue; continue;
} }
Optional<Player> playerOptional = findPlayer(game, command.player); Player player = findPlayer(game, command.player, feedbackPlayer);
if (!playerOptional.isPresent()) { if (player == null) {
String mes = String.format("Unknown player: %s", line); String mes = String.format("Unknown player: %s", line);
errorsList.add(mes); errorsList.add(mes);
logger.warn(mes); logger.warn(mes);
continue; continue;
} }
Player player = playerOptional.get();
// SPECIAL token/emblem call (without SET name) // SPECIAL token/emblem call (without SET name)
if ("token".equalsIgnoreCase(command.zone)) { if ("token".equalsIgnoreCase(command.zone)) {
@ -860,17 +859,54 @@ public final class SystemUtil {
return false; return false;
} }
/** private static Player findPlayer(Game game, String needName, Player feedbackPlayer) {
* Find player by name. // real names
* Player res = game.getPlayerList().stream()
* @param game .map(game::getPlayer)
* @param name .filter(Objects::nonNull)
* @return .filter(player -> player.getName().equals(needName))
*/ .findFirst()
private static Optional<Player> findPlayer(Game game, String name) { .orElse(null);
return game.getPlayers().values().stream()
.filter(player -> player.getName().equals(name)).findFirst();
// test names - cheat commands will be compatible in both modes (quick test and normal)
if (res == null) {
switch (needName.toLowerCase(Locale.ENGLISH)) {
case "me":
case "human":
case "human 1":
res = feedbackPlayer;
break;
case "opponent":
case "opponent 1":
case "computer":
case "computer 1": // multiplayer game uses Computer 2+ naming
case "ai":
case "ai 1":
// try AI
res = game.getPlayerList().stream()
.map(game::getPlayer)
.filter(Objects::nonNull)
.filter(Player::isInGame)
.filter(Player::isComputer)
.findFirst()
.orElse(null);
if (res == null) {
// try opponent (human only games)
res = game.getOpponents(feedbackPlayer.getId(), true).stream()
.map(game::getPlayer)
.filter(Objects::nonNull)
.filter(Player::isInGame)
.findFirst()
.orElse(null);
}
break;
default:
// raise error message due unknown player name
break;
}
}
return res;
} }
public static String sanitize(String input) { public static String sanitize(String input) {