UserProperty.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. Dictionary<string, int> foods = JsonConvert.DeserializeObject<Dictionary<string, int>>(props["food"].ToString());
  58. Dictionary<string, int> toys = JsonConvert.DeserializeObject<Dictionary<string, int>>(props["toy"].ToString());
  59. Dictionary<string, int> others = JsonConvert.DeserializeObject<Dictionary<string, int>>(props["other"].ToString());
  60. foreach (var food in foods)
  61. UserProperty.food.Add(food.Key, food.Value);
  62. foreach (var toy in toys)
  63. UserProperty.toy.Add(toy.Key, toy.Value);
  64. foreach (var other in others)
  65. UserProperty.other.Add(other.Key, other.Value);
  66. }
  67. // 更新用户名下狗的信息
  68. public static void FreshDogInfo(string dogsJson)
  69. {
  70. UserProperty.dogs.Clear();
  71. DogProperty[] dogProperties = JsonConvert.DeserializeObject<DogProperty[]>(dogsJson);
  72. foreach (var dog in dogProperties)
  73. {
  74. UserProperty.dogs.Add(dog);
  75. }
  76. }
  77. // 用Dog id搜索Gamedata.focusDog的下标
  78. public static int GetDogIndexById(string dogId)
  79. {
  80. for (int i = 0; i < UserProperty.dogs.Count; i++)
  81. {
  82. if (UserProperty.dogs[i].d_id == dogId)
  83. {
  84. return i;
  85. }
  86. }
  87. return -1;
  88. }
  89. // 用Gamedata.focusDog的下标获取Dog id
  90. public static string GetDogIdByIndex(int dogIndex)
  91. {
  92. if (dogIndex < 0 || dogIndex >= UserProperty.dogs.Count)
  93. {
  94. return null;
  95. }
  96. return UserProperty.dogs[dogIndex].d_id;
  97. }
  98. }
  99. // 待删除代码,废弃
  100. // string id 表示具体的物品id,如food_00001
  101. // qty 表示数量
  102. public class Item
  103. {
  104. public string id;
  105. public int qty;
  106. public Item(string id, int qty)
  107. {
  108. this.id = id;
  109. this.qty = qty;
  110. }
  111. }