package live_logic import ( "dcg/config" "dcg/game/logic" pbCommon "dcg/game/pb/common" pbGameZhghz "dcg/game/pb/game/zhghz" pbMq "dcg/game/pb/mq" pbRoom "dcg/game/pb/room" "dcg/pkg/cmd" "fmt" "strconv" ) type ( ZhghzGameLogic struct { *LiveGameLogic // giftLogics 礼物处理 platform-cmd:logic giftLogics map[string]giftLogic } giftLogic struct { giftIds []int64 fn giftLogicFunc } giftLogicFunc func(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) ) func NewZhghzLiveGameLogic() *ZhghzGameLogic { resp := &ZhghzGameLogic{ LiveGameLogic: NewLiveGameLogic(pbRoom.GameType_ZHGHZ, cmd.NewCMDParser(false, config.Config.Game.Zhghz.Commands...)), giftLogics: make(map[string]giftLogic), } resp.RegisterCMDHandler(resp.handleJoinGame, "j", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8") resp.RegisterCMDHandler(resp.handleMove, "m1", "m2", "m3", "m4") resp.RegisterCMDHandler(resp.handleShoot, "f") resp.RegisterCMDHandler(resp.handleWai, "w", "我在哪") // gift | game.chargeShield | game.giftShoot resp.RegisterGiftHandler(resp.handleGift) return resp } func (h *ZhghzGameLogic) handleJoinGame(roomId int64, cmd string, user *pbCommon.PbUser) { room, err := logic.GameLogic.RoomManager.RoomByLiveRoomId(roomId) if err != nil { return } var team int32 if len(cmd) >= 2 { v, _ := strconv.Atoi(string(cmd[1])) team = int32(v) } room.PushToLiveRoom(roomId, "game.join", &pbGameZhghz.JoinGame{ User: user, Team: team, }) } func (h *ZhghzGameLogic) handleMove(roomId int64, cmd string, user *pbCommon.PbUser) { room, err := logic.GameLogic.RoomManager.RoomByLiveRoomId(roomId) if err != nil { return } if len(cmd) < 2 { return } v, _ := strconv.Atoi(string(cmd[1])) room.PushToLiveRoom(roomId, "game.move", &pbGameZhghz.Move{ User: user, Direction: int32(v), }) } func (h *ZhghzGameLogic) handleShoot(roomId int64, _ string, user *pbCommon.PbUser) { room, err := logic.GameLogic.RoomManager.RoomByLiveRoomId(roomId) if err != nil { return } room.PushToLiveRoom(roomId, "game.shoot", &pbGameZhghz.Shoot{ User: user, }) } func (h *ZhghzGameLogic) handleWai(roomId int64, _ string, user *pbCommon.PbUser) { room, err := logic.GameLogic.RoomManager.RoomByLiveRoomId(roomId) if err != nil { return } room.PushToLiveRoom(roomId, "game.wai", &pbGameZhghz.Where{User: user}) } func (h *ZhghzGameLogic) handleGiftLogic(cmd string, roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) bool { if gl, ok := h.giftLogics[cmd]; ok { for _, id := range gl.giftIds { if gift.GiftId == id { gl.fn(roomId, user, gift) return true } } return true } else { return false } } func (h *ZhghzGameLogic) handleGift(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) { room, err := logic.GameLogic.RoomManager.RoomByLiveRoomId(roomId) if err != nil { return } // bilibili { logicKey := fmt.Sprintf("%s-game.chargeShield", gift.Platform) if ok := h.handleGiftLogic(logicKey, roomId, user, gift); !ok { // 1 辣条 | 30607 小心心 > 魔法护盾 h.giftLogics[logicKey] = giftLogic{ giftIds: []int64{1, 30607}, fn: func(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) { room.PushToLiveRoom(roomId, "game.chargeShield", &pbGameZhghz.ChargeShield{ User: user, Points: int32(gift.Num * 1), }) }, } } logicKey = fmt.Sprintf("%s-game.giftShoot", gift.Platform) if ok := h.handleGiftLogic(logicKey, roomId, user, gift); !ok { // 31036 小花花 | 31039 牛哇牛哇 > 魔法飞弹 h.giftLogics[logicKey] = giftLogic{ giftIds: []int64{31036, 31039}, fn: func(roomId int64, user *pbCommon.PbUser, gift *pbMq.MqGift) { room.PushToLiveRoom(roomId, "game.giftShoot", &pbGameZhghz.ChargeShield{ User: user, Points: int32(gift.Num * 1), }) }, } } } }