AccountFileInfo.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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<EventLinkData> eventLinks = new List<EventLinkData>();
  273. public List<int> selectEventLinkIds = new List<int>();
  274. public int curStep;
  275. public bool isCompleted;
  276. }
  277. /// <summary>
  278. /// 重建索引表
  279. /// 主要给道具用,比较多,调用频繁
  280. /// </summary>
  281. private void RestoreIndexMap()
  282. {
  283. itemIndexMap.Clear();
  284. for (int i = 0; i < playerData.ItemListData.Count; i++)
  285. {
  286. itemIndexMap[playerData.ItemListData[i].guid] = i;
  287. }
  288. }
  289. /// <summary>
  290. /// 加载玩家数据,一切数据:从这里开始
  291. /// </summary>
  292. public void LoadPlayerData()
  293. {
  294. #if UNITY_WEIXINMINIGAME && !UNITY_EDITOR
  295. persistentDataPath = WX.env.USER_DATA_PATH + fileName;
  296. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  297. if (wxFileSystemManager.AccessSync(persistentDataPath).Equals("access:ok"))
  298. {
  299. string data = wxFileSystemManager.ReadFileSync(persistentDataPath, "utf8");
  300. playerData = new PlayerData();
  301. JsonUtility.FromJsonOverwrite(data, playerData);
  302. }
  303. else
  304. {
  305. ClearInitPlayerData();
  306. SavePlayerData();
  307. }
  308. #else
  309. if (!File.Exists(persistentDataPath))
  310. {
  311. LogTool.Log("没有文件: " + persistentDataPath);
  312. ClearInitPlayerData();
  313. // File.Create(persistentDataPath).Close();
  314. }
  315. LogTool.Log("读取=文件: " + persistentDataPath);
  316. StreamReader sr = File.OpenText(persistentDataPath);
  317. string data = sr.ReadToEnd();
  318. sr.Close();
  319. playerData = new PlayerData();
  320. JsonUtility.FromJsonOverwrite(data, playerData);
  321. #endif
  322. //初始化索引
  323. RestoreIndexMap();
  324. }
  325. private int lastHeroIdx = 0;
  326. private int lastItemIdx = 0;
  327. /// <summary>
  328. /// 保存item数据
  329. /// </summary>
  330. /// <param name="itemInfo"></param>
  331. /// <param name="now">是否立即保存</param>
  332. /// <param name="zeroDel">count为0时,是否删除</param>
  333. public void SaveItemData(ItemInfo itemInfo, bool now = true)
  334. {
  335. if (itemIndexMap.TryGetValue(itemInfo.guid, out int index))
  336. {
  337. //count为零且需要立即删除时,移除Data并移除索引(也有count为0,不删除的情况,就走else的逻辑)
  338. // if (itemInfo.count.Value == 0 && zeroDel)
  339. // {
  340. // playerData.ItemListData.RemoveAt(index);
  341. // itemIndexMap.Remove(itemInfo.guid);
  342. // }
  343. // else
  344. // {
  345. // // 快速找到索引,直接修改
  346. // playerData.ItemListData[index] = itemInfo.ToItemData();
  347. // }
  348. // 快速找到索引,直接修改
  349. playerData.ItemListData[index] = itemInfo.ToItemData();
  350. }
  351. else
  352. {
  353. foreach (ItemData itemData in playerData.ItemListData)
  354. {
  355. if (itemData.guid == itemInfo.guid)
  356. {
  357. LogTool.Error("??? guid重复了" + itemInfo.itemID + "-" + itemData.guid);
  358. }
  359. }
  360. // 添加新数据并更新索引表
  361. playerData.ItemListData.Add(itemInfo.ToItemData());
  362. itemIndexMap[itemInfo.guid] = playerData.ItemListData.Count - 1;
  363. }
  364. if (now)
  365. {
  366. SavePlayerData();
  367. }
  368. }
  369. // public void SaveEqGUID()
  370. // {
  371. // SavePlayerData();
  372. // }
  373. /// <summary>
  374. /// 清除空数据,重建索引
  375. /// </summary>
  376. private void CleanEmptyData()
  377. {
  378. // 从后往前遍历,这样删除元素,可以边循环,边删除
  379. for (int i = playerData.ItemListData.Count - 1; i >= 0; i--)
  380. {
  381. // string ss = "";
  382. ItemData itemData = playerData.ItemListData[i];
  383. if (itemData.itemCount == 0)
  384. {
  385. //不是装备,删除
  386. if (itemData.eqData == null || itemData.eqData.zyEqId == 0)
  387. {
  388. playerData.ItemListData.RemoveAt(i);
  389. // ss = "删除道具:" + itemData.guid;
  390. }
  391. else if (itemData.eqData != null && itemData.eqData.zyEqId != 0 && !itemData.eqData.isEquip)
  392. {
  393. // ss = "删除装备:" + itemData.guid;
  394. playerData.ItemListData.RemoveAt(i);
  395. }
  396. // LogTool.Log(ss);
  397. }
  398. }
  399. RestoreIndexMap();
  400. }
  401. public void SavePlayerData(bool clean = false)
  402. {
  403. if (clean)
  404. {
  405. CleanEmptyData();
  406. }
  407. if (!string.IsNullOrEmpty(persistentDataPath))
  408. {
  409. string playerSettingJson = JsonManager.ToJson(playerData);
  410. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  411. WXFileSystemManager wxFileSystemManager = WX.GetFileSystemManager();
  412. wxFileSystemManager.WriteFileSync(persistentDataPath, playerSettingJson, "utf8");
  413. #else
  414. File.WriteAllText(persistentDataPath, playerSettingJson);
  415. #endif
  416. }
  417. }
  418. public void DeleteFile(string filePath)
  419. {
  420. #if UNITY_WEIXINMINIGAME&& !UNITY_EDITOR
  421. playerData = new PlayerData();
  422. SavePlayerData();
  423. ClearInitPlayerData();
  424. #else
  425. if (File.Exists(filePath))
  426. {
  427. File.Delete(filePath); // 删除文件
  428. LogTool.Log($"文件已删除:{filePath}");
  429. }
  430. else
  431. {
  432. LogTool.Log($"文件不存在:{filePath}");
  433. }
  434. #endif
  435. }
  436. /// <summary>
  437. /// 不要服务器的话,这里初始化玩家的起始数据
  438. /// </summary>
  439. public void ClearInitPlayerData()
  440. {
  441. ItemData coin = new ItemData(GlobalParam.Item_Coin_ID, 0);
  442. ItemData diamond = new ItemData(GlobalParam.Item_Diamond_ID);
  443. ItemData heroExp = new ItemData(GlobalParam.Item_HeroExp_ID, 0);
  444. playerData.ItemListData.Add(coin);
  445. playerData.ItemListData.Add(diamond);
  446. playerData.ItemListData.Add(heroExp);
  447. // DaoYouData daoYouData = new DaoYouData();
  448. // daoYouData.id = 1;
  449. // daoYouData.favorabilityLv = 1;
  450. // playerData.daoYouDatas.Add(daoYouData);
  451. //测试道具
  452. playerData.ItemListData.Add(new ItemData(1201, 10));
  453. playerData.ItemListData.Add(new ItemData(1202, 10));
  454. playerData.ItemListData.Add(new ItemData(1301, 10));
  455. playerData.ItemListData.Add(new ItemData(1302, 10));
  456. playerData.ItemListData.Add(new ItemData(1303, 10));
  457. playerData.ItemListData.Add(new ItemData(1304, 10));
  458. //
  459. playerData.ItemListData.Add(new ItemData(1401, 10));
  460. playerData.ItemListData.Add(new ItemData(1402, 10));
  461. playerData.ItemListData.Add(new ItemData(1403, 10));
  462. playerData.ItemListData.Add(new ItemData(1404, 10));
  463. playerData.ItemListData.Add(new ItemData(1601, 10));
  464. // var allSkill = new[] { 1101};
  465. // foreach (var i in allSkill)
  466. // {
  467. // SkillData skillData = new SkillData();
  468. // skillData.id = i;
  469. // skillData.star = 1;
  470. // skillData.level = 1;
  471. // skillData.useIndex = -1;
  472. // playerData.AllSkillDatas.Add(skillData);
  473. // }
  474. //添加所有测试法宝和功法
  475. List<SkillConfig> fabaoPowerupConfigs = ConfigComponent.Instance.GetAll<SkillConfig>().ToList();
  476. List<SkillConfig> gongfa = fabaoPowerupConfigs.Where(s => s.SkillType == 1 || s.SkillType == 2).ToList();
  477. var fanappower = gongfa.GroupBy(f => f.IDGroup).Select(g => new { config = g.OrderBy(f => f.ID).FirstOrDefault() }).ToList();
  478. foreach (var x1 in fanappower)
  479. {
  480. SkillData skillData = new SkillData();
  481. skillData.id = x1.config.IDGroup;
  482. skillData.star = 1;
  483. skillData.level = 1;
  484. skillData.useIndex = -1;
  485. playerData.AllSkillDatas.Add(skillData);
  486. }
  487. FabaoConfig[] fabaoConfig = ConfigComponent.Instance.GetAll<FabaoConfig>();
  488. foreach (var i in fabaoConfig)
  489. {
  490. FaBaoData faaData = new FaBaoData();
  491. faaData.id = i.ID;
  492. faaData.level = 1;
  493. faaData.useIndex = -1;
  494. playerData.AllFaBaoDatas.Add(faaData);
  495. }
  496. HeroData heroData = new HeroData();
  497. playerData.heroData = heroData;
  498. heroData.heroModelId = 101;
  499. heroData.heroPowerId = 1;
  500. heroData.upTime = TimeHelper.ClientNow();
  501. //添加第一关
  502. PlacesData placesData = new PlacesData();
  503. placesData.id = 1;
  504. playerData.placesDatas.Add(placesData);
  505. SavePlayerData();
  506. }
  507. [System.Serializable]
  508. public class HeroData
  509. {
  510. /// <summary>
  511. /// 英雄基础信息
  512. /// </summary>
  513. public int heroModelId;
  514. /// <summary>
  515. /// 英雄等级
  516. /// </summary>
  517. public int heroPowerId;
  518. public int exp;
  519. public long upTime;
  520. public bool isCombat;
  521. public int TaoismSkillId;
  522. public List<int> ImmortalBond = new List<int>();
  523. }
  524. [System.Serializable]
  525. public class ItemData
  526. {
  527. public int itemId;
  528. public long itemCount;
  529. public string guid;
  530. public EqData eqData;
  531. public ItemData(int itemId, long itemCount = 0, string guid = "")
  532. {
  533. this.itemId = itemId;
  534. this.itemCount = itemCount;
  535. if (string.IsNullOrEmpty(guid))
  536. {
  537. this.guid = itemId.ToString();
  538. }
  539. else
  540. {
  541. this.guid = guid;
  542. }
  543. // eqData = null;
  544. }
  545. }
  546. /// <summary>
  547. /// 装备数据
  548. /// </summary>
  549. [System.Serializable]
  550. public class EqData
  551. {
  552. // public string guid;
  553. // public int count;
  554. /// <summary>
  555. /// HeroBasicEquipConfig ID
  556. /// </summary>
  557. public int zyEqId;
  558. // public int dropLv;
  559. public int quality;
  560. /// <summary>
  561. /// 是否穿了(职业装备)
  562. /// </summary>
  563. public bool isEquip;
  564. // /// <summary>
  565. // /// 穿在哪个职业身上
  566. // /// </summary>
  567. // public int zy;
  568. }
  569. }