AccountFileInfo.cs 15 KB

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