AccountFileInfo.cs 14 KB

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