AccountFileInfo.cs 16 KB

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