AccountFileInfo.cs 14 KB

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