AccountFileInfo.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Core.Utility;
  5. using Fort23.Core;
  6. using Fort23.UTool;
  7. using GameLogic.Bag;
  8. using GameLogic.Hero;
  9. using UnityEngine;
  10. using Utility;
  11. #if UNITY_WEIXINMINIGAME
  12. using WeChatWASM;
  13. #endif
  14. public class AccountFileInfo : Singleton<AccountFileInfo>
  15. {
  16. public string persistentDataPath = Application.persistentDataPath + "/playerData.txt";
  17. public PlayerData playerData = new PlayerData();
  18. /// <summary>
  19. /// 索引,用于快速查找
  20. /// </summary>
  21. private Dictionary<string, int> itemIndexMap = new Dictionary<string, int>();
  22. public string fileName = "/playerData.txt";
  23. [System.Serializable]
  24. public class PlayerData
  25. {
  26. // public SkillData[] usrSkill = new SkillData[10];
  27. //
  28. // public FaBaoData[] UseFaBaoDatas = new FaBaoData[4];
  29. /// <summary>
  30. /// 商店数据
  31. /// </summary>
  32. public List<ShopData> shopDatas = new List<ShopData>();
  33. public List<SkillData> AllSkillDatas = new List<SkillData>();
  34. public List<FaBaoData> AllFaBaoDatas = new List<FaBaoData>();
  35. //每天刷新时间
  36. public long nextRefence;
  37. public int todayUseExpElixrPanelCount = 0;
  38. //地图探索进度
  39. public int senceExplorationProgress;
  40. public int divineSenseLevel = 1;
  41. public int divineSensePoint = 0;
  42. public int divineSenseexp = 0;
  43. public List<EventData> events = new List<EventData>();
  44. public List<EventList> eventList = new List<EventList>();
  45. public List<ItemData> ItemListData = new List<ItemData>();
  46. public HeroData heroData;
  47. /// <summary>
  48. /// 关卡进度
  49. /// </summary>
  50. public int levelBattle = 1;
  51. /// <summary>
  52. /// 是否全部阵亡一次
  53. /// </summary>
  54. public bool isAllHeroDie;
  55. /// <summary>
  56. /// 装备的GUID
  57. /// </summary>
  58. public long eqGUID = 0;
  59. /// <summary>
  60. /// 战斗引导下标
  61. /// </summary>
  62. public int combatGuideIndex = 0;
  63. // /// <summary>
  64. // /// 后备英雄(共享等级)
  65. // /// </summary>
  66. // public List<HeroData> HeroListInBackDatas = new List<HeroData>();
  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. // var allFaBao = new[] { };
  312. //
  313. // foreach (var i in allFaBao)
  314. // {
  315. // FaBaoData faaData = new FaBaoData();
  316. // faaData.id = i;
  317. // faaData.level = 1;
  318. // faaData.useIndex = -1;
  319. // playerData.AllFaBaoDatas.Add(faaData);
  320. // }
  321. HeroData heroData = new HeroData();
  322. playerData.heroData = heroData;
  323. heroData.heroModelId = 101;
  324. heroData.heroPowerId = 1;
  325. heroData.upTime = TimeHelper.ClientNow();
  326. // BagController.Instance.AddCoin(10000);
  327. // playerData.HeroListData.Add(heroData2);
  328. // playerData.HeroListData.Add(heroData3);
  329. // playerData.HeroListData.Add(heroData4);
  330. SavePlayerData();
  331. }
  332. [System.Serializable]
  333. public class HeroData
  334. {
  335. /// <summary>
  336. /// 英雄基础信息
  337. /// </summary>
  338. public int heroModelId;
  339. /// <summary>
  340. /// 英雄等级
  341. /// </summary>
  342. public int heroPowerId;
  343. public int exp;
  344. public long upTime;
  345. public bool isCombat;
  346. public int TaoismSkillId;
  347. }
  348. [System.Serializable]
  349. public class ItemData
  350. {
  351. public int itemId;
  352. public long itemCount;
  353. public string guid;
  354. public EqData eqData;
  355. public ItemData(int itemId, long itemCount = 0, string guid = "")
  356. {
  357. this.itemId = itemId;
  358. this.itemCount = itemCount;
  359. if (string.IsNullOrEmpty(guid))
  360. {
  361. this.guid = itemId.ToString();
  362. }
  363. else
  364. {
  365. this.guid = guid;
  366. }
  367. // eqData = null;
  368. }
  369. }
  370. /// <summary>
  371. /// 装备数据
  372. /// </summary>
  373. [System.Serializable]
  374. public class EqData
  375. {
  376. // public string guid;
  377. // public int count;
  378. /// <summary>
  379. /// HeroBasicEquipConfig ID
  380. /// </summary>
  381. public int zyEqId;
  382. // public int dropLv;
  383. public int quality;
  384. /// <summary>
  385. /// 是否穿了(职业装备)
  386. /// </summary>
  387. public bool isEquip;
  388. // /// <summary>
  389. // /// 穿在哪个职业身上
  390. // /// </summary>
  391. // public int zy;
  392. }
  393. }