Test framework: simplified AI logic and tests, added usage comments. Devs recommendations:

* in card's code use player.isComputer instead player.isHuman (it help to split Human/AI logic and test both by unit tests);
* usage example: AI hint to skip or auto-calculate choices instead call of real choose dialogs;
* unit tests for Human logic: call normal commands;
* unit tests for AI logic: call aiXXX commands;
This commit is contained in:
Oleg Agafonov 2021-03-07 22:51:58 +04:00
parent 00c7b3753c
commit 2906f86324
22 changed files with 106 additions and 47 deletions

View file

@ -319,7 +319,7 @@ public abstract class AbilityImpl implements Ability {
// unit tests only: it allows to add targets/choices by two ways:
// 1. From cast/activate command params (process it here)
// 2. From single addTarget/setChoice, it's a preffered method for tests (process it in normal choose dialogs like human player)
if (controller.isTestMode()) {
if (controller.isTestsMode()) {
if (!controller.addTargets(this, game)) {
return false;
}

View file

@ -94,8 +94,9 @@ public class AssistAbility extends SimpleStaticAbility implements AlternateManaP
}
// AI can't use assist (can't ask another player to help), maybe in teammode it can be enabled, but tests must works all the time
// Outcome.AIDontUseIt
Player controller = game.getPlayer(source.getControllerId());
if (controller != null && !controller.isTestMode() && !controller.isHuman()) {
if (controller != null && controller.isComputer()) {
return options;
}
@ -170,7 +171,7 @@ class AssistEffect extends OneShotEffect {
if (controller != null && spell != null && targetPlayer != null) {
// AI can't assist other players, maybe for teammates only (but tests must work as normal)
int amountToPay = 0;
if (targetPlayer.isHuman() || targetPlayer.isTestMode()) {
if (!targetPlayer.isComputer()) {
amountToPay = targetPlayer.announceXMana(0, unpaid.getMana().getGeneric(),
"How much mana to pay as assist for " + controller.getName() + "?", game, source);
}

View file

@ -1418,7 +1418,7 @@ public abstract class GameImpl implements Game, Serializable {
+ ex.getMessage());
}
Player activePlayer = this.getPlayer(getActivePlayerId());
if (activePlayer != null && !activePlayer.isTestMode()) {
if (activePlayer != null && !activePlayer.isTestsMode()) {
errorContinueCounter++;
continue;
} else {

View file

@ -48,8 +48,32 @@ import java.util.*;
*/
public interface Player extends MageItem, Copyable<Player> {
/**
* Current player is real life player (human). Try to use in GUI and network engine only.
*
* WARNING, you must use isComputer instead isHuman in card's code (for good Human/AI logic testing in unit tests)
* TODO: check combat code and other and replace isHuman to isComputer usage if possible (if AI support that actions)
* @return
*/
boolean isHuman();
boolean isTestsMode();
/**
* Current player is AI. Use it in card's code and all other places.
*
* It help to split Human/AI logic and test both by unit tests.
*
* Usage example: AI hint to skip or auto-calculate choices instead call of real choose dialogs
* - unit tests for Human logic: call normal commands
* - unit tests for AI logic: call aiXXX commands
*
* @return
*/
default boolean isComputer() {
return !isHuman();
}
String getName();
String getLogName();
@ -314,8 +338,6 @@ public interface Player extends MageItem, Copyable<Player> {
void setGameUnderYourControl(boolean value, boolean fullRestore);
boolean isTestMode();
void setTestMode(boolean value);
void addAction(String action);

View file

@ -2715,7 +2715,7 @@ public abstract class PlayerImpl implements Player, Serializable {
}
// only humans can use it
if (!targetPlayer.isHuman() && !targetPlayer.isTestMode()) {
if (targetPlayer.isComputer()) {
return false;
}
@ -3866,7 +3866,7 @@ public abstract class PlayerImpl implements Player, Serializable {
}
@Override
public boolean isTestMode() {
public boolean isTestsMode() {
return isTestMode;
}