diff --git a/gate/net/kcp_conn_manager.go b/gate/net/kcp_conn_manager.go index ce5cd488..b5038626 100644 --- a/gate/net/kcp_conn_manager.go +++ b/gate/net/kcp_conn_manager.go @@ -33,7 +33,7 @@ const ( ConnSendTimeout = 10 // 发包超时时间 秒 MaxClientConnNumLimit = 1000 // 最大客户端连接数限制 TcpNoDelay = true // 是否禁用tcp的nagle - SessionSendChanLen = 100 // 会话发送管道缓存包容量 + SessionSendChanLen = 1000 // 会话发送管道缓存包容量 ) var CLIENT_CONN_NUM int32 = 0 // 当前客户端连接数 diff --git a/gs/game/game_command_controller.go b/gs/game/game_command_controller.go index 818b31ed..efa66b60 100644 --- a/gs/game/game_command_controller.go +++ b/gs/game/game_command_controller.go @@ -23,42 +23,45 @@ type CommandController struct { // InitControllerList 初始化控制器列表 func (c *CommandManager) InitControllerList() { - c.commandControllerList = []*CommandController{ - // 权限等级 0: 普通玩家 - HelpCommandController, - GotoCommandController, - JumpCommandController, - EquipCommandController, - ItemCommandController, - AvatarCommandController, - GiveCommandController, - KillCommandController, - QuestCommandController, - PointCommandController, - XLuaDebugCommandController, - GcgCommandController, + controllerList := []*CommandController{ // 权限等级 1: GM 1级 - AssignCommandController, + c.NewAssignCommandController(), + // 权限等级 0: 普通玩家 + c.NewHelpCommandController(), + c.NewGotoCommandController(), + c.NewJumpCommandController(), + c.NewEquipCommandController(), + c.NewItemCommandController(), + c.NewAvatarCommandController(), + c.NewGiveCommandController(), + c.NewKillCommandController(), + c.NewQuestCommandController(), + c.NewPointCommandController(), + c.NewXLuaDebugCommandController(), + c.NewGcgCommandController(), } + c.RegAllController(controllerList...) } // 指定命令 -var AssignCommandController = &CommandController{ - Name: "指定", - AliasList: []string{"assign"}, - Description: "{alias} 设置命令指定玩家", - UsageList: []string{ - "{alias} <目标UID> 指定某个玩家", - }, - Perm: CommandPermGM, - Func: AssignCommand, +func (c *CommandManager) NewAssignCommandController() *CommandController { + return &CommandController{ + Name: "指定", + AliasList: []string{"assign"}, + Description: "{alias} 设置命令指定玩家", + UsageList: []string{ + "{alias} <目标UID> 指定某个玩家", + }, + Perm: CommandPermGM, + Func: AssignCommand, + } } -func AssignCommand(c *CommandContent) { +func AssignCommand(c *CommandContent) bool { var assignUid uint32 - c.Dynamic("uint32", func(param any) bool { + return c.Dynamic("uint32", func(param any) bool { value := param.(uint32) // 指定uid assignUid = value @@ -73,44 +76,25 @@ func AssignCommand(c *CommandContent) { // 帮助命令 -var HelpCommandController = &CommandController{ - Name: "帮助", - AliasList: []string{"help"}, - Description: "{alias} 查看简要帮助信息", - UsageList: []string{ - "{alias} 查看简要帮助信息", - "{alias} <序号/命令别名> 查看详细帮助信息", - }, - Perm: CommandPermNormal, - Func: HelpCommand, +func (c *CommandManager) NewHelpCommandController() *CommandController { + return &CommandController{ + Name: "帮助", + AliasList: []string{"help"}, + Description: "{alias} 查看简要帮助信息", + UsageList: []string{ + "{alias} 查看简要帮助信息", + "{alias} <序号/命令别名> 查看详细帮助信息", + }, + Perm: CommandPermNormal, + Func: HelpCommand, + } } -func HelpCommand(c *CommandContent) { +func HelpCommand(c *CommandContent) bool { var controller *CommandController // 命令控制器 var alias string // 别名 - c.SetElse(func() { - // 显示简要帮助信息 - helpText := "================/ 帮 助 /================\n" - for i, controller := range COMMAND_MANAGER.commandControllerList { - // 权限不足跳过 - if c.Executor.CmdPerm < uint8(controller.Perm) { - continue - } - // GM命令和普通命令区分颜色 - var permColor string - switch controller.Perm { - case CommandPermNormal: - permColor = "#CCFFCC" - case CommandPermGM: - permColor = "#FF9999" - } - helpText += fmt.Sprintf("%v. %v - %v\n", permColor, strconv.Itoa(i+1), controller.Name, strings.ReplaceAll(controller.Description, "{alias}", controller.AliasList[0])) - } - helpText += "\nhelp <命令别名> 能查看详细用法哦~\n" - helpText += "<> 代表必填参数 [] 代表可选参数 / 代表或者" - c.SendMessage(c.Executor, helpText) - }).Dynamic("string", func(param any) bool { + return c.Option("string", func(param any) bool { value := param.(string) // 通过别名获取 controller = COMMAND_MANAGER.commandControllerMap[value] @@ -120,6 +104,32 @@ func HelpCommand(c *CommandContent) { alias = value return true }).Execute(func() bool { + if alias == "" { + // 显示简要帮助信息 + helpText := "================/ 帮 助 /================\n" + commandCount := 0 // 权限足够的命令 + for _, controller := range COMMAND_MANAGER.commandControllerList { + // 权限不足跳过 + if c.Executor.CmdPerm < uint8(controller.Perm) { + continue + } + commandCount++ + // GM命令和普通命令区分颜色 + var permColor string + switch controller.Perm { + case CommandPermNormal: + permColor = "#CCFFCC" + case CommandPermGM: + permColor = "#FF9999" + } + helpText += fmt.Sprintf("%v. %v - %v\n", permColor, commandCount, controller.Name, strings.ReplaceAll(controller.Description, "{alias}", controller.AliasList[0])) + } + helpText += "\nhelp <命令别名> 能查看详细用法哦~\n" + helpText += "<> 代表必填参数 [] 代表可选参数 / 代表或者" + c.SendMessage(c.Executor, helpText) + return true + } + // 命令详细用法 usage := "命令用法:\n" for i, s := range controller.UsageList { s = strings.ReplaceAll(s, "{alias}", alias) @@ -137,18 +147,20 @@ func HelpCommand(c *CommandContent) { // 传送坐标命令 -var GotoCommandController = &CommandController{ - Name: "传送坐标", - AliasList: []string{"goto"}, - Description: "{alias} 传送到指定坐标", - UsageList: []string{ - "{alias} <坐标X> <坐标Y> <坐标Z> 传送至指定坐标", - }, - Perm: CommandPermNormal, - Func: GotoCommand, +func (c *CommandManager) NewGotoCommandController() *CommandController { + return &CommandController{ + Name: "传送坐标", + AliasList: []string{"goto"}, + Description: "{alias} 传送到指定坐标", + UsageList: []string{ + "{alias} <坐标X> <坐标Y> <坐标Z> 传送至指定坐标", + }, + Perm: CommandPermNormal, + Func: GotoCommand, + } } -func GotoCommand(c *CommandContent) { +func GotoCommand(c *CommandContent) bool { // 计算相对坐标 parseRelativePosFunc := func(param string, pos float64) (float64, bool) { // 不以 ~ 开头代表使用绝对坐标 @@ -175,7 +187,7 @@ func GotoCommand(c *CommandContent) { var posX, posY, posZ float64 // 解析命令 - c.Dynamic("string", func(param any) bool { + return c.Dynamic("string", func(param any) bool { // 坐标x value := param.(string) pos, ok := parseRelativePosFunc(value, c.AssignPlayer.Pos.X) @@ -204,21 +216,23 @@ func GotoCommand(c *CommandContent) { // 传送场景命令 -var JumpCommandController = &CommandController{ - Name: "传送场景", - AliasList: []string{"jump"}, - Description: "{alias} 传送到至指定场景", - UsageList: []string{ - "{alias} <场景ID> 传送至指定场景", - }, - Perm: CommandPermNormal, - Func: JumpCommand, +func (c *CommandManager) NewJumpCommandController() *CommandController { + return &CommandController{ + Name: "传送场景", + AliasList: []string{"jump"}, + Description: "{alias} 传送到至指定场景", + UsageList: []string{ + "{alias} <场景ID> 传送至指定场景", + }, + Perm: CommandPermNormal, + Func: JumpCommand, + } } -func JumpCommand(c *CommandContent) { +func JumpCommand(c *CommandContent) bool { var sceneId uint32 // 场景id - c.Dynamic("uint32", func(param any) bool { + return c.Dynamic("uint32", func(param any) bool { // 场景id sceneId = param.(uint32) return true @@ -246,24 +260,26 @@ func JumpCommand(c *CommandContent) { // 管理武器命令 -var EquipCommandController = &CommandController{ - Name: "管理武器", - AliasList: []string{"equip", "weapon"}, - Description: "{alias} 管理你的武器", - UsageList: []string{ - "{alias} add <武器ID/all> [武器等级] [突破等级] 添加武器", - }, - Perm: CommandPermNormal, - Func: EquipCommand, +func (c *CommandManager) NewEquipCommandController() *CommandController { + return &CommandController{ + Name: "管理武器", + AliasList: []string{"equip", "weapon"}, + Description: "{alias} 管理你的武器", + UsageList: []string{ + "{alias} add <武器ID/all> [武器等级] [突破等级] 添加武器", + }, + Perm: CommandPermNormal, + Func: EquipCommand, + } } -func EquipCommand(c *CommandContent) { +func EquipCommand(c *CommandContent) bool { var mode string // 模式 var param1 string // 参数1 var level uint8 = 1 // 武器等级 var promote uint8 // 突破等级 - c.Dynamic("string", func(param any) bool { + return c.Dynamic("string", func(param any) bool { // 模式 mode = param.(string) return true @@ -305,23 +321,25 @@ func EquipCommand(c *CommandContent) { // 管理物品命令 -var ItemCommandController = &CommandController{ - Name: "管理物品", - AliasList: []string{"item"}, - Description: "{alias} 管理你的物品", - UsageList: []string{ - "{alias} add <物品ID/all> [数量] 添加物品", - }, - Perm: CommandPermNormal, - Func: ItemCommand, +func (c *CommandManager) NewItemCommandController() *CommandController { + return &CommandController{ + Name: "管理物品", + AliasList: []string{"item"}, + Description: "{alias} 管理你的物品", + UsageList: []string{ + "{alias} add <物品ID/all> [数量] 添加物品", + }, + Perm: CommandPermNormal, + Func: ItemCommand, + } } -func ItemCommand(c *CommandContent) { +func ItemCommand(c *CommandContent) bool { var mode string // 模式 var param1 string // 参数1 var count uint32 = 1 // 数量 - c.Dynamic("string", func(param any) bool { + return c.Dynamic("string", func(param any) bool { // 模式 mode = param.(string) return true @@ -359,22 +377,24 @@ func ItemCommand(c *CommandContent) { // 管理角色命令 -var AvatarCommandController = &CommandController{ - Name: "管理角色", - AliasList: []string{"avatar"}, - Description: "{alias} 管理你的角色", - UsageList: []string{ - "{alias} add <角色ID/all>", - }, - Perm: CommandPermNormal, - Func: AvatarCommand, +func (c *CommandManager) NewAvatarCommandController() *CommandController { + return &CommandController{ + Name: "管理角色", + AliasList: []string{"avatar"}, + Description: "{alias} 管理你的角色", + UsageList: []string{ + "{alias} add <角色ID/all>", + }, + Perm: CommandPermNormal, + Func: AvatarCommand, + } } -func AvatarCommand(c *CommandContent) { +func AvatarCommand(c *CommandContent) bool { var mode string // 模式 var param1 string // 参数1 - c.Dynamic("string", func(param any) bool { + return c.Dynamic("string", func(param any) bool { // 模式 mode = param.(string) return true @@ -408,26 +428,28 @@ func AvatarCommand(c *CommandContent) { // 给予命令 -var GiveCommandController = &CommandController{ - Name: "给予物品", - AliasList: []string{"give"}, - Description: "{alias} 获得全部物品", - UsageList: []string{ - "模式:ID / item (所有物品) / weapon (所有武器) / reliquary (所有圣遗物) / avatar (所有角色) / costume (所有时装) / flycloak (所有风之翼) / all (全部)\n不要加上括号内的中文!!", - "{alias} <模式> [数量] 给予指定物品", - "数量仅物品、武器、圣遗物可用", - }, - Perm: CommandPermNormal, - Func: GiveCommand, +func (c *CommandManager) NewGiveCommandController() *CommandController { + return &CommandController{ + Name: "给予物品", + AliasList: []string{"give"}, + Description: "{alias} 获得全部物品", + UsageList: []string{ + "模式:ID / item (所有物品) / weapon (所有武器) / reliquary (所有圣遗物) / avatar (所有角色) / costume (所有时装) / flycloak (所有风之翼) / all (全部)\n不要加上括号内的中文!!", + "{alias} <模式> [数量] 给予指定物品", + "数量仅物品、武器、圣遗物可用", + }, + Perm: CommandPermNormal, + Func: GiveCommand, + } } -func GiveCommand(c *CommandContent) { +func GiveCommand(c *CommandContent) bool { // 给予物品 var mode string // 给予的模式 var itemId uint32 // 物品id var count uint32 = 1 // 数量 - c.Dynamic("string", func(param any) bool { + return c.Dynamic("string", func(param any) bool { value := param.(string) // 给予的物品 switch value { @@ -540,23 +562,25 @@ func GiveCommand(c *CommandContent) { // 杀死实体命令 -var KillCommandController = &CommandController{ - Name: "杀死实体", - AliasList: []string{"kill"}, - Description: "{alias} 杀死讨厌的实体", - UsageList: []string{ - "{alias} self 杀死自己", - "{alias} monster <实体ID/all> 杀死怪物", - }, - Perm: CommandPermNormal, - Func: KillCommand, +func (c *CommandManager) NewKillCommandController() *CommandController { + return &CommandController{ + Name: "杀死实体", + AliasList: []string{"kill"}, + Description: "{alias} 杀死讨厌的实体", + UsageList: []string{ + "{alias} self 杀死自己", + "{alias} monster <实体ID/all> 杀死怪物", + }, + Perm: CommandPermNormal, + Func: KillCommand, + } } -func KillCommand(c *CommandContent) { +func KillCommand(c *CommandContent) bool { var mode string // 模式 var param1 string // 参数 - c.Dynamic("string", func(param any) bool { + return c.Dynamic("string", func(param any) bool { // 模式 mode = param.(string) return true @@ -601,23 +625,25 @@ func KillCommand(c *CommandContent) { // 管理任务命令 -var QuestCommandController = &CommandController{ - Name: "管理任务", - AliasList: []string{"quest"}, - Description: "{alias} 管理你的任务", - UsageList: []string{ - "{alias} <任务ID> 接受任务", - "{alias} finish <任务ID/all> 完成任务", - }, - Perm: CommandPermNormal, - Func: QuestCommand, +func (c *CommandManager) NewQuestCommandController() *CommandController { + return &CommandController{ + Name: "管理任务", + AliasList: []string{"quest"}, + Description: "{alias} 管理你的任务", + UsageList: []string{ + "{alias} <任务ID> 接受任务", + "{alias} finish <任务ID/all> 完成任务", + }, + Perm: CommandPermNormal, + Func: QuestCommand, + } } -func QuestCommand(c *CommandContent) { +func QuestCommand(c *CommandContent) bool { var mode string // 模式 var param1 string // 参数1 - c.Dynamic("string", func(param any) bool { + return c.Dynamic("string", func(param any) bool { // 模式 mode = param.(string) return true @@ -661,40 +687,32 @@ func QuestCommand(c *CommandContent) { // 解锁锚点命令 -var PointCommandController = &CommandController{ - Name: "解锁锚点", - AliasList: []string{"point"}, - Description: "{alias} 解锁地图上的锚点", - UsageList: []string{ - "{alias} [场景ID] <锚点ID/all> 解锁锚点", - }, - Perm: CommandPermNormal, - Func: PointCommand, +func (c *CommandManager) NewPointCommandController() *CommandController { + return &CommandController{ + Name: "解锁锚点", + AliasList: []string{"point"}, + Description: "{alias} 解锁地图上的锚点", + UsageList: []string{ + "{alias} [场景ID] <锚点ID/all> 解锁锚点", + }, + Perm: CommandPermNormal, + Func: PointCommand, + } } -func PointCommand(c *CommandContent) { +func PointCommand(c *CommandContent) bool { var sceneId = c.AssignPlayer.SceneId // 场景id var param1 string // 参数1 - switch len(c.ParamList) { - case 1: - c.Dynamic("string", func(param any) bool { - // 参数1 - param1 = param.(string) - return true - }) - case 2: - c.Dynamic("uint32", func(param any) bool { - // 场景id - sceneId = param.(uint32) - return true - }).Dynamic("string", func(param any) bool { - // 参数1 - param1 = param.(string) - return true - }) - } - c.Execute(func() bool { + return c.Option("uint32", func(param any) bool { + // 场景id + sceneId = param.(uint32) + return true + }).Dynamic("string", func(param any) bool { + // 参数1 + param1 = param.(string) + return true + }).Execute(func() bool { if param1 == "all" { // 解锁当前场景所有锚点 COMMAND_MANAGER.gmCmd.GMUnlockAllPoint(c.AssignPlayer.PlayerId, sceneId) @@ -714,19 +732,21 @@ func PointCommand(c *CommandContent) { // xLua调试命令 -var XLuaDebugCommandController = &CommandController{ - Name: "xLua调试", - AliasList: []string{"xluadebug"}, - Description: "{alias} 开关xLua调试", - UsageList: []string{ - "{alias} 开关xLua调试", - }, - Perm: CommandPermNormal, - Func: XLuaDebugCommand, +func (c *CommandManager) NewXLuaDebugCommandController() *CommandController { + return &CommandController{ + Name: "xLua调试", + AliasList: []string{"xluadebug"}, + Description: "{alias} 开关xLua调试", + UsageList: []string{ + "{alias} 开关xLua调试", + }, + Perm: CommandPermNormal, + Func: XLuaDebugCommand, + } } -func XLuaDebugCommand(c *CommandContent) { - c.Execute(func() bool { +func XLuaDebugCommand(c *CommandContent) bool { + return c.Execute(func() bool { // 主动开关客户端XLUA调试 if !c.AssignPlayer.XLuaDebug { c.AssignPlayer.XLuaDebug = true @@ -741,19 +761,21 @@ func XLuaDebugCommand(c *CommandContent) { // 七圣召唤测试命令 -var GcgCommandController = &CommandController{ - Name: "七圣召唤测试", - AliasList: []string{"gcgtest"}, - Description: "{alias} 测试七圣召唤", - UsageList: []string{ - "{alias} 测试七圣召唤", - }, - Perm: CommandPermNormal, - Func: GcgCommand, +func (c *CommandManager) NewGcgCommandController() *CommandController { + return &CommandController{ + Name: "七圣召唤测试", + AliasList: []string{"gcgtest"}, + Description: "{alias} 测试七圣召唤", + UsageList: []string{ + "{alias} 测试七圣召唤", + }, + Perm: CommandPermNormal, + Func: GcgCommand, + } } -func GcgCommand(c *CommandContent) { - c.Execute(func() bool { +func GcgCommand(c *CommandContent) bool { + return c.Execute(func() bool { // 开始七圣召唤对局 GAME.GCGStartChallenge(c.AssignPlayer) c.SendSuccMessage(c.Executor, "已开始七圣召唤对局,指定UID:%v。", c.AssignPlayer.PlayerId) diff --git a/gs/game/game_command_gm.go b/gs/game/game_command_gm.go index 08909907..2282af54 100644 --- a/gs/game/game_command_gm.go +++ b/gs/game/game_command_gm.go @@ -2,7 +2,6 @@ package game import ( "encoding/base64" - "hk4e/common/constant" "hk4e/gdconf" "hk4e/gs/model" @@ -556,19 +555,24 @@ func (g *GMCmd) SendMsgToPlayer(cmdName string, userId uint32, msgJson string) { } func (g *GMCmd) StartPubg(v bool) { - iPlugin := PLUGIN_MANAGER.GetIPlugin(PLUGIN_NAME_PUBG) - if iPlugin != nil { - pubg := iPlugin.(*PluginPubg) - pubg.StartPubg() + pluginPubg := &PluginPubg{} + err := PLUGIN_MANAGER.GetPlugin(pluginPubg) + if err != nil { + logger.Error("get plugin pubg error: %v", err) + return } + logger.Error("plugin: %v", pluginPubg) + pluginPubg.StartPubg() } func (g *GMCmd) StopPubg(v bool) { - iPlugin := PLUGIN_MANAGER.GetIPlugin(PLUGIN_NAME_PUBG) - if iPlugin != nil { - pubg := iPlugin.(*PluginPubg) - pubg.StopPubg() + pluginPubg := &PluginPubg{} + err := PLUGIN_MANAGER.GetPlugin(pluginPubg) + if err != nil { + logger.Error("get plugin pubg error: %v", err) + return } + pluginPubg.StopPubg() } func (g *GMCmd) SetPhysicsEngineParam(pathTracing bool, acc float32, drag float32, pao float32, is float32, ayo float32) { diff --git a/gs/game/game_command_manager.go b/gs/game/game_command_manager.go index 4670c097..a4d54b3e 100644 --- a/gs/game/game_command_manager.go +++ b/gs/game/game_command_manager.go @@ -22,7 +22,7 @@ const ( ) // CommandFunc 命令执行函数 -type CommandFunc func(content *CommandContent) +type CommandFunc func(content *CommandContent) bool const ( PlayerChatGM = iota // 玩家聊天GM @@ -42,15 +42,25 @@ type CommandMessage struct { ParamList []string // 函数参数列表 } -// ContentParamResult 命令内容参数返回 -type ContentParamResult uint8 +// CommandContentStepFunc 命令步骤处理函数 +type CommandContentStepFunc func(param any) bool + +// CommandContentStepType 命令步骤类型 +type CommandContentStepType uint8 const ( - ContentParamResultInit ContentParamResult = iota - ContentParamResultFailed // 失败 - ContentParamResultSuccess // 成功 + CommandContentStepTypeNone = CommandContentStepType(iota) + CommandContentStepTypeDynamic // 动态 + CommandContentStepTypeOption // 可选 ) +// CommandContentStep 命令步骤结构 +type CommandContentStep struct { + StepType CommandContentStepType // 步骤类型 + ParamTypeStr string // 当前步骤参数类型 + StepFunc CommandContentStepFunc // 步骤处理函数 +} + // CommandContent 命令内容 type CommandContent struct { Executor *model.Player // 执行者 @@ -59,9 +69,9 @@ type CommandContent struct { ParamList []string // 玩家输入的参数列表 Controller *CommandController // 命令控制器 // 执行时数据 - paramIndex uint8 // 当前执行到的参数索引 - paramResult ContentParamResult // 当前参数执行结果 - elseFunc func() // 参数错误处理函数 + paramIndex uint8 // 当前执行到的参数索引 + elseFunc func() // 参数错误处理函数 + stepList []*CommandContentStep // 步骤处理函数列表 } // SendMessage 发送消息 @@ -182,51 +192,69 @@ func (c *CommandContent) getNextParam(typeStr string) (param any, ok bool) { } // Dynamic 动态参数执行 -func (c *CommandContent) Dynamic(typeStr string, dynamicFunc func(param any) bool) *CommandContent { - // 上个参数执行错误则跳出 - if c.paramResult == ContentParamResultFailed { - return c - } - // 拥有下个参数则继续执行 - nowParam, ok := c.getNextParam(typeStr) - if ok && dynamicFunc(nowParam) { - c.paramResult = ContentParamResultSuccess - } else { - c.paramResult = ContentParamResultFailed +func (c *CommandContent) Dynamic(typeStr string, stepFunc CommandContentStepFunc) *CommandContent { + step := &CommandContentStep{ + StepType: CommandContentStepTypeDynamic, + ParamTypeStr: typeStr, + StepFunc: stepFunc, } + c.stepList = append(c.stepList, step) return c } // Option 可选参数执行 -func (c *CommandContent) Option(typeStr string, ifDynamicFunc func(param any) bool) *CommandContent { - // 上个参数执行错误则跳出 - if c.paramResult == ContentParamResultFailed { - return c - } - // 条件成立则执行 - if len(c.ParamList) > int(c.paramIndex) { - return c.Dynamic(typeStr, ifDynamicFunc) +func (c *CommandContent) Option(typeStr string, stepFunc CommandContentStepFunc) *CommandContent { + step := &CommandContentStep{ + StepType: CommandContentStepTypeOption, + ParamTypeStr: typeStr, + StepFunc: stepFunc, } + c.stepList = append(c.stepList, step) return c } // Execute 执行命令实际业务并返回结果 -func (c *CommandContent) Execute(thenFunc func() bool) { - // 上个参数执行错误则跳出 - if c.paramResult == ContentParamResultFailed { - return +func (c *CommandContent) Execute(thenFunc func() bool) bool { + dynamicStepCount := 0 // 必填参数数量 + dynamicStepIndex := 0 // 必填参数最后的位置 + for i, step := range c.stepList { + if step.StepType == CommandContentStepTypeDynamic { + dynamicStepCount++ + dynamicStepIndex = i + } + } + // 执行每个步骤 + for i, step := range c.stepList { + switch step.StepType { + case CommandContentStepTypeOption: + // 可选参数 参数不足则不执行 + if i <= dynamicStepIndex { + if len(c.ParamList) <= i+dynamicStepCount { + continue + } + } else if len(c.ParamList) <= i { + continue + } + } + // 获取当前参数 + param, ok := c.getNextParam(step.ParamTypeStr) + if !ok { + return false + } + // 执行处理函数 + if !step.StepFunc(param) { + return false + } } if thenFunc() { - c.paramResult = ContentParamResultSuccess - } else { - c.paramResult = ContentParamResultFailed + return true } + return false } // SetElse 设置参数执行错误处理 -func (c *CommandContent) SetElse(elseFunc func()) *CommandContent { +func (c *CommandContent) SetElse(elseFunc func()) { c.elseFunc = elseFunc - return c } // CommandManager 命令管理器 @@ -243,6 +271,8 @@ type CommandManager struct { func NewCommandManager() *CommandManager { r := new(CommandManager) // 初始化 + r.commandControllerList = make([]*CommandController, 0) + r.commandControllerMap = make(map[string]*CommandController) r.commandMessageInput = make(chan *CommandMessage, 1000) r.InitController() // 初始化控制器 r.gmCmd = new(GMCmd) @@ -264,25 +294,50 @@ func (c *CommandManager) InitController() { // 初始化命令控制器列表 c.InitControllerList() // 注册所有的命令控制器 - c.RegisterAllController() + c.RegAllController() } -// RegisterAllController 注册所有命令控制器 -func (c *CommandManager) RegisterAllController() { - c.commandControllerMap = make(map[string]*CommandController) +// RegAllController 注册所有命令控制器 +func (c *CommandManager) RegAllController(controllerList ...*CommandController) { + for _, controller := range controllerList { + c.RegController(controller) + } +} - for _, controller := range c.commandControllerList { - // 支持一个命令拥有多个别名 - for _, name := range controller.AliasList { - // 命令名统一转为小写 - name = strings.ToLower(name) - // 如果命令已注册则报错 后者覆盖前者 - _, ok := c.commandControllerMap[name] - if ok { - logger.Error("register command repeat, name: %v", name) - } - // 记录命令 - c.commandControllerMap[name] = controller +// RegController 注册命令控制器 +func (c *CommandManager) RegController(controller *CommandController) { + // 支持一个命令拥有多个别名 + for _, name := range controller.AliasList { + // 命令名统一转为小写 + name = strings.ToLower(name) + // 如果命令已注册则报错 后者覆盖前者 + _, ok := c.commandControllerMap[name] + if ok { + logger.Error("register command repeat, name: %v", name) + } + // 记录命令 + c.commandControllerMap[name] = controller + } + c.commandControllerList = append(c.commandControllerList, controller) +} + +// DelAllController 卸载所有命令控制器 +func (c *CommandManager) DelAllController(controllerList ...*CommandController) { + for _, controller := range controllerList { + c.DelController(controller) + } +} + +// DelController 卸载命令控制器 +func (c *CommandManager) DelController(controller *CommandController) { + // 支持一个命令拥有多个别名 + for _, name := range controller.AliasList { + delete(c.commandControllerMap, name) + } + // 卸载列表上的控制器 + for i, commandController := range c.commandControllerList { + if commandController == controller { + c.commandControllerList = append(c.commandControllerList[:i], c.commandControllerList[i+1:]...) } } } @@ -475,9 +530,8 @@ func (c *CommandManager) ExecCommand(cmd *CommandMessage) { content.AssignPlayer = target } // 执行命令 - controller.Func(content) - // 命令执行过程中没有问题就跳出 - if content.paramResult == ContentParamResultSuccess { + if controller.Func(content) { + // 命令执行过程中没有问题就跳出 return } // 命令参数错误处理 diff --git a/gs/game/game_plugin_manager.go b/gs/game/game_plugin_manager.go index a64960fc..0d9ed7fa 100644 --- a/gs/game/game_plugin_manager.go +++ b/gs/game/game_plugin_manager.go @@ -1,9 +1,12 @@ package game import ( + "errors" + "fmt" "hk4e/gs/model" "hk4e/pkg/logger" "hk4e/protocol/proto" + "reflect" "sort" ) @@ -17,6 +20,10 @@ func (p *PluginManager) InitPlugin() { p.RegAllPlugin(iPluginList...) } +// 事件定义 +// 添加事件编号 事件结构 +// 即可触发以及监听事件 + // PluginEventId 事件编号 type PluginEventId uint16 @@ -121,21 +128,20 @@ type PluginUserTimerFunc func(player *model.Player, data []any) // Plugin 插件结构 type Plugin struct { - PluginName string // 插件名 遵守小驼峰命名法 - - isEnable bool // 是否启用 - eventMap map[PluginEventId][]*PluginEventInfo // 事件集合 - globalTickMap map[PluginGlobalTick][]func() // 全局tick集合 - userTimerMap map[uint64]PluginUserTimerFunc // 用户timer集合 + isEnable bool // 是否启用 + eventMap map[PluginEventId][]*PluginEventInfo // 事件集合 + globalTickMap map[PluginGlobalTick][]func() // 全局tick集合 + userTimerMap map[uint64]PluginUserTimerFunc // 用户timer集合 + commandControllerList []*CommandController // 命令控制器列表 } -func NewPlugin(pluginName string) *Plugin { +func NewPlugin() *Plugin { return &Plugin{ - PluginName: pluginName, - isEnable: true, - eventMap: make(map[PluginEventId][]*PluginEventInfo), - globalTickMap: make(map[PluginGlobalTick][]func()), - userTimerMap: make(map[uint64]PluginUserTimerFunc), + isEnable: true, + eventMap: make(map[PluginEventId][]*PluginEventInfo), + globalTickMap: make(map[PluginGlobalTick][]func()), + userTimerMap: make(map[uint64]PluginUserTimerFunc), + commandControllerList: make([]*CommandController, 0), } } @@ -191,14 +197,19 @@ func (p *Plugin) CreateUserTimer(userId uint32, delay uint32, timerFunc PluginUs TICK_MANAGER.CreateUserTimer(userId, UserTimerActionPlugin, delay, data...) } +// RegCommandController 注册命令控制器 +func (p *Plugin) RegCommandController(controller *CommandController) { + COMMAND_MANAGER.RegController(controller) +} + type PluginManager struct { - pluginMap map[string]IPlugin // 插件集合 - userTimerCounter uint64 // 用户timer计数器 + pluginMap map[reflect.Type]IPlugin // 插件集合 + userTimerCounter uint64 // 用户timer计数器 } func NewPluginManager() *PluginManager { r := new(PluginManager) - r.pluginMap = make(map[string]IPlugin) + r.pluginMap = make(map[reflect.Type]IPlugin) return r } @@ -211,17 +222,18 @@ func (p *PluginManager) RegAllPlugin(iPluginList ...IPlugin) { // RegPlugin 注册插件 func (p *PluginManager) RegPlugin(iPlugin IPlugin) { - plugin := iPlugin.GetPlugin() + // 反射类型 + refType := reflect.TypeOf(iPlugin) // 校验插件名是否已被注册 - _, exist := p.pluginMap[plugin.PluginName] + _, exist := p.pluginMap[refType] if exist { - logger.Error("plugin has been register, name: %v", plugin.PluginName) + logger.Error("plugin has been register,refType: %v", refType) return } - logger.Info("plugin enable, name: %v", plugin.PluginName) + logger.Info("plugin enable, refType: %v", refType) // 调用插件启用的生命周期 iPlugin.OnEnable() - p.pluginMap[plugin.PluginName] = iPlugin + p.pluginMap[refType] = iPlugin } // DelAllPlugin 卸载全部插件 @@ -233,27 +245,53 @@ func (p *PluginManager) DelAllPlugin() { // DelPlugin 卸载插件 func (p *PluginManager) DelPlugin(iPlugin IPlugin) { - plugin := iPlugin.GetPlugin() + // 反射类型 + refType := reflect.TypeOf(iPlugin) // 校验插件是否注册 - _, exist := p.pluginMap[plugin.PluginName] + _, exist := p.pluginMap[refType] if !exist { - logger.Error("plugin not exist, name: %v", plugin.PluginName) + logger.Error("plugin not exist, refType: %v", refType) return } - logger.Info("plugin disable, name: %v", plugin.PluginName) + logger.Info("plugin disable, refType: %v", refType) // 调用插件禁用的生命周期 iPlugin.OnDisable() - delete(p.pluginMap, plugin.PluginName) + // 卸载插件注册的命令 + plugin := iPlugin.GetPlugin() + for _, controller := range plugin.commandControllerList { + COMMAND_MANAGER.DelAllController(controller) + } + delete(p.pluginMap, refType) } -// GetIPlugin 获取抽象插件 -func (p *PluginManager) GetIPlugin(pluginName string) IPlugin { - iPlugin, exist := p.pluginMap[pluginName] - if !exist { - logger.Error("plugin not exist, name: %v", pluginName) - return nil +// GetPlugin 获取插件实例 +func (p *PluginManager) GetPlugin(value IPlugin) (err error) { + defer func() { + if r := recover(); r != nil { + switch x := r.(type) { + case string: + err = errors.New(x) + case error: + err = x + default: + err = errors.New("unknown panic") + } + } + }() + refValue := reflect.ValueOf(value) + if refValue.Kind() != reflect.Pointer || refValue.IsNil() { + return errors.New("value is not pointer") } - return iPlugin + refType := refValue.Type() + // 校验插件是否注册 + plugin, exist := p.pluginMap[refType] + if !exist { + err = errors.New(fmt.Sprintf("plugin not exist, refType: %v", refType)) + return + } + refPlugin := reflect.ValueOf(plugin) + refValue.Elem().Set(refPlugin.Elem()) + return } // TriggerEvent 触发事件 diff --git a/gs/game/game_plugin_pubg.go b/gs/game/game_plugin_pubg.go index c5b4626b..e9ad2be7 100644 --- a/gs/game/game_plugin_pubg.go +++ b/gs/game/game_plugin_pubg.go @@ -23,8 +23,6 @@ const ( PUBG_FIRST_AREA_REDUCE_TIME = 600.0 ) -const PLUGIN_NAME_PUBG = "pubg" // 插件名 - // PluginPubg pubg游戏插件 type PluginPubg struct { *Plugin @@ -42,7 +40,7 @@ type PluginPubg struct { func NewPluginPubg() *PluginPubg { p := &PluginPubg{ - Plugin: NewPlugin(PLUGIN_NAME_PUBG), + Plugin: NewPlugin(), world: nil, blueAreaCenterPos: &model.Vector{X: 0.0, Y: 0.0, Z: 0.0}, blueAreaRadius: 0.0, @@ -54,13 +52,15 @@ func NewPluginPubg() *PluginPubg { areaReduceZSpeed: 0.0, areaPointList: make([]*proto.MapMarkPoint, 0), } - // 注册事件 + // 监听事件 p.ListenEvent(PluginEventIdPlayerKillAvatar, PluginEventPriorityNormal, p.EventKillAvatar) p.ListenEvent(PluginEventIdMarkMap, PluginEventPriorityNormal, p.EventMarkMap) p.ListenEvent(PluginEventIdAvatarDieAnimationEnd, PluginEventPriorityNormal, p.EventAvatarDieAnimationEnd) // 添加全局定时器 p.AddGlobalTick(PluginGlobalTickSecond, p.GlobalTickPubg) p.AddGlobalTick(PluginGlobalTickHour, p.GlobalTickHourStart) + // 注册命令 + p.RegCommandController(p.NewPubgCommandController()) return p } @@ -175,6 +175,47 @@ func (p *PluginPubg) UserTimerPubgDieExit(player *model.Player, data []any) { GAME.ReLoginPlayer(player.PlayerId, true) } +/************************************************** 命令控制器 **************************************************/ + +// pubg游戏命令 + +func (p *PluginPubg) NewPubgCommandController() *CommandController { + return &CommandController{ + Name: "PUBG游戏", + AliasList: []string{"pubg"}, + Description: "{alias} 测试pubg游戏", + UsageList: []string{ + "{alias} 开始或关闭pubg游戏", + }, + Perm: CommandPermNormal, + Func: p.PubgCommand, + } +} + +func (p *PluginPubg) PubgCommand(c *CommandContent) bool { + var mode string // 模式 + + return c.Dynamic("string", func(param any) bool { + // 模式 + mode = param.(string) + return true + }).Execute(func() bool { + switch mode { + case "start": + // 开始游戏 + p.StartPubg() + c.SendSuccMessage(c.Executor, "已开始PUBG游戏。") + case "stop": + // 结束游戏 + p.StopPubg() + c.SendSuccMessage(c.Executor, "已结束PUBG游戏。") + default: + return false + } + return true + }) +} + /************************************************** 插件功能 **************************************************/ // StartPubg 开始pubg游戏 diff --git a/gs/game/player_chat.go b/gs/game/player_chat.go index a1417646..cc7d6edf 100644 --- a/gs/game/player_chat.go +++ b/gs/game/player_chat.go @@ -266,7 +266,7 @@ func (g *Game) SendPrivateChat(player *model.Player, targetUid uint32, content a privateChatNotify := &proto.PrivateChatNotify{ ChatInfo: chatInfo, } - g.SendMsg(cmd.PrivateChatNotify, targetPlayer.PlayerId, player.ClientSeq, privateChatNotify) + g.SendMsg(cmd.PrivateChatNotify, targetPlayer.PlayerId, targetPlayer.ClientSeq, privateChatNotify) } } @@ -289,6 +289,7 @@ func (g *Game) ConvChatInfoToChatMsg(chatInfo *proto.ChatInfo) (chatMsg *model.C chatMsg.MsgType = model.ChatMsgTypeIcon chatMsg.Icon = chatInfo.Content.(*proto.ChatInfo_Icon).Icon default: + logger.Error("chat info content type error, contentType: %T", chatInfo.Content) } return chatMsg } @@ -312,6 +313,7 @@ func (g *Game) ConvChatMsgToChatInfo(chatMsg *model.ChatMsg) (chatInfo *proto.Ch Icon: chatMsg.Icon, } default: + logger.Error("chat info content type error, msgType: %v", chatMsg.MsgType) } return chatInfo }