AccountFileInfo.cs 12 KB

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