-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescInterp.go
301 lines (268 loc) · 6.45 KB
/
descInterp.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package main
import (
"fmt"
"strings"
"time"
)
const LOGIN_IDLE = time.Second * 30
const MENU_IDLE = time.Minute * 10
const CHARACTER_IDLE = time.Minute * 30
const NO_LINK_TIME = time.Minute * 2
const BUILDER_IDLE = time.Hour
func (desc *descData) interp() bool {
var input string
desc.inputLock.Lock()
if desc.numInputLines == 0 {
//Return if there are no lines
desc.inputLock.Unlock()
return false
} else {
//Get oldest line
input = desc.inputLines[0]
defer reportPanic(desc, "desc: %v", input)
if desc.numInputLines == 1 {
//If only one line, reset buffer
desc.inputLines = []string{}
desc.numInputLines = 0
} else {
//otherwise, delete oldest entry and decrement count
desc.inputLines = desc.inputLines[1:]
desc.numInputLines--
}
desc.inputLock.Unlock()
}
desc.idleTime = time.Now().UTC()
//If playing
if desc.state == CON_PLAYING {
if desc.character != nil {
//Run command
desc.character.idleTime = time.Now().UTC()
desc.character.handleCommands(input)
mudLog("%v: %v", desc.character.Name, input)
}
return true
}
//Block empty lines, unless login state is set otherwise
if input == "" && !loginStateList[desc.state].anyKey {
//Ignore blank lines, unless set
return true
}
//Run login state function
if loginStateList[desc.state].goDo != nil {
loginStateList[desc.state].goDo(desc, input)
}
showStatePrompt(desc)
//Suppress echo for passwords
if loginStateList[desc.state].hideInfo {
if !desc.telnet.hideEcho {
desc.telnet.hideEcho = true
//errLog("#%v Suppressing echo for login/pass", desc.id)
sendCmd(desc.conn, TermCmd_WILL, TermOpt_ECHO)
}
} else {
if desc.telnet.hideEcho {
desc.telnet.hideEcho = false
//errLog("#%v No longer suppressing echo for login/pass", desc.id)
sendCmd(desc.conn, TermCmd_WONT, TermOpt_ECHO)
}
}
return true
}
func showStatePrompt(desc *descData) {
//Show prompt from next state
if loginStateList[desc.state].goPrompt != nil {
loginStateList[desc.state].goPrompt(desc)
} else if loginStateList[desc.state].prompt != "" {
desc.sendln(NEWLINE + loginStateList[desc.state].prompt)
}
}
func (player *characterData) listCommands(input string) {
if input == "" {
player.send("Commands: help <command> for additional help.")
}
var lastLevel int
for _, item := range cmdList {
if input != "" {
if !strings.EqualFold(input, item.name) {
continue
}
}
if item.hide {
continue
}
if item.level > player.Level {
continue
}
if lastLevel != item.level {
player.send(NEWLINE+"Level: %v", levelToName[item.level].Name)
lastLevel = item.level
}
var parts string
for i, arg := range item.args {
if i > 0 {
parts += ", "
}
parts += fmt.Sprintf("<%v>", arg)
}
if parts != "" {
parts += " "
}
if len(item.modargs) > 0 {
parts += "mod-only: "
for i, arg := range item.modargs {
if i > 0 {
parts += ", "
}
parts += fmt.Sprintf("<%v>", arg)
}
if parts != "" {
parts += " "
}
}
player.sendWW("%10v %v-- %v", cEllip(item.name, 10), parts, item.hint)
}
if input == "" {
return
}
buf := NEWLINE + "Channels: "
count := 0
for _, ch := range channels {
if ch.disabled {
continue
}
if ch.talkLevel > player.Level {
continue
}
if count > 0 {
buf = buf + ", "
}
buf = buf + ch.cmd
count++
}
player.sendWW(buf)
}
func updateOLCHere(player *characterData) {
if player.OLCEditor.OLCMode != OLC_NONE &&
player.Config.hasFlag(CONFIG_OLCHERE) {
player.OLCEditor.Location = player.Loc
player.OLCEditor.area = player.room.pArea
player.OLCEditor.room = player.room
}
}
func (player *characterData) handleCommands(input string) {
defer updateOLCHere(player)
if draftNote(player, input) {
return
}
if !player.Config.hasFlag(CONFIG_OLC) &&
player.OLCEditor.OLCMode != OLC_NONE {
args := strings.SplitN(input, " ", 2)
if input != "" &&
strings.EqualFold(args[0], "cmd") {
if len(args) == 2 {
parseCommand(player, args[1])
} else {
parseCommand(player, "")
}
return
}
}
parseCommand(player, input)
}
func parseCommand(player *characterData, input string) {
cmdStr, args, _ := strings.Cut(input, " ")
cmdStr = strings.ToLower(cmdStr)
command := cmdMap[cmdStr]
if command != nil {
if command.disabled {
player.send("That command is disabled.")
return
}
if command.checkCommandLevel(player) {
if command.forceArg != "" {
command.goDo(player, command.forceArg)
} else {
command.goDo(player, args)
}
}
} else {
if cmdChat(player, input) {
if !findCommandMatch(cmdList, player, cmdStr, args) {
if player.OLCEditor.OLCMode == OLC_NONE {
player.listCommands("")
} else {
goForce(player, "")
}
}
}
}
}
func findCommandMatch(list []*commandData, player *characterData, cmdStr string, args string) bool {
//Find best partial match
var scores map[*commandData]int = make(map[*commandData]int)
cmdStrLen := len(cmdStr)
var highScoreCmd *commandData
var highScore = 0
for x := 0; x < 2; x++ {
for _, cmd := range list {
if cmd.disabled {
continue
}
//Dont match against specific crititcal commands
if x == 0 && cmd.noShort {
continue
}
c := cmd.name
cLen := len(c) - 1
//If command name is shorter, skip
if cLen < cmdStrLen {
continue
}
//Check if all characters match
for x := 0; x < cmdStrLen; x++ {
if c[x] == cmdStr[x] {
scores[cmd]++
continue
} else {
scores[cmd] = 0
break
}
}
//Save highest scores
if scores[cmd] > highScore {
highScore = scores[cmd]
highScoreCmd = cmd
}
}
}
//If we found a match, process
if highScore > 0 && highScoreCmd != nil {
if highScoreCmd.noShort {
//Let player know this command cannot be a partial match
player.send("Did you mean %v? You must type that command in full.", highScoreCmd.name)
return true
} else {
//Run the command
if highScoreCmd.goDo != nil {
if highScoreCmd.checkCommandLevel(player) {
if highScoreCmd.forceArg != "" {
highScoreCmd.goDo(player, highScoreCmd.forceArg)
} else {
highScoreCmd.goDo(player, args)
}
}
return true
}
}
}
//player.send("That isn't an available command.")
return false
}
// Returns true if allowed
func (command *commandData) checkCommandLevel(player *characterData) bool {
if command != nil && command.level > player.Level {
player.send("Sorry, you aren't high enough level to use the '%v' command.", command.name)
return false
}
return true
}