AccountFileInfo.cs 19 KB

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