UserProperty.cs 4.2 KB

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