AccountFileInfo.cs 15 KB

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