C2G_BattleNotifyHandler.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.Json;
  5. namespace ET.Server
  6. {
  7. /// <summary>
  8. /// 向战斗服发送特定消息(触发编辑器事件)
  9. /// </summary>
  10. [MessageHandler(SceneType.Game)]
  11. [FriendOfAttribute(typeof(ET.Server.GameMapComponent))]
  12. public class C2G_BattleNotifyHandler : AMRpcHandler<C2G_BattleNotify, G2C_BattleNotify>
  13. {
  14. protected override async ETTask Run(Session session, C2G_BattleNotify request, G2C_BattleNotify response, Action reply)
  15. {
  16. WNPlayer player = session.GetComponent<SessionPlayerComponent>().GetMyPlayer();
  17. if (player == null)
  18. {
  19. Log.Debug($"操作错误, player is null");
  20. response.Error = ErrorCode.ERR_OperationError;
  21. reply();
  22. return;
  23. }
  24. // 判断参数
  25. if (string.IsNullOrEmpty(request.Message))
  26. {
  27. Log.Debug($"参数错误, request.NameType={request.Message}");
  28. response.Error = ErrorCode.ERR_ParameterError;
  29. reply();
  30. return;
  31. }
  32. //客户端测试功能
  33. if (request.Message.StartsWith("test:"))
  34. {
  35. var map = player.Map;
  36. var players = map.UnitPlayers.Values.ToArray();
  37. if(players.Length == 0)
  38. {
  39. Log.Debug("Player count == 0, abort cmd");
  40. return;
  41. }
  42. var rand = new Random();
  43. var randunit = players[rand.Next(players.Length)];
  44. long roomId = 0;
  45. var maphash = GameMapComponent.Instance.rooms;
  46. foreach ( var rid in maphash.Keys )
  47. {
  48. if (maphash[rid] == map.Id)
  49. {
  50. roomId = rid;
  51. break;
  52. }
  53. }
  54. long instanceid = 0;
  55. List<StartSceneConfig> list = RealmGateAddressHelper.GetAllGame(1);
  56. foreach (StartSceneConfig config in list.Where(config => config is { Id: 10001 }))
  57. {
  58. instanceid = config.InstanceId;
  59. break;
  60. }
  61. var funcIndex = int.Parse(request.Message[5..]);
  62. if (funcIndex == 7)
  63. {
  64. MessageHelper.SendActor(instanceid, new R2G_LiveLike() { OpenId = randunit.OpenId, RoomId = roomId, NickName = randunit.Name, Url = randunit.Url, Likes = 10 });
  65. }
  66. else
  67. {
  68. string[] GIFT = { DouyinItem.GiftId_1, DouyinItem.GiftId_10, DouyinItem.GiftId_52, DouyinItem.GiftId_99, DouyinItem.GiftId_199, DouyinItem.GiftId_520 };
  69. int cnt = 1;
  70. switch(funcIndex)
  71. {
  72. case 1:
  73. cnt = (int)Math.Sqrt(rand.Next(1, 100));
  74. break;
  75. case 2:
  76. cnt = rand.Next(1, 10);
  77. break;
  78. }
  79. MessageHelper.SendActor(instanceid, new R2G_LiveGift() { OpenId = randunit.OpenId, RoomId = roomId, NickName = randunit.Name, Url = randunit.Url, GiftId = GIFT[funcIndex-1], GiftNum = cnt, GiftValue = funcIndex * 80 });
  80. }
  81. return;
  82. }
  83. // 通知战斗服
  84. player.GetXmdsManager().notifyBattleServer(player.Map.Id.ToString(), NotifyBSName.TriggerEvent, JsonSerializer.Serialize(new Struct.TriggerEventNotify() { message = request.Message }));
  85. reply();
  86. await ETTask.CompletedTask;
  87. }
  88. }
  89. }