UserProperty.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. using UnityEngine;
  7. /* 本文件中包含2各类文件
  8. * UserProperty用于保存用户游戏中的相关数据
  9. * GameRunningData 保存游戏中全局的数据 */
  10. [System.Serializable]
  11. public static class UserProperty
  12. {
  13. // Start is called before the first frame update
  14. public static string userId = "JTYNZP9O";
  15. public static string name = "任天堂";
  16. public static int coin = 1005;
  17. public static string email, mobile, level;
  18. public static bool isRegUser = false;
  19. public static int advWatchTimes = 0; // 记录用户当天一共看过几次广告
  20. //public static List<Item> food = new();
  21. //public static List<Item> toy = new();
  22. //public static List<Item> other = new(); // 这里dict string对应的是物品大类food, toy, other
  23. public static Dictionary<string, int> food = new();
  24. public static Dictionary<string, int> toy = new();
  25. public static Dictionary<string, int> other = new();
  26. public static List<DogProperty> dogs = new();
  27. public static string runEnv = "Release"; // NA, Dev, Release
  28. public static string dogNames()
  29. {
  30. // 获取狗名字列表
  31. List<string> dogNameList = new List<string>();
  32. foreach (var dog in UserProperty.dogs)
  33. {
  34. dogNameList.Add(dog.dog_name);
  35. }
  36. string dogNames = string.Join(", ", dogNameList);
  37. return dogNames;
  38. }
  39. // 更新用户信息
  40. public static void RefreshUserInfo(string userInfoJson)
  41. {
  42. Dictionary<string, string> userInfo = JsonConvert.DeserializeObject<Dictionary<string, string>>(userInfoJson);
  43. UserProperty.name = userInfo["user_name"];
  44. UserProperty.coin = int.Parse(userInfo["coin"]);
  45. UserProperty.email = userInfo["email"];
  46. UserProperty.mobile = userInfo["mobile"];
  47. UserProperty.level = userInfo["level"];
  48. if (UserProperty.level == "basic") { EnviromentSetting.maxDogQty = 2; }
  49. if (UserProperty.level == "pro") { EnviromentSetting.maxDogQty = 3; }
  50. UserProperty.isRegUser = bool.Parse(userInfo["isRegUser"]);
  51. }
  52. // 更新用户food, toy, other数据
  53. public static void RefreshUserItems(string itemsJson)
  54. {
  55. UserProperty.food.Clear();
  56. UserProperty.toy.Clear();
  57. UserProperty.other.Clear();
  58. // 保存道具数据
  59. Dictionary<string, object> props = JsonConvert.DeserializeObject<Dictionary<string, object>>(itemsJson);
  60. Dictionary<string, int> foods = JsonConvert.DeserializeObject<Dictionary<string, int>>(props["food"].ToString());
  61. Dictionary<string, int> toys = JsonConvert.DeserializeObject<Dictionary<string, int>>(props["toy"].ToString());
  62. Dictionary<string, int> others = JsonConvert.DeserializeObject<Dictionary<string, int>>(props["other"].ToString());
  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 RefreshDogInfo(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 GetDogIndexById(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 GetDogIdByIndex(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. }