webemsim/BusinessLogic/Tournament.cs

222 lines
6 KiB
C#

namespace WebEMSim
{
public class Tournament
{
private readonly string _id;
private List<Group> _groups;
private Dictionary<string, Team> _ranks;
private Dictionary<string, Match> _matches;
private Table _overallTable = new([]);
private bool _hasPlayed = false;
public Tournament()
{
_id = System.Guid.NewGuid().ToString();
_ranks = new Dictionary<string, Team>();
_matches = new Dictionary<string, Match>();
_groups = [];
GenerateGroups();
}
public string ID { get => _id; }
public bool HasPlayed { get => _hasPlayed; }
public Dictionary<string, Match> Matches { get => _matches; }
private void GenerateGroups()
{
List<List<Team>> buckets = BaseData.GetBuckets();
for (int i = 0; i < 6; i++)
{
Group group = new(i);
foreach (var bucket in buckets)
{
Team chosenTeam = Helpers.GetRandomFromListAndRemove(bucket);
group.AddTeam(chosenTeam);
_overallTable.AddTeam(chosenTeam);
}
_groups.Add(group);
}
}
private string MakeRankString(char letter, int rank)
{
return String.Format("{0}{1}", rank, letter);
}
private void PerformGroupPhase(Table thirdPlaceTable)
{
foreach (var group in _groups)
{
group.PerformGroupPlayout();
List<Team> rankedTeams = group.Table.GetTable();
for (int rank = 0; rank < 2; rank++)
{
_ranks.Add(MakeRankString(group.Letter, rank + 1), rankedTeams[rank]);
}
thirdPlaceTable.AddTeam(rankedTeams[2]);
}
}
private void ChooseThirdPlaces(Table thirdPlaceTable)
{
List<Team> teams = thirdPlaceTable.GetTruncatedTable(4); // TODO: Check this
if (teams.Count != 4) throw new Exception("FUCK");
while(true)
{
teams = teams.OrderBy(x => Random.Shared.Next()).ToList();
int matchB = teams[0].Group;
int matchC = teams[1].Group;
int matchE = teams[2].Group;
int matchF = teams[3].Group;
if (!new List<int> { 0, 3, 4, 5 /* ADEF */ }.Contains(matchB))
{
continue;
}
if (!new List<int> { 3, 4, 5 /* DEF */ }.Contains(matchC))
{
continue;
}
if (!new List<int> { 0, 1, 2, 3 /* ABCD */ }.Contains(matchE))
{
continue;
}
if (!new List<int> { 0, 1, 2 /* ABC */ }.Contains(matchF))
{
continue;
}
break;
}
_ranks.Add("3ADEF", teams[0]);
_ranks.Add("3DEF", teams[1]);
_ranks.Add("3ABCD", teams[2]);
_ranks.Add("3ABC", teams[3]);
}
private Match DoMatch(string teamA, string teamB)
{
Team teamAObj = _ranks[teamA];
Team teamBObj = _ranks[teamB];
Match match = new(teamAObj, teamBObj);
match.Play();
return match;
}
public record PlayoutMatch(string teamA, string teamB, string result);
private void PlayoutGeneric(List<PlayoutMatch> playoutMatches)
{
foreach (var playoutMatch in playoutMatches)
{
Match match = DoMatch(playoutMatch.teamA, playoutMatch.teamB);
Team winner = match.Winner;
_ranks.Add(playoutMatch.result, winner);
_matches.Add(playoutMatch.result, match);
}
}
public List<PlayoutMatch> Playout16Matches = [
new PlayoutMatch("1B", "3ADEF", "W39"),
new PlayoutMatch("1A", "2C", "W37"),
new PlayoutMatch("1F", "3ABC", "W41"),
new PlayoutMatch("2D", "2E", "W42"),
new PlayoutMatch("1E", "3ABCD", "W43"),
new PlayoutMatch("1D", "2F", "W44"),
new PlayoutMatch("1C", "3DEF", "W40"),
new PlayoutMatch("2A", "2B", "W38"),
];
private void Playout16()
{
PlayoutGeneric(Playout16Matches);
}
public List<PlayoutMatch> Playout4Matches = [
new PlayoutMatch("W39", "W37", "W45"),
new PlayoutMatch("W41", "W42", "W46"),
new PlayoutMatch("W43", "W44", "W47"),
new PlayoutMatch("W40", "W38", "W48"),
];
private void Playout4()
{
PlayoutGeneric(Playout4Matches);
}
public List<PlayoutMatch> Playout2Matches = [
new PlayoutMatch("W45", "W46", "W49"),
new PlayoutMatch("W47", "W48", "W50"),
];
private void Playout2()
{
PlayoutGeneric(Playout2Matches);
}
public List<PlayoutMatch> PlayoutFinalMatches = [
new PlayoutMatch("W49", "W50", "WIN"),
];
private void PlayoutFinal()
{
PlayoutGeneric(PlayoutFinalMatches);
}
public Team GetTeamForRank(string rank)
{
return _ranks[rank];
}
public Match GetMatchForRank(string rank)
{
return _matches[rank];
}
public void PlayoutTournament()
{
Table thirdPlaceTable = new([]);
_hasPlayed = true;
PerformGroupPhase(thirdPlaceTable);
ChooseThirdPlaces(thirdPlaceTable);
Playout16();
Playout4();
Playout2();
PlayoutFinal();
}
public List<Group> Groups { get => _groups; }
public Table OverallTable { get => _overallTable; }
}
}