AccountFileInfo.cs 17 KB

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