UserProxy.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. using CSharpUtil;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. /// <summary>
  8. /// 玩家相关
  9. /// </summary>
  10. internal class UserProxy : ProxyBase<UserProxy>
  11. {
  12. public UserProxy() => this.opeCode = OpeCode.ope_user;
  13. #region 数据接口
  14. public const string UID_qq = "wqq";
  15. public string userName;
  16. public string pwd;
  17. public Player player { get; set; }
  18. /// <summary>
  19. /// 服务器和本地的时间差
  20. /// </summary>
  21. private long serverTS = 0;
  22. /// <summary>
  23. /// 服务器时间戳
  24. /// </summary>
  25. /// <returns></returns>
  26. public uint GetCurrentUnixTimeStamp()
  27. {
  28. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  29. return Convert.ToUInt32(ts.TotalSeconds + serverTS);
  30. }
  31. /// <summary>
  32. /// 服务器时间
  33. /// </summary>
  34. /// <returns></returns>
  35. public static DateTime GetCurrentDateTime() => DateTime.Now.FromUnixStamp(UserProxy.Instance.GetCurrentUnixTimeStamp());
  36. #endregion 数据接口
  37. /// <summary>
  38. /// [6000]获取分区列表
  39. /// </summary>
  40. /// <param name="uid">玩家Uid</param>
  41. /// <param name="callback">回调函数</param>
  42. public void GetZoneList(string uid, bool getRecommended, Action<UserProx_GetZoneListRetVo> callback)
  43. {
  44. Post<UserProx_GetZoneListRetVo>(CmdCode.cmd_user_getzonelist, uid, 1, new object[] { getRecommended },callback);
  45. }
  46. /// <summary>
  47. /// [6001] 玩家登录获取玩家信息
  48. /// </summary>
  49. /// <param name="uid"></param>
  50. /// <param name="zoneid"></param>
  51. /// <param name="callback"></param>
  52. public void UserLogin(string uid, int zoneid, Action<JObject> callback, Action onNewUser)
  53. {
  54. Post(CmdCode.cmd_user_loginuserinfo, uid, zoneid, new object[] { },
  55. resp =>
  56. {
  57. if (null != resp.result["isNewUser"] && bool.Parse(resp.result["isNewUser"].ToString()))
  58. {
  59. onNewUser?.Invoke();
  60. }
  61. else
  62. {
  63. var p = new Player();
  64. p.uid = uid;
  65. p.zoneid = zoneid;
  66. p.Initlize(resp.result);
  67. UserProxy.Instance.player = p;
  68. callback?.Invoke(resp.result);
  69. SystemProxy.HearbeatBehavor.Instance.Start(); // 启动心跳机制
  70. PollingManager.Instance.Register($"{Enum_PollingIDs.RefreshTili:G}", 60, () => { // 开启轮询,刷新体力
  71. ActiveProxy.Instance.GetCurTili(null);
  72. });
  73. MessageCenter.Instance.Init();
  74. MapTcpProxy.Instance.Login(); // 初始化地图数据(长连)
  75. }
  76. });
  77. }
  78. /// <summary>
  79. /// [6006]注册新角色
  80. /// </summary>
  81. /// <param name="uid"></param>
  82. /// <param name="zoneid"></param>
  83. /// <param name="nickname"></param>
  84. /// <param name="sex"></param>
  85. /// <param name="img"></param>
  86. /// <param name="callback"></param>
  87. public void RegisterRole(string uid, int zoneid, string nickname, string sex, string img, Action<JObject> callback, Action faild)
  88. {
  89. Post(CmdCode.cmd_user_registerNewRole, uid, zoneid, new object[] { nickname, sex, img },
  90. resp =>
  91. {
  92. if (null == resp.result["用户已存在"])
  93. {
  94. var p = new Player();
  95. p.uid = uid;
  96. p.zoneid = zoneid;
  97. p.Initlize(resp.result);
  98. UserProxy.Instance.player = p;
  99. callback?.Invoke(resp.result);
  100. MessageCenter.Instance.Init();
  101. MapTcpProxy.Instance.Login(); // 初始化地图数据
  102. }
  103. else
  104. {
  105. faild?.Invoke();
  106. }
  107. });
  108. }
  109. /// <summary>
  110. /// [6002]获取常量数据
  111. /// </summary>
  112. /// <param name="versionts"></param>
  113. /// <param name="callback"></param>
  114. public void DownLoadGameConfig(string versionts, Action<JObject> callback)
  115. {
  116. Post(CmdCode.cmd_user_gameconstinfo, "", 0, new object[] { versionts },
  117. resp =>
  118. {
  119. TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
  120. serverTS = resp.ts - Convert.ToInt64(ts.TotalSeconds);
  121. LogHelper.Log($"与服务器的时间差:{serverTS}s");
  122. JObject obj = null;
  123. string data = resp.result["data"]?.ToString() ?? "";
  124. if (!string.IsNullOrEmpty(data))
  125. {
  126. obj = JObject.Parse(Base64Util.Decode(data, true));
  127. using var sw = new StreamWriter(Const.ServerConfigFileName); // 更新本地缓存文件
  128. sw.Write(data);
  129. }
  130. callback?.Invoke(obj);
  131. });
  132. }
  133. /// <summary>
  134. /// 6015 收集玩家意见和bug
  135. /// </summary>
  136. /// <param name="ctx"></param>
  137. /// <param name="phoneId"></param>
  138. /// <param name="callback"></param>
  139. public void UserCtxBack(string ctx, string type, string phoneId, Action callback)
  140. {
  141. Post(CmdCode.cmd_user_ctxBack, new object[] { ctx, type, phoneId },
  142. resp => callback?.Invoke());
  143. }
  144. /// <summary>
  145. /// 6016 拉取其他玩家的信息
  146. /// </summary>
  147. /// <param name="ctx"></param>
  148. /// <param name="phoneId"></param>
  149. /// <param name="callback"></param>
  150. public void UserOtherPlayerInfo(string other_UID, Action<Player> callback)
  151. {
  152. Post(CmdCode.cmd_user_other_info, new object[] { other_UID },
  153. resp =>
  154. {
  155. Player p = new Player();
  156. p.Initlize(resp.result);
  157. callback?.Invoke(p);
  158. });
  159. }
  160. /// <summary>
  161. /// 6016 删除角色
  162. /// </summary>
  163. /// <param name="callback"></param>
  164. public void DeleteUserUId(Action callback)
  165. {
  166. Post(CmdCode.cmd_user_deleteUserUId, new object[] { },
  167. resp =>
  168. {
  169. //Player p = new Player();
  170. //p.Initlize(resp.result);
  171. callback?.Invoke();
  172. });
  173. }
  174. #region " 测试/送审 "
  175. /// <summary>
  176. /// [6004] 测试-注册新玩家UID
  177. /// </summary>
  178. /// <param name="uid"></param>
  179. /// <param name="zoneid"></param>
  180. /// <param name="nickname"></param>
  181. /// <param name="sex"></param>
  182. /// <param name="img"></param>
  183. /// <param name="callback"></param>
  184. public void testRegisterNewUID(string uid, string pwd, string mail, string phone, Action<bool, string> callback)
  185. {
  186. Post(CmdCode.cmd_user_testRegisterNewUID, uid, 1, new object[] { uid, pwd, mail, phone },
  187. resp =>
  188. {
  189. bool result = false;
  190. string msg = "失败";
  191. bool bReturned = null != resp.result["ret"];
  192. if (!bReturned)
  193. {
  194. msg = "未知的返回值"; // 失败原因1
  195. }
  196. if (resp.result["ret"].ToObject<int>() != ErrCode.succeed)
  197. {
  198. msg = resp.result["msg"].ToObject<string>(); // 失败原因2
  199. }
  200. else// 成功
  201. {
  202. result = true;
  203. msg = "成功";
  204. }
  205. if (callback != null)
  206. {
  207. callback.Invoke(result, msg); // 回调
  208. }
  209. });
  210. }
  211. /// <summary>
  212. /// [6005]测试-验证账号密码
  213. /// </summary>
  214. /// <param name="uid"></param>
  215. /// <param name="zoneid"></param>
  216. /// <param name="nickname"></param>
  217. /// <param name="sex"></param>
  218. /// <param name="img"></param>
  219. /// <param name="callback"></param>
  220. public void testUserLogin(string uid, string pwd, Action<bool, string> callback)
  221. {
  222. Post(CmdCode.cmd_user_testUserLogin, uid, 1, new object[] { uid, pwd },
  223. resp =>
  224. {
  225. bool result = false;
  226. string msg = "失败";
  227. bool bReturned = null != resp.result["ret"];
  228. if (!bReturned)
  229. {
  230. msg = "未知的返回值"; // 失败原因1
  231. }
  232. if (resp.result["ret"].ToObject<int>() != ErrCode.succeed)
  233. {
  234. msg = resp.result["msg"].ToObject<string>(); // 失败原因2
  235. }
  236. else// 成功
  237. {
  238. result = true;
  239. msg = "成功";
  240. }
  241. if (callback != null)
  242. {
  243. callback.Invoke(result, msg); // 回调
  244. }
  245. });
  246. }
  247. #endregion
  248. #region ' 新手引导 '
  249. /// <summary>
  250. /// [6007] 新手引导的状态变更 2020.6.11
  251. /// </summary>
  252. /// <param name="guideIndex">引导步骤</param>
  253. /// <param name="callback"></param>
  254. public void UpdateNewbieGuideStep(int guideIndex, Action callback)
  255. {
  256. Post(CmdCode.cmd_user_completeNewbieGuide, new object[] { guideIndex }, resp =>
  257. {
  258. var p = UserProxy.Instance.player;
  259. p.collectHero.InitData((JObject)resp.result["heros"], p);
  260. p.NewbieGuideInfo.guideStep = guideIndex;
  261. callback?.Invoke();
  262. });
  263. }
  264. /// <summary>
  265. /// 查询当前引导步骤
  266. /// </summary>
  267. /// <returns></returns>
  268. public int GetNewbieGuideStep() => player.NewbieGuideInfo.guideStep;
  269. #endregion
  270. #region ' 玩家形象 '
  271. /// <summary>
  272. /// [6010] 设置修改玩家昵称
  273. /// </summary>
  274. /// <param name="nick"></param>
  275. public void SetUserNickName(string nick, Action<JObject> callback)
  276. {
  277. Post(CmdCode.cmd_user_setNickName, new object[] { nick },
  278. resp =>
  279. {
  280. player.baseInfo.name = nick;
  281. player.baseInfo.cash -= Convert.ToInt32(GameConfigData.Ins.globalsettings.User_SetNickname_Cost);
  282. callback?.Invoke(resp.result);
  283. });
  284. }
  285. /// <summary>
  286. /// 6011 更改头像框
  287. /// </summary>
  288. /// <param name="headImageBorderId"></param>
  289. /// <param name="callback"></param>
  290. public void SetUserImageBorder(int headImageBorderId, Action<JObject> callback)
  291. {
  292. Post(CmdCode.cmd_user_SetUserHeadImageBorder, new object[] { headImageBorderId },
  293. resp =>
  294. {
  295. player.baseInfo.imgBorderId = headImageBorderId;
  296. callback?.Invoke(resp.result);
  297. });
  298. }
  299. /// <summary>
  300. /// 6012 更改形象
  301. /// </summary>
  302. /// <param name="headImage"></param>
  303. /// <param name="callback"></param>
  304. public void SetUserImage(string image, Action<JObject> callback)
  305. {
  306. Post(CmdCode.cmd_user_SetUserImage, new object[] { image },
  307. resp =>
  308. {
  309. player.baseInfo.img = image;
  310. callback?.Invoke(resp.result);
  311. });
  312. }
  313. /// <summary>
  314. /// 6013 更改头像框
  315. /// </summary>
  316. /// <param name="headImage"></param>
  317. public void SetUserHeadImage(string headImage, Action<JObject> callback)
  318. {
  319. Post(CmdCode.cmd_user_changeUserHeadImage, new object[] { headImage },
  320. resp =>
  321. {
  322. player.baseInfo.headImg = headImage;
  323. callback?.Invoke(resp.result);
  324. });
  325. }
  326. /// <summary>
  327. /// 6014 初始化火山引擎服务端数据
  328. /// </summary>
  329. /// <param name="app_name">应用名称(言灵世界)</param>
  330. /// <param name="app_package">应用包名</param>
  331. /// <param name="app_channel">渠道</param>
  332. /// <param name="app_version">应用版本号</param>
  333. /// <param name="os_name">系统名称安卓/ios</param>
  334. /// <param name="os_version">系统版本号</param>
  335. /// <param name="device_model">设备型号</param>
  336. /// <param name="ab_version">Ab测试,分组信息</param>
  337. /// <param name="traffic_type">流量类型(wifi/2G/3G/4G/5G...)</param>
  338. public void InitVolcEngineData(string app_name, string app_package, string app_channel, string app_version, string os_name, string os_version, string device_model, string ab_version, string traffic_type)
  339. {
  340. Post(CmdCode.cmd_user_stat_initvolc, new object[] { app_name, app_package, app_channel, app_version, os_name, os_version, device_model, ab_version, traffic_type }, resp => { });
  341. }
  342. #endregion
  343. #region 废弃
  344. /// <summary>.
  345. /// 6009 新手引导—— 结束引导
  346. /// </summary>
  347. /// <param name="uid"></param>
  348. /// <param name="zoneid"></param>
  349. /// <param name="callback"></param>
  350. [Obsolete("暂未启用-wg 2020.6.11")]
  351. public void SetGuideOver(Action<JObject> callback)
  352. {
  353. Post(CmdCode.cmd_user_setNewbieGuideOver, new object[] { }, resp => callback?.Invoke(resp.result));
  354. }
  355. /// <summary>
  356. /// 完整观看(开篇)重要剧情奖励(2钻石)
  357. /// </summary>
  358. public void SendImportantStoryViewFullReward() {
  359. // 奖励2钻石
  360. ItemProxy.Instance.AddItem(2, 2);
  361. ItemProxy.Instance.UpdateItems();
  362. }
  363. /// <summary>
  364. /// [6008] 获取新手引导战斗奖励(卡牌)
  365. /// </summary>
  366. /// <param name="uid"></param>
  367. /// <param name="zoneid"></param>
  368. /// <param name="nick"></param>
  369. [Obsolete("已过时-wg")]
  370. public void GetGuideBattleReward(Action<JObject> callback)
  371. {
  372. Post(CmdCode.cmd_user_getGuideBattleReward, new object[] { }, resp => callback?.Invoke(resp.result));
  373. }
  374. #endregion
  375. #region 辅助内部类
  376. /// <summary>
  377. /// 用户信息 界面详情
  378. /// </summary>
  379. public class UIVO_UserInfo
  380. {
  381. private Player p = UserProxy.Instance.player;
  382. public int Level => p.baseInfo.level;
  383. public int Exp => p.baseInfo.xp;
  384. public int Gold => p.baseInfo.gold;
  385. public int Cash => p.baseInfo.cash;
  386. public int CurMaxXp => p.baseInfo.maxXp;
  387. public string headImg => p.baseInfo.headImg;
  388. public string Name
  389. {
  390. get
  391. {
  392. if (p.baseInfo.name.Contains("No."))
  393. {
  394. return "新玩家";
  395. }
  396. else
  397. {
  398. return p.baseInfo.name;
  399. }
  400. }
  401. }
  402. public int FightPower => p.collectHero.GetTotalFightPower();
  403. public Info_HeroTeamConfig Team => p.heroTeamConfig;
  404. public string UID => p.uid;
  405. public string ZoneName => p.zonename;
  406. public string guildName => "未加入公会";
  407. /// <summary>
  408. /// 改名消耗
  409. /// </summary>
  410. public int ChangeNicknameCost => Convert.ToInt32(GameConfigData.Ins.globalsettings.User_SetNickname_Cost);
  411. /// <summary>
  412. /// 选择头像界面
  413. /// </summary>
  414. public class UI_SelectUserHeadImg
  415. {
  416. private Player p = UserProxy.Instance.player;
  417. public List<HeadImgInfo> headList() =>
  418. GameConfigData.Ins.item_yanling.Values.ToList().ConvertAll<HeadImgInfo>(y => new HeadImgInfo(y));
  419. public class HeadImgInfo
  420. {
  421. private Player p = UserProxy.Instance.player;
  422. private sm_item_yanling mo;
  423. public HeadImgInfo(sm_item_yanling ylmo) => mo = ylmo;
  424. /// <summary>
  425. /// 图标资源(头像)
  426. /// </summary>
  427. public string icon => mo.GeneralData.icon;
  428. /// <summary>
  429. /// 已解锁
  430. /// </summary>
  431. public bool isUnlocked => p.collectYanling.items.Values.Count(yl => yl.nMo.typeId == mo.typeId) > 0;
  432. public string name => mo.GeneralData.name;
  433. public string desc => $"获得{name}解锁头像.";
  434. }
  435. }
  436. /// <summary>
  437. /// 选择形像界面
  438. /// </summary>
  439. public class UI_SelectUserImage
  440. {
  441. private Player p = UserProxy.Instance.player;
  442. public List<ImageInfo> headList() =>
  443. GameConfigData.Ins.item_yanling.Values.ToList().ConvertAll<ImageInfo>(y => new ImageInfo(y));
  444. public class ImageInfo
  445. {
  446. private Player p = UserProxy.Instance.player;
  447. private sm_item_yanling mo;
  448. public ImageInfo(sm_item_yanling ylmo) => mo = ylmo;
  449. /// <summary>
  450. /// 图标
  451. /// </summary>
  452. public string icon => mo.GeneralData.icon;
  453. /// <summary>
  454. /// 龙骨资源
  455. /// </summary>
  456. public string img => mo.GeneralData.res;
  457. /// <summary>
  458. /// 已解锁
  459. /// </summary>
  460. public bool isUnlocked => p.collectYanling.items.Values.Count(yl => yl.nMo.typeId == mo.typeId) > 0;
  461. public string name => mo.GeneralData.name;
  462. public string desc => $"获得{name}解锁此形象.";
  463. }
  464. }
  465. }
  466. #endregion
  467. }