AccountFileInfo.cs 19 KB

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