AccountFileInfo.cs 20 KB

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