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

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/item"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) executeCook(sess *net.Session, args []string, rawInput string) {
	g.doCook(sess, strings.Join(args, " "))
}

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

	stationDefID, stationName := g.findStation(p.RoomID, []string{"fire", "cooking_range"})
	if stationDefID == "" {
		sess.WriteLine("You need a fire or cooking range to cook.")
		return
	}

	items := g.CraftIndex.ByType("cooking")

	if input == "" {
		g.showCookMenu(sess, p, items, stationDefID, stationName)
		return
	}

	qty, itemName := parseQty(input)
	_ = qty

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

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

	itemID := matches[0].ID

	var recipes []*item.ItemDef
	for _, item := range items {
		c := item.FirstCraft()
		if stationMatch(stationDefID, c.Station) && craftMatchesEntry(c, itemID) && craftHasAllItems(c, p.HasItem) {
			recipes = append(recipes, item)
		}
	}

	title := fmt.Sprintf("What would you like to cook on the %s?", stationName)
	g.showRecipeChoice(sess, p, recipes, "You can't cook that.", title, "")
}

func (g *Game) showCookMenu(sess *net.Session, p *player.Player, items []*item.ItemDef, stationDefID, stationName string) {
	seen := make(map[string]bool)
	var recipes []*item.ItemDef

	for _, item := range items {
		c := item.FirstCraft()
		if !stationMatch(stationDefID, c.Station) {
			continue
		}
		if seen[item.ID] {
			continue
		}
		if !craftHasAllItems(c, p.HasItem) {
			continue
		}
		seen[item.ID] = true
		recipes = append(recipes, item)
	}

	title := fmt.Sprintf("What would you like to cook on the %s?", stationName)
	g.showRecipeChoice(sess, p, recipes, "You don't have anything you can cook.", title, "cook_all")
}