AccountFileInfo.cs 14 KB

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