AccountFileInfo.cs 15 KB

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