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.
82 lines
2.3 KiB
82 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="player"></param>
|
|
/// <returns>int</returns>
|
|
public int IsPlayerIn(Player player)
|
|
{
|
|
for (int i = 0; i < leaderboard.Count; i++)
|
|
{
|
|
if (player.NameTag == 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<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)
|
|
{
|
|
leaderboard[i].Date = now;
|
|
|
|
if (first)
|
|
{
|
|
leaderboard[i].Victories++;
|
|
}
|
|
|
|
leaderboard[i].Points = pair.Value;
|
|
|
|
}
|
|
else
|
|
{
|
|
int v = 0;
|
|
if (first)
|
|
{
|
|
v = 1;
|
|
}
|
|
Score score = new Score(pair.Key.NameTag, now, pair.Value, v);
|
|
leaderboard.Add(score);
|
|
}
|
|
|
|
first = false;
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|