C2G_BindPlayerHandler.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace ET.Server
  5. {
  6. /// <summary>
  7. /// 选角绑定角色
  8. /// </summary>
  9. [MessageHandler(SceneType.Game)]
  10. public class C2G_BindPlayerHandler: AMRpcHandler<C2G_BindPlayer, G2C_BindPlayer>
  11. {
  12. protected override async ETTask Run(Session session, C2G_BindPlayer request, G2C_BindPlayer response, Action reply)
  13. {
  14. if (string.IsNullOrEmpty(request.UserId.ToString().Trim()))
  15. {
  16. Log.Debug($"参数错误,账号id为空");
  17. response.Error = ErrorCode.ERR_ParameterError;
  18. reply();
  19. return;
  20. }
  21. Scene scene = session.DomainScene();
  22. // 预先创建数据
  23. long _id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone());
  24. PlayerInfo playerInfo = new PlayerInfo();
  25. playerInfo.Id = _id;
  26. playerInfo.UserId = session.GetComponent<SessionPlayerComponent>().UserId;
  27. playerInfo.Name = "玩家-" + _id;
  28. playerInfo.Sex = 0;
  29. playerInfo.Pro = (int)PlayerProType.CANG_LANG;
  30. playerInfo.Level = 1;
  31. playerInfo.Exp = 0;
  32. // 简易版登录,直接创建一个player
  33. if (request.PlayerId <= 0)
  34. {
  35. // 角色列表是否有数据
  36. List<PlayerInfo> playerList = session.GetComponent<SessionPlayerComponent>().PlayerList;
  37. if (playerList is { Count: > 0 })
  38. {
  39. foreach (PlayerInfo info in playerList.Where(info => info != null))
  40. {
  41. playerInfo = info;
  42. break;
  43. }
  44. }
  45. else
  46. {
  47. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.CreatePlayer, _id.ToString().Trim().GetHashCode()))
  48. {
  49. await DBManagerComponent.Instance.GetZoneDB(session.DomainZone()).Save(playerInfo);
  50. }
  51. }
  52. }
  53. else
  54. {
  55. // 正常选角,判断参数
  56. if (!session.GetComponent<SessionPlayerComponent>().IsExist(request.PlayerId))
  57. {
  58. Log.Debug($"选角出错,没有选择的角色");
  59. response.Error = ErrorCode.ERR_BindPlayerError;
  60. reply();
  61. return;
  62. }
  63. playerInfo = session.GetComponent<SessionPlayerComponent>().Get(request.PlayerId);
  64. if (playerInfo == null)
  65. {
  66. Log.Debug($"选角出错,没有选择的角色");
  67. response.Error = ErrorCode.ERR_BindPlayerError;
  68. reply();
  69. return;
  70. }
  71. long bindPlayerId = session.GetComponent<SessionPlayerComponent>().PlayerId;
  72. switch (bindPlayerId)
  73. {
  74. case > 0 when bindPlayerId == request.PlayerId:
  75. Log.Debug($"该角色已经绑定");
  76. response.Error = ErrorCode.ERR_PlayerIdIsBind;
  77. reply();
  78. return;
  79. case > 0 when bindPlayerId != request.PlayerId:
  80. // todo 老角色下线
  81. WNPlayer oldPlayer = scene.GetComponent<GamePlayerComponent>().Get(bindPlayerId);
  82. if (oldPlayer != null)
  83. {
  84. oldPlayer.Dispose();
  85. scene.GetComponent<GamePlayerComponent>().Remove(bindPlayerId);
  86. }
  87. break;
  88. }
  89. }
  90. WNPlayer player = scene.GetComponent<GamePlayerComponent>().Get(request.PlayerId);
  91. if (player != null)
  92. {
  93. // todo 顶号
  94. player.Dispose();
  95. scene.GetComponent<GamePlayerComponent>().Remove(request.PlayerId);
  96. }
  97. player ??= scene.GetComponent<GamePlayerComponent>()
  98. .AddChild<WNPlayer, Session, PlayerInfo>(session, playerInfo);
  99. await player.GetComponent<PlayerTempDataComponent>().InitFromDB();
  100. // 绑定选择的玩家
  101. session.GetComponent<SessionPlayerComponent>().PlayerId = request.PlayerId > 0 ? request.PlayerId : player.GetId();
  102. session.AddComponent<MailBoxComponent, MailboxType>(MailboxType.GateSession);
  103. // todo 分配场景
  104. Map map = MapHelper.BindPlayerDispatch(player);
  105. MapConfig mapConfig = map.Prop;
  106. // 登录数据
  107. player.OnLogin();
  108. map.OnPlayerLogin(player);
  109. response.Player = PlayerHelper.PlayerInfoToProto(player);
  110. response.Player.areaId = mapConfig.TemplateID;
  111. response.Player.instanceId = map.Id;
  112. response.Player.mapId = map.MapId;
  113. reply();
  114. await ETTask.CompletedTask;
  115. }
  116. }
  117. }