C2G_BindPlayerHandler.cs 4.6 KB

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