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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
package game
import (
"fmt"
"sort"
"strconv"
"strings"
"thehouseoficarus/internal/behavior"
"thehouseoficarus/internal/net"
"thehouseoficarus/internal/player"
"thehouseoficarus/internal/ui"
)
func (g *Game) executeBank(sess *net.Session, args []string, rawInput string) {
g.doBank(sess)
}
func (g *Game) handleBankInput(sess *net.Session, input string) {
if sess.Player == nil {
sess.State = net.StateGame
g.writePrompt(sess)
return
}
input = strings.TrimSpace(input)
parts := strings.Fields(strings.ToLower(input))
if len(parts) == 0 {
g.showBankBrowse(sess)
return
}
cmd := parts[0]
switch cmd {
case "deposit":
if len(parts) < 2 {
sess.WriteLine("Deposit what?")
} else if parts[1] == "credits" {
g.doBankDepositCredits(sess, parts)
} else if parts[1] == "all" {
g.doBankDepositAll(sess)
} else {
g.doBankDeposit(sess, strings.Join(parts[1:], " "))
}
case "withdraw":
if len(parts) < 2 {
sess.WriteLine("Withdraw what?")
} else if parts[1] == "credits" {
g.doBankWithdrawCredits(sess, parts)
} else {
g.doBankWithdraw(sess, strings.Join(parts[1:], " "))
}
case "browse", "list":
g.showBankBrowse(sess)
case "leave", "bye", "exit", "quit":
g.leaveBank(sess)
return
default:
sess.WriteLine("Commands: deposit <item>, deposit credits <n>, deposit all, withdraw <item>, withdraw credits <n>, browse, leave")
}
g.writeBankPrompt(sess)
}
func (g *Game) writeBankPrompt(sess *net.Session) {
sess.WritePrompt(g.colorize(sess, "dialog", "Bank> "))
}
func (g *Game) showBankBrowse(sess *net.Session) {
p := sess.Player
unicode := p.OptionBool("unicode")
t := ui.Table{
Title: "Bank Contents",
Columns: []string{"#", "Item", "Quantity"},
}
type bankEntry struct {
slot int
name string
id string
qty int
}
var entries []bankEntry
for _, i := range p.BankSlots() {
slot := p.BankSlot(i)
if slot == nil {
continue
}
def, _ := g.ItemStore.Load(slot.ItemID)
itemName := slot.ItemID
if def != nil {
itemName = def.Name
}
entries = append(entries, bankEntry{slot: i, name: itemName, id: slot.ItemID, qty: slot.Quantity})
}
if len(entries) == 0 && p.BankedCredits <= 0 {
sess.WriteLine("Your bank is empty.")
return
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].slot < entries[j].slot
})
for _, e := range entries {
def, _ := g.ItemStore.Load(e.id)
t.Rows = append(t.Rows, []string{
fmt.Sprintf("%d", e.slot+1),
g.itemColorize(sess, def, e.name),
fmt.Sprintf("%d", e.qty),
})
}
for _, line := range t.Render(unicode, p.OptionInt("wrap_width")) {
sess.WriteLine(line)
}
if p.BankedCredits > 0 {
sess.WriteLine(g.colorize(sess, "credits_pickup", fmt.Sprintf("Banked credits: %s", formatInt(p.BankedCredits))))
}
}
func (g *Game) doBankDeposit(sess *net.Session, input string) {
p := sess.Player
matches := g.findInventoryMatches(input, p)
if len(matches) == 0 {
sess.WriteLine("You don't have that item.")
return
}
if len(matches) > 1 {
g.showWhichOne(sess, matches)
return
}
match := matches[0]
slot := p.InvSlot(match.Slot)
if slot == nil {
return
}
itemID := slot.ItemID
qty := slot.Quantity
def, _ := g.ItemStore.Load(itemID)
p.SetInvSlot(match.Slot, nil)
bankIdx := p.NextBankSlot()
p.SetBankSlot(bankIdx, &player.InventorySlot{
ItemID: itemID,
Quantity: qty,
Quality: slot.Quality,
})
g.AccountStore.SaveCharacter(p)
displayName := itemID
if def != nil {
displayName = def.Name
}
itemColor := g.itemColorize(sess, def, displayName)
if qty > 1 {
sess.WriteLine(fmt.Sprintf("You deposit %d %ss.", qty, itemColor))
} else {
sess.WriteLine(fmt.Sprintf("You deposit a %s.", itemColor))
}
}
func (g *Game) doBankDepositAll(sess *net.Session) {
p := sess.Player
var deposited int
for i := 0; i < 28; i++ {
slot := p.InvSlot(i)
if slot == nil {
continue
}
bankIdx := p.NextBankSlot()
p.SetBankSlot(bankIdx, &player.InventorySlot{
ItemID: slot.ItemID,
Quantity: slot.Quantity,
Quality: slot.Quality,
})
p.SetInvSlot(i, nil)
deposited++
}
if deposited == 0 {
sess.WriteLine("Your inventory is already empty.")
return
}
g.AccountStore.SaveCharacter(p)
sess.WriteLine(fmt.Sprintf("You deposit %d item%s.", deposited, plural(deposited)))
}
func (g *Game) doBankWithdraw(sess *net.Session, input string) {
p := sess.Player
freeSlot := p.FirstFreeSlot()
if freeSlot == -1 {
sess.WriteLine("Your inventory is full.")
return
}
var entries []struct {
slot int
name string
id string
qty int
}
for _, i := range p.BankSlots() {
bs := p.BankSlot(i)
if bs == nil {
continue
}
def, _ := g.ItemStore.Load(bs.ItemID)
itemName := bs.ItemID
if def != nil {
itemName = def.Name
}
entries = append(entries, struct {
slot int
name string
id string
qty int
}{slot: i, name: itemName, id: bs.ItemID, qty: bs.Quantity})
}
idx, err := parseChoiceIndex(input)
if err == nil && idx > 0 && idx <= len(entries) {
e := entries[idx-1]
g.withdrawBankItem(sess, p, e.slot)
return
}
var matches []struct {
slot int
name string
id string
qty int
}
lower := strings.ToLower(input)
for _, e := range entries {
if behavior.WordPrefixMatch(e.name, lower) {
matches = append(matches, e)
}
}
if len(matches) == 1 {
g.withdrawBankItem(sess, p, matches[0].slot)
return
}
for _, e := range entries {
def, _ := g.ItemStore.Load(e.id)
if def != nil && def.MatchesName(input) {
matches = append(matches, e)
}
}
if len(matches) == 0 {
sess.WriteLine("That item isn't in your bank. Use 'browse' to see your bank contents.")
return
}
if len(matches) > 1 {
var names []string
for _, m := range matches {
names = append(names, m.name)
}
sess.WriteLine("That's ambiguous, which one?")
for _, n := range names {
sess.WriteLine(fmt.Sprintf(" - %s", n))
}
return
}
g.withdrawBankItem(sess, p, matches[0].slot)
}
func (g *Game) withdrawBankItem(sess *net.Session, p *player.Player, bankSlotIdx int) {
bs := p.BankSlot(bankSlotIdx)
if bs == nil {
return
}
freeSlot := p.FirstFreeSlot()
if freeSlot == -1 {
sess.WriteLine("Your inventory is full.")
return
}
itemID := bs.ItemID
qty := bs.Quantity
def, _ := g.ItemStore.Load(itemID)
if def != nil && def.Stackable {
p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: itemID, Quantity: qty})
} else {
p.SetInvSlot(freeSlot, &player.InventorySlot{ItemID: itemID, Quantity: 1})
if qty > 1 {
bs.Quantity--
g.AccountStore.SaveCharacter(p)
displayName := itemID
if def != nil {
displayName = def.Name
}
itemColor := g.itemColorize(sess, def, displayName)
sess.WriteLine(fmt.Sprintf("You withdraw a %s.", itemColor))
return
}
}
p.SetBankSlot(bankSlotIdx, nil)
g.AccountStore.SaveCharacter(p)
displayName := itemID
if def != nil {
displayName = def.Name
}
itemColor := g.itemColorize(sess, def, displayName)
if qty > 1 {
sess.WriteLine(fmt.Sprintf("You withdraw %d %ss.", qty, itemColor))
} else {
sess.WriteLine(fmt.Sprintf("You withdraw a %s.", itemColor))
}
}
func (g *Game) doBankDepositCredits(sess *net.Session, parts []string) {
p := sess.Player
if len(parts) < 3 {
sess.WriteLine("Usage: deposit credits <amount>")
return
}
amount, err := strconv.Atoi(parts[2])
if err != nil || amount <= 0 {
sess.WriteLine("Invalid amount.")
return
}
if p.Credits < amount {
sess.WriteLine("You don't have that many credits.")
return
}
p.Credits -= amount
p.BankedCredits += amount
g.AccountStore.SaveCharacter(p)
sess.WriteLine(g.colorize(sess, "credits_pickup", fmt.Sprintf("You deposit %s credits. (Banked: %s)", formatInt(amount), formatInt(p.BankedCredits))))
g.showBankBrowse(sess)
}
func (g *Game) doBankWithdrawCredits(sess *net.Session, parts []string) {
p := sess.Player
if len(parts) < 3 {
sess.WriteLine("Usage: withdraw credits <amount>")
return
}
amount, err := strconv.Atoi(parts[2])
if err != nil || amount <= 0 {
sess.WriteLine("Invalid amount.")
return
}
if p.BankedCredits < amount {
sess.WriteLine("You don't have that many banked credits.")
return
}
p.BankedCredits -= amount
p.Credits += amount
g.AccountStore.SaveCharacter(p)
sess.WriteLine(g.colorize(sess, "credits_pickup", fmt.Sprintf("You withdraw %s credits.", formatInt(amount))))
g.showBankBrowse(sess)
}
func (g *Game) canUseBank(p *player.Player) bool {
instances := g.World.FindObjInstances(p.RoomID, "bank booth")
return len(instances) > 0
}
func (g *Game) doBank(sess *net.Session) {
p := sess.Player
if !g.canUseBank(p) {
sess.WriteLine("There is no bank here.")
return
}
sess.State = net.StateBank
sess.WriteLine(g.colorize(sess, "dialog", "You access the bank terminal."))
g.showBankBrowse(sess)
g.writeBankPrompt(sess)
}
func (g *Game) leaveBank(sess *net.Session) {
sess.State = net.StateGame
g.writePrompt(sess)
}
|