AccountFileInfo.cs 19 KB

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