Merge pull request #11411 from tirth1/refactor-gameController

This commit is contained in:
Oleg Agafonov 2023-11-16 04:43:47 +03:00 committed by GitHub
commit 892b4ebd95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 191 additions and 77 deletions

View file

@ -1322,87 +1322,13 @@ public class GameController implements GameCallback {
List<String> fixActions = new ArrayList<>(); // for logs info
// fix active
Player playerActive = game.getPlayer(state.getActivePlayerId());
sb.append("<br>Fixing active player: ").append(getName(playerActive));
if (playerActive != null && !playerActive.canRespond()) {
fixActions.add("active player fix");
sb.append("<br><font color='red'>WARNING, active player can't respond.</font>");
sb.append("<br>Try to concede...");
playerActive.concede(game);
playerActive.leave(); // abort any wait response actions
sb.append(" (").append(asWarning("OK")).append(", concede done)");
sb.append("<br>Try to skip step...");
Phase currentPhase = game.getPhase();
if (currentPhase != null) {
currentPhase.getStep().skipStep(game, state.getActivePlayerId());
fixedAlready = true;
sb.append(" (").append(asWarning("OK")).append(", skip step done)");
} else {
sb.append(" (").append(asBad("FAIL")).append(", step is null)");
}
} else {
sb.append(playerActive != null ? " (" + asGood("OK") + ", can respond)" : " (" + asGood("OK") + ", no player)");
}
fixedAlready = fixPlayer(game.getPlayer(state.getActivePlayerId()), state, "active", sb, fixActions, fixedAlready);
// fix lost choosing dialog
Player choosingPlayer = game.getPlayer(state.getChoosingPlayerId());
sb.append("<br>Fixing choosing player: ").append(getName(choosingPlayer));
if (choosingPlayer != null && !choosingPlayer.canRespond()) {
fixActions.add("choosing player fix");
sb.append("<br><font color='red'>WARNING, choosing player can't respond.</font>");
sb.append("<br>Try to concede...");
choosingPlayer.concede(game);
choosingPlayer.leave(); // abort any wait response actions
sb.append(" (").append(asWarning("OK")).append(", concede done)");
sb.append("<br>Try to skip step...");
if (fixedAlready) {
sb.append(" (OK, already skipped before)");
} else {
Phase currentPhase = game.getPhase();
if (currentPhase != null) {
currentPhase.getStep().skipStep(game, state.getActivePlayerId());
fixedAlready = true;
sb.append(" (").append(asWarning("OK")).append(", skip step done)");
} else {
sb.append(" (").append(asBad("FAIL")).append(", step is null)");
}
}
} else {
sb.append(choosingPlayer != null ? " (" + asGood("OK") + ", can respond)" : " (" + asGood("OK") + ", no player)");
}
fixedAlready = fixPlayer(game.getPlayer(state.getChoosingPlayerId()), state, "choosing", sb, fixActions, fixedAlready);
// fix lost priority
Player priorityPlayer = game.getPlayer(state.getPriorityPlayerId());
sb.append("<br>Fixing priority player: ").append(getName(priorityPlayer));
if (priorityPlayer != null && !priorityPlayer.canRespond()) {
fixActions.add("priority player fix");
sb.append("<br><font color='red'>WARNING, priority player can't respond.</font>");
sb.append("<br>Try to concede...");
priorityPlayer.concede(game);
priorityPlayer.leave(); // abort any wait response actions
sb.append(" (").append(asWarning("OK")).append(", concede done)");
sb.append("<br>Try to skip step...");
if (fixedAlready) {
sb.append(" (").append(asWarning("OK")).append(", already skipped before)");
} else {
Phase currentPhase = game.getPhase();
if (currentPhase != null) {
currentPhase.getStep().skipStep(game, state.getActivePlayerId());
fixedAlready = true;
sb.append(" (").append(asWarning("OK")).append(", skip step done)");
} else {
sb.append(" (").append(asBad("FAIL")).append(", step is null)");
}
}
} else {
sb.append(priorityPlayer != null ? " (" + asGood("OK") + ", can respond)" : " (" + asGood("OK") + ", no player)");
}
fixedAlready = fixPlayer(game.getPlayer(state.getPriorityPlayerId()), state, "priority", sb, fixActions, fixedAlready);
// fix timeout
sb.append("<br>Fixing future timeout: ");
@ -1441,4 +1367,35 @@ public class GameController implements GameCallback {
return sb.toString();
}
private boolean fixPlayer(Player player, GameState state, String fixType, StringBuilder sb, List<String> fixActions, boolean fixedAlready) {
sb.append("<br>Fixing ").append(fixType).append(" player: ").append(getName(player));
if (player != null && !player.canRespond()) {
fixActions.add(fixType + " fix");
sb.append("<br><font color='red'>WARNING, ").append(fixType).append(" player can't respond.</font>");
sb.append("<br>Try to concede...");
player.concede(game);
player.leave(); // abort any wait response actions
sb.append(" (").append(asWarning("OK")).append(", concede done)");
sb.append("<br>Try to skip step...");
if (!fixedAlready) {
Phase currentPhase = game.getPhase();
if (currentPhase != null) {
currentPhase.getStep().skipStep(game, state.getActivePlayerId());
fixedAlready = true;
sb.append(" (").append(asWarning("OK")).append(", skip step done)");
} else {
sb.append(" (").append(asBad("FAIL")).append(", step is null)");
}
} else {
sb.append(" (OK, already skipped before)");
}
} else {
sb.append(player != null ? " (" + asGood("OK") + ", can respond)" : " (" + asGood("OK") + ", no player)");
}
return fixedAlready;
}
}

View file

@ -7,6 +7,7 @@ import com.google.common.collect.ImmutableSortedSet;
import mage.constants.WatcherScope;
import mage.game.Game;
import mage.game.events.GameEvent;
import mage.players.PlayerList;
import mage.watchers.Watcher;
import org.junit.Test;
@ -187,6 +188,8 @@ public class WatcherTest {
private Map<String, SortedSet<String>> sortedSetInMapField = new HashMap<>();
private Map<String, PlayerList> playerListInMapField = new HashMap<>();
public TestWatcher(WatcherScope scope) {
super(scope);
}
@ -248,8 +251,41 @@ public class WatcherTest {
return sortedSetInMapField;
}
public Map<String, PlayerList> getPlayerListInMapField() {
return playerListInMapField;
}
public void setSortedSetInMapField(Map<String, SortedSet<String>> sortedSetInMapField) {
this.sortedSetInMapField = sortedSetInMapField;
}
public void setPlayerListInMapField(Map<String, PlayerList> playerListInMapField) {
this.playerListInMapField = playerListInMapField;
}
}
@Test
public void testDeepCopyMapOfPlayerList() {
// Given
Map<String, PlayerList> playerListInMapField = new HashMap<>();
playerListInMapField.put("pl1", new PlayerList());
playerListInMapField.put("pl2", new PlayerList());
TestWatcher testWatcher = new TestWatcher(GAME);
testWatcher.setPlayerListInMapField(playerListInMapField);
// When
TestWatcher copy = testWatcher.copy();
// And
testWatcher.getPlayerListInMapField().put("pl3", new PlayerList());
// Then
Map<String, PlayerList> copyPlayerListInMapField = copy.getPlayerListInMapField();
assertEquals(2, copyPlayerListInMapField.size());
assertTrue(copyPlayerListInMapField.containsKey("pl1"));
assertTrue(copyPlayerListInMapField.containsKey("pl2"));
assertFalse(copyPlayerListInMapField.containsKey("pl3"));
assertEquals(copyPlayerListInMapField.get("pl1").getClass(), playerListInMapField.get("pl1").getClass());
}
}

View file

@ -85,4 +85,27 @@ public class CounterTest {
assertEquals(1, defaultCounter.getCount());
assertEquals("default", defaultCounter.getName());
}
@Test
public void testHashCodeConsistency() {
// given
// when
Counter copy = new Counter(counter);
//then
assertEquals(counter.hashCode(), copy.hashCode());
}
@Test
public void testEqualsReturnsFalse() {
// given
Counter testCounter = new Counter("default", 1);
// when
//then
assertFalse(counter.equals(testCounter));
assertFalse(counter.equals(null));
}
}

View file

@ -0,0 +1,98 @@
package mage.counters;
import org.junit.Before;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.*;
public class CountersTest {
private Counters counters;
@Before
public void setUp() {
counters = new Counters();
}
@Test
public void testCopyCounter() {
// given
counters.addCounter("test", 4);
// when
Counters copy = counters.copy();
// then
int amount = copy.getCount("test");
assertEquals(5, amount);
}
@Test
public void testRemoveCounter() {
// given
counters.addCounter("test1", 5);
counters.addCounter("test2", 5);
// when
//then
assertTrue(counters.removeCounter("test1", 6));
assertFalse(counters.containsKey("test1"));
assertTrue(counters.removeCounter("test2", 2));
assertTrue(counters.containsKey("test2"));
assertFalse(counters.removeCounter("test3", 5));
}
@Test
public void testAddCounterWithNewCounter() {
// given
counters.addCounter("test1", 5);
// when
counters.addCounter("test1", 10);
counters.addCounter("test2", 5);
// then
assertNotEquals(15, counters.getCount("test1"));
assertEquals(16, counters.getCount("test1"));
assertTrue(counters.containsKey("test2"));
assertEquals(6, counters.getCount("test2"));
}
@Test
public void testRemoveAllCounter() {
// given
counters.addCounter("test", 10);
// when
counters.removeAllCounters("test");
// then
assertFalse(counters.containsKey("test"));
}
@Test
public void testGetCount() {
// given
counters.addCounter("test1", 5);
// when
int count1 = counters.getCount("test1");
int count2 = counters.getCount("test2");
// then
assertEquals(0, count2);
assertEquals(6, count1);
}
@Test
public void testContainsKey() {
// given
counters.addCounter("test1", 5);
// when
// then
assertTrue(counters.containsKey("test1"));
assertFalse(counters.containsKey("test2"));
}
}