AccountFileInfo.cs 19 KB

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