def test_undo_then_do_different(): game_name = "game" playername = "player" parent_markerid = "Rd01" child_markerid = "Rd02" other_markerid = "Rd03" parent_creature_names = 4 * [None] child_creature_names = 4 * [None] history = History.History() action = Action.SplitLegion(game_name, playername, parent_markerid, child_markerid, parent_creature_names, child_creature_names) history.update(None, action, None) undo_action = Action.UndoSplit(game_name, playername, parent_markerid, child_markerid, parent_creature_names, child_creature_names) history.update(None, undo_action, None) action2 = Action.SplitLegion(game_name, playername, parent_markerid, other_markerid, parent_creature_names, child_creature_names) history.update(None, action2, None) assert history.actions == [action2] assert history.undone == [] assert history.can_undo(playername) assert not history.can_redo(playername)
def split_legion(self, parent_markerid, child_markerid, parent_creature_names, child_creature_names): logging.info("%s %s %s %s", parent_markerid, child_markerid, parent_creature_names, child_creature_names) parent = self.markerid_to_legion.get(parent_markerid) if parent is None: return if child_markerid not in self.markerids_left: raise AssertionError("illegal marker") if (bag(parent.creature_names) != bag(parent_creature_names).union( bag(child_creature_names)) and bag(parent_creature_names).union(bag(child_creature_names)) != bag({"Unknown": len(parent)})): raise AssertionError("wrong creatures", "parent.creature_names", parent.creature_names, "parent_creature_names", parent_creature_names, "child_creature_names", child_creature_names) new_legion1 = Legion.Legion(self, parent_markerid, Creature.n2c(parent_creature_names), parent.hexlabel) new_legion2 = Legion.Legion(self, child_markerid, Creature.n2c(child_creature_names), parent.hexlabel) if not parent.is_legal_split(new_legion1, new_legion2): raise AssertionError("illegal split") del new_legion1 parent.creatures = Creature.n2c(parent_creature_names) for creature in parent.creatures: creature.legion = parent self.take_marker(child_markerid) new_legion2.add_observer(self.game) self.markerid_to_legion[child_markerid] = new_legion2 del parent # One action for our player with creature names action = Action.SplitLegion(self.game.name, self.name, parent_markerid, child_markerid, parent_creature_names, child_creature_names) logging.info(action) self.notify(action, names=[self.name]) # Another action for everyone (including our player, who will # ignore it as a duplicate) without creature names. action = Action.SplitLegion(self.game.name, self.name, parent_markerid, child_markerid, len(parent_creature_names) * ["Unknown"], len(child_creature_names) * ["Unknown"]) logging.info(action) self.notify(action)
def test_find_last_split(): game_name = "game" playername = "player" parent_markerid = "Rd01" child_markerid = "Rd02" parent_creature_names = 4 * [None] child_creature_names = 4 * [None] history = History.History() action = Action.SplitLegion(game_name, playername, parent_markerid, child_markerid, parent_creature_names, child_creature_names) assert history.find_last_split(playername, parent_markerid, child_markerid) is None history.update(None, action, None) assert history.find_last_split(playername, parent_markerid, child_markerid) == action assert history.find_last_split(playername, "Rd03", "Rd04") is None