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

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/net"
)

func (g *Game) executeSearch(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	g.cancelAction(p)
	if len(args) == 0 {
		sess.WriteLine("Search what?")
	} else {
		g.doSearch(sess, strings.Join(args, " "))
	}
}

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

	lower := strings.ToLower(strings.TrimSpace(input))
	if lower == "" {
		sess.WriteLine("Search what?")
		return
	}

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

	unique := uniqueItemNames(matches)
	if len(unique) > 1 {
		g.showWhichOne(sess, matches)
		return
	}

	itemID := matches[0].ID
	g.doSearchItem(sess, p, itemID)
}