AccountFileInfo.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using Core.Utility;
  7. using Excel2Json;
  8. using Fort23.Core;
  9. using Fort23.UTool;
  10. using GameLogic.Bag;
  11. using GameLogic.Hero;
  12. using UnityEngine;
  13. using UnityEngine.Serialization;
  14. using Utility;
  15. #if UNITY_WEIXINMINIGAME
  16. using WeChatWASM;
  17. #endif
  18. public class AccountFileInfo : Singleton<AccountFileInfo>
  19. {
  20. public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
  21. public PlayerData playerData = new PlayerData();
  22. /// <summary>
  23. /// 索引,用于快速查找
  24. /// </summary>
  25. private Dictionary<string, int> itemIndexMap = new Dictionary<string, int>();
  26. public string fileName = "/playerData.txt";
  27. [System.Serializable]
  28. public class PlayerData
  29. {
  30. /// <summary>
  31. /// 神识等级
  32. /// </summary>
  33. public int divineSenseLevel = 1;
  34. /// <summary>
  35. /// 神识点
  36. /// </summary>
  37. public float divineSensePoint = 0;
  38. /// <summary>
  39. /// 今天回复的神识值
  40. /// </summary>
  41. public float todayDivineSensePoint = 0;
  42. /// <summary>
  43. /// 神识经验
  44. /// </summary>
  45. public int divineSenseexp = 0;
  46. //每天刷新时间
  47. public long nextRefence;
  48. /// <summary>
  49. /// 洞府经验丹使用数量
  50. /// </summary>
  51. public int todayUseExpElixrPanelCount = 0;
  52. public string playerName = "";
  53. /// <summary>
  54. /// 英雄信息
  55. /// </summary>
  56. public HeroData heroData;
  57. public HeroData yindaoHeroData;
  58. public int CurrentZuiZhongEventListId;
  59. public List<DaoYouData> daoYouDatas = new List<DaoYouData>();
  60. public List<DaoYouGuaiJiData> daoYouGuaiJiDatas = new List<DaoYouGuaiJiData>();
  61. /// <summary>
  62. /// 道具信息
  63. /// </summary>
  64. public List<ItemData> ItemListData = new List<ItemData>();
  65. //地图区域数据
  66. public List<PlacesData> placesDatas = new List<PlacesData>();
  67. //小地图数据
  68. public List<SmallPlacesData> smallPlacesDatas = new List<SmallPlacesData>();
  69. /// <summary>
  70. /// 商店数据
  71. /// </summary>
  72. public List<ShopData> shopDatas = new List<ShopData>();
  73. /// <summary>
  74. /// 功法数据
  75. /// </summary>
  76. public List<SkillData> AllSkillDatas = new List<SkillData>();
  77. /// <summary>
  78. /// 法宝数据
  79. /// </summary>
  80. public List<FaBaoData> AllFaBaoDatas = new List<FaBaoData>();
  81. /// <summary>
  82. /// 完成的事件
  83. /// </summary>
  84. public List<EventList> completeEvents = new List<EventList>();
  85. /// <summary>
  86. /// 背包里的事件
  87. ///
  88. /// </summary>
  89. public List<EventList> eventList = new List<EventList>();
  90. public List<SentimentData> SentimentDatas = new List<SentimentData>();
  91. //塔数据
  92. public List<TowerData> TowerDatas = new List<TowerData>();
  93. //掉落数据
  94. public List<QiankundaiData> QiankundaiDatas = new List<QiankundaiData>();
  95. public List<ItemData> QiankundaiItemDatas = new List<ItemData>();
  96. //招募数据
  97. public List<SummonData> SummonDatas = new List<SummonData>();
  98. /// <summary>
  99. /// 挂机掉落了多少时间
  100. /// </summary>
  101. public int QiankundaiDropTimer;
  102. /// <summary>
  103. /// 领取挂机每日掉落时间
  104. /// </summary>
  105. public long QiankundaiLingQuDailyDropTime;
  106. public long ExitTime;
  107. public bool boxFree;
  108. public bool isTiggerPlot;
  109. public int curPhase;
  110. public int curStep;
  111. public bool isForceDone = false;
  112. public bool isTriggerDone = false;
  113. public List<int> phaseKey = new List<int>();
  114. public List<int> phaseValue = new List<int>();
  115. }
  116. public class QiankundaiData
  117. {
  118. public int id;
  119. public long refenceTime;
  120. public int dropCount;
  121. public int dropTime;
  122. }
  123. /// <summary>
  124. /// 塔数据
  125. /// </summary>
  126. [System.Serializable]
  127. public class TowerData
  128. {
  129. public int id;
  130. public List<TowerLevelData> towerLevelDatas = new List<TowerLevelData>();
  131. public int GetMaxUlockid()
  132. {
  133. int id = 0;
  134. TowerConfig towerConfig = ConfigComponent.Instance.Get<TowerConfig>(this.id);
  135. List<TowerInfoConfig> towerInfoConfigs = ConfigComponent.Instance.GetAll<TowerInfoConfig>().ToList();
  136. var configs = towerInfoConfigs.Where(c => c.groupId == towerConfig.towerinfoIdGroup).ToList();
  137. foreach (var towerInfoConfig in configs)
  138. {
  139. var lastTowerLevelData = towerLevelDatas.FirstOrDefault(td => td.id == towerInfoConfig.ID - 1);
  140. if ((lastTowerLevelData != null &&
  141. lastTowerLevelData.finish) ||
  142. towerInfoConfig.level == 1)
  143. {
  144. if (towerInfoConfig.ID > id)
  145. {
  146. id = towerInfoConfig.ID;
  147. }
  148. }
  149. }
  150. return id;
  151. }
  152. }
  153. /// <summary>
  154. /// 塔层数据
  155. /// </summary>
  156. [System.Serializable]
  157. public class TowerLevelData
  158. {
  159. public int id;
  160. public bool finish;
  161. public bool reward;
  162. }
  163. /// <summary>
  164. /// 心境数据
  165. /// </summary>
  166. [System.Serializable]
  167. public class SentimentData
  168. {
  169. public int id;
  170. //主属性
  171. public SentimentProperty mainSentiment;
  172. public List<SentimentProperty> sentimentProperties = new List<SentimentProperty>();
  173. }
  174. /// <summary>
  175. /// 心境属性
  176. /// </summary>
  177. [System.Serializable]
  178. public class SentimentProperty
  179. {
  180. // 属性类别
  181. public int groupId;
  182. // 词条档位
  183. public int level = 0;
  184. }
  185. [System.Serializable]
  186. public class DaoYouGuaiJiData
  187. {
  188. public long leaveTime;
  189. public long guajiTime;
  190. public bool isLeave;
  191. public List<ItemData> items = new List<ItemData>();
  192. public int daoyouDataID;
  193. }
  194. /// <summary>
  195. /// 道友数据
  196. /// </summary>
  197. [System.Serializable]
  198. public class DaoYouData
  199. {
  200. public int id;
  201. /// <summary>
  202. /// 喜爱礼物
  203. /// </summary>
  204. public List<int> loveIds = new List<int>();
  205. /// <summary>
  206. /// 好感等级
  207. /// </summary>
  208. public int favorabilityLv;
  209. /// <summary>
  210. /// 经验
  211. /// </summary>
  212. public int exp;
  213. /// <summary>
  214. /// 已经领取奖励的好感等级
  215. /// </summary>
  216. public List<int> awardLevels = new List<int>();
  217. /// <summary>
  218. /// 心情
  219. /// </summary>
  220. public int emotion;
  221. /// <summary>
  222. /// 心情刷新时间
  223. /// </summary>
  224. public long emotionTime;
  225. //心情值
  226. public int emotionValue;
  227. }
  228. //地图区域数据
  229. [System.Serializable]
  230. public class PlacesData
  231. {
  232. //区域Id
  233. public int id;
  234. //地图进度
  235. public int progress;
  236. }
  237. /// <summary>
  238. /// 小地图数据
  239. /// </summary>
  240. [System.Serializable]
  241. public class SmallPlacesData
  242. {
  243. public int id;
  244. public int completionEventCount;
  245. }
  246. /// <summary>
  247. /// 商店数据
  248. /// </summary>
  249. [System.Serializable]
  250. public class ShopData
  251. {
  252. public int id;
  253. public List<ShopItem> shopItemList = new List<ShopItem>();
  254. // 商店刷新时间
  255. public long refreshTime = 0;
  256. }
  257. [System.Serializable]
  258. public class ShopItem
  259. {
  260. // 商品ID
  261. public int id;
  262. // 商品购买次数
  263. public int buyCount = 0;
  264. // 商品结束时间
  265. public long endTime = -1;
  266. }
  267. [System.Serializable]
  268. public class FaBaoData
  269. {
  270. public int id;
  271. public int level;
  272. public int useIndex;
  273. }
  274. [System.Serializable]
  275. public class SkillData
  276. {
  277. public int id;
  278. public int level;
  279. public int star;
  280. public int useIndex;
  281. }
  282. [System.Serializable]
  283. public class EventData
  284. {
  285. public int eventID;
  286. //完成cishu
  287. public int completeCount = 0;
  288. }
  289. /// <summary>
  290. /// 刷出来的事件列表
  291. /// </summary>
  292. [System.Serializable]
  293. public class EventLinkData
  294. {
  295. public int eventLinkId;
  296. public int eventId;
  297. /// <summary>
  298. /// 事件监听列表
  299. /// </summary>
  300. public List<EventConditionData> eventConditions = new List<EventConditionData>();
  301. }
  302. /// <summary>
  303. /// 刷出来的事件列表
  304. /// </summary>
  305. [System.Serializable]
  306. public class EventConditionData
  307. {
  308. public int eventId;
  309. public int eventCondition;
  310. public int fishCount;
  311. }
  312. /// <summary>
  313. /// 刷出来的事件列表
  314. /// </summary>
  315. [System.Serializable]
  316. public class EventList
  317. {
  318. public int guid;
  319. public int eventID;
  320. public int heroLevel;
  321. public List<ItemData> itemInfos = new List<ItemData>();
  322. public List<EventLinkData> eventLinks = new List<EventLinkData>();
  323. public List<int> selectEventLinkIds = new List<int>();
  324. public int curStep;
  325. public bool isCompleted;
  326. public bool isCompleted1;
  327. }
  328. [System.Serializable]
  329. public class SummonData
  330. {
  331. // 招募ID
  332. public int id;
  333. //免费招募次数
  334. public int oneFreeCount;
  335. //下一次免费招募时间
  336. public long nextOneFreeTime;
  337. //十连招募次数
  338. public int tenFreeCount;
  339. //下一次十连招募时间
  340. public long nextTenFreeTime;
  341. public int onePayCount;
  342. public int tenPayCount;
  343. //保底次数
  344. public int baodiCount1;
  345. public int baodiCount2;
  346. // 存储最近几次十连抽的结果
  347. public List<IntList> drawHistory = new List<IntList>();
  348. public int score;
  349. public List<SummonSocreData> summonSocreData = new List<SummonSocreData>();
  350. public bool isSummonSocre;
  351. }
  352. [System.Serializable]
  353. public class IntList
  354. {
  355. public List<int> list;
  356. }
  357. [System.Serializable]
  358. public class SummonSocreData
  359. {
  360. public int id;
  361. public int count;
  362. }
  363. /// <summary>
  364. /// 重建索引表
  365. /// 主要给道具用,比较多,调用频繁
  366. /// </summary>
  367. private void RestoreIndexMap()
  368. {
  369. itemIndexMap.Clear();
  370. for (int i = 0; i < playerData.ItemListData.Count; i++)
  371. {
  372. itemIndexMap[playerData.ItemListData[i].guid] = i;
  373. }
  374. }
  375. /// <summary>
  376. /// 加载玩家数据,一切数据:从这里开始
  377. /// </summary>
  378. public void LoadPlayerData()
  379. {
  380. #if UNITY_WEIXINMINIGAME && !UNITY_EDITOR
  381. persistentDataPath = WX.env.USER_DATA_PATH + fileName;
  382. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  383. if (wxFileSystemManager.AccessSync(persistentDataPath).Equals("access:ok"))
  384. {
  385. string data = wxFileSystemManager.ReadFileSync(persistentDataPath, "utf8");
  386. playerData = new PlayerData();
  387. JsonUtility.FromJsonOverwrite(data, playerData);
  388. }
  389. else
  390. {
  391. ClearInitPlayerData();
  392. SavePlayerData();
  393. }
  394. #else
  395. if (!File.Exists(persistentDataPath))
  396. {
  397. LogTool.Log("没有文件: " + persistentDataPath);
  398. ClearInitPlayerData();
  399. // File.Create(persistentDataPath).Close();
  400. }
  401. LogTool.Log("读取=文件: " + persistentDataPath);
  402. StreamReader sr = File.OpenText(persistentDataPath);
  403. string data = sr.ReadToEnd();
  404. sr.Close();
  405. playerData = new PlayerData();
  406. JsonUtility.FromJsonOverwrite(data, playerData);
  407. #endif
  408. //初始化索引
  409. RestoreIndexMap();
  410. }
  411. private int lastHeroIdx = 0;
  412. private int lastItemIdx = 0;
  413. /// <summary>
  414. /// 保存item数据
  415. /// </summary>
  416. /// <param name="itemInfo"></param>
  417. /// <param name="now">是否立即保存</param>
  418. /// <param name="zeroDel">count为0时,是否删除</param>
  419. public void SaveItemData(ItemInfo itemInfo, bool now = true)
  420. {
  421. if (itemIndexMap.TryGetValue(itemInfo.guid, out int index))
  422. {
  423. //count为零且需要立即删除时,移除Data并移除索引(也有count为0,不删除的情况,就走else的逻辑)
  424. // if (itemInfo.count.Value == 0 && zeroDel)
  425. // {
  426. // playerData.ItemListData.RemoveAt(index);
  427. // itemIndexMap.Remove(itemInfo.guid);
  428. // }
  429. // else
  430. // {
  431. // // 快速找到索引,直接修改
  432. // playerData.ItemListData[index] = itemInfo.ToItemData();
  433. // }
  434. // 快速找到索引,直接修改
  435. playerData.ItemListData[index] = itemInfo.ToItemData();
  436. }
  437. else
  438. {
  439. foreach (ItemData itemData in playerData.ItemListData)
  440. {
  441. if (itemData.guid == itemInfo.guid)
  442. {
  443. LogTool.Error("??? guid重复了" + itemInfo.itemID + "-" + itemData.guid);
  444. }
  445. }
  446. // 添加新数据并更新索引表
  447. playerData.ItemListData.Add(itemInfo.ToItemData());
  448. itemIndexMap[itemInfo.guid] = playerData.ItemListData.Count - 1;
  449. }
  450. if (now)
  451. {
  452. SavePlayerData();
  453. }
  454. }
  455. // public void SaveEqGUID()
  456. // {
  457. // SavePlayerData();
  458. // }
  459. /// <summary>
  460. /// 清除空数据,重建索引
  461. /// </summary>
  462. private void CleanEmptyData()
  463. {
  464. // 从后往前遍历,这样删除元素,可以边循环,边删除
  465. for (int i = playerData.ItemListData.Count - 1; i >= 0; i--)
  466. {
  467. // string ss = "";
  468. ItemData itemData = playerData.ItemListData[i];
  469. if (itemData.itemCount == 0)
  470. {
  471. //不是装备,删除
  472. if (itemData.eqData == null || itemData.eqData.zyEqId == 0)
  473. {
  474. playerData.ItemListData.RemoveAt(i);
  475. // ss = "删除道具:" + itemData.guid;
  476. }
  477. else if (itemData.eqData != null && itemData.eqData.zyEqId != 0 && !itemData.eqData.isEquip)
  478. {
  479. // ss = "删除装备:" + itemData.guid;
  480. playerData.ItemListData.RemoveAt(i);
  481. }
  482. // LogTool.Log(ss);
  483. }
  484. }
  485. RestoreIndexMap();
  486. }
  487. public void SavePlayerData(bool clean = false)
  488. {
  489. if (clean)
  490. {
  491. CleanEmptyData();
  492. }
  493. if (!string.IsNullOrEmpty(persistentDataPath))
  494. {
  495. string playerSettingJson = JsonManager.ToJson(playerData);
  496. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  497. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  498. wxFileSystemManager.WriteFileSync(persistentDataPath, playerSettingJson, "utf8");
  499. #else
  500. File.WriteAllText(persistentDataPath, playerSettingJson);
  501. #endif
  502. }
  503. }
  504. public void DeleteFile(string filePath)
  505. {
  506. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  507. playerData = new PlayerData();
  508. SavePlayerData();
  509. ClearInitPlayerData();
  510. #else
  511. if (File.Exists(filePath))
  512. {
  513. File.Delete(filePath); // 删除文件
  514. LogTool.Log($"文件已删除:{filePath}");
  515. }
  516. else
  517. {
  518. LogTool.Log($"文件不存在:{filePath}");
  519. }
  520. #endif
  521. }
  522. /// <summary>
  523. /// 不要服务器的话,这里初始化玩家的起始数据
  524. /// </summary>
  525. public void ClearInitPlayerData()
  526. {
  527. //初始化道具
  528. InitialPlayerConfig initialPlayerConfig = ConfigComponent.Instance.Get<InitialPlayerConfig>(1);
  529. for (var i = 0; i < initialPlayerConfig.itemId.Length; i++)
  530. {
  531. ItemData coin = new ItemData(initialPlayerConfig.itemId[i], initialPlayerConfig.itemAmount[i]);
  532. playerData.ItemListData.Add(coin);
  533. }
  534. //初始化法宝
  535. for (var i = 0; i < initialPlayerConfig.fabaoId.Length; i++)
  536. {
  537. FaBaoData faaData = new FaBaoData();
  538. faaData.id = initialPlayerConfig.fabaoId[i];
  539. faaData.level = 1;
  540. faaData.useIndex = i;
  541. playerData.AllFaBaoDatas.Add(faaData);
  542. }
  543. //初始化功法
  544. for (var i = 0; i < initialPlayerConfig.gongfaId.Length; i++)
  545. {
  546. SkillData skillData = new SkillData();
  547. skillData.id = initialPlayerConfig.gongfaId[i];
  548. skillData.star = 1;
  549. skillData.level = 1;
  550. if (i <= 0)
  551. {
  552. skillData.useIndex = i;
  553. }
  554. playerData.AllSkillDatas.Add(skillData);
  555. }
  556. playerData.divineSensePoint = initialPlayerConfig.shenshiPoint;
  557. //添加英雄数据
  558. HeroData heroData = new HeroData();
  559. playerData.heroData = heroData;
  560. heroData.heroModelId = 101;
  561. heroData.heroPowerId = 1;
  562. heroData.upTime = TimeHelper.ClientNow();
  563. HeroData yindaoHeroData = new HeroData();
  564. playerData.yindaoHeroData = yindaoHeroData;
  565. yindaoHeroData.heroModelId = 101;
  566. yindaoHeroData.heroPowerId = 200;
  567. yindaoHeroData.upTime = TimeHelper.ClientNow();
  568. //添加第一关地图数据
  569. PlacesData placesData = new PlacesData();
  570. placesData.id = 1;
  571. playerData.placesDatas.Add(placesData);
  572. SmallPlacesData smallPlacesData = new SmallPlacesData();
  573. smallPlacesData.id = 1;
  574. playerData.smallPlacesDatas.Add(smallPlacesData);
  575. SavePlayerData();
  576. }
  577. [System.Serializable]
  578. public class HeroData
  579. {
  580. /// <summary>
  581. /// 英雄基础信息
  582. /// </summary>
  583. public int heroModelId;
  584. /// <summary>
  585. /// 英雄等级
  586. /// </summary>
  587. public int heroPowerId;
  588. public float exp;
  589. public long upTime;
  590. public bool isCombat;
  591. public int TaoismSkillId;
  592. public List<int> ImmortalBond = new List<int>();
  593. }
  594. [System.Serializable]
  595. public class ItemData
  596. {
  597. public int itemId;
  598. public long itemCount;
  599. public string guid;
  600. public EqData eqData;
  601. public ItemData(int itemId, long itemCount = 0, string guid = "")
  602. {
  603. this.itemId = itemId;
  604. this.itemCount = itemCount;
  605. if (string.IsNullOrEmpty(guid))
  606. {
  607. this.guid = itemId.ToString();
  608. }
  609. else
  610. {
  611. this.guid = guid;
  612. }
  613. // eqData = null;
  614. }
  615. }
  616. /// <summary>
  617. /// 装备数据
  618. /// </summary>
  619. [System.Serializable]
  620. public class EqData
  621. {
  622. // public string guid;
  623. // public int count;
  624. /// <summary>
  625. /// HeroBasicEquipConfig ID
  626. /// </summary>
  627. public int zyEqId;
  628. // public int dropLv;
  629. public int quality;
  630. /// <summary>
  631. /// 是否穿了(职业装备)
  632. /// </summary>
  633. public bool isEquip;
  634. // /// <summary>
  635. // /// 穿在哪个职业身上
  636. // /// </summary>
  637. // public int zy;
  638. }
  639. }