C2G_CreatePlayerHandler.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. namespace ET.Server
  3. {
  4. /// <summary>
  5. /// 创建角色
  6. /// </summary>
  7. [MessageHandler(SceneType.Game)]
  8. public class C2G_CreatePlayerHandler: AMRpcHandler<C2G_CreatePlayer, G2C_CreatePlayer>
  9. {
  10. protected override async ETTask Run(Session session, C2G_CreatePlayer request, G2C_CreatePlayer response, Action reply)
  11. {
  12. // 重复请求
  13. if (session.GetComponent<SessionLockComponent>() != null)
  14. {
  15. response.Error = ErrorCode.ERR_RequestRepeatedly;
  16. reply();
  17. return;
  18. }
  19. session.AddComponent<SessionLockComponent>();
  20. if (session.DomainScene().SceneType != SceneType.Game)
  21. {
  22. Log.Debug($"Game 请求的Scene错误...SceneType={session.DomainScene().SceneType}");
  23. session.Dispose();
  24. return;
  25. }
  26. // 判断参数
  27. if (string.IsNullOrEmpty(request.Name.Trim()))
  28. {
  29. Log.Debug($"请输入角色名");
  30. response.Error = ErrorCode.ERR_PlayerNameIsNull;
  31. reply();
  32. return;
  33. }
  34. // todo 名字是否合法
  35. if (request.Pro < (byte) PlayerProType.COMMON || request.Pro > (byte) PlayerProType.LI_NHU)
  36. {
  37. Log.Debug($"该职业有误");
  38. response.Error = ErrorCode.ERR_ProNotProper;
  39. reply();
  40. return;
  41. }
  42. if (request.Sex != (byte) SexType.Man && request.Sex != (byte) SexType.Woman)
  43. {
  44. Log.Debug($"该性别有误");
  45. response.Error = ErrorCode.ERR_SexNotProper;
  46. reply();
  47. return;
  48. }
  49. // todo 服务器是否已达上限
  50. // 是否有配置
  51. Character prop = CharacterCategory.Instance.Get(request.Pro);
  52. if (prop == null)
  53. {
  54. Log.Debug($"该职业未配置, pro={request.Pro}");
  55. response.Error = ErrorCode.ERR_ProNotProper;
  56. reply();
  57. return;
  58. }
  59. Scene scene = session.DomainScene();
  60. // 创建角色
  61. PlayerInfo playerInfo;
  62. long _id = IdGenerater.Instance.GenerateUnitId(scene.DomainZone());
  63. using (await CoroutineLockComponent.Instance.Wait(CoroutineLockType.CreatePlayer, _id.ToString().Trim().GetHashCode()))
  64. {
  65. playerInfo = new PlayerInfo();
  66. playerInfo.Id = _id;
  67. playerInfo.UserId = session.GetComponent<SessionPlayerComponent>().UserId;
  68. playerInfo.Name = request.Name;
  69. playerInfo.Sex = request.Sex;
  70. playerInfo.Pro = request.Pro;
  71. playerInfo.Level = prop.initLevel;
  72. playerInfo.Exp = 0;
  73. await DBManagerComponent.Instance.GetZoneDB(session.DomainZone()).Save(playerInfo);
  74. }
  75. // 创建玩家实体, 初始化各模块数据
  76. WNPlayer player = scene.GetComponent<GamePlayerComponent>()
  77. .AddChildWithId<WNPlayer, Session, PlayerInfo>(playerInfo.Id, session, playerInfo);
  78. // 添加绑定session角色
  79. session.GetComponent<SessionPlayerComponent>().Add(playerInfo);
  80. Log.Info($"创建角色, playerId={playerInfo.Id}, name={playerInfo.Name}, userId={playerInfo.UserId}");
  81. response.Player = PlayerHelper.PlayerInfoToProto(player);
  82. reply();
  83. }
  84. }
  85. }