AccountFileInfo.cs 19 KB

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