R2G_LiveGiftHandler.cs 7.8 KB

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