AccountFileInfo.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Core.Utility;
  5. using Fort23.Core;
  6. using Fort23.UTool;
  7. using GameLogic.Bag;
  8. using GameLogic.Hero;
  9. using UnityEngine;
  10. using Utility;
  11. #if UNITY_WEIXINMINIGAME
  12. using WeChatWASM;
  13. #endif
  14. public class AccountFileInfo : Singleton<AccountFileInfo>
  15. {
  16. public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
  17. public PlayerData playerData = new PlayerData();
  18. /// <summary>
  19. /// 索引,用于快速查找
  20. /// </summary>
  21. private Dictionary<string, int> itemIndexMap = new Dictionary<string, int>();
  22. public string fileName = "/playerData.txt";
  23. [System.Serializable]
  24. public class PlayerData
  25. {
  26. //每天刷新时间
  27. public long nextRefence;
  28. public int todayUseExpElixrPanelCount = 0;
  29. //地图探索进度
  30. public int senceExplorationProgress;
  31. public int divineSenseLevel = 1;
  32. public int divineSensePoint = 0;
  33. public int divineSenseexp = 0;
  34. public List<EventData> events = new List<EventData>();
  35. public List<EventList> eventList = new List<EventList>();
  36. public List<ItemData> ItemListData = new List<ItemData>();
  37. public HeroData heroData;
  38. /// <summary>
  39. /// 关卡进度
  40. /// </summary>
  41. public int levelBattle = 1;
  42. /// <summary>
  43. /// 是否全部阵亡一次
  44. /// </summary>
  45. public bool isAllHeroDie;
  46. /// <summary>
  47. /// 装备的GUID
  48. /// </summary>
  49. public long eqGUID = 0;
  50. /// <summary>
  51. /// 战斗引导下标
  52. /// </summary>
  53. public int combatGuideIndex = 0;
  54. // /// <summary>
  55. // /// 后备英雄(共享等级)
  56. // /// </summary>
  57. // public List<HeroData> HeroListInBackDatas = new List<HeroData>();
  58. }
  59. [System.Serializable]
  60. public class EventData
  61. {
  62. public int eventID;
  63. //完成cishu
  64. public int completeCount = 0;
  65. }
  66. /// <summary>
  67. /// 刷出来的事件列表
  68. /// </summary>
  69. [System.Serializable]
  70. public class EventList
  71. {
  72. public int eventID;
  73. public int curStep;
  74. }
  75. /// <summary>
  76. /// 重建索引表
  77. /// 主要给道具用,比较多,调用频繁
  78. /// </summary>
  79. private void RestoreIndexMap()
  80. {
  81. itemIndexMap.Clear();
  82. for (int i = 0; i < playerData.ItemListData.Count; i++)
  83. {
  84. itemIndexMap[playerData.ItemListData[i].guid] = i;
  85. }
  86. }
  87. /// <summary>
  88. /// 加载玩家数据,一切数据:从这里开始
  89. /// </summary>
  90. public void LoadPlayerData()
  91. {
  92. #if UNITY_WEIXINMINIGAME && !UNITY_EDITOR
  93. persistentDataPath = WX.env.USER_DATA_PATH + fileName;
  94. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  95. if (wxFileSystemManager.AccessSync(persistentDataPath).Equals("access:ok"))
  96. {
  97. string data = wxFileSystemManager.ReadFileSync(persistentDataPath, "utf8");
  98. playerData = new PlayerData();
  99. JsonUtility.FromJsonOverwrite(data, playerData);
  100. }
  101. else
  102. {
  103. ClearInitPlayerData();
  104. SavePlayerData();
  105. }
  106. #else
  107. if (!File.Exists(persistentDataPath))
  108. {
  109. LogTool.Log("没有文件: " + persistentDataPath);
  110. ClearInitPlayerData();
  111. // File.Create(persistentDataPath).Close();
  112. }
  113. LogTool.Log("读取=文件: " + persistentDataPath);
  114. StreamReader sr = File.OpenText(persistentDataPath);
  115. string data = sr.ReadToEnd();
  116. sr.Close();
  117. playerData = new PlayerData();
  118. JsonUtility.FromJsonOverwrite(data, playerData);
  119. #endif
  120. //初始化索引
  121. RestoreIndexMap();
  122. }
  123. private int lastHeroIdx = 0;
  124. private int lastItemIdx = 0;
  125. /// <summary>
  126. /// 保存item数据
  127. /// </summary>
  128. /// <param name="itemInfo"></param>
  129. /// <param name="now">是否立即保存</param>
  130. /// <param name="zeroDel">count为0时,是否删除</param>
  131. public void SaveItemData(ItemInfo itemInfo, bool now = true)
  132. {
  133. if (itemIndexMap.TryGetValue(itemInfo.guid, out int index))
  134. {
  135. // //count为零且需要立即删除时,移除Data并移除索引(也有count为0,不删除的情况,就走else的逻辑)
  136. // if (itemInfo.count.Value == 0 && zeroDel)
  137. // {
  138. // playerData.ItemListData.RemoveAt(index);
  139. // itemIndexMap.Remove(itemInfo.guid);
  140. // }
  141. // else
  142. // {
  143. // // 快速找到索引,直接修改
  144. // playerData.ItemListData[index] = itemInfo.ToItemData();
  145. // }
  146. // 快速找到索引,直接修改
  147. // playerData.ItemListData[index] = itemInfo.ToItemData();
  148. }
  149. else
  150. {
  151. foreach (ItemData itemData in playerData.ItemListData)
  152. {
  153. if (itemData.guid == itemInfo.guid)
  154. {
  155. LogTool.Error("??? guid重复了" + itemInfo.itemID + "-" + itemData.guid);
  156. }
  157. }
  158. // 添加新数据并更新索引表
  159. // playerData.ItemListData.Add(itemInfo.ToItemData());
  160. itemIndexMap[itemInfo.guid] = playerData.ItemListData.Count - 1;
  161. }
  162. if (now)
  163. {
  164. SavePlayerData();
  165. }
  166. // if (itemInfo.guid == playerData.ItemListData[lastItemIdx].guid)
  167. // {
  168. // playerData.ItemListData[lastItemIdx] = itemInfo.ToItemData();
  169. // SavePlayerData();
  170. // return;
  171. // }
  172. //
  173. // for (int i = 0; i < playerData.ItemListData.Count; i++)
  174. // {
  175. // ItemData itemData = playerData.ItemListData[i];
  176. // if (itemData.guid == itemInfo.guid)
  177. // {
  178. // playerData.ItemListData[i] = itemInfo.ToItemData();
  179. // //存下来,用于快速查找
  180. // lastItemIdx = i;
  181. // SavePlayerData();
  182. // return;
  183. // }
  184. // }
  185. // playerData.ItemListData.Add(itemInfo.ToItemData());
  186. // SavePlayerData();
  187. }
  188. // public void SaveEqGUID()
  189. // {
  190. // SavePlayerData();
  191. // }
  192. /// <summary>
  193. /// 清除空数据,重建索引
  194. /// </summary>
  195. private void CleanEmptyData()
  196. {
  197. // 从后往前遍历,这样删除元素,可以边循环,边删除
  198. for (int i = playerData.ItemListData.Count - 1; i >= 0; i--)
  199. {
  200. // string ss = "";
  201. ItemData itemData = playerData.ItemListData[i];
  202. if (itemData.itemCount == 0)
  203. {
  204. //不是装备,删除
  205. if (itemData.eqData == null || itemData.eqData.zyEqId == 0)
  206. {
  207. playerData.ItemListData.RemoveAt(i);
  208. // ss = "删除道具:" + itemData.guid;
  209. }
  210. else if (itemData.eqData != null && itemData.eqData.zyEqId != 0 && !itemData.eqData.isEquip)
  211. {
  212. // ss = "删除装备:" + itemData.guid;
  213. playerData.ItemListData.RemoveAt(i);
  214. }
  215. // LogTool.Log(ss);
  216. }
  217. }
  218. RestoreIndexMap();
  219. }
  220. public void SavePlayerData(bool clean = false)
  221. {
  222. if (clean)
  223. {
  224. CleanEmptyData();
  225. }
  226. if (!string.IsNullOrEmpty(persistentDataPath))
  227. {
  228. string playerSettingJson = JsonManager.ToJson(playerData);
  229. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  230. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  231. wxFileSystemManager.WriteFileSync(persistentDataPath, playerSettingJson, "utf8");
  232. #else
  233. File.WriteAllText(persistentDataPath, playerSettingJson);
  234. #endif
  235. }
  236. }
  237. public void DeleteFile(string filePath)
  238. {
  239. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  240. playerData = new PlayerData();
  241. SavePlayerData();
  242. ClearInitPlayerData();
  243. #else
  244. if (File.Exists(filePath))
  245. {
  246. File.Delete(filePath); // 删除文件
  247. LogTool.Log($"文件已删除:{filePath}");
  248. }
  249. else
  250. {
  251. LogTool.Log($"文件不存在:{filePath}");
  252. }
  253. #endif
  254. }
  255. /// <summary>
  256. /// 不要服务器的话,这里初始化玩家的起始数据
  257. /// </summary>
  258. public void ClearInitPlayerData()
  259. {
  260. ItemData coin = new ItemData(GlobalParam.Item_Coin_ID, 0);
  261. ItemData diamond = new ItemData(GlobalParam.Item_Diamond_ID);
  262. ItemData heroExp = new ItemData(GlobalParam.Item_HeroExp_ID, 0);
  263. playerData.ItemListData.Add(coin);
  264. playerData.ItemListData.Add(diamond);
  265. playerData.ItemListData.Add(heroExp);
  266. //测试道具
  267. playerData.ItemListData.Add(new ItemData(1201,10));
  268. playerData.ItemListData.Add(new ItemData(1202,10));
  269. playerData.ItemListData.Add(new ItemData(1301,10));
  270. playerData.ItemListData.Add(new ItemData(1302,10));
  271. playerData.ItemListData.Add(new ItemData(1303,10));
  272. playerData.ItemListData.Add(new ItemData(1304,10));
  273. playerData.ItemListData.Add(new ItemData(1401,10));
  274. playerData.ItemListData.Add(new ItemData(1402,10));
  275. playerData.ItemListData.Add(new ItemData(1403,10));
  276. playerData.ItemListData.Add(new ItemData(1404,10));
  277. HeroData heroData = new HeroData();
  278. playerData.heroData = heroData;
  279. heroData.heroModelId = 101;
  280. heroData.heroPowerId = 1;
  281. heroData.upTime = TimeHelper.ClientNow();
  282. // BagController.Instance.AddCoin(10000);
  283. // playerData.HeroListData.Add(heroData2);
  284. // playerData.HeroListData.Add(heroData3);
  285. // playerData.HeroListData.Add(heroData4);
  286. SavePlayerData();
  287. }
  288. [System.Serializable]
  289. public class HeroData
  290. {
  291. /// <summary>
  292. /// 英雄基础信息
  293. /// </summary>
  294. public int heroModelId;
  295. /// <summary>
  296. /// 英雄等级
  297. /// </summary>
  298. public int heroPowerId;
  299. public int exp;
  300. public long upTime;
  301. public bool isCombat;
  302. }
  303. [System.Serializable]
  304. public class ItemData
  305. {
  306. public int itemId;
  307. public long itemCount;
  308. public string guid;
  309. public EqData eqData;
  310. public ItemData(int itemId, long itemCount = 0, string guid = "")
  311. {
  312. this.itemId = itemId;
  313. this.itemCount = itemCount;
  314. if (string.IsNullOrEmpty(guid))
  315. {
  316. this.guid = itemId.ToString();
  317. }
  318. else
  319. {
  320. this.guid = guid;
  321. }
  322. // eqData = null;
  323. }
  324. }
  325. /// <summary>
  326. /// 装备数据
  327. /// </summary>
  328. [System.Serializable]
  329. public class EqData
  330. {
  331. // public string guid;
  332. // public int count;
  333. /// <summary>
  334. /// HeroBasicEquipConfig ID
  335. /// </summary>
  336. public int zyEqId;
  337. // public int dropLv;
  338. public int quality;
  339. /// <summary>
  340. /// 是否穿了(职业装备)
  341. /// </summary>
  342. public bool isEquip;
  343. // /// <summary>
  344. // /// 穿在哪个职业身上
  345. // /// </summary>
  346. // public int zy;
  347. }
  348. }