AccountFileInfo.cs 20 KB

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