AccountFileInfo.cs 18 KB

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