AccountFileInfo.cs 20 KB

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