AccountFileInfo.cs 18 KB

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