Space-Smash-Out/Assets/Scripts/MatchLogic.cs

122 lines
3.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace GameLogic
{
/// <summary>
/// Updates and checks a matches rules according to the state of the match.
/// </summary>
public static class MatchLogic
{
public static MatchRule currentRule;
/// <summary>
/// Update match conditions and check for match conclusion.
/// </summary>
/// <param name="p">Affected player</param>
/// <param name="args">Update to the matches conditions</param>
/// <param name="mps">Dictionary of the players in the match and their current stats</param>
/// <returns></returns>
public static void UpdateMatchResult(Player p, MatchConditionUpdate args,
Dictionary<Player, MatchPlayerStatistic> 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();
}
}
/// <summary>
/// Data class which records the results of a Match
/// </summary>
public class GameResult
{
/// <summary>
/// Indicates whether a round or the whole match was won
/// </summary>
public bool IsMatchWon { get; private set; }
public bool IsRoundWon { get; set; }
public int RoundsPlayed { get; private set; }
public Player Winner { get; private set; }
public List<Player> Opponents { get; private set; }
/// <summary>
/// Checks whether a round is won and if that round decided the match.
/// Sets the class properties accordingly.
/// </summary>
/// <param name="mps">Dictionary of players with their statistics for this match</param>
/// <param name="rules">The rules for this match</param>
public void UpdateGameResult(Dictionary<Player, MatchPlayerStatistic> 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;
IsRoundWon = true;
}
else
{
Winner = null;
}
foreach (Player p in mps.Keys)
{
if (mps[p].RoundsWon > rules.rounds / 2)
{
IsMatchWon = true;
Winner = p;
Opponents = mps.Keys.Where(player => player != Winner).ToList();
return;
}
}
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();
return;
}
foreach (var statistic in mps.Values)
{
statistic.IsOut = false;
}
}
}
}