GameCenterRanks.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace TowerDevil
  5. {
  6. [Serializable]
  7. internal sealed class RankEntry
  8. {
  9. public int rank;
  10. public string name;
  11. public long score;
  12. public bool mine;
  13. }
  14. [Serializable]
  15. internal sealed class RankResult
  16. {
  17. public string error;
  18. public RankEntry[] entries;
  19. }
  20. internal static class GameCenterRanks
  21. {
  22. #if UNITY_IOS && !UNITY_EDITOR
  23. static Profile activeProfile;
  24. public static void Sync(Profile profile)
  25. {
  26. activeProfile = profile;
  27. var standing = profile.CurrentStanding;
  28. GameCenterNative.Sync(standing?.playerID, standing?.bestNight ?? 0, standing?.totalScore ?? 0, SignedIn);
  29. }
  30. static void SignedIn(string playerID)
  31. {
  32. if (activeProfile.CurrentStanding?.playerID == playerID) { Sync(activeProfile); return; }
  33. activeProfile.SelectGameCenterPlayer(playerID);
  34. activeProfile.Save();
  35. }
  36. public static void Load(bool byScore, Profile profile, Action<RankResult> completed)
  37. {
  38. GameCenterNative.Load(byScore ? 1 : 0, json => completed?.Invoke(JsonUtility.FromJson<RankResult>(json)));
  39. }
  40. #else
  41. public static void Sync(Profile profile) { }
  42. public static void Load(bool byScore, Profile profile, Action<RankResult> completed)
  43. {
  44. string[] names = { "GrimWarden", "AshVex", "MolochJr", "SableHorn", "Pyre",
  45. "Nyx", "BoneTail", "Emberly", "DuskWing", "Ruin" };
  46. int[] values = byScore ? new[] { 31240, 26880, 24110, 20930, 17420, 14880, 12060, 9840, 7620, 4980 }
  47. : new[] { 48, 43, 39, 34, 29, 25, 21, 17, 14, 9 };
  48. var rows = new List<RankEntry>();
  49. for (int i = 0; i < names.Length; i++)
  50. rows.Add(new RankEntry { name = names[i], score = values[i] });
  51. rows.Add(new RankEntry { name = profile.name, score = byScore ? profile.score : profile.bestNight, mine = true });
  52. rows.Sort((a, b) => b.score.CompareTo(a.score));
  53. for (int i = 0; i < rows.Count; i++) rows[i].rank = i + 1;
  54. completed(new RankResult { entries = rows.ToArray() });
  55. }
  56. #endif
  57. }
  58. }