AccountFileInfo.cs 13 KB

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