C2G_LoginGameHandler.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace ET.Server
  6. {
  7. /// <summary>
  8. /// 登入游戏服.获取角色列表
  9. /// </summary>
  10. [MessageHandler(SceneType.Game)]
  11. public class C2G_LoginGameHandler : AMRpcHandler<C2G_LoginGame, G2C_LoginGame>
  12. {
  13. protected override async ETTask Run(Session session, C2G_LoginGame request, G2C_LoginGame response, Action reply)
  14. {
  15. if (session.DomainScene().SceneType != SceneType.Game)
  16. {
  17. Log.Debug($"Game 请求的Scene错误...SceneType={session.DomainScene().SceneType}");
  18. session.Dispose();
  19. return;
  20. }
  21. // 判断参数
  22. if (string.IsNullOrEmpty(request.Token.Trim()) || request.UserId <= 0)
  23. {
  24. Log.Debug($"Game token 登陆超时");
  25. response.Error = ErrorCore.ERR_ConnectGateKeyError;
  26. response.Message = "Game token 登陆超时";
  27. reply();
  28. session.Disconnect().Coroutine();
  29. return;
  30. }
  31. // 移除session自动超时组件
  32. session.RemoveComponent<SessionAcceptTimeoutComponent>();
  33. // 添加session组件,用于绑定角色
  34. session.AddComponent<SessionPlayerComponent>().UserId = request.UserId;
  35. session.AddComponent<MailBoxComponent, MailboxType>(MailboxType.GateSession);
  36. response.Players = new List<PlayerBasic>();
  37. // 获取角色列表
  38. List<PlayerInfo> list = await DBManagerComponent.Instance.GetZoneDB(session.DomainZone())
  39. .Query<PlayerInfo>(p => p.UserId == request.UserId);
  40. if (list is { Count: > 0 })
  41. {
  42. foreach (PlayerInfo info in list.Where(playerInfo => playerInfo != null))
  43. {
  44. session.GetComponent<SessionPlayerComponent>().Add(info);
  45. response.Players.Add(PlayerHelper.PlayerInfoToPlayerBasicProto(info));
  46. }
  47. }
  48. reply();
  49. }
  50. }
  51. }