C2G_LoginGameHandler.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using Newtonsoft.Json.Linq;
  3. namespace ET.Server
  4. {
  5. /// <summary>
  6. /// 登入游戏服.获取角色列表
  7. /// </summary>
  8. [MessageHandler(SceneType.Game)]
  9. public class C2G_LoginGameHandler : AMRpcHandler<C2G_LoginGame, G2C_LoginGame>
  10. {
  11. protected override async ETTask Run(Session session, C2G_LoginGame request, G2C_LoginGame response, Action reply)
  12. {
  13. if (session.DomainScene().SceneType != SceneType.Game)
  14. {
  15. Log.Error($"Game 请求的Scene错误...SceneType={session.DomainScene().SceneType}");
  16. session.Dispose();
  17. return;
  18. }
  19. // 移除session自动超时组件
  20. session.RemoveComponent<SessionAcceptTimeoutComponent>();
  21. Scene scene = session.DomainScene();
  22. bool tokenIsNull = string.IsNullOrEmpty(request.Token.Trim());
  23. // 预先创建数据
  24. PlayerInfo playerInfo = new PlayerInfo();
  25. playerInfo.Id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone());
  26. if (tokenIsNull)
  27. {
  28. // Token为空初始化信息
  29. playerInfo.RoomId = playerInfo.Id;
  30. playerInfo.AnchorOpenId = playerInfo.Id.ToString();
  31. playerInfo.AvatarUrl = "";
  32. playerInfo.Name = "主播-" + playerInfo.Id;
  33. playerInfo.TokenIsNull = true;
  34. }
  35. else
  36. {
  37. JObject roomInfo = scene.GetComponent<GameDouyinComponent>().GetRoomInfo(request.Token.Trim());
  38. if (roomInfo == null)
  39. {
  40. Log.Error($"获取抖音roomInfo失败...");
  41. response.Error = ErrorCode.ERR_RoomInfoError;
  42. reply();
  43. return;
  44. }
  45. JToken info = roomInfo.SelectToken("data").SelectToken("info");
  46. playerInfo.RoomId = Convert.ToInt64(info.SelectToken("room_id"));
  47. playerInfo.AnchorOpenId = Convert.ToString(info.SelectToken("anchor_open_id"));
  48. playerInfo.AvatarUrl = Convert.ToString(info.SelectToken("avatar_url"));
  49. playerInfo.Name = Convert.ToString(info.SelectToken("nick_name"));
  50. playerInfo.TokenIsNull = false;
  51. }
  52. playerInfo.Sex = 0;
  53. playerInfo.Pro = (int)PlayerProType.CANG_LANG;
  54. playerInfo.Level = 1;
  55. playerInfo.Exp = 0;
  56. // 添加session组件,用于绑定角色
  57. session.AddComponent<SessionPlayerComponent, PlayerInfo>(playerInfo);
  58. session.AddComponent<MailBoxComponent, MailboxType>(MailboxType.GateSession);
  59. response.Player = PlayerHelper.PlayerInfoToPlayerBasicProto(playerInfo);
  60. reply();
  61. await ETTask.CompletedTask;
  62. }
  63. }
  64. }