R2G_LiveGiftHandler.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Linq;
  3. using System.Numerics;
  4. using System.Text;
  5. using System.Text.Json;
  6. namespace ET.Server
  7. {
  8. /// <summary>
  9. /// 抖音推送回调, 送礼物
  10. /// </summary>
  11. [ActorMessageHandler(SceneType.Game)]
  12. public class R2G_LiveGiftHandler: AMActorHandler<Scene, R2G_LiveGift>
  13. {
  14. protected override async ETTask Run(Scene scene, R2G_LiveGift request)
  15. {
  16. // 房间是否存在
  17. Map map = scene.GetComponent<GameMapComponent>().GetMapByRoomId(request.RoomId);
  18. if (map == null)
  19. {
  20. Log.Error($"未找到房间...roomId={request.RoomId}");
  21. return;
  22. }
  23. Struct.UnitPlayerData unitPlayerData = map.GetUnitPlayerByOpenId(request.OpenId);
  24. // 数据是否存在
  25. if (unitPlayerData == null)
  26. {
  27. Log.Error($"未找到单位玩家数据...openId={request.OpenId}");
  28. return;
  29. }
  30. unitPlayerData.GiftMoney += (int)request.GiftValue;
  31. // 推送客户端
  32. if (map.Player != null)
  33. {
  34. MessageHelper.SendToClient(map.Player, new G2C_GiftInfoPush { NickName = request.NickName, GiftType = DouyinItem.GiftHash[request.GiftId],
  35. GiftNum = (int)request.GiftNum, Url = request.Url, TotalMoney = unitPlayerData.GiftMoney, UnitId = unitPlayerData.ObjId});
  36. }
  37. int[] units = new int[] { 101, 121, 111, 131 };
  38. Struct.TriggerEventNotify notify = null;
  39. if (DouyinItem.GiftId_1.Equals(request.GiftId) || DouyinItem.GiftId_10.Equals(request.GiftId))
  40. {
  41. // 仙女棒 自己死了复活自己, 自己没死复活其他人, 不够则补充单位
  42. // 能量药丸 自己死了复活自己, 自己没死复活其他3个人, 不够则补充单位
  43. StringBuilder objIds = new StringBuilder();
  44. notify = new Struct.TriggerEventNotify();
  45. if (unitPlayerData.DeadState == 1)
  46. {
  47. objIds.Append(unitPlayerData.ObjId);
  48. }
  49. else
  50. {
  51. long cnt = request.GiftNum * (DouyinItem.GiftId_1.Equals(request.GiftId)? 1 : 3);
  52. foreach (int objId in map.DeadUnitPlayer)
  53. {
  54. if (objId > 0)
  55. {
  56. objIds.Append(objId).Append(',');
  57. cnt -= 1;
  58. }
  59. if (cnt <= 0)
  60. {
  61. break;
  62. }
  63. }
  64. if (cnt > 0)
  65. {
  66. // 补充单位
  67. for (int i = 0; i < cnt; i++)
  68. {
  69. Vector2 pos = map.GetRandomPlayerPos();
  70. Struct.MonsterUnit unit = new Struct.MonsterUnit();
  71. unit.id = RandomGenerator.RandomArray(units);
  72. unit.force = 1;
  73. unit.x = pos.X;
  74. unit.y = pos.Y;
  75. unit.autoGuard = true;
  76. unit.name = map.GetRandomPlayerName();
  77. int _objId = await map.AddUnits(unit, true);
  78. // 临时openId
  79. string _openId = (10000 + new Random().Next(999)).ToString();
  80. map.AddUnitPlayer(_openId, unit.id, _objId, 0, unit.name, "");
  81. }
  82. }
  83. }
  84. notify.message = DouyinItem.GiftId_1.Equals(request.GiftId)? BattleNotify.TiktokGift_1.ToString() : BattleNotify.TiktokGift_10.ToString();
  85. notify.TriggerUnits = objIds.ToString();
  86. map.GetXmdsManager().notifyBattleServer(map.Id.ToString(), NotifyBSName.TriggerEvent, JsonSerializer.Serialize(notify));
  87. // Log.Debug($"============================================仙女棒/能量药丸 notifyBattleServer - data={JsonSerializer.Serialize(notify)}");
  88. }
  89. else
  90. {
  91. for (int i = 0; i < request.GiftNum; i++)
  92. {
  93. switch (request.GiftId)
  94. {
  95. case DouyinItem.GiftId_52:
  96. // 甜甜圈
  97. const long maxLevel = 3; // 等级上限
  98. notify = new Struct.TriggerEventNotify();
  99. if (unitPlayerData.Level < maxLevel)
  100. {
  101. ++unitPlayerData.Level;
  102. notify.message = BattleNotify.TiktokGift_52.ToString();
  103. }
  104. else
  105. {
  106. notify.message = BattleNotify.TiktokGift_52_ext.ToString();
  107. }
  108. notify.TriggerUnits = unitPlayerData.ObjId.ToString();
  109. break;
  110. case DouyinItem.GiftId_99:
  111. // 能量电池
  112. notify = new Struct.TriggerEventNotify();
  113. notify.message = BattleNotify.TiktokGift_99.ToString();
  114. notify.TriggerUnits = unitPlayerData.ObjId.ToString();
  115. // 对应的贡献值
  116. map.AddContributeValue(request.OpenId, 99);
  117. break;
  118. case DouyinItem.GiftId_199:
  119. // 恶魔炸弹
  120. notify = new Struct.TriggerEventNotify();
  121. notify.message = BattleNotify.TiktokGift_199.ToString();
  122. notify.TriggerUnits = unitPlayerData.ObjId.ToString();
  123. // 对应的贡献值
  124. map.AddContributeValue(request.OpenId, 199);
  125. break;
  126. case DouyinItem.GiftId_520:
  127. // 神秘空投
  128. notify = new Struct.TriggerEventNotify();
  129. notify.message = BattleNotify.TiktokGift_520.ToString();
  130. notify.TriggerUnits = unitPlayerData.ObjId.ToString();
  131. // 对应的贡献值
  132. map.AddContributeValue(request.OpenId, 520);
  133. break;
  134. }
  135. map.GetXmdsManager().notifyBattleServer(map.Id.ToString(), NotifyBSName.TriggerEvent, JsonSerializer.Serialize(notify));
  136. //间隔30ms,触发一次礼物效果
  137. await TimerComponent.Instance.WaitAsync(30);
  138. }
  139. }
  140. }
  141. }
  142. }