using System; using System.Collections.Generic; using SUISS.Storage; namespace SUISS.Cloud { public class Leaderboard : IStorable { public Leaderboard() { } public Leaderboard(int size, int startsAt) { this._size = size; this._firstRank = startsAt; this._records = new Leaderboard.Entries(); } public Leaderboard(int size, int startsAt, List records) { this._size = size; this._firstRank = startsAt; this._records = new Leaderboard.Entries(); this._records.AddRange(records); this.sortLeaderboard(null); } public List Records { get { return this._records; } } public int Count { get { return this._records.Count; } } public int FirstRank { get { return this._firstRank; } } public Leaderboard ReOrder(Comparison sort) { this.sortLeaderboard(sort); return this; } public int GetRank(LeaderboardEntry entry) { return this._records.FindIndex((LeaderboardEntry u) => u.Sagid.Equals(entry.Sagid)) + this.FirstRank; } public void UpdateRecord(string sagid, string username, int primaryScore, int secondaryScore, int tertiaryScore, int quaternaryScore, Comparison sort) { if (this._records.Exists((LeaderboardEntry u) => u.Sagid.Equals(sagid))) { LeaderboardEntry leaderboardEntry = this._records.Find((LeaderboardEntry u) => u.Sagid.Equals(sagid)); leaderboardEntry.Username = username; leaderboardEntry.PrimaryScore = primaryScore; leaderboardEntry.SecondaryScore = secondaryScore; leaderboardEntry.TertiaryScore = tertiaryScore; leaderboardEntry.QuaternaryScore = quaternaryScore; } else { LeaderboardEntry leaderboardEntry = new LeaderboardEntry(sagid, username, primaryScore, secondaryScore, tertiaryScore, quaternaryScore, 0, false); this._records.Add(leaderboardEntry); } this.sortLeaderboard(sort); if (this._size < this._records.Count) { this._records.RemoveAt(this._records.Count - 1); } } public void ShrinkBoardAround(string sagid, int paddingSize, Comparison sort) { this.sortLeaderboard(sort); int num = this._records.FindIndex((LeaderboardEntry u) => u.Sagid.Equals(sagid)); if (num != -1) { int num2 = Math.Min(this._records.Count, Math.Max(0, num - paddingSize)); int num3 = Math.Min(this._records.Count, num + paddingSize + 1); List range = this._records.GetRange(num2, num3 - num2); this._records = new Leaderboard.Entries(); this._records.AddRange(range); this._firstRank += num2; } } private void sortLeaderboard(Comparison sort) { if (sort != null) { this._records.Sort(sort); } int firstRank = this._firstRank; foreach (LeaderboardEntry leaderboardEntry in this._records) { leaderboardEntry.SetRank(firstRank++); } } void IStorable.FromStorage(IDictionary dict) { if (dict.ContainsKey("entries")) { this._records = (Leaderboard.Entries)dict["entries"]; } else { this._records = new Leaderboard.Entries(); } if (dict.ContainsKey("firstRank")) { this._firstRank = (int)dict["firstRank"]; } if (dict.ContainsKey("size")) { this._size = (int)dict["size"]; } } IDictionary IStorable.ToStorage() { Dictionary dictionary = new Dictionary(); dictionary["entries"] = this._records; dictionary["firstRank"] = this._firstRank; dictionary["size"] = this._size; return dictionary; } private Leaderboard.Entries _records = new Leaderboard.Entries(); private int _firstRank = 1; private int _size = 20; private Comparison sort; private const string RecordsKey = "entries"; private const string FirstRankKey = "firstRank"; private const string SizeKey = "size"; private class Entries : StorableList { } } }