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

import (
	"fmt"

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

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

	matches := g.findInventoryMatches(input, p)
	if len(matches) == 0 {
		sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", input))
		return
	}

	unique := uniqueItemNames(matches)
	if len(unique) > 1 {
		sess.WriteLine("Which one?")
		for _, name := range unique {
			sess.WriteLine(fmt.Sprintf("  - %s", name))
		}
		return
	}

	itemID := matches[0].ID
	slotIdx := matches[0].Slot
	slot := p.InvSlot(slotIdx)
	def, _ := g.ItemStore.Load(itemID)

	qty := 1
	name := itemID
	if def != nil {
		name = def.Name
	}

	if slot.Quantity > qty {
		slot.Quantity -= qty
	} else {
		p.SetInvSlot(slotIdx, nil)
	}

	g.World.AddGroundItem(p.RoomID, itemID, qty)
	g.AccountStore.SaveCharacter(p)
	sess.WriteLine(fmt.Sprintf("You drop a %s.", name))
}