Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

271 lignes
9.7 KiB

  1. using CIG.Translation;
  2. using SUISS.Cloud;
  3. using SUISS.Core;
  4. using SUISS.Scheduling;
  5. using SUISSEngine;
  6. using System;
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. [PersistentManager]
  11. public sealed class CIGSparkSocServices : SingletonMonobehaviour<CIGSparkSocServices>
  12. {
  13. public string sparkSocGameId;
  14. public string leaderboardName = "boards";
  15. public int leaderboardSize = 100;
  16. public int leaderboardSyncInterval = 600;
  17. public string leaderboardSecret = "blaht";
  18. private IPlayernameController _sparkSocController;
  19. private ILeaderboardController _leaderboardController;
  20. private string _countryCode;
  21. private bool _failedToLoad;
  22. public Comparison<LeaderboardEntry> LeaderboardSort => delegate (LeaderboardEntry y, LeaderboardEntry x)
  23. {
  24. if (x.PrimaryScore != y.PrimaryScore)
  25. {
  26. return x.PrimaryScore - y.PrimaryScore;
  27. }
  28. if (x.SecondaryScore != y.SecondaryScore)
  29. {
  30. return x.SecondaryScore - y.SecondaryScore;
  31. }
  32. return (x.TertiaryScore != y.TertiaryScore) ? (x.TertiaryScore - y.TertiaryScore) : (x.QuaternaryScore - y.QuaternaryScore);
  33. };
  34. protected override void Awake()
  35. {
  36. base.Awake();
  37. if (string.IsNullOrEmpty(_countryCode) || string.IsNullOrEmpty(sparkSocGameId) || string.IsNullOrEmpty(leaderboardSecret))
  38. {
  39. _failedToLoad = true;
  40. UnityEngine.Debug.LogWarning("CIG sparksoc services was loaded with missing editor variables! (Game not loaded from loading screen?)");
  41. }
  42. _sparkSocController = new PlayernameController(sparkSocGameId, 26088, "");
  43. _leaderboardController = new LeaderboardController(_sparkSocController, leaderboardName, _countryCode, leaderboardSize, leaderboardSyncInterval, sparkSocGameId, 26088);
  44. }
  45. protected override void OnDestroy()
  46. {
  47. base.OnDestroy();
  48. }
  49. public void StartLeaderboards()
  50. {
  51. if (!_failedToLoad)
  52. {
  53. if (!_sparkSocController.IsInstallRegistered)
  54. {
  55. _sparkSocController.RegisterInstall();
  56. }
  57. StartCoroutine(leaderboardUpdateLoop());
  58. }
  59. }
  60. private IEnumerator leaderboardUpdateLoop()
  61. {
  62. while (true)
  63. {
  64. _leaderboardController.UpdateLeaderboards();
  65. yield return new WaitForSeconds(leaderboardSyncInterval);
  66. }
  67. }
  68. public string GetInstallUuid()
  69. {
  70. return _sparkSocController.InstallUuid;
  71. }
  72. public bool IsRegisteredInstall()
  73. {
  74. return _sparkSocController.IsInstallRegistered;
  75. }
  76. public string GetPlayerName()
  77. {
  78. return _sparkSocController.PlayerName;
  79. }
  80. public string GetPlayerUuid()
  81. {
  82. return _sparkSocController.PlayerUuid;
  83. }
  84. public void VoteForPlayer(string installUuid)
  85. {
  86. _leaderboardController.Vote(installUuid, upVote: true);
  87. }
  88. public bool IsLinkedToPlayername()
  89. {
  90. return _sparkSocController.IsLinkedToPlayerName && _sparkSocController.IsPlayerLoggedIn;
  91. }
  92. public bool IsTopPlayer()
  93. {
  94. return _leaderboardController.InGlobalTop || _leaderboardController.InCountryTop;
  95. }
  96. public bool IsLogedInAndLinked()
  97. {
  98. return _sparkSocController.IsLinkedToPlayerName && _sparkSocController.IsPlayerLoggedIn;
  99. }
  100. public bool IsValidUsernameAndPassword(string username, string password)
  101. {
  102. return _sparkSocController.IsValidPassword(password) && _sparkSocController.IsValidPlayername(username);
  103. }
  104. public ILocalizedString LocalizedServerErrorCode(PlayernameErrors errorcode)
  105. {
  106. switch (errorcode)
  107. {
  108. case PlayernameErrors.DuplicatePlayername:
  109. return Localization.Key("SSP_DUPLICATE_USER");
  110. case PlayernameErrors.ProfanePlayername:
  111. return Localization.Key("SSP_INVALID_USERNAME_OR_PASSWORD");
  112. case PlayernameErrors.PlayernameNotFound:
  113. return Localization.Key("SSP_INVALID_USERNAME_OR_PASSWORD");
  114. case PlayernameErrors.IncorrectPassword:
  115. return Localization.Key("SSP_INVALID_USERNAME_OR_PASSWORD");
  116. default:
  117. return Localization.Key("SSP_ERROR");
  118. }
  119. }
  120. public CloudRequest<PlayernameErrors?> CreateSparkSocPlayer(string username, string password)
  121. {
  122. return _sparkSocController.CreatePlayerName(username, password);
  123. }
  124. public CloudRequest<IAccessToken, PlayernameErrors?> GetVisitingIslandsAccessToken()
  125. {
  126. return _sparkSocController.RequestInstallAccessToken("gamestates");
  127. }
  128. public CloudRequest<PlayernameErrors?> LoginSparkSocPlayer(string username, string password)
  129. {
  130. return _sparkSocController.LinkToPlayerName(username, password);
  131. }
  132. public Leaderboard GetGlobalLeaderboard(int score1, int score2, int score3, int score4)
  133. {
  134. Leaderboard leaderboard = _leaderboardController.GetGlobalTop().ReOrder(LeaderboardSort);
  135. CapPlayerLevels(leaderboard);
  136. if (_sparkSocController.IsLinkedToPlayerName)
  137. {
  138. leaderboard.UpdateRecord(_sparkSocController.InstallUuid, _sparkSocController.PlayerName, score1, score2, score3, score4, LeaderboardSort);
  139. }
  140. return leaderboard;
  141. }
  142. public Leaderboard GetCountryLeaderboard(int score1, int score2, int score3, int score4)
  143. {
  144. Leaderboard leaderboard = _leaderboardController.GetCountryTop().ReOrder(LeaderboardSort);
  145. CapPlayerLevels(leaderboard);
  146. if (_sparkSocController.IsLinkedToPlayerName)
  147. {
  148. leaderboard.UpdateRecord(_sparkSocController.InstallUuid, _sparkSocController.PlayerName, score1, score2, score3, score4, LeaderboardSort);
  149. }
  150. return leaderboard;
  151. }
  152. public Leaderboard GetPersonalCountryLeaderboard(int score1, int score2, int score3, int score4)
  153. {
  154. Leaderboard leaderboard = _leaderboardController.GetCountryLocal().ReOrder(LeaderboardSort);
  155. CapPlayerLevels(leaderboard);
  156. if (leaderboard.Records.Count != 0)
  157. {
  158. leaderboard.UpdateRecord(_sparkSocController.InstallUuid, _sparkSocController.PlayerName, score1, score2, score3, score4, LeaderboardSort);
  159. leaderboard.ShrinkBoardAround(_sparkSocController.InstallUuid, 4, LeaderboardSort);
  160. }
  161. return leaderboard;
  162. }
  163. public Leaderboard GetPersonalGlobalLeaderboard(int score1, int score2, int score3, int score4)
  164. {
  165. Leaderboard leaderboard = _leaderboardController.GetGlobalLocal().ReOrder(LeaderboardSort);
  166. CapPlayerLevels(leaderboard);
  167. if (leaderboard.Records.Count != 0)
  168. {
  169. leaderboard.UpdateRecord(_sparkSocController.InstallUuid, _sparkSocController.PlayerName, score1, score2, score3, score4, LeaderboardSort);
  170. leaderboard.ShrinkBoardAround(_sparkSocController.InstallUuid, 4, LeaderboardSort);
  171. }
  172. return leaderboard;
  173. }
  174. public int GetMyGlobalServerRank()
  175. {
  176. return _leaderboardController.GlobalRank;
  177. }
  178. public void LogoutSparkSocPlayer()
  179. {
  180. _sparkSocController.LogOutPlayer();
  181. }
  182. public void PostNewHighScores(int score1, int score2, int score3, int score4)
  183. {
  184. if (!_failedToLoad && _sparkSocController != null && SingletonMonobehaviour<CIGVisitingIslandsManager>.IsAvailable && _sparkSocController.IsLinkedToPlayerName && _sparkSocController.IsInstallRegistered && _sparkSocController.IsPlayerLoggedIn)
  185. {
  186. ScoreRecord scoreRecord = new ScoreRecord();
  187. scoreRecord.Username = _sparkSocController.PlayerName;
  188. scoreRecord.Sauid = _sparkSocController.PlayerUuid;
  189. scoreRecord.Sagid = _sparkSocController.InstallUuid;
  190. scoreRecord.GameUuid = sparkSocGameId;
  191. scoreRecord.CountryCode = _countryCode;
  192. scoreRecord.Data = string.Empty;
  193. scoreRecord.PrimaryScore = score1;
  194. scoreRecord.SecondaryScore = score2;
  195. scoreRecord.TertiaryScore = score3;
  196. scoreRecord.QuaternaryScore = score4;
  197. scoreRecord.IslandReady = SingletonMonobehaviour<CIGVisitingIslandsManager>.Instance.IslandIsSent();
  198. scoreRecord.Signature = _leaderboardController.SignRecord(scoreRecord, leaderboardSecret);
  199. _leaderboardController.UpdateScore(scoreRecord);
  200. }
  201. }
  202. public void Run(IEnumerator func)
  203. {
  204. SingletonMonobehaviour<Scheduler>.Instance.StartRoutine(func);
  205. }
  206. public void Run(string name, IEnumerator func)
  207. {
  208. throw new NotImplementedException("Use CIGSparkSocService.Run(IEnumerator func)");
  209. }
  210. public void Stop(string name)
  211. {
  212. throw new NotImplementedException("Not supported by CIGSparkSocService");
  213. }
  214. private void CapPlayerLevels(Leaderboard leaderboard)
  215. {
  216. if (SingletonMonobehaviour<CIGGameState>.Instance.Level > 150)
  217. {
  218. return;
  219. }
  220. List<LeaderboardEntry> list = leaderboard.Records.FindAll((LeaderboardEntry x) => x.PrimaryScore > 150);
  221. int count = list.Count;
  222. int num = count - 1;
  223. for (int i = 0; i < count; i++)
  224. {
  225. LeaderboardEntry leaderboardEntry = list[i];
  226. if (leaderboardEntry.PrimaryScore >= 150)
  227. {
  228. leaderboard.UpdateRecord(leaderboardEntry.Sagid, leaderboardEntry.Username, 150, leaderboardEntry.SecondaryScore, leaderboardEntry.TertiaryScore, 0, (i != num) ? null : LeaderboardSort);
  229. }
  230. }
  231. }
  232. }