You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

279 lines
6.8 KiB
Go

package live_logic
import (
"dcg/app/user_center/usercenter"
"dcg/config"
"dcg/game/manager"
"dcg/game/pb"
pbCommon "dcg/game/pb/common"
pbGameZhgMang "dcg/game/pb/game/zhgmang"
pbMq "dcg/game/pb/mq"
pbRoom "dcg/game/pb/room"
"dcg/game/svc"
"dcg/pkg/cmd"
"git.noahlan.cn/northlan/ntools-go/logger"
"strconv"
"strings"
)
type ZhgmangGameLogic struct {
svcCtx *svc.ServiceContext
}
func NewZhgmangLiveGameLogic(svcCtx *svc.ServiceContext) *ZhgmangGameLogic {
return &ZhgmangGameLogic{
svcCtx: svcCtx,
}
}
func (h *ZhgmangGameLogic) WithGameType() LogicOption {
return WithGameType(pbRoom.GameType_name[int32(pbRoom.GameType_ZHGMang)])
}
func (h *ZhgmangGameLogic) WithCmdParserLogic(p []cmd.Pattern) LogicOption {
pattern := []cmd.Pattern{
{
Prefix: "j",
Alias: []string{"加入", "加入游戏"},
ContentMaxLen: 1,
}, {
Prefix: "c",
Alias: []string{"抽卡"},
ContentMaxLen: 1,
}, {
Prefix: "w",
Alias: []string{"我在哪"},
ContentMaxLen: 0,
}, {
Prefix: "g",
Alias: []string{"苟"},
ContentMaxLen: 0,
}, {
Prefix: "m",
Alias: []string{"莽"},
ContentMaxLen: 0,
}, {
Prefix: "l",
Alias: []string{"升级"},
ContentMaxLen: 0,
}, {
Prefix: "n",
ContentMaxLen: 8,
},
}
pattern = append(pattern, p...)
cmdParser := cmd.NewCMDParser(pattern...)
cmdParser.SetDistinct(false)
return WithCmdParser(cmdParser)
}
func (h *ZhgmangGameLogic) WithCmdHandlers() LogicOption {
return func(logic LiveLogic) {
logic.RegisterCMDHandler(h.handleJoinGame, "j")
logic.RegisterCMDHandler(h.handleAddUnit, "c")
logic.RegisterCMDHandler(h.handleWai, "w")
logic.RegisterCMDHandler(h.handleLevelUp, "l")
logic.RegisterCMDHandler(h.handleMockGift, "n")
}
}
func (h *ZhgmangGameLogic) WithGiftHandler() LogicOption {
return WithGiftHandler(func(next GiftHandlerFunc) GiftHandlerFunc {
return func(liveRoom *LiveRoom, user *pbCommon.PbUser, gift *pbMq.MqGift) {
h.handleGift(liveRoom, user, gift)
next(liveRoom, user, gift)
}
})
}
func (h *ZhgmangGameLogic) handleJoinGame(liveRoom *LiveRoom, _ string, _ []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
details, err := h.svcCtx.UserCenterRpc.GetUserDetails(h.svcCtx.Ctx, &usercenter.UserIdReq{
UserId: user.UserId,
})
if err != nil {
return
}
resp := &pbGameZhgMang.JoinGame{
User: user,
NobilityLevel: details.NobilityLevel,
}
logger.SLog.Debugf("用户 [%s] 加入游戏", user.Username)
room.Broadcast(pb.PushZhgmangJoinGame, resp)
}
func (h *ZhgmangGameLogic) handleAddUnit(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
if len(content) < 1 {
return
}
var card int32
if len(content) > 0 {
t, err := strconv.ParseInt(string(content[0]), 10, 32)
if err == nil {
card = int32(t)
}
}
if card > 5 {
return
}
logger.SLog.Debugf("用户 [%s] 抽卡 [%d]", user.Username, card)
room.Broadcast(pb.PushZhgmangAddUnit, &pbGameZhgMang.AddUnit{
User: user,
Card: card,
})
}
func (h *ZhgmangGameLogic) handleWai(liveRoom *LiveRoom, _ string, _ []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
logger.SLog.Debugf("用户 [%s] 我在哪", user.Username)
room.Broadcast(pb.PushZhgmangWai, &pbGameZhgMang.Wai{User: user})
}
func (h *ZhgmangGameLogic) handleLevelUp(liveRoom *LiveRoom, _ string, _ []rune, user *pbCommon.PbUser) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
logger.SLog.Debugf("用户 [%s] 基地升级", user.Username)
room.Broadcast(pb.PushZhgmangLevelUp, &pbGameZhgMang.LevelUp{
User: user,
})
}
func (h *ZhgmangGameLogic) handleMockGift(liveRoom *LiveRoom, _ string, content []rune, user *pbCommon.PbUser) {
var err error
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
var isAdminUser bool
for _, i := range h.svcCtx.Config.Game.AdminUserId {
if user.UserId == i {
isAdminUser = true
break
}
}
if !isAdminUser {
return
}
if len(content) < 1 {
return
}
contentStr := string(content)
spaceIdx := strings.Index(contentStr, " ")
if spaceIdx < 0 {
return
}
giftType := contentStr[:spaceIdx]
var count int64 = 1
if count, err = strconv.ParseInt(contentStr[spaceIdx+1:], 10, 32); err != nil {
return
}
if count <= 0 {
count = 1
}
logger.SLog.Debugf("用户 [%s] gift [%s] count [%d]", user.Username, giftType, count)
// 禁止 c w l n j
switch giftType {
case "tt":
h.goldSpeedUp(room, user, float64(count)*0.1)
case "qp":
h.clearScreen(room, user)
case "rd":
h.randomAddon(room, user)
case "hb":
h.redPackAddon(room, user)
default:
h.goldSpeedUp(room, user, float64(count))
}
}
/*
*/
func (h *ZhgmangGameLogic) handleGift(liveRoom *LiveRoom, user *pbCommon.PbUser, gift *pbMq.MqGift) {
room, err := manager.GameManager.RoomByLiveRoom(liveRoom.RoomId, liveRoom.Platform)
if err != nil {
return
}
cfg := h.svcCtx.Config.Game.Zhgmang
cfgGiftType := cfg.ParseGiftType(gift.GiftId)
switch gift.Type {
case pbMq.MqGift_NORMAL:
switch cfgGiftType {
case config.ZhgmangGiftFree:
h.goldSpeedUp(room, user, float64(gift.GiftNum)*0.1)
case config.ZhgmangGiftClearScreen:
for i := 0; i < int(gift.GiftNum); i++ {
h.clearScreen(room, user)
}
case config.ZhgmangGiftRandomAddon:
for i := 0; i < int(gift.GiftNum); i++ {
h.randomAddon(room, user)
}
default:
h.goldSpeedUp(room, user, float64(gift.GiftNum*gift.Price)*0.01)
}
case pbMq.MqGift_PACK:
if cfgGiftType == config.ZhgmangGiftClearScreen {
for i := 0; i < int(gift.GiftNum); i++ {
h.clearScreen(room, user)
}
} else {
h.goldSpeedUp(room, user, float64(gift.GiftNum)*0.01)
}
case pbMq.MqGift_RED_PACK:
// 红包
h.redPackAddon(room, user)
}
}
func (h *ZhgmangGameLogic) goldSpeedUp(room *manager.Room, user *pbCommon.PbUser, battery float64) {
room.Broadcast(pb.PushZhgmangGoldSpeedUp, &pbGameZhgMang.SpeedUp{
User: user,
Second: int32(battery * 10),
})
}
func (h *ZhgmangGameLogic) clearScreen(room *manager.Room, user *pbCommon.PbUser) {
room.Broadcast(pb.PushZhgmangClearScreen, &pbGameZhgMang.ClearScreen{
User: user,
})
}
func (h *ZhgmangGameLogic) randomAddon(room *manager.Room, user *pbCommon.PbUser) {
room.Broadcast(pb.PushZhgmangRandomAddon, &pbGameZhgMang.RandomAddon{
User: user,
})
}
func (h *ZhgmangGameLogic) redPackAddon(room *manager.Room, user *pbCommon.PbUser) {
room.Broadcast(pb.PushZhgmangRedPackAddon, &pbGameZhgMang.RedPackAddon{
User: user,
})
}