AccountFileInfo.cs 14 KB

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