private void ReportBattleWon() { foreach (IFleet fleet in DisabledFleets) { fleet.LeaveField(this); } IFleet winner = Fleets.Where(x => x.WorkingStarShips.Count > 0).FirstOrDefault(); BattleResults.Messages.Add("The battle has been won and only the " + winner.Name + " fleet survives!"); BattleResults.Messages.Add("The " + winner.Name + " fleet still has " + Fleets.Where(x => x.WorkingStarShips.Count > 0).FirstOrDefault().WorkingStarShips.Count + " ship's left!"); }
public void Update(DateTime currentTime) { if (this.Waiting) { if (currentTime > gameStart) { if (Players.Count == 2) { this.Waiting = false; } else { if (currentTime > gameQuit) { this.Waiting = false; this.Status = "Nobody joined your game, I guess that means you win."; GameOver = true; } else { UpdateTimeInfo(gameStart.AddSeconds(2)); } } } return; } if (GameOver) { this.Stop(); return; } // check if we are in the server window if (currentTime >= endPlayerTurn) { // server processing Processing = true; // Grow ships on planets foreach (var planet in Planets) { // if the planet is not controlled by neutral update if (planet.OwnerId != -1) { planet.NumberOfShips += planet.GrowthRate; } } // Send fleets foreach (var fleet in Fleets) { // travel 1 unit distance each turn fleet.NumberOfTurnsToDestination--; } // Resolve planet battles foreach (var planet in Planets) { var combatants = new Dictionary <int, int>(); combatants.Add(planet.OwnerId, planet.NumberOfShips); // find fleets destined for this planet var fleets = Fleets.Where(f => f.Destination.Id == planet.Id && f.NumberOfTurnsToDestination <= 0); foreach (var fleet in fleets) { if (combatants.ContainsKey(fleet.OwnerId)) { combatants[fleet.OwnerId] += fleet.NumberOfShips; } else { combatants.Add(fleet.OwnerId, fleet.NumberOfShips); } } if (fleets.Count() <= 0) { continue; } KeyValuePair <int, int> second = new KeyValuePair <int, int>(1, 0); KeyValuePair <int, int> winner = new KeyValuePair <int, int>(2, 0); foreach (var keyval in combatants) { if (keyval.Value > second.Value) { if (keyval.Value > winner.Value) { second = winner; winner = keyval; } else { second = keyval; } } } if (winner.Value > second.Value) { planet.NumberOfShips = winner.Value - second.Value; planet.OwnerId = winner.Key; } else { planet.NumberOfShips = 0; } } // remove finished fleets lock (synclock) { _fleets.RemoveAll(f => f.NumberOfTurnsToDestination <= 0); } // Check game over conditions var player1NoPlanets = !Planets.Any(p => p.OwnerId == 1); var player1NoShips = !Fleets.Any(f => f.OwnerId == 1); var player2NoPlanet = !Planets.Any(p => p.OwnerId == 2); var player2NoShips = !Fleets.Any(f => f.OwnerId == 2); if (player1NoPlanets && player1NoShips) { // player 2 has won var winner = Players.Values.FirstOrDefault(p => p.Id == 2); var loser = Players.Values.FirstOrDefault(p => p.Id == 1); this.Status = $"{winner?.PlayerName} has defeated {loser?.PlayerName}!"; this.GameOver = true; } if (player2NoPlanet && player2NoShips) { // player 1 has won var winner = Players.Values.FirstOrDefault(p => p.Id == 1); var loser = Players.Values.FirstOrDefault(p => p.Id == 2); this.Status = $"{winner?.PlayerName} has defeated {loser?.PlayerName}!"; this.GameOver = true; } PlayerAScoreOverTime.Add(_getPlayerScore(1)); PlayerBScoreOverTime.Add(_getPlayerScore(2)); // Turn complete Turn++; endPlayerTurn = currentTime.AddMilliseconds(PLAYER_TURN_LENGTH); endServerTurn = endPlayerTurn.AddMilliseconds(SERVER_TURN_LENGTH); Processing = false; System.Diagnostics.Debug.WriteLine($"Game {Id} : Turn {Turn} : Next Turn Start {endServerTurn.Subtract(DateTime.UtcNow).TotalMilliseconds}ms"); } if (Turn >= MAX_TURN) { var player1 = _getPlayerForId(1)?.PlayerName; var player2 = _getPlayerForId(2)?.PlayerName; var player1Score = _getPlayerScore(1); var player2Score = _getPlayerScore(2); if (player1Score == player2Score) { this.Status = $"{player1} has tied {player2}!"; } else if (player1Score > player2Score) { this.Status = $"{player1} has defeated {player2}!"; } else { this.Status = $"{player2} has defeated {player1}!"; } this.GameOver = true; } }
private IEnumerable <Fleet> _getFleetsForPlayer(int id) { return(Fleets.Where(f => f.OwnerId == id)); }