AccountFileInfo.cs 20 KB

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