AccountFileInfo.cs 13 KB

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