using System.Linq; using System.Numerics; using System.Text.Json; namespace ET.Server { /// /// 抖音推送回调, 送礼物 /// [ActorMessageHandler(SceneType.Game)] public class R2G_LiveGiftHandler: AMActorHandler { protected override async ETTask Run(Scene scene, R2G_LiveGift request) { // 房间是否存在 Map map = scene.GetComponent().GetMapByRoomId(request.RoomId); if (map == null) { Log.Error($"未找到房间...roomId={request.RoomId}"); return; } Struct.UnitPlayerData unitPlayerData = map.GetUnitPlayerByOpenId(request.OpenId); // 数据是否存在 if (unitPlayerData == null) { Log.Error($"未找到单位玩家数据...openId={request.OpenId}"); return; } unitPlayerData.GiftMoney += (int)request.GiftValue; // 推送客户端 if (map.Player != null) { MessageHelper.SendToClient(map.Player, new G2C_GiftInfoPush { NickName = request.NickName, GiftType = DouyinItem.GiftHash[request.GiftId], GiftNum = (int)request.GiftNum, Url = request.Url, TotalMoney = unitPlayerData.GiftMoney, UnitId = unitPlayerData.ObjId}); } int[] units = new int[] { 101, 121, 111, 131 }; Struct.TriggerEventNotify notify = null; for (int i = 0; i < request.GiftNum; i++) { int objId = 0; switch (request.GiftId) { case DouyinItem.GiftId_1: // 仙女棒 // 自己死了复活自己, 自己没死复活其他人 objId = unitPlayerData.DeadState == 1? unitPlayerData.ObjId : map.DeadUnitPlayer.Count > 0? map.DeadUnitPlayer.First() : 0; if (objId > 0) { map.GetXmdsManager().notifyBattleServer(map.Id.ToString(), NotifyBSName.TriggerEvent, JsonSerializer.Serialize(new Struct.TriggerEventNotify() { message = BattleNotify.TiktokGift_1.ToString(), TriggerUnits = objId.ToString() })) ; } else { // 补充一个单位 Vector2 pos = map.GetRandomPlayerPos(); Struct.MonsterUnit unit = new Struct.MonsterUnit(); unit.id = RandomGenerator.RandomArray(units); unit.force = 1; unit.x = pos.X; unit.y = pos.Y; unit.autoGuard = true; unit.name = "Nickname"; unit.alias = "Url"; int _objId = await map.AddUnits(unit, true); map.AddUnitPlayer(request.OpenId, unit.id, _objId, 0, request.NickName, request.Url); } break; case DouyinItem.GiftId_10: // 能量药丸 objId = 0; // 自己死了复活自己 if (unitPlayerData.DeadState == 1) { objId = unitPlayerData.ObjId; } else { // 自己没死复活其他3个人 int index = 0; foreach (int deadObjId in map.DeadUnitPlayer.Where(deadObjId => deadObjId > 0 && index < 3)) { map.GetXmdsManager().notifyBattleServer(map.Id.ToString(), NotifyBSName.TriggerEvent, JsonSerializer.Serialize(new Struct.TriggerEventNotify() { message = BattleNotify.TiktokGift_10.ToString(), TriggerUnits = deadObjId.ToString() })) ; index += 1; } // 不够则补充单位 if (3 - index > 0) { for (int j = 0; j < 3 - index; j++) { Vector2 pos = map.GetRandomPlayerPos(); Struct.MonsterUnit unit = new Struct.MonsterUnit(); unit.id = RandomGenerator.RandomArray(units); unit.force = 1; unit.x = pos.X; unit.y = pos.Y; unit.autoGuard = true; unit.name = "Nickname"; unit.alias = "Url"; int _objId = await map.AddUnits(unit, true); map.AddUnitPlayer(request.OpenId, unit.id, _objId, 0, request.NickName, request.Url); } } } break; case DouyinItem.GiftId_52: // 甜甜圈 const long maxLevel = 3; // 等级上限 notify = new Struct.TriggerEventNotify(); if (unitPlayerData.Level < maxLevel) { ++unitPlayerData.Level; notify.message = BattleNotify.TiktokGift_52.ToString(); } else { notify.message = BattleNotify.TiktokGift_52_ext.ToString(); } notify.TriggerUnits = unitPlayerData.ObjId.ToString(); break; case DouyinItem.GiftId_99: // 能量电池 notify = new Struct.TriggerEventNotify(); notify.message = BattleNotify.TiktokGift_99.ToString(); notify.TriggerUnits = unitPlayerData.ObjId.ToString(); // 对应的贡献值 map.AddContributeValue(request.OpenId, 99); break; case DouyinItem.GiftId_199: // 恶魔炸弹 notify = new Struct.TriggerEventNotify(); notify.message = BattleNotify.TiktokGift_199.ToString(); notify.TriggerUnits = unitPlayerData.ObjId.ToString(); // 对应的贡献值 map.AddContributeValue(request.OpenId, 199); break; case DouyinItem.GiftId_520: // 神秘空投 notify = new Struct.TriggerEventNotify(); notify.message = BattleNotify.TiktokGift_520.ToString(); notify.TriggerUnits = unitPlayerData.ObjId.ToString(); // 对应的贡献值 map.AddContributeValue(request.OpenId, 520); break; } map.GetXmdsManager().notifyBattleServer(map.Id.ToString(), NotifyBSName.TriggerEvent, JsonSerializer.Serialize(notify)); //间隔30ms,触发一次礼物效果 await TimerComponent.Instance.WaitAsync(30); } } } }