AccountFileInfo.cs 21 KB

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