// // compares 2 RolledModel objects for identity. used for testing Redo. public bool Equals(RolledModel rolledModel) { if (PlayerIndex != rolledModel.PlayerIndex || PlayerName != rolledModel.PlayerName || OldState != rolledModel.OldState || Action != rolledModel.Action || Rolled != rolledModel.Rolled || PlayerToResources.Count != rolledModel.PlayerToResources.Count) { return(false); } foreach (KeyValuePair <string, PlayerRollData> kvp in PlayerToResources) { bool exists = rolledModel.PlayerToResources.TryGetValue(kvp.Key, out PlayerRollData rollData); if (!exists) { return(false); } if (kvp.Value.MaxRollNoResources != rolledModel.PlayerToResources[kvp.Key].MaxRollNoResources) { return(false); } if (kvp.Value.ResourceList.Count != rollData.ResourceList.Count) { return(false); } for (int i = 0; i < rollData.ResourceList.Count; i++) { if (kvp.Value.ResourceList[i].Value != rollData.ResourceList[i].Value || kvp.Value.ResourceList[i].ResourceType != rollData.ResourceList[i].ResourceType || kvp.Value.ResourceList[i].BlockedByBaron != rollData.ResourceList[i].BlockedByBaron) { return(false); } } } return(true); }
/// <summary> /// /// the controller will fill out the RollModel which has the complete set of state impacted by this roll. /// /// 1. push the roll /// 2. Update roll stats /// 3. Update resource counts /// 4. do Baron tracking /// 5. set state to WaitingForNext (always comes after Roll) /// </summary> /// <returns></returns> public static RolledModel Do(MainPage page, int roll) { var currentPlayer = page.CurrentPlayer; RolledModel rolledModel = new RolledModel() { Page = page, Rolled = roll, Player = currentPlayer, PlayerIndex = currentPlayer.AllPlayerIndex, PlayerName = currentPlayer.PlayerName, OldState = page.NewGameState, Action = (roll == 7) ? CatanAction.RolledSeven : CatanAction.Rolled }; page.Rolls.Push(rolledModel.Rolled); // all the rolls... page.LastRoll = rolledModel.Rolled; page.UpdateGlobalRollStats(); // just math rolledModel.PlayerToResources = RolledController.GetResourceModelForRoll(page, rolledModel.Rolled); if (rolledModel.Rolled != 7) { currentPlayer.GameData.MovedBaronAfterRollingSeven = null; rolledModel.NewState = GameState.WaitingForNext; } else { currentPlayer.GameData.MovedBaronAfterRollingSeven = false; rolledModel.NewState = GameState.MustMoveBaron; } return(rolledModel); }
internal static void Redo(MainPage page, RolledModel model) { var newModel = RolledController.Do(page, model.Rolled); if (newModel.Equals(model) == false) { throw new Exception("new and old Roll models must match on Redo!"); } }
public static bool Undo(MainPage page, RolledModel rollModel) { int roll = page.PopRoll(); Debug.Assert(roll == rollModel.Rolled); page.UpdateGlobalRollStats(); // // we will go through each of the playing players to see if they are in the roll dictionary. if they are, we'll undo // the resource allocation. if they are not, we know they had a bad roll and will deal with it. // foreach (var player in page.MainPageModel.PlayingPlayers) { if (rollModel.PlayerToResources.TryGetValue(player.PlayerName, out PlayerRollData playerRollData) == true) { if (playerRollData.ResourceList.Count > 0) { player.GameData.RollsWithResource--; foreach (RollResourcesModel resourceModel in playerRollData.ResourceList) { player.GameData.UpdateResourceCount(resourceModel, LogState.Undo); } } else { player.GameData.NoResourceCount--; } player.GameData.MaxNoResourceRolls = playerRollData.MaxRollNoResources; } else { throw new Exception("We should have *all* players in the rollModel!"); } } // // reset the flag that says they need to move the baron if (rollModel.Rolled != 7) { page.CurrentPlayer.GameData.MovedBaronAfterRollingSeven = null; } else { page.CurrentPlayer.GameData.MovedBaronAfterRollingSeven = false; } // // get rid of any highlighting foreach (TileCtrl tile in page.GameContainer.AllTiles) { tile.StopHighlightingTile(); } return(true); }