aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_cook.go
blob: ce33ccd68bfad1c176faf4728284bc33e49387c3 (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
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.(*player.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 {
		sess.WriteLine("Which one?")
		for _, name := range unique {
			sess.WriteLine(fmt.Sprintf("  - %s", name))
		}
		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(recipes)
	sess.State = net.StateRecipeChoice
	sess.WriteLine(fmt.Sprintf("\nWhat would you like to cook on the %s?", stationName))
	for i, r := range recipes {
		sess.WriteLine(fmt.Sprintf("  %d) %s", i+1, g.recipeName(sess, &r)))
	}
	sess.WriteLine(fmt.Sprintf("  %d) Nothing", len(recipes)+1))
}

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 {
		g.promptHowMany(sess, entries[0].Recipe.ID)
		return
	}

	sess.State = net.StateRecipeChoice
	sess.WriteLine(fmt.Sprintf("\nWhat would you like to cook on the %s?", stationName))
	for i, e := range entries {
		sess.WriteLine(fmt.Sprintf("  %d) %s", i+1, e.ItemName))
	}
	sess.WriteLine(fmt.Sprintf("  %d) Nothing", len(entries)+1))
	sess.PendingMenu = entryMenuData(entries)
}

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()
}