AccountFileInfo.cs 19 KB

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