AccountFileInfo.cs 20 KB

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