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

import (
	"fmt"
	"strings"

	"thirdcollapse/internal/net"
	"thirdcollapse/internal/object"
	"thirdcollapse/internal/player"
)

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

	lower := strings.ToLower(strings.TrimSpace(input))
	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
	if def, err := g.ItemStore.Load(foundItemID); err == nil {
		name = def.Name
	}
	sess.WriteLine(fmt.Sprintf("You remove %s.", name))
}