using System; using System.Collections.Generic; using System.Linq; namespace GameLogic { /// /// Updates and checks a matches rules according to the state of the match. /// public static class MatchLogic { public static MatchRule currentRule; /// /// Update match conditions and check for match conclusion. /// /// Affected player /// Update to the matches conditions /// Dictionary of the players in the match and their current stats /// public static void UpdateMatchResult(Player p, MatchConditionUpdate args, Dictionary mps, GameResult result) { switch (args.Condition) { case WinCondition.Lives: UpdateLives(mps[p], args.Count); break; case WinCondition.Score: UpdateScore(mps[p], args.Count); break; case WinCondition.Time: UpdateTime(mps[p], args.Count); break; } result.UpdateGameResult(mps, currentRule); } public static void UpdateLives(MatchPlayerStatistic mps, int count) { mps.Lives += count; if (mps.Lives <= 0) { mps.IsOut = true; } } public static void UpdateScore(MatchPlayerStatistic mps, int count) { throw new NotImplementedException(); } public static void UpdateTime(MatchPlayerStatistic mps, int count) { throw new NotImplementedException(); } } /// /// Data class which records the results of a Match /// public class GameResult { /// /// Indicates whether a round or the whole match was won /// public bool IsMatchWon { get; private set; } public int RoundsPlayed { get; private set; } public Player Winner { get; private set; } public List Opponents { get; private set; } /// /// Checks whether a round is won and if that round decided the match. /// Sets the class properties accordingly. /// /// Dictionary of players with their statistics for this match /// The rules for this match public void UpdateGameResult(Dictionary mps, MatchRule rules) { int outPlayers = mps.Count(p => p.Value.IsOut); // TODO: blatant error here right now if (outPlayers == mps.Count - 1) { Winner = mps.First(p => p.Value.IsOut != true).Key; Opponents = mps.Keys.Where(player => player != Winner).ToList(); mps[Winner].RoundsWon += 1; RoundsPlayed += 1; } else { Winner = null; } // TODO: this is wrong winning 2 rounds can decide the match if (RoundsPlayed == rules.rounds) { IsMatchWon = true; Winner = mps.Aggregate((p1, p2) => p1.Value.RoundsWon > p2.Value.RoundsWon ? p1 : p2).Key; Opponents = mps.Keys.Where(player => player != Winner).ToList(); } } } }