-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathadmin.locate.go
More file actions
167 lines (138 loc) · 4.1 KB
/
admin.locate.go
File metadata and controls
167 lines (138 loc) · 4.1 KB
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
package usercommands
import (
"fmt"
"math"
"sort"
"strings"
"github.com/volte6/gomud/internal/mobs"
"github.com/volte6/gomud/internal/rooms"
"github.com/volte6/gomud/internal/templates"
"github.com/volte6/gomud/internal/users"
)
func Locate(rest string, user *users.UserRecord, room *rooms.Room, flags UserCommandFlag) (bool, error) {
if rest == "" {
infoOutput, _ := templates.Process("admincommands/help/command.locate", nil)
user.SendText(infoOutput)
return true, nil
}
locateUser := users.GetByCharacterName(rest)
if locateUser != nil {
room := rooms.LoadRoom(locateUser.Character.RoomId)
if room == nil {
return false, fmt.Errorf(`room %d not found`, locateUser.Character.RoomId)
}
user.SendText(
fmt.Sprintf(`<ansi fg="username">%s</ansi> is in room #<ansi fg="yellow-bold">%d</ansi> - <ansi fg="magenta">%s</ansi> <ansi fg="red">【%s】</ansi>`, locateUser.Character.Name, room.RoomId, room.Title, locateUser.Character.Zone),
)
locateUser.SendText(
`You get the feeling someone is looking for you...`,
)
return true, nil
}
allMobNames := []string{}
allMobsByName := map[string][]mobs.Mob{}
allMobCt := 0
// Now look for mobs?
headers := []string{"MobName", "Room", "Room Title", "Zone", "Stray"}
rows := [][]string{}
listAll := false
startsWith := false
endsWith := false
contains := false
searchTerm := strings.ToLower(rest)
if searchTerm == `*` {
listAll = true
}
if strings.HasPrefix(searchTerm, `*`) {
searchTerm = strings.TrimPrefix(searchTerm, `*`)
endsWith = true
}
if strings.HasSuffix(searchTerm, `*`) {
searchTerm = strings.TrimSuffix(searchTerm, `*`)
startsWith = true
}
if startsWith && endsWith {
startsWith = false
endsWith = false
contains = true
}
for _, mobId := range mobs.GetAllMobInstanceIds() {
mob := mobs.GetInstance(mobId)
if !listAll {
testName := strings.ToLower(mob.Character.Name)
if contains {
if !strings.Contains(testName, searchTerm) {
continue
}
} else if startsWith {
if !strings.HasPrefix(testName, searchTerm) {
continue
}
} else if endsWith {
if !strings.HasSuffix(testName, searchTerm) {
continue
}
} else if testName != searchTerm {
continue
}
}
if _, ok := allMobsByName[mob.Character.Name]; !ok {
allMobsByName[mob.Character.Name] = []mobs.Mob{}
allMobNames = append(allMobNames, mob.Character.Name)
}
allMobsByName[mob.Character.Name] = append(allMobsByName[mob.Character.Name], *mob)
allMobCt++
}
if allMobCt > 0 {
matchesPerPage := 20
pageCt := int(math.Ceil(float64(allMobCt) / float64(matchesPerPage)))
pageNow := 0
sort.Strings(allMobNames)
ct := 0
for _, mobName := range allMobNames {
for _, mob := range allMobsByName[mobName] {
room := rooms.LoadRoom(mob.Character.RoomId)
ct++
// trunacte room.Title to only 20 chars
roomTitle := room.Title
if len(roomTitle) > 24 {
roomTitle = roomTitle[0:23] + `…`
}
mobName := mob.Character.Name
if mob.Character.Aggro != nil {
mobName = `*` + mobName
}
if len(mobName) > 24 {
mobName = mobName[0:23] + `…`
}
rows = append(rows, []string{
fmt.Sprintf(`%-24s`, mobName),
fmt.Sprintf(`%-4d`, mob.Character.RoomId),
fmt.Sprintf(`%-24s`, roomTitle),
fmt.Sprintf(`%-14s`, mob.Character.Zone),
fmt.Sprintf(`%-5s`, fmt.Sprintf(`%d/%d`, len(mob.RoomStack), mob.MaxWander)),
})
if ct >= matchesPerPage {
onlineTableData := templates.GetTable(fmt.Sprintf(`Matches for "%s" [Page %d/%d]`, rest, pageNow+1, pageCt), headers, rows)
tplTxt, _ := templates.Process("tables/generic", onlineTableData)
user.SendText(tplTxt)
rows = [][]string{}
ct = 0
pageNow++
continue
}
}
}
// Final flush
if pageNow < pageCt {
onlineTableData := templates.GetTable(fmt.Sprintf(`Matches for "%s" [Page %d/%d]`, rest, pageNow+1, pageCt), headers, rows)
tplTxt, _ := templates.Process("tables/generic", onlineTableData)
user.SendText(tplTxt)
}
return true, nil
}
user.SendText(
fmt.Sprintf("No user or mob found with the name %s", rest),
)
return true, nil
}