-
Notifications
You must be signed in to change notification settings - Fork 0
/
keywords.go
136 lines (126 loc) · 3.29 KB
/
keywords.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
package events
import (
"log"
"strings"
"github.com/qbradq/sharduo/internal/game"
)
func init() {
reg("KeywordsBanker", KeywordsBanker)
reg("KeywordsCommand", KeywordsCommand)
reg("KeywordsStablemaster", KeywordsStablemaster)
reg("KeywordsVendor", KeywordsVendor)
}
// speechTarget returns true if the given speech event refers to this object.
// The second return value is the list of words following the name of this
// object.
func speechTarget(names []string, receiver, source game.Object, v any) (bool, []string) {
names = append(names, strings.ToLower(receiver.Name()))
if rm, ok := receiver.(game.Mobile); ok && rm.ControlMaster() != nil &&
rm.ControlMaster().Serial() == source.Serial() {
names = append(names, "all")
}
line := strings.ToLower(v.(string))
words := strings.Split(line, " ")
for i, word := range words {
for _, name := range names {
if word == name {
if i >= len(words)-1 {
return true, nil
}
return true, words[i+1:]
}
}
}
return false, words
}
// doKeywords handles the available keywords in a standardized way
func doKeywords(hotWords []string, receiver, source game.Object, words []string) bool {
for i, w := range words {
for _, hw := range hotWords {
if w == hw {
fn, found := keywordEvents[w]
if found {
if i >= len(words)-1 {
return fn(receiver, source, "")
} else {
return fn(receiver, source, words[i+1])
}
} else {
log.Printf("error: hot keyword given without handler \"%s\"", w)
return false
}
}
}
}
return false
}
// KeywordsBanker handles banker speech triggers.
func KeywordsBanker(receiver, source game.Object, v any) bool {
words := strings.Split(v.(string), " ")
return doKeywords([]string{
"balance",
"bank",
"check",
"deposit",
"withdraw",
}, receiver, source, words)
}
// KeywordsVendor handles common vendor speech triggers.
func KeywordsVendor(receiver, source game.Object, v any) bool {
f, words := speechTarget([]string{"vendor"}, receiver, source, v)
if !f {
return false
}
return doKeywords([]string{
"buy",
"sell",
}, receiver, source, words)
}
// KeywordsStablemaster handles stablemaster speech triggers.
func KeywordsStablemaster(receiver, source game.Object, v any) bool {
f, words := speechTarget([]string{"vendor"}, receiver, source, v)
if !f {
return doKeywords([]string{
"stable",
"claim",
}, receiver, source, words)
}
return doKeywords([]string{
"buy",
"stable",
"claim",
}, receiver, source, words)
}
// KeywordsCommand handles command-able creature speech triggers.
func KeywordsCommand(receiver, source game.Object, v any) bool {
f, words := speechTarget(nil, receiver, source, v)
if !f {
return false
}
return doKeywords([]string{
"come",
"drop",
"follow",
"release",
"stay",
"stop",
}, receiver, source, words)
}
// keywordEvents maps keywords to the event handlers they belong to
var keywordEvents = map[string]EventHandler{
"balance": BankBalance,
"bank": OpenBankBox,
"buy": VendorBuy,
"check": BankCheck,
"claim": ClaimAllPets,
"come": CommandFollowMe,
"deposit": BankDeposit,
"drop": CommandDrop,
"follow": CommandFollow,
"release": CommandRelease,
"sell": VendorSell,
"stable": StablePet,
"stay": CommandStay,
"stop": CommandStay,
"withdraw": BankWithdraw,
}