aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_cook.go
blob: 5b7874581a3ae81eb14a28d0281318683d64ad28 (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
125
126
127
128
129
130
131
132
133
package game

import (
	"fmt"

	"thehouseoficarus/internal/action"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

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
	}

	allRecipes, err := g.RecipeStore.LoadAll()
	if err != nil {
		sess.WriteLine("Error loading recipes.")
		return
	}

	if input == "" {
		g.showCookMenu(sess, p, allRecipes, 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 []action.RecipeDef
	for _, r := range allRecipes {
		if r.Type != "cooking" {
			continue
		}
		if stationMatch(stationDefID, r.Station) && r.MatchesEntry(itemID) && r.HasAllItems(p.HasItem) {
			recipes = append(recipes, r)
		}
	}

	if len(recipes) == 0 {
		sess.WriteLine("You can't cook that.")
		return
	}

	if len(recipes) == 1 {
		g.promptHowMany(sess, recipes[0].ID)
		return
	}

	sess.PendingMenu = recipeMenuData(recipeDefIDs(recipes))
	sess.State = net.StateRecipeChoice
	var names []string
	for _, r := range recipes {
		names = append(names, g.recipeName(sess, &r))
	}
	g.showMenuTable(sess, fmt.Sprintf("What would you like to cook on the %s?", stationName), names)
}

func (g *Game) showCookMenu(sess *net.Session, p *player.Player, allRecipes []action.RecipeDef, stationDefID, stationName string) {
	seen := make(map[string]bool)
	var entries []recipeEntry

	for _, r := range allRecipes {
		if r.Type != "cooking" {
			continue
		}
		if !stationMatch(stationDefID, r.Station) {
			continue
		}
		if seen[r.ID] {
			continue
		}
		if !r.HasAllItems(p.HasItem) {
			continue
		}
		seen[r.ID] = true
		entries = append(entries, recipeEntry{g.recipeName(sess, &r), r})
	}

	if len(entries) == 0 {
		sess.WriteLine("You don't have anything you can cook.")
		return
	}

	if len(entries) == 1 {
		if p.OptionBool("cook_all") {
			g.startProductionFromRecipe(sess, p, &entries[0].Recipe, 0)
			return
		}
		g.promptHowMany(sess, entries[0].Recipe.ID)
		return
	}

	sess.State = net.StateRecipeChoice
	var names []string
	for _, e := range entries {
		names = append(names, e.ItemName)
	}
	g.showMenuTable(sess, fmt.Sprintf("What would you like to cook on the %s?", stationName), names)
	ids := make([]string, len(entries))
	for i, e := range entries {
		ids[i] = e.Recipe.ID
	}
	sess.PendingMenu = recipeMenuData(ids)
}

func (g *Game) recipeName(sess *net.Session, r *action.RecipeDef) string {
	if r.Output != "" {
		if def, err := g.ItemStore.Load(r.Output); err == nil {
			return g.itemColorize(sess, def, def.Name)
		}
	}
	return r.DisplayName()
}