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 Lb => leaderboard.AsReadOnly(); [DataMember] private readonly List leaderboard = new(); /// /// Returns the index of the player in the leaderboard, -1 if the player is not in the leaderboard /// /// /// int public int IsPlayerIn(string playerTag) { for (int i = 0; i < leaderboard.Count; i++) { if (playerTag == leaderboard[i].PlayerName) { return i; } } return -1; } /// /// Adds the score of the players in the leaderboard with the date of the day and the number of victories /// /// public void AddScoreInLead(ReadOnlyDictionary scoreBoard) { DateTime now = DateTime.Now; bool first = true; var sb = scoreBoard.OrderByDescending(x => x.Value).ThenBy(x => x.Key); foreach (KeyValuePair 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; } } } }