aboutsummaryrefslogtreecommitdiff
path: root/internal/game/cmd_mix.go
blob: 8fc600b8b6c9f239200369d1e50305615fbca838 (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
package game

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

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

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

	if input == "" {
		g.showMixMenu(sess, p, allRecipes)
		return
	}

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

	var matched []action.RecipeDef
	for _, r := range allRecipes {
		if r.Type != "pharmacy" {
			continue
		}
		outDef, _ := g.ItemStore.Load(r.Output)
		name := r.Output
		if outDef != nil {
			name = outDef.Name
		}
		if action.WordPrefixMatch(itemName, name) {
			matched = append(matched, r)
		}
	}

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

	var available []action.RecipeDef
	for _, r := range matched {
		if r.HasAllItems(p.HasItem) && p.Level(player.Pharmacy) >= r.Level {
			available = append(available, r)
		}
	}

	if len(available) == 0 {
		sess.WriteLine("You don't have the materials for that.")
		return
	}

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

	sess.State = net.StateRecipeChoice
	var names []string
	for _, r := range available {
		names = append(names, g.recipeName(sess, &r))
	}
	g.showMenuTable(sess, "What would you like to mix?", names)
	sess.PendingMenu = recipeMenuData(recipeDefIDs(available))
}

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

	for _, r := range allRecipes {
		if r.Type != "pharmacy" {
			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 mix.")
		return
	}

	if len(entries) == 1 {
		if p.OptionBool("mix_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, "What would you like to mix?", names)
	ids := make([]string, len(entries))
	for i, e := range entries {
		ids[i] = e.Recipe.ID
	}
	sess.PendingMenu = recipeMenuData(ids)
}