aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_remove.go
blob: 8f1a413ca74222ae80a4629e62ec41ee8775b9db (plain)
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
package game

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/object"
	"thehouseoficarus/internal/player"
)

func (g *Game) doRemove(sess *net.Session, input string) {
	p := sess.Player.(*player.Player)

	lower := strings.ToLower(strings.TrimSpace(input))
	if lower == "all" {
		g.doRemoveAll(sess)
		return
	}
	if lower == "" {
		sess.WriteLine("Remove what?")
		return
	}

	g.CancelAction(p)

	var foundSlot object.EquipSlot
	var foundItemID string

	for _, slot := range EquipSlots {
		itemID, ok := p.Equipment[slot]
		if !ok {
			continue
		}
		if strings.ToLower(string(slot)) == lower {
			foundSlot = slot
			foundItemID = itemID
			break
		}
		def, err := g.ItemStore.Load(itemID)
		if err != nil {
			continue
		}
		if def.MatchesName(input) {
			foundSlot = slot
			foundItemID = itemID
			break
		}
	}

	if foundSlot == "" {
		sess.WriteLine(fmt.Sprintf("You aren't wearing any '%s'.", input))
		return
	}

	freeSlot := p.FirstFreeSlot()
	if freeSlot == -1 {
		sess.WriteLine("Nowhere to put that!")
		return
	}

	delete(p.Equipment, foundSlot)
	invSlot := &player.InventorySlot{ItemID: foundItemID, Quantity: 1}
	if q, ok := p.EquipQuality[foundItemID]; ok {
		invSlot.Quality = q
		if def, _ := g.ItemStore.Load(foundItemID); def != nil {
			invSlot.MaxQuality = def.MaxQuality
		}
		delete(p.EquipQuality, foundItemID)
	}
	p.SetInvSlot(freeSlot, invSlot)
	g.AccountStore.SaveCharacter(p)

	name := foundItemID
	def, _ := g.ItemStore.Load(foundItemID)
	if def != nil {
		name = def.Name
	}
	sess.WriteLine(fmt.Sprintf("You remove %s.", g.itemColorize(sess, def, name)))
}

func (g *Game) doRemoveAll(sess *net.Session) {
	p := sess.Player.(*player.Player)

	if len(p.Equipment) == 0 {
		sess.WriteLine("You aren't wearing anything.")
		return
	}

	if len(p.Equipment) > p.FreeSlots() {
		sess.WriteLine("You don't have room to remove everything!")
		return
	}

	g.CancelAction(p)

	for _, slot := range EquipSlots {
		itemID, ok := p.Equipment[slot]
		if !ok {
			continue
		}

		freeSlot := p.FirstFreeSlot()
		delete(p.Equipment, slot)
		invSlot := &player.InventorySlot{ItemID: itemID, Quantity: 1}
		if q, ok := p.EquipQuality[itemID]; ok {
			invSlot.Quality = q
			if def, _ := g.ItemStore.Load(itemID); def != nil {
				invSlot.MaxQuality = def.MaxQuality
			}
			delete(p.EquipQuality, itemID)
		}
		p.SetInvSlot(freeSlot, invSlot)

		name := itemID
		def, _ := g.ItemStore.Load(itemID)
		if def != nil {
			name = def.Name
		}
		sess.WriteLine(fmt.Sprintf("You remove %s.", g.itemColorize(sess, def, name)))
	}

	g.AccountStore.SaveCharacter(p)
}