AccountFileInfo.cs 12 KB

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