You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sae201_qwirkle/Qwirkle/QwirkleClassLibrary/Players/Leaderboard.cs

69 lines
1.7 KiB

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Text;
using System.Threading.Tasks;
namespace QwirkleClassLibrary.Players
{
public class Leaderboard
{
public ReadOnlyCollection<Score> LB => leaderb.AsReadOnly();
private readonly List<Score> leaderb = new();
public Leaderboard() { }
public int IsPlayerIn(Player player)
{
for (int i = 0; i < leaderb.Count; i++)
{
if (player.NameTag == leaderb[i].PlayerName)
{
return i;
}
}
return -1;
}
public void AddScoreInLead(ReadOnlyDictionary<Player, int> ScoreBoard)
{
DateTime now = DateTime.Today;
bool first = true;
var sb = ScoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key.NameTag);
foreach (KeyValuePair<Player, int> pair in sb)
{
int i = IsPlayerIn(pair.Key);
if (i != -1)
{
leaderb[i].Date = now;
if (first)
{
leaderb[i].Victories++;
}
leaderb[i].Points = pair.Value;
}
else
{
int v = 0;
if (first)
{
v = 1;
}
Score score = new Score(pair.Key.NameTag, now, pair.Value, v);
leaderb.Add(score);
}
first = false;
}
}
}
}