25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

163 lines
4.1 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using SUISS.Storage;
  4. namespace SUISS.Cloud
  5. {
  6. public class Leaderboard : IStorable
  7. {
  8. public Leaderboard()
  9. {
  10. }
  11. public Leaderboard(int size, int startsAt)
  12. {
  13. this._size = size;
  14. this._firstRank = startsAt;
  15. this._records = new Leaderboard.Entries();
  16. }
  17. public Leaderboard(int size, int startsAt, List<LeaderboardEntry> records)
  18. {
  19. this._size = size;
  20. this._firstRank = startsAt;
  21. this._records = new Leaderboard.Entries();
  22. this._records.AddRange(records);
  23. this.sortLeaderboard(null);
  24. }
  25. public List<LeaderboardEntry> Records
  26. {
  27. get
  28. {
  29. return this._records;
  30. }
  31. }
  32. public int Count
  33. {
  34. get
  35. {
  36. return this._records.Count;
  37. }
  38. }
  39. public int FirstRank
  40. {
  41. get
  42. {
  43. return this._firstRank;
  44. }
  45. }
  46. public Leaderboard ReOrder(Comparison<LeaderboardEntry> sort)
  47. {
  48. this.sortLeaderboard(sort);
  49. return this;
  50. }
  51. public int GetRank(LeaderboardEntry entry)
  52. {
  53. return this._records.FindIndex((LeaderboardEntry u) => u.Sagid.Equals(entry.Sagid)) + this.FirstRank;
  54. }
  55. public void UpdateRecord(string sagid, string username, int primaryScore, int secondaryScore, int tertiaryScore, int quaternaryScore, Comparison<LeaderboardEntry> sort)
  56. {
  57. if (this._records.Exists((LeaderboardEntry u) => u.Sagid.Equals(sagid)))
  58. {
  59. LeaderboardEntry leaderboardEntry = this._records.Find((LeaderboardEntry u) => u.Sagid.Equals(sagid));
  60. leaderboardEntry.Username = username;
  61. leaderboardEntry.PrimaryScore = primaryScore;
  62. leaderboardEntry.SecondaryScore = secondaryScore;
  63. leaderboardEntry.TertiaryScore = tertiaryScore;
  64. leaderboardEntry.QuaternaryScore = quaternaryScore;
  65. }
  66. else
  67. {
  68. LeaderboardEntry leaderboardEntry = new LeaderboardEntry(sagid, username, primaryScore, secondaryScore, tertiaryScore, quaternaryScore, 0, false);
  69. this._records.Add(leaderboardEntry);
  70. }
  71. this.sortLeaderboard(sort);
  72. if (this._size < this._records.Count)
  73. {
  74. this._records.RemoveAt(this._records.Count - 1);
  75. }
  76. }
  77. public void ShrinkBoardAround(string sagid, int paddingSize, Comparison<LeaderboardEntry> sort)
  78. {
  79. this.sortLeaderboard(sort);
  80. int num = this._records.FindIndex((LeaderboardEntry u) => u.Sagid.Equals(sagid));
  81. if (num != -1)
  82. {
  83. int num2 = Math.Min(this._records.Count, Math.Max(0, num - paddingSize));
  84. int num3 = Math.Min(this._records.Count, num + paddingSize + 1);
  85. List<LeaderboardEntry> range = this._records.GetRange(num2, num3 - num2);
  86. this._records = new Leaderboard.Entries();
  87. this._records.AddRange(range);
  88. this._firstRank += num2;
  89. }
  90. }
  91. private void sortLeaderboard(Comparison<LeaderboardEntry> sort)
  92. {
  93. if (sort != null)
  94. {
  95. this._records.Sort(sort);
  96. }
  97. int firstRank = this._firstRank;
  98. foreach (LeaderboardEntry leaderboardEntry in this._records)
  99. {
  100. leaderboardEntry.SetRank(firstRank++);
  101. }
  102. }
  103. void IStorable.FromStorage(IDictionary<string, object> dict)
  104. {
  105. if (dict.ContainsKey("entries"))
  106. {
  107. this._records = (Leaderboard.Entries)dict["entries"];
  108. }
  109. else
  110. {
  111. this._records = new Leaderboard.Entries();
  112. }
  113. if (dict.ContainsKey("firstRank"))
  114. {
  115. this._firstRank = (int)dict["firstRank"];
  116. }
  117. if (dict.ContainsKey("size"))
  118. {
  119. this._size = (int)dict["size"];
  120. }
  121. }
  122. IDictionary<string, object> IStorable.ToStorage()
  123. {
  124. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  125. dictionary["entries"] = this._records;
  126. dictionary["firstRank"] = this._firstRank;
  127. dictionary["size"] = this._size;
  128. return dictionary;
  129. }
  130. private Leaderboard.Entries _records = new Leaderboard.Entries();
  131. private int _firstRank = 1;
  132. private int _size = 20;
  133. private Comparison<LeaderboardEntry> sort;
  134. private const string RecordsKey = "entries";
  135. private const string FirstRankKey = "firstRank";
  136. private const string SizeKey = "size";
  137. private class Entries : StorableList<LeaderboardEntry>
  138. {
  139. }
  140. }
  141. }