using System; using System.Collections.Generic; using System.Linq; using UnityEditor; 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 MatchResult UpdateMatchResult(Player p, MatchConditionUpdate args, Dictionary mps) { 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; } return DetectMatchResult(mps); } 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(); } /// /// Detects if a match has concluded. /// Currently only checks if all but on player is out. /// /// Dictonary of the players and their match data. /// Match result object, if the match has concluded private static MatchResult DetectMatchResult(Dictionary mps) { int outPlayers = mps.Count(p => p.Value.IsOut); if (outPlayers == mps.Count - 1) { return new MatchResult(mps); } return null; } } /// /// Data class which records the results of a Match /// public class MatchResult { public Player Winner { get; private set; } public List Opponents { get; private set; } public GUID GameID { get; private set; } public MatchResult(Dictionary mps) { GameID = GUID.Generate(); Winner = mps.First(p => p.Value.IsOut != true).Key; Opponents = mps.Keys.Where(player => player != Winner).ToList(); } } }