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

85 lines
2.3 KiB

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.ExceptionServices;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace QwirkleClassLibrary.Players
{
[DataContract]
public class Leaderboard
{
public ReadOnlyCollection<Score> Lb => leaderboard.AsReadOnly();
[DataMember]
private readonly List<Score> leaderboard = new();
/// <summary>
/// Returns the index of the player in the leaderboard, -1 if the player is not in the leaderboard
/// </summary>
/// <param name="playerTag"></param>
/// <returns>int</returns>
public int IsPlayerIn(string playerTag)
{
for (int i = 0; i < leaderboard.Count; i++)
{
if (playerTag == leaderboard[i].PlayerName)
{
return i;
}
}
return -1;
}
/// <summary>
/// Adds the score of the players in the leaderboard with the date of the day and the number of victories
/// </summary>
/// <param name="scoreBoard"></param>
public void AddScoreInLead(ReadOnlyDictionary<string, int> scoreBoard)
{
DateTime now = DateTime.Now;
bool first = true;
var sb = scoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key);
foreach (KeyValuePair<string, int> pair in sb)
{
int i = IsPlayerIn(pair.Key);
if (i != -1)
{
leaderboard[i].Date = now;
if (first)
{
leaderboard[i].Victories++;
}
if (pair.Value > leaderboard[i].Points)
{
leaderboard[i].Points = pair.Value;
}
}
else
{
int v = 0;
if (first)
{
v = 1;
}
Score score = new Score(pair.Key, now, pair.Value, v);
leaderboard.Add(score);
}
first = false;
}
}
}
}