R2G_LiveGiftHandler.cs 7.6 KB

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