AccountFileInfo.cs 21 KB

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