AccountFileInfo.cs 13 KB

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