AccountFileInfo.cs 20 KB

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