AccountFileInfo.cs 13 KB

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