UserProperty.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. /* 本文件中包含2各类文件
  7. * UserProperty用于保存用户游戏中的相关数据
  8. * GameRunningData 保存游戏中全局的数据 */
  9. [System.Serializable]
  10. public static class UserProperty
  11. {
  12. // Start is called before the first frame update
  13. public static string userId = "JTYNZP9O";
  14. public static string name = "任天堂";
  15. public static int coin = 1005;
  16. public static string email, mobile, level;
  17. public static bool isRegUser = false;
  18. public static int advWatchTimes = 0; // 记录用户当天一共看过几次广告
  19. //public static List<Item> food = new();
  20. //public static List<Item> toy = new();
  21. //public static List<Item> other = new(); // 这里dict string对应的是物品大类food, toy, other
  22. public static Dictionary<string, int> food = new();
  23. public static Dictionary<string, int> toy = new();
  24. public static Dictionary<string, int> other = new();
  25. public static List<DogProperty> dogs = new();
  26. public static string dogNames()
  27. {
  28. // 获取狗名字列表
  29. List<string> dogNameList = new List<string>();
  30. foreach (var dog in UserProperty.dogs)
  31. {
  32. dogNameList.Add(dog.dog_name);
  33. }
  34. string dogNames = string.Join(", ", dogNameList);
  35. return dogNames;
  36. }
  37. // 更新用户信息
  38. public static void FreshUserInfo(string userInfoJson)
  39. {
  40. Dictionary<string, string> userInfo = JsonConvert.DeserializeObject<Dictionary<string, string>>(userInfoJson);
  41. UserProperty.name = userInfo["user_name"];
  42. UserProperty.coin = int.Parse(userInfo["coin"]);
  43. UserProperty.email = userInfo["email"];
  44. UserProperty.mobile = userInfo["mobile"];
  45. UserProperty.level = userInfo["level"];
  46. if (UserProperty.level == "pro") { EnviromentSetting.maxDogQty = 3; }
  47. UserProperty.isRegUser = bool.Parse(userInfo["isRegUser"]);
  48. }
  49. // 更新用户food, toy, other数据
  50. public static void FreshUserItems(string itemsJson)
  51. {
  52. UserProperty.food.Clear();
  53. UserProperty.toy.Clear();
  54. UserProperty.other.Clear();
  55. // 保存道具数据
  56. Dictionary<string, object> props = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemsJson);
  57. string tempFood = "{\"food_00001\":10,\"food_00002\":99}";
  58. string tempToy = "{\"toy_00001\":10}";
  59. string tempOther = "{\"other_00001\":10}";
  60. Dictionary<string, int> foods = JsonConvert.DeserializeObject<Dictionary<string, int>>(tempFood);
  61. Dictionary<string, int> toys = JsonConvert.DeserializeObject<Dictionary<string, int>>(tempToy);
  62. Dictionary<string, int> others = JsonConvert.DeserializeObject<Dictionary<string, int>>(tempOther);
  63. foreach (var food in foods)
  64. UserProperty.food.Add(food.Key, food.Value);
  65. foreach (var toy in toys)
  66. UserProperty.toy.Add(toy.Key, toy.Value);
  67. foreach (var other in others)
  68. UserProperty.other.Add(other.Key, other.Value);
  69. }
  70. // 更新用户名下狗的信息
  71. public static void FreshDogInfo(string dogsJson)
  72. {
  73. UserProperty.dogs.Clear();
  74. DogProperty[] dogProperties = JsonConvert.DeserializeObject<DogProperty[]>(dogsJson);
  75. foreach (var dog in dogProperties)
  76. {
  77. UserProperty.dogs.Add(dog);
  78. }
  79. }
  80. // 用Dog id搜索Gamedata.focusDog的下标
  81. public static int GetDogIndex(string dogId)
  82. {
  83. for (int i = 0; i < UserProperty.dogs.Count; i++)
  84. {
  85. if (UserProperty.dogs[i].d_id == dogId)
  86. {
  87. return i;
  88. }
  89. }
  90. return -1;
  91. }
  92. // 用Gamedata.focusDog的下标获取Dog id
  93. public static string GetDogId(int dogIndex)
  94. {
  95. if (dogIndex < 0 || dogIndex >= UserProperty.dogs.Count)
  96. {
  97. return null;
  98. }
  99. return UserProperty.dogs[dogIndex].d_id;
  100. }
  101. }
  102. // 待删除代码,废弃
  103. // string id 表示具体的物品id,如food_00001
  104. // qty 表示数量
  105. public class Item
  106. {
  107. public string id;
  108. public int qty;
  109. public Item(string id, int qty)
  110. {
  111. this.id = id;
  112. this.qty = qty;
  113. }
  114. }