C2G_BindPlayerHandler.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using System;
  2. namespace ET.Server
  3. {
  4. /// <summary>
  5. /// 选角绑定角色
  6. /// </summary>
  7. [MessageHandler(SceneType.Game)]
  8. public class C2G_BindPlayerHandler: AMRpcHandler<C2G_BindPlayer, G2C_BindPlayer>
  9. {
  10. protected override async ETTask Run(Session session, C2G_BindPlayer request, G2C_BindPlayer response, Action reply)
  11. {
  12. if (session.DomainScene().SceneType != SceneType.Game)
  13. {
  14. Log.Debug($"Game 请求的Scene错误...SceneType={session.DomainScene().SceneType}");
  15. session.Dispose();
  16. return;
  17. }
  18. Scene scene = session.DomainScene();
  19. PlayerInfo playerInfo;
  20. if (request.PlayerId <= 0)
  21. {
  22. long _id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone());
  23. // 简易版登录,直接创建一个player
  24. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.CreatePlayer, _id.ToString().Trim().GetHashCode()))
  25. {
  26. playerInfo = new PlayerInfo();
  27. playerInfo.Id = _id;
  28. playerInfo.UserId = request.UserId;
  29. playerInfo.Name = "玩家-" + _id;
  30. playerInfo.Sex = 0;
  31. playerInfo.Pro = 0;
  32. playerInfo.Level = 1;
  33. playerInfo.Exp = 0;
  34. await DBManagerComponent.Instance.GetZoneDB(session.DomainZone()).Save(playerInfo);
  35. }
  36. }
  37. else
  38. {
  39. // 正常选角,判断参数
  40. if (!session.GetComponent<SessionPlayerComponent>().PlayerList.ContainsKey(request.PlayerId))
  41. {
  42. Log.Debug($"选角出错,没有选择的角色");
  43. response.Error = ErrorCode.ERR_BindPlayerError;
  44. reply();
  45. return;
  46. }
  47. playerInfo = session.GetComponent<SessionPlayerComponent>().PlayerList[request.PlayerId];
  48. if (playerInfo == null)
  49. {
  50. Log.Debug($"选角出错,没有选择的角色");
  51. response.Error = ErrorCode.ERR_BindPlayerError;
  52. reply();
  53. return;
  54. }
  55. long bindPlayerId = session.GetComponent<SessionPlayerComponent>().PlayerId;
  56. switch (bindPlayerId)
  57. {
  58. case > 0 when bindPlayerId == request.PlayerId:
  59. Log.Debug($"该角色已经绑定");
  60. response.Error = ErrorCode.ERR_PlayerIdIsBind;
  61. reply();
  62. return;
  63. case > 0 when bindPlayerId != request.PlayerId:
  64. // todo 老角色下线
  65. break;
  66. }
  67. }
  68. WNPlayer player = scene.GetComponent<GamePlayerComponent>().Get(request.PlayerId);
  69. if (player != null)
  70. {
  71. // todo 顶号
  72. }
  73. player ??= scene.GetComponent<GamePlayerComponent>()
  74. .AddChildWithId<WNPlayer, Session, PlayerInfo>(playerInfo.Id, session, playerInfo);
  75. session.AddComponent<MailBoxComponent, MailboxType>(MailboxType.GateSession);
  76. session.GetComponent<SessionPlayerComponent>().PlayerId = request.PlayerId;
  77. // todo 分配场景
  78. Map map = MapHelper.BindPlayerDispatch(player);
  79. MapConfig mapConfig = map.Prop;
  80. // 登录数据
  81. player.OnLogin();
  82. map.OnPlayerLogin(player);
  83. response.Player = PlayerHelper.PlayerInfoToProto(player);
  84. response.Player.areaId = mapConfig.TemplateID;
  85. response.Player.instanceId = map.InstanceId;
  86. response.Player.mapId = map.MapId;
  87. reply();
  88. await ETTask.CompletedTask;
  89. }
  90. }
  91. }