/
command.go
104 lines (88 loc) · 2.85 KB
/
command.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package commands
import (
"strings"
log "github.com/sirupsen/logrus"
"github.com/AntoineMeresse/flibot-urt/src/models"
"github.com/AntoineMeresse/flibot-urt/src/utils"
"github.com/AntoineMeresse/flibot-urt/src/utils/msg"
)
type Command struct {
Function interface{}
Level int
Usage string
}
func isCommand(text string) (bool) {
return utils.IsVoteCommand(text) || (len(text) > 1 && (text[0] == '!' || text[0] == '@'))
}
func isCommandGlobal(text string) bool {
return text[0] == '@'
}
func replaceShortcutByKnownCommand(cmd *string) {
if val, ok := CommandsShortcut[*cmd]; ok {
*cmd = val;
}
}
func extractCmdInfos(action_params []string) (iscommand bool, command Command, isGlobal bool, params []string) {
text := action_params[2]
if isCommand(text) {
var command string;
if utils.IsVoteCommand(text) {
command = text
} else {
command = strings.ToLower(text[1:])
}
replaceShortcutByKnownCommand(&command)
if cmd, ok := Commands[command]; ok {
return true, cmd, isCommandGlobal(text), utils.CleanEmptyElements(action_params[3:])
}
}
return false, Command{}, false, nil
}
func checkPlayerRights(playerNumber string, command Command, server *models.Server) (canAccess bool, required int, got int) {
log.Debugf("-------------------------------------------------------------")
if (command.Level == 0) {
log.Debug("Command that can be used by everyone.")
return true, 0, 0
}
player, err := server.Players.GetPlayer(playerNumber)
var canUseCmd bool = false;
role := 0
if (err == nil) {
role = player.Role
log.Debugf("checkPlayerRights | player (%v)", player)
canUseCmd = role >= command.Level
}
return canUseCmd, command.Level, role
}
func HandleCommand(action_params []string, server *models.Server) {
playerNumber := action_params[0]
isCommand, command, isGlobal, command_params := extractCmdInfos(action_params)
if isCommand {
canAccess, level, role := checkPlayerRights(playerNumber, command, server)
if canAccess {
displayCommandInfos(action_params[2], playerNumber, command_params, isGlobal)
args := models.CommandsArgs{
Server: server,
PlayerId: playerNumber,
Params: command_params,
IsGlobal: isGlobal,
Usage: command.Usage,
}
command.Function.(func(*models.CommandsArgs))(&args)
} else {
log.Errorf("Player with id (%s) doesn't have enough rights to use command %s (required: %d | got: %d) ",
playerNumber,
action_params[2],
level,
role,
)
server.RconText(false, playerNumber, msg.NOT_ENOUGH_RIGHTS, action_params[2], level, role)
}
}
}
func displayCommandInfos(commandname string, playerNumber string, command_params []string, isGlobal bool) {
log.Debugf("Command: %s", commandname)
log.Debugf(" |-> isGlobal: %v", isGlobal)
log.Debugf(" |-> Playernumber: %s", playerNumber)
log.Debugf(" |-> Params (%d): %v\n", len(command_params), command_params)
}