AccountFileInfo.cs 19 KB

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