Player.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using NetData;
  6. /// <summary>
  7. /// 玩家数据结构
  8. /// </summary>
  9. public class Player
  10. {
  11. #region 基础字段
  12. /// <summary>
  13. /// 游戏ID
  14. /// </summary>
  15. public string uid { get; set; }
  16. /// <summary>
  17. /// 分区ID
  18. /// </summary>
  19. public int zoneid { get; set; }
  20. /// <summary>
  21. /// 分区名称
  22. /// </summary>
  23. public string zonename { get; set; }
  24. /// <summary>
  25. /// http通讯中采用的token
  26. /// </summary>
  27. public string TK="";
  28. //========================================
  29. /// <summary>
  30. /// 类型数值 含义
  31. ///-1 未实名
  32. ///0 0 到 7 岁
  33. ///8 8 到 15 岁
  34. ///16 16 到 17 岁
  35. ///18 成年玩家
  36. /// </summary>
  37. public int ageRange = -1;
  38. #endregion
  39. //-----------------分割线----------------------------
  40. #region 集合型字段
  41. private Info_UserBase _baseInfo;
  42. private int _baseInfoHash = 0;
  43. /// <summary>
  44. /// 基础信息
  45. /// </summary>
  46. public Info_UserBase baseInfo
  47. {
  48. get
  49. {
  50. if (_baseInfo == null || _baseInfoHash != _userGameVer)
  51. {
  52. _baseInfo = _userGame?["baseInfo"].ToObject<Info_UserBase>() ?? null;
  53. _baseInfoHash = _userGameVer;
  54. }
  55. return _baseInfo;
  56. }
  57. set
  58. {
  59. _baseInfo = value;
  60. }
  61. }
  62. /// <summary>
  63. /// 玩家圈养的 英雄数据列表
  64. /// </summary>
  65. public GameCollectHero collectHero;
  66. /// <summary>
  67. /// 物品数据
  68. /// </summary>
  69. public GameCollectItem collectItem;
  70. /// <summary>
  71. /// 装备
  72. /// </summary>
  73. public GameCollectEquips collectEquip;
  74. /// <summary>
  75. /// 言灵
  76. /// </summary>
  77. public GameCollectYanling collectYanling;
  78. public GameCollectTaskCard collectTaskCard;
  79. /// <summary>
  80. /// 触发条件得到的限时随机礼包
  81. /// </summary>
  82. public List<Ins_Gift> triggerLimitTsGift = new List<Ins_Gift>();
  83. /// <summary>
  84. /// 玩家的配置的战队信息
  85. /// </summary>
  86. public Info_HeroTeamConfig heroTeamConfig;
  87. private Info_NewMap _newMap;
  88. private int _newMap_ver;
  89. /// <summary>
  90. /// 地图解锁信息
  91. /// </summary>
  92. public Info_NewMap newMap
  93. {
  94. get
  95. {
  96. if (null == _newMap || _newMap_ver != _userGameVer)
  97. {
  98. _newMap = _userGame.TryGetValue("newMap", out var jo)?jo.ToObject<Info_NewMap>() : new Info_NewMap();
  99. _newMap_ver = _userGameVer;
  100. }
  101. return _newMap;
  102. }
  103. set
  104. {
  105. _newMap = value;
  106. }
  107. }
  108. private Info_PrivateState _privateState;
  109. private int _privateStateVer = 0;
  110. /// <summary>
  111. /// 私有数据
  112. /// </summary>
  113. public Info_PrivateState PrivateState
  114. {
  115. get
  116. {
  117. if (null == _privateState || _privateStateVer != _userGameVer)
  118. {
  119. if (null != _userGame && null != _userGame["privateState"])
  120. {
  121. _privateState = _userGame["privateState"].ToObject<Info_PrivateState>();
  122. _privateStateVer = _userGameVer;
  123. }
  124. }
  125. return _privateState;
  126. }
  127. set
  128. {
  129. _privateState = value;
  130. }
  131. }
  132. private Info_NewbieGuide _newbieGuide;
  133. private int _newbieGuideVer = 0;
  134. /// <summary>
  135. /// 新手引导
  136. /// </summary>
  137. public Info_NewbieGuide NewbieGuideInfo
  138. {
  139. get
  140. {
  141. if (null == _newbieGuide || _newbieGuideVer != _userGameVer)
  142. {
  143. _newbieGuide = _userGame.TryGetValue("NewbieGuide",out var jo)?jo.ToObject<Info_NewbieGuide>() : new Info_NewbieGuide();
  144. _newbieGuideVer = _userGameVer;
  145. }
  146. return _newbieGuide;
  147. }
  148. set
  149. {
  150. _newbieGuide = value;
  151. }
  152. }
  153. private Info_UserTask _task;
  154. private int _task_ver;
  155. /// <summary>
  156. /// 任务数据 2020年7月8日19:07:57
  157. /// </summary>
  158. public Info_UserTask task
  159. {
  160. get
  161. {
  162. if (null == _task || _task_ver != _userGameVer)
  163. {
  164. _task = _userGame.TryGetValue("task", out var jo) ? jo.ToObject<Info_UserTask>() : new Info_UserTask();
  165. _task_ver = _userGameVer;
  166. }
  167. return _task;
  168. }
  169. set
  170. {
  171. _task = value;
  172. }
  173. }
  174. /// <summary>
  175. /// 商城数据
  176. /// </summary>
  177. public Info_UserShop shopdata = new Info_UserShop();
  178. private Info_UserPVP _pvpInfo;
  179. private int _pvpInfo_ver;
  180. /// <summary>
  181. /// PVP的数据记录(2020年5月11日 更新)
  182. /// </summary>
  183. public Info_UserPVP pvpRecord
  184. {
  185. get
  186. {
  187. if (null == _pvpInfo || _pvpInfo_ver != _userGameVer)
  188. {
  189. _pvpInfo = _userGame.TryGetValue("pvp", out var jo) ? jo.ToObject<Info_UserPVP>() : new Info_UserPVP();
  190. _pvpInfo_ver = _userGameVer;
  191. }
  192. return _pvpInfo;
  193. }
  194. set
  195. {
  196. _pvpInfo = value;
  197. }
  198. }
  199. private Info_TaskCard_Shop _taskCard_Shop;
  200. private int _taskCard_shop_ver = 0;
  201. /// <summary>
  202. /// 玩家任务卡商店数据
  203. /// </summary>
  204. public Info_TaskCard_Shop taskCardShop
  205. {
  206. get
  207. {
  208. if (null == _taskCard_Shop || _taskCard_shop_ver != _userGameVer)
  209. {
  210. _taskCard_Shop = _userGame.TryGetValue("taskCardShop", out var jo) ? jo.ToObject<Info_TaskCard_Shop>() : new Info_TaskCard_Shop();
  211. _taskCard_shop_ver = _userGameVer;
  212. }
  213. return _taskCard_Shop;
  214. }
  215. set
  216. {
  217. _taskCard_Shop = value;
  218. }
  219. }
  220. /// <summary>
  221. /// 圣哲学院
  222. /// </summary>
  223. public CollegeInfo collegeInfo = new CollegeInfo();
  224. /// <summary>
  225. /// 神庙
  226. /// </summary>
  227. public ShenmiaoInfo shenmiaoInfo = new ShenmiaoInfo();
  228. public Info_Gem gemInfo = new Info_Gem();
  229. public Info_Pay payInfo = new Info_Pay();
  230. /// <summary>
  231. /// 武器商店每日的武器信息
  232. /// </summary>
  233. public Dictionary<int, Dictionary<int, List<int>>> weaponPool = new Dictionary<int, Dictionary<int, List<int>>>();
  234. /// <summary>
  235. /// 购买武器记录 ---因为有重置所以这里做记录
  236. /// </summary>
  237. public Dictionary<int, Dictionary<int, List<int>>> weaponReward = new Dictionary<int, Dictionary<int, List<int>>>();
  238. /// <summary>
  239. ///
  240. /// </summary>
  241. public Dictionary<int, StorageVo> storage = new Dictionary<int, StorageVo>();
  242. public StoreNewFeild StoreNewFeild;
  243. #endregion
  244. #region 不需要同步字段
  245. #endregion
  246. #region ' 暂时废弃不用的字段 '
  247. //================好友===============
  248. /// <summary>
  249. /// 冒险类型 观其的数据记录
  250. /// </summary>
  251. [Obsolete("关卡地图")]
  252. public ArenasRecord areansRecord;
  253. #endregion
  254. private JObject _userGame;
  255. private int _userGameVer = 0;
  256. /// <summary>
  257. /// 初始化玩家的数据
  258. /// </summary>
  259. public void Initlize(JObject game)
  260. {
  261. LogHelper.Log("玩家数据反序列化 1");
  262. areansRecord = new ArenasRecord();
  263. collectHero = new GameCollectHero();
  264. collectItem = new GameCollectItem();
  265. collectEquip = new GameCollectEquips();
  266. collectYanling = new GameCollectYanling();
  267. collectTaskCard = new GameCollectTaskCard();
  268. StoreNewFeild = new StoreNewFeild();
  269. zonename = PlayerPrefs.GetString("ZoneName");
  270. if (game != null)
  271. {
  272. TK = game["TK"].ToString();
  273. _userGame = game;
  274. _userGameVer = _userGame.GetHashCode();
  275. InitFromStore((JObject)game["store"]);
  276. areansRecord.InitArenasGateData((JObject)game["gates"]);
  277. collectHero.InitData((JObject)game["heros"], this);
  278. shopdata = game.TryGetValue("shopdata", out var jo) ? jo.ToObject<Info_UserShop>() : new Info_UserShop();
  279. heroTeamConfig = game.TryGetValue("heroTeamConfig", out var htj) ? htj.ToObject<Info_HeroTeamConfig>() : new Info_HeroTeamConfig(); // 队伍配置信息
  280. collegeInfo.Initlize(game["college"]);
  281. shenmiaoInfo.Initlize(game["shenmiao"]);
  282. gemInfo.Initlize(game["Gem"]);
  283. payInfo.Initlize(game["pay"]);
  284. }
  285. LogHelper.Log("玩家数据反序列化 完");
  286. }
  287. public void InitFromStore(JObject store)
  288. {
  289. collectItem.InitItemData(store); // 普通道具
  290. collectEquip.InitItemData(store); // 装备从item中独立出来
  291. collectYanling.InitItemData(store); // 言灵
  292. collectTaskCard.InitData(store); // 任务卡
  293. StoreNewFeild.InitItemData(store);
  294. this.initWeaponShopInfo(store); //商店武器信息
  295. }
  296. public void initWeaponShopInfo(JObject store)
  297. {
  298. this.weaponPool.Clear();
  299. this.weaponReward.Clear();
  300. JObject jweaponPoolDic = JObject.Parse(store["weaponPool"].ToString());
  301. //Dictionary<int, Dictionary<int, List<int>>> weaponPool = new Dictionary<int, Dictionary<int, List<int>>>();
  302. foreach (KeyValuePair<string, JToken> kv in jweaponPoolDic)
  303. {
  304. JObject jDic = JObject.Parse(kv.Value.ToString());
  305. Dictionary<int, List<int>> dic = new Dictionary<int, List<int>>();
  306. foreach (KeyValuePair<string, JToken> kt in jDic)
  307. {
  308. JArray arr = JArray.Parse(kt.Value.ToString());
  309. List<int> list = new List<int>();
  310. foreach (JToken item in arr)
  311. {
  312. list.Add(int.Parse(item.ToString()));
  313. }
  314. dic[int.Parse(kt.Key)] = list;
  315. }
  316. this.weaponPool[int.Parse(kv.Key)] = dic;
  317. }
  318. JObject jweaponRewardDic = JObject.Parse(store["weaponReward"].ToString());
  319. //Dictionary<int, Dictionary<int, List<int>>> weaponReward = new Dictionary<int, Dictionary<int, List<int>>>();
  320. foreach (KeyValuePair<string, JToken> kv in jweaponRewardDic)
  321. {
  322. JObject jDic = JObject.Parse(kv.Value.ToString());
  323. Dictionary<int, List<int>> dic = new Dictionary<int, List<int>>();
  324. foreach (KeyValuePair<string, JToken> kt in jDic)
  325. {
  326. JArray arr = JArray.Parse(kt.Value.ToString());
  327. List<int> list = new List<int>();
  328. foreach (JToken item in arr)
  329. {
  330. list.Add(int.Parse(item.ToString()));
  331. }
  332. dic[int.Parse(kt.Key)] = list;
  333. }
  334. this.weaponReward[int.Parse(kv.Key)] = dic;
  335. }
  336. initStorageInfo(store["storage"]);
  337. }
  338. public void initStorageInfo(JToken Jstorage)
  339. {
  340. this.storage.Clear();
  341. Dictionary<int, StorageVo> tempdic = new Dictionary<int, StorageVo>();
  342. JObject jstorageDic = JObject.Parse(Jstorage.ToString());
  343. foreach (KeyValuePair<string, JToken> kv in jstorageDic)
  344. {
  345. JObject jitem = JObject.Parse(kv.Value.ToString());
  346. JObject itemDic = JObject.Parse(jitem["itemDic"].ToString());
  347. Dictionary<string, ItemVo> idic = new Dictionary<string, ItemVo>();
  348. foreach (KeyValuePair<string, JToken> ki in itemDic)
  349. {
  350. ItemVo vo = new ItemVo();
  351. vo.typeId = ki.Key;
  352. vo.count = int.Parse(ki.Value.ToString());
  353. idic[ki.Key] = vo;
  354. }
  355. tempdic[int.Parse(kv.Key)] = new StorageVo();
  356. tempdic[int.Parse(kv.Key)].itemDic = idic;
  357. Dictionary<int, UserEquipmentVo> equipments = jitem["equipment"].ToObject<Dictionary<int, UserEquipmentVo>>();
  358. foreach (var ke in equipments)
  359. {
  360. ke.Value.uid = ke.Key.ToString();
  361. }
  362. tempdic[int.Parse(kv.Key)].equipment = equipments;
  363. Dictionary<int, UserYanlingVo> yanling = jitem["yanling"].ToObject<Dictionary<int, UserYanlingVo>>();
  364. foreach (var ky in yanling)
  365. {
  366. ky.Value.uid = ky.Key.ToString();
  367. }
  368. tempdic[int.Parse(kv.Key)].yanling = yanling;
  369. }
  370. this.storage = tempdic;
  371. }
  372. /// <summary>
  373. /// 检查玩家账户余额是否充足
  374. /// </summary>
  375. /// <param name="type">消费类型</param>
  376. /// <param name="cashPara">消费钻石数</param>
  377. /// <param name="goldPara">消费金币数</param>
  378. /// <param name="sparPara">消费晶石数</param>
  379. /// <returns>true = 有足够的账户余额 false = 不足</returns>
  380. public bool CheckHasEngoughMoney(AccountType type, int cashPara = 0, int goldPara = 0, int sparPara = 0, int friendPara = 0)
  381. {
  382. if (type == AccountType.none)
  383. {
  384. return false;
  385. }
  386. else if (type == AccountType.gold)
  387. {
  388. return baseInfo.gold >= goldPara;
  389. }
  390. else if (type == AccountType.cash)
  391. {
  392. return baseInfo.cash >= cashPara;
  393. }
  394. else if (type == AccountType.spar)
  395. {
  396. return baseInfo.spar >= sparPara;
  397. }
  398. else
  399. {
  400. return false;
  401. }
  402. }
  403. /// <summary>
  404. /// //声明一个委托,其实就是个“命令”
  405. /// </summary>
  406. public delegate void UserAccountChangedEventHandler();
  407. /// <summary>
  408. /// 玩家账号的金钱信息发生变化事件
  409. /// </summary>
  410. public UserAccountChangedEventHandler myAccountChangedDelegate;
  411. /// <summary>
  412. /// 玩家的邮件系统发生变化
  413. /// </summary>
  414. public EventHandler RefreshEmailChangedEvent;
  415. /// <summary>
  416. /// 玩家账号信息发生变化
  417. /// </summary>
  418. /// <param name="type">消费账号类型</param>
  419. /// <param name="para">变化数值</param>
  420. public void ChangeUserAccount(AccountType type, int para)
  421. {
  422. if (type == AccountType.none)
  423. return;
  424. if (type == AccountType.gold)
  425. {
  426. baseInfo.gold += para;
  427. }
  428. else if (type == AccountType.cash)
  429. {
  430. baseInfo.cash += para;
  431. }
  432. else if (type == AccountType.spar)
  433. {
  434. baseInfo.spar += para;
  435. }
  436. else if (type == AccountType.exp)
  437. {
  438. //判断是否升级啦,以及升到哪一级啦
  439. int temp = baseInfo.level;
  440. ////根据李宁提供的玩家经验表,和李宁沟通后,采用新的函数.20170406
  441. getNewXp(para);
  442. }
  443. else
  444. {
  445. }
  446. if (myAccountChangedDelegate != null)
  447. {
  448. myAccountChangedDelegate.Invoke();
  449. }
  450. }
  451. #region 玩家获得经验
  452. /// <summary>
  453. /// 通过等级获取当前升级需要的经验数据
  454. /// </summary>
  455. /// <param name="lvl"></param>
  456. /// <returns></returns>
  457. public sm_playerlevel getUserLvById(int lvl) => sm_playerlevel.getUserLvById(lvl);
  458. /// <summary>
  459. /// 新玩家获得经验----根据李宁提供的玩家经验表,和李宁沟通后,采用新的函数.20170406
  460. /// </summary>
  461. /// <param name="num"></param>
  462. private void getNewXp(int num) => baseInfo.getNewXp(num);
  463. #endregion
  464. /// <summary>
  465. /// 获取关卡战斗中,玩家上阵的英雄队伍配置
  466. /// </summary>
  467. /// <returns></returns>
  468. public Ins_TeamInfo CurrentBattleteam
  469. {
  470. get
  471. {
  472. Ins_TeamInfo _arenasBattleteam = heroTeamConfig.CurTeam;
  473. for (int i = 0; i < _arenasBattleteam.heros.Count; i++)
  474. {
  475. string uid = _arenasBattleteam.heros[i];
  476. GameHeroVo vo = collectHero.GetHeroGameInfoByHeroUId(uid);
  477. if (vo == null)
  478. {
  479. _arenasBattleteam.heros[i] = "";
  480. }
  481. }
  482. return _arenasBattleteam;
  483. }
  484. }
  485. #region 背包/道具相关操作
  486. /// <summary>
  487. /// 发放奖励字符串
  488. /// </summary>
  489. /// <param name="rewardStr">id,num;id,num;...</param>
  490. internal void putItemToStore(string rewardstr)
  491. {
  492. Dictionary<string, int> mRewardDic = new Dictionary<string, int>();
  493. int totalGold = 0; // 金币
  494. int totalCash = 0; // 钻石
  495. int totalTi = 0; // 体力
  496. if (!string.IsNullOrEmpty(rewardstr))
  497. {
  498. string[] arr = rewardstr.Split(';');
  499. foreach (string s in arr)
  500. {
  501. string[] rewards = s.Split(',');
  502. string itemid = rewards[0];
  503. int itemnum = Convert.ToInt32(rewards[1]);
  504. if (mRewardDic.ContainsKey(itemid))
  505. {
  506. mRewardDic[itemid] += itemnum;
  507. }
  508. else
  509. {
  510. mRewardDic.Add(itemid, itemnum);
  511. }
  512. }
  513. foreach (var kv in mRewardDic)
  514. {
  515. var itemMo = sm_item_base.GetMoById(int.Parse(kv.Key));
  516. if (null != itemMo)
  517. {
  518. var subType = (EItemSubType)itemMo.subType;
  519. switch (subType)
  520. {
  521. case EItemSubType.EXP: // 0: 指挥官经验
  522. break;
  523. case EItemSubType.Gold: // 1: 金币
  524. totalGold += kv.Value;
  525. break;
  526. case EItemSubType.Gem: // 2: 钻石
  527. totalCash += kv.Value;
  528. break;
  529. case EItemSubType.PVPCoin: // 5: PVP币
  530. break;
  531. case EItemSubType.Weapon: // 101: 武器
  532. case EItemSubType.YanLing: // 401: 言灵
  533. LogHelper.LogError("言灵和武器不可以在客户直接加,需要服务端生成uid,然后同步给客户端.");
  534. break;
  535. case EItemSubType.Segment: // 201: 碎片
  536. case EItemSubType.YanlingBookSegement: // 202: 言灵碎片
  537. case EItemSubType.StrengthStone: // 312: 进阶材料
  538. case EItemSubType.YanlingBook: // 313: 言灵召唤书
  539. case EItemSubType.AdvancedStone: // 321: 进阶材料
  540. case EItemSubType.AdvancedFineStone: // 322: 进阶材料
  541. case EItemSubType.ForgingMaterial: // 323: 锻造材料
  542. case EItemSubType.Gene: // 311: 基因,经验丹
  543. collectItem.AddItems(kv.Key, kv.Value);
  544. break;
  545. case EItemSubType.BattleItem_HP: // 341: 战场掉落补血丹 不进包裹
  546. case EItemSubType.BattleItem_MP: // 342: 战场掉落补魔丹 不进包裹
  547. case EItemSubType.BattleItem_Box: // 343: 战场掉落宝箱 不进包裹
  548. LogHelper.LogError("战场掉落道具不进入包裹!");
  549. break;
  550. default:
  551. LogHelper.LogError("落入包裹时,出现非法物品!");
  552. break;
  553. }
  554. }
  555. }
  556. }
  557. UserProxy.Instance.player.ChangeUserAccount(AccountType.cash, totalCash);
  558. UserProxy.Instance.player.ChangeUserAccount(AccountType.gold, totalGold);
  559. }
  560. #endregion
  561. }
  562. /// <summary>
  563. /// 账号数据类型
  564. /// </summary>
  565. public enum AccountType
  566. {
  567. none,
  568. gold,
  569. spar,
  570. cash,
  571. exp,
  572. friend
  573. }