AccountFileInfo.cs 13 KB

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