diff --git a/cards_framework/src/main/java/src/gent/timdemey/cards/model/entities/commands/CommandHistory.java b/cards_framework/src/main/java/src/gent/timdemey/cards/model/entities/commands/CommandHistory.java index 9f05bad4..b550ef1e 100644 --- a/cards_framework/src/main/java/src/gent/timdemey/cards/model/entities/commands/CommandHistory.java +++ b/cards_framework/src/main/java/src/gent/timdemey/cards/model/entities/commands/CommandHistory.java @@ -102,9 +102,10 @@ public int getSize() private int indexOf(UUID id, boolean forwards) { + int startIdx = forwards ? 0 : getSize() - 1; int stopIdx = forwards ? getLastIndex() + 1 : -1; int incr = forwards ? 1 : -1; - for (int i = getCurrentIndex(); i != stopIdx; i += incr) + for (int i = startIdx; i != stopIdx; i += incr) { CommandExecution cmdExecution = execLine.get(i); @@ -114,9 +115,7 @@ private int indexOf(UUID id, boolean forwards) } } - int min = Math.min(stopIdx, getCurrentIndex()); - int max = Math.max(stopIdx, getCurrentIndex()); - String msg = String.format("No CommandExecution found in the history between [{0} - {1}]", min, max); + String msg = String.format("No CommandExecution found in the command history for id=%s", id); throw new IllegalArgumentException(msg); } @@ -371,6 +370,10 @@ private List redo(List list, State state, bo throw new IllegalStateException("Cannot execute the command " + command.getName() + " because of an error in state, reason: " + resp.reason); } + if (resp.execState == ExecutionState.No) + { + Logger.trace("Cannot execute command because: %s", resp.reason); + } if (execState == CommandExecutionState.Unexecuted) { @@ -553,19 +556,31 @@ public List reject(UUID commandId, State state) { throw new IllegalStateException("Cannot remove!"); } + + CommandExecution cmdExec = getCommandExecution(commandId); + + // a quarantined command is unexecuted so can be safely removed from the + // command history, as if it never existed + if (cmdExec.getExecutionState() == CommandExecutionState.Quarantined) + { + execLine.remove(cmdExec); + return new ArrayList<>(); // there are no failures as no commands need to be redone + } + else + { + // undo all the commands [idx(commandId) <---- currentIdx] + undo(commandId, state); - // undo all the commands [idx(commandId) <---- currentIdx] - undo(commandId, state); - - // enlist all unexecuted commands except the one to erase - List list = new ArrayList<>(execLine.subList(getCurrentIndex() + 2, getLastIndex() + 1)); + // enlist all unexecuted commands except the one to erase + List list = new ArrayList<>(execLine.subList(getCurrentIndex() + 2, getLastIndex() + 1)); - // remove the rejected command - execLine.remove(getCurrentIndex() + 1); + // remove the rejected command + execLine.remove(getCurrentIndex() + 1); - // redo all the unexecuted commands, failures are allowed - List fails = redo(list, state, false); - return fails; + // redo all the unexecuted commands, failures are allowed + List fails = redo(list, state, false); + return fails; + } } /** diff --git a/cards_framework/src/main/java/test/gent/timdemey/cards/test/common/TestBase.java b/cards_framework/src/main/java/test/gent/timdemey/cards/test/common/TestBase.java index 83beec38..57152201 100644 --- a/cards_framework/src/main/java/test/gent/timdemey/cards/test/common/TestBase.java +++ b/cards_framework/src/main/java/test/gent/timdemey/cards/test/common/TestBase.java @@ -1,15 +1,26 @@ package gent.timdemey.cards.test.common; +import java.util.List; +import java.util.UUID; + import org.junit.BeforeClass; import gent.timdemey.cards.App; import gent.timdemey.cards.ICardPlugin; import gent.timdemey.cards.MockCardPlugin; import gent.timdemey.cards.Services; +import gent.timdemey.cards.logging.ILogManager; +import gent.timdemey.cards.model.entities.cards.Card; +import gent.timdemey.cards.model.entities.cards.PlayerConfiguration; +import gent.timdemey.cards.readonlymodel.ReadOnlyCard; +import gent.timdemey.cards.readonlymodel.ReadOnlyCardStack; +import gent.timdemey.cards.readonlymodel.ReadOnlyList; import gent.timdemey.cards.services.context.ContextService; import gent.timdemey.cards.services.context.ContextType; +import gent.timdemey.cards.services.interfaces.ICardGameService; import gent.timdemey.cards.services.interfaces.IContextService; import gent.timdemey.cards.services.interfaces.INetworkService; +import gent.timdemey.cards.test.mock.MockLogManager; import gent.timdemey.cards.test.mock.MockNetworkService; public class TestBase @@ -34,6 +45,31 @@ public boolean isUiThread() ctxtService.initialize(ContextType.UI); App.getServices().install(IContextService.class, ctxtService); App.getServices().install(INetworkService.class, new MockNetworkService()); + App.getServices().install(ILogManager.class, new MockLogManager()); + App.getServices().install(ICardGameService.class, new ICardGameService() + { + + @Override + public int getScore(ReadOnlyCardStack srcCardStack, ReadOnlyCardStack dstCardStack, ReadOnlyList transferedCards) + { + // TODO Auto-generated method stub + return 0; + } + + @Override + public List> getCards() + { + // TODO Auto-generated method stub + return null; + } + + @Override + public List createStacks(List playerIds, List> playerCards) + { + // TODO Auto-generated method stub + return null; + } + }); } public static void installMockCardPlugin() diff --git a/cards_framework/src/main/java/test/gent/timdemey/cards/test/mock/MockLogManager.java b/cards_framework/src/main/java/test/gent/timdemey/cards/test/mock/MockLogManager.java new file mode 100644 index 00000000..ef9154f0 --- /dev/null +++ b/cards_framework/src/main/java/test/gent/timdemey/cards/test/mock/MockLogManager.java @@ -0,0 +1,48 @@ +package gent.timdemey.cards.test.mock; + +import gent.timdemey.cards.logging.ILogManager; +import gent.timdemey.cards.logging.LogLevel; + +public class MockLogManager implements ILogManager +{ + + @Override + public void log(LogLevel lvl, Object msg) + { + log(lvl, msg.toString()); + } + + @Override + public void log(LogLevel lvl, String msg, Object... params) + { + String msgstr = String.format(msg, params); + log(lvl, msgstr); + } + + @Override + public void log(Throwable ex) + { + ex.printStackTrace(System.err); + } + + @Override + public void log(String msg, Throwable ex) + { + log(LogLevel.ERROR, msg); + log(ex); + } + + @Override + public void setLogLevel(LogLevel lvl) + { + // we log everything in the mocks + } + + private void log(LogLevel lvl, String msg) + { + String format = "MockLogManager :: %10s :: %s"; + String msgstr = String.format(format, lvl, msg); + System.out.println(msgstr); + } + +} diff --git a/cards_solitaireshowdown/src/main/java/test/gent/timdemey/cards/services/context/CommandHistoryUnitTest.java b/cards_solitaireshowdown/src/main/java/test/gent/timdemey/cards/services/context/CommandHistoryUnitTest.java index 6d8636b7..c6413dc3 100644 --- a/cards_solitaireshowdown/src/main/java/test/gent/timdemey/cards/services/context/CommandHistoryUnitTest.java +++ b/cards_solitaireshowdown/src/main/java/test/gent/timdemey/cards/services/context/CommandHistoryUnitTest.java @@ -17,6 +17,7 @@ import gent.timdemey.cards.model.entities.commands.CommandExecution; import gent.timdemey.cards.model.entities.commands.CommandExecutionState; import gent.timdemey.cards.model.entities.commands.CommandHistory; +import gent.timdemey.cards.model.entities.game.GameState; import gent.timdemey.cards.model.entities.game.Player; import gent.timdemey.cards.model.entities.game.UDPServer; import gent.timdemey.cards.model.state.State; @@ -71,6 +72,7 @@ public void resetGame() // reset card game cardGame = SolShowCardGameHelper.createFixedSolShowCardGame(player1, player2); state.setCardGame(cardGame); + state.setGameState(GameState.Started); cs_p1depot = cardGame.getCardStack(SolShowTestIds.P1_DEPOT); cs_p1turnover = cardGame.getCardStack(SolShowTestIds.P1_TURNOVER); cs_p1special = cardGame.getCardStack(SolShowTestIds.P1_SPECIAL); @@ -143,7 +145,7 @@ public void awaitAcceptAwaitAwaitAcceptAccept() assertEquals(3, cmdHistory.getSize()); // server accepts cmd3 - cmdHistory.accept(cmd2.id, state); + cmdHistory.accept(cmd3.id, state); // test state assertEquals(2, cmdHistory.getAcceptedIndex()); assertEquals(2, cmdHistory.getCurrentIndex());