public void Add(DominionBase.Game game, DominionBase.Players.Player player) { // Make sure it exists if (!_GlobalStatistics.ContainsKey(game.Players.Count)) { _GlobalStatistics[game.Players.Count] = new NPlayersStatistics(game.Players.Count); } // Add the game to the correct Statistics section _GlobalStatistics[game.Players.Count].Add(game, player); }
public void Add(DominionBase.Game game) { Add(game, null); }
public void Add(DominionBase.Game game, DominionBase.Players.Player player) { if (game.Players.Count != _NumberOfPlayers) { throw new Exception("Incorrect number of players in game!"); } if (player != null) { if (!game.Players.Contains(player)) { throw new Exception("Player not found in game!"); } // One more game played! this.GamesPlayed++; // If the game actually finished properly, instead of being aborted if (game.State == DominionBase.GameState.Ended) { // Player won (or at least tied in winning) if (game.Winners.Contains(player)) { this.GamesWon++; } if (player.VictoryPoints < this.LowestScore) { this.LowestScore = player.VictoryPoints; } if (player.VictoryPoints > this.HighestScore) { this.HighestScore = player.VictoryPoints; } _TotalScore += player.VictoryPoints; } } if (game.State == DominionBase.GameState.Ended) { foreach (DominionBase.Players.Player winner in game.Winners) { // Since properly-used cards like Feast, Mining Village, & Embargo never end up in a player's hand, // we need to count how many of the cards were played during the game and add them to the player's // total as well as the total number of cards in the player's hand IEnumerable <DominionBase.Cards.Card> trashedCards = game.TurnsTaken.Where(t => t.Player == winner). SelectMany <DominionBase.Turn, DominionBase.Cards.Card>( t => t.CardsPlayed.Where(c => c.CardType == DominionBase.Cards.Base.TypeClass.Feast || c.CardType == DominionBase.Cards.Seaside.TypeClass.Embargo || (c.CardType == DominionBase.Cards.Intrigue.TypeClass.MiningVillage && t.CardsTrashed.Contains(c)) || (c.CardType == DominionBase.Cards.Cornucopia.TypeClass.HornOfPlenty && t.CardsTrashed.Contains(c)) ) ); int handCount = winner.Hand.Count + trashedCards.Count(); foreach (DominionBase.Piles.Supply supply in game.Table.Supplies.Values) { Type cardType = supply.CardType; String cardTypeKey = cardType.AssemblyQualifiedName; int cardTypeCount = winner.Hand.Count(c => c.CardType == cardType) + trashedCards.Count(c => c.CardType == cardType); if (!_WinningCardCounts.ContainsKey(cardTypeKey)) { _WinningCardCounts[cardTypeKey] = new CardStatistics(cardType); } _WinningCardCounts[cardTypeKey].Add(cardTypeCount, handCount); if (winner.PlayerType == DominionBase.Players.PlayerType.Human) { if (!_WinningHumanCardCounts.ContainsKey(cardTypeKey)) { _WinningHumanCardCounts[cardTypeKey] = new CardStatistics(cardType); } _WinningHumanCardCounts[cardTypeKey].Add(cardTypeCount, handCount); } } } } }
private void StartGame(DominionBase.GameSettings settings) { _StartingNewGame = false; ReleaseGame(); wMain.Settings = Settings.Load(); // Clean out the Image Repository before starting a new game -- // so we don't allocate too much memory for cards we're not even using Caching.ImageRepository.Reset(); dpGameInfo.Visibility = System.Windows.Visibility.Visible; glMain.TearDown(); glMain.Clear(); LayoutSupplyPiles(); while (tcAreas.Items.Count > 2) tcAreas.Items.RemoveAt(tcAreas.Items.Count - 1); dpMatsandPiles.Children.Clear(); dpGameStuff.Children.Clear(); _TradeRouteLabel = null; // Try to force garbage collection to save some memory GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); try { game = new DominionBase.Game( _Settings.NumberOfHumanPlayers, _Settings.PlayerSettings.Take(_Settings.NumberOfPlayers).Select(ps => ps.Name), _Settings.PlayerSettings.Take(_Settings.NumberOfPlayers).Select(ps => ps.AIClassType), settings); game.SelectCards(); if (game.Settings.Preset != null || Settings.AutomaticallyAcceptKingdomCards) this.AcceptGame(); else { wCardSelection selector = new wCardSelection(); selector.Owner = this; if (selector.ShowDialog() == true) { Settings.AutomaticallyAcceptKingdomCards = (selector.cbAutoAccept.IsChecked == true); Settings.Save(); this.AcceptGame(); } else game = null; } } catch (DominionBase.Cards.ConstraintException ce) { wMessageBox.Show(ce.Message, "Constraint exception!", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } catch (DominionBase.GameCreationException gce) { wMessageBox.Show(gce.Message, "Game creation exception!", MessageBoxButton.OK, MessageBoxImage.Exclamation); return; } }
private void Game_Load_Click(object sender, RoutedEventArgs e) { Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog(); ofd.DefaultExt = ".save"; ofd.Filter = "Saved game files (.save)|*.save"; Nullable<Boolean> dialogResult = ofd.ShowDialog(this); if (dialogResult != true) return; this.game = new DominionBase.Game(); this.game.Load(ofd.FileName); // Clean out the Image Repository before starting a new game -- // so we don't allocate too much memory for cards we're not even using Caching.ImageRepository.Reset(); dpGameInfo.Visibility = System.Windows.Visibility.Visible; glMain.TearDown(); glMain.Clear(); LayoutSupplyPiles(); while (tcAreas.Items.Count > 2) tcAreas.Items.RemoveAt(tcAreas.Items.Count - 1); dpMatsandPiles.Children.Clear(); dpGameStuff.Children.Clear(); _TradeRouteLabel = null; // Try to force garbage collection to save some memory GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); // ------------------------------------------------------------------------------- game.GameEndedEvent += new DominionBase.Game.GameEndedEventHandler(game_GameEndedEvent); wMain.Settings.PlayerSettings.ForEach(ps => glMain.AddPlayerColor(ps.Name, ps.UIColor)); if (game.ActivePlayer != null) { glMain.NewSection(String.Format("Game loaded from {0}.", game.StartTime)); glMain.Log("It's ", game.ActivePlayer, "'s turn"); } game.Table.TokenPiles.TokenCollectionsChanged += new DominionBase.TokenCollections.TokenCollectionsChangedEventHandler(TokenPiles_TokenCollectionsChanged); game.Table.Trash.PileChanged += new DominionBase.Piles.Pile.PileChangedEventHandler(Trash_PileChanged); Trash_PileChanged(game.Table.Trash, new DominionBase.Piles.PileChangedEventArgs(DominionBase.Piles.PileChangedEventArgs.Operation.Reset)); game.GameMessage += new DominionBase.Game.GameMessageEventHandler(game_GameMessage); foreach (DominionBase.Players.Player player in game.Players.FindAll(p => p.PlayerType == DominionBase.Players.PlayerType.Human)) player.Choose = player_Choose; if (game.Players.Any(player => player.PlayerType == DominionBase.Players.PlayerType.Human)) _Player = game.Players.OfType<DominionBase.Players.Human>().First(); else _Player = null; // ------------------------------------------------------------------------------- Type[] specialTypes = new Type[] { DominionBase.Cards.Promotional.TypeClass.BlackMarketSupply, DominionBase.Cards.Cornucopia.TypeClass.PrizeSupply, DominionBase.Cards.DarkAges.TypeClass.Madman, DominionBase.Cards.DarkAges.TypeClass.Mercenary, DominionBase.Cards.DarkAges.TypeClass.Spoils }; foreach (Type specialType in specialTypes) { if (game.Table.SpecialPiles.ContainsKey(specialType)) { _MatEventHandlers[specialType] = new DominionBase.Piles.Pile.PileChangedEventHandler(GamePile_PileChanged); game.Table.SpecialPiles[specialType].PileChanged += _MatEventHandlers[specialType]; GamePile_PileChanged(game.Table.SpecialPiles[specialType], new DominionBase.Piles.PileChangedEventArgs(DominionBase.Piles.PileChangedEventArgs.Operation.Reset)); } } if (game.Table.Supplies.ContainsKey(DominionBase.Cards.Prosperity.TypeClass.TradeRoute)) { if (dpGameStuff.Children.Count > 0) { Border bDiv = new Border(); bDiv.BorderThickness = new Thickness(2); bDiv.BorderBrush = Brushes.Black; Panel.SetZIndex(bDiv, 1); DockPanel.SetDock(bDiv, Dock.Left); dpGameStuff.Children.Add(bDiv); } Label lTradeRoute = new Label(); lTradeRoute.Content = "Trade Route Tokens:"; lTradeRoute.FontSize = 16d; lTradeRoute.FontWeight = FontWeights.Bold; lTradeRoute.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Right; lTradeRoute.Background = Caching.BrushRepository.GetBackgroundBrush(DominionBase.Cards.Category.Treasure); DockPanel.SetDock(lTradeRoute, Dock.Left); dpGameStuff.Children.Add(lTradeRoute); _TradeRouteLabel = new Label(); _TradeRouteLabel.Content = game.Table.TokenPiles[DominionBase.Cards.Prosperity.TypeClass.TradeRouteToken].Count; _TradeRouteLabel.FontWeight = FontWeights.Bold; _TradeRouteLabel.VerticalAlignment = System.Windows.VerticalAlignment.Stretch; _TradeRouteLabel.VerticalContentAlignment = System.Windows.VerticalAlignment.Center; _TradeRouteLabel.Background = Caching.BrushRepository.GetBackgroundBrush(DominionBase.Cards.Category.Treasure); _TradeRouteLabel.Padding = new Thickness(0, 0, 5, 0); _TradeRouteLabel.BorderThickness = new Thickness(0, 0, 1, 0); DockPanel.SetDock(_TradeRouteLabel, Dock.Left); dpGameStuff.Children.Add(_TradeRouteLabel); } if (dpGameStuff.Children.Count > 0) bStuffDivider.Visibility = System.Windows.Visibility.Visible; else bStuffDivider.Visibility = System.Windows.Visibility.Collapsed; foreach (DominionBase.Players.Player player in game.Players) { TabItem tiPlayer = new TabItem(); DockPanel dpHeader = new DockPanel(); Image iHeader = new Image(); iHeader.Stretch = Stretch.None; iHeader.Margin = new Thickness(0, 0, 5, 0); DockPanel.SetDock(iHeader, Dock.Left); switch (player.PlayerType) { case DominionBase.Players.PlayerType.Human: iHeader.Source = (BitmapImage)this.Resources["imHuman"]; break; case DominionBase.Players.PlayerType.Computer: iHeader.Source = (BitmapImage)this.Resources["imComputer"]; break; } dpHeader.Children.Add(iHeader); TextBlock tbHeader = new TextBlock(); tbHeader.Text = player.Name; dpHeader.Children.Add(tbHeader); tiPlayer.Header = dpHeader; tcAreas.Items.Add(tiPlayer); Controls.ucPlayerDisplay ucpdPlayer = new Controls.ucPlayerDisplay(); tiPlayer.Content = ucpdPlayer; ucpdPlayer.IsUIPlayer = (player == _Player); ucpdPlayer.Player = player; PlayerSettings playerSettings = _Settings.PlayerSettings.FirstOrDefault(ps => ps.Name == player.Name); if (playerSettings != null) { ColorHls hlsValue = HLSColor.RgbToHls(playerSettings.UIColor); Color cPlayer = HLSColor.HlsToRgb(hlsValue.H, Math.Min(1d, hlsValue.L * 1.125), hlsValue.S * 0.95, hlsValue.A); GradientStopCollection gsc = new GradientStopCollection(); gsc.Add(new GradientStop(cPlayer, 0)); gsc.Add(new GradientStop(playerSettings.UIColor, 0.25)); gsc.Add(new GradientStop(playerSettings.UIColor, 0.75)); gsc.Add(new GradientStop(cPlayer, 1)); gsc.Freeze(); tiPlayer.Background = new LinearGradientBrush(gsc, 0); //tiPlayer.Background = new SolidColorBrush(playerSettings.UIColor); ucpdPlayer.ColorFocus = playerSettings.UIColor; } ToolTip tt = new System.Windows.Controls.ToolTip(); Controls.ucPlayerOverview ucpo = new Controls.ucPlayerOverview(); ucpo.Player = player; tt.Content = ucpo; ToolTipService.SetToolTip(dpHeader, tt); if (Settings.ToolTipShowDuration == ToolTipShowDuration.Off) ToolTipService.SetIsEnabled(dpHeader, false); else { ToolTipService.SetIsEnabled(dpHeader, true); ToolTipService.SetShowDuration(dpHeader, (int)Settings.ToolTipShowDuration); } dpHeader.MouseDown += new MouseButtonEventHandler(tiPlayer_MouseDown); dpHeader.MouseUp += new MouseButtonEventHandler(tiPlayer_MouseUp); player.Revealed.PileChanged += new DominionBase.Piles.Pile.PileChangedEventHandler(Revealed_PileChanged); player.BenefitReceiving += new DominionBase.Players.Player.BenefitReceivingEventHandler(player_BenefitReceiving); //player.DiscardPile.PileChanged += new DominionBase.Pile.PileChangedEventHandler(DiscardPile_PileChanged); player.CardPlaying += new DominionBase.Players.Player.CardPlayingEventHandler(player_CardPlaying); player.CardPlayed += new DominionBase.Players.Player.CardPlayedEventHandler(player_CardPlayed); player.CardUndoPlaying += new DominionBase.Players.Player.CardUndoPlayingEventHandler(player_CardUndoPlaying); player.CardUndoPlayed += new DominionBase.Players.Player.CardUndoPlayedEventHandler(player_CardUndoPlayed); player.CardBuying += new DominionBase.Players.Player.CardBuyingEventHandler(player_CardBuying); player.CardBought += new DominionBase.Players.Player.CardBoughtEventHandler(player_CardBought); player.CardBuyFinished += new DominionBase.Players.Player.CardBuyFinishedEventHandler(player_CardBuyFinished); player.CardGaining += new DominionBase.Players.Player.CardGainingEventHandler(player_CardGaining); player.CardGainedInto += new DominionBase.Players.Player.CardGainedIntoEventHandler(player_CardGainedInto); player.CardGainFinished += new DominionBase.Players.Player.CardGainFinishedEventHandler(player_CardGainFinished); player.TokenPlaying += new DominionBase.Players.Player.TokenPlayingEventHandler(player_TokenPlaying); player.TokenPlayed += new DominionBase.Players.Player.TokenPlayedEventHandler(player_TokenPlayed); player.Trashing += new DominionBase.Players.Player.TrashingEventHandler(player_Trashing); player.TrashedFinished += new DominionBase.Players.Player.TrashedFinishedEventHandler(player_Trashed); player.PhaseChanged += new DominionBase.Players.Player.PhaseChangedEventHandler(player_PhaseChangedEvent); player.PlayerModeChanged += new DominionBase.Players.Player.PlayerModeChangedEventHandler(player_PlayerModeChangedEvent); player.CardsDrawn += new DominionBase.Players.Player.CardsDrawnEventHandler(player_CardsDrawn); player.TurnStarting += new DominionBase.Players.Player.TurnStartingEventHandler(player_TurnStarting); player.Shuffling += new DominionBase.Players.Player.ShufflingEventHandler(player_Shuffle); player.CardsAddedToDeck += new DominionBase.Players.Player.CardsAddedToDeckEventHandler(player_CardsAddedToDeck); player.CardsAddedToHand += new DominionBase.Players.Player.CardsAddedToHandEventHandler(player_CardsAddedToHand); player.CardsDiscarded += new DominionBase.Players.Player.CardsDiscardedEventHandler(player_CardsDiscarded); player.PlayerMats.CardMatsChanged += new DominionBase.Piles.CardMats.CardMatsChangedEventHandler(PlayerMats_DecksChanged); player.TokenPiles.TokenCollectionsChanged += new DominionBase.TokenCollections.TokenCollectionsChangedEventHandler(PlayerTokenPiles_TokenCollectionsChanged); player.BenefitsChanged += new DominionBase.Players.Player.BenefitsChangedEventHandler(player_BenefitsChanged); if (player == _Player) { tcAreas.SelectedItem = tiPlayer; player.CardReceived += new DominionBase.Players.Player.CardReceivedEventHandler(player_CardReceived); } } miNewGame.IsEnabled = false; miLoadGame.IsEnabled = false; miEndGame.IsEnabled = true; miSaveGame.IsEnabled = false; gameThread = new Thread(game.StartAsync); gameThread.Start(); if (game.ActivePlayer != null) { if (game.Players[0] != game.ActivePlayer) glMain.NewTurn(game.TurnsTaken.TurnNumber(game.ActivePlayer)); player_BenefitsChanged(game.ActivePlayer, new DominionBase.Players.BenefitsChangedEventArgs(game.ActivePlayer) { Actions = game.ActivePlayer.Actions, Buys = game.ActivePlayer.Buys, Currency = game.ActivePlayer.Currency, Player = game.ActivePlayer }); } if (_Player.Phase == DominionBase.Players.PhaseEnum.Starting || _Player.Phase == DominionBase.Players.PhaseEnum.Buy || _Player.PlayerMode == DominionBase.Players.PlayerMode.Waiting) CheckBuyable(_Player); else ClearBuyable(); UpdateDisplay(); }
private void ReleaseGame() { _Player = null; uccChooser.IsReady = false; cardTrash.Pile = null; foreach (TabItem ti in tcAreas.Items) { if (ti.Content is Controls.ucPlayerDisplay) { (ti.Content as Controls.ucPlayerDisplay).TearDown(); (((ti.Header as DockPanel).ToolTip as ToolTip).Content as Controls.ucPlayerOverview).TearDown(); } } foreach (StackPanel sp in stackPanelSupplyPiles.Children.OfType<StackPanel>()) { foreach (SupplyControl sc in sp.Children.OfType<SupplyControl>()) { sc.Supply = null; } } if (game == null) return; ReleaseEvents(); game.Clear(); game = null; GC.Collect(); GC.Collect(1); GC.Collect(2); GC.Collect(3); GC.WaitForPendingFinalizers(); GC.Collect(); }