AccountFileInfo.cs 20 KB

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