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

import (
	"fmt"

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

func (g *Game) doSmelt(sess *net.Session, input string) {
	p := sess.Player.(*player.Player)
	g.CancelAction(p)

	stationDefID, stationName := g.findStation(p.RoomID, []string{"furnace"})
	if stationDefID == "" {
		sess.WriteLine("You need a furnace to smelt.")
		return
	}

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

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

	matches := g.findInventoryMatches(input, p)
	if len(matches) == 0 {
		sess.WriteLine(fmt.Sprintf("You don't have any '%s'.", input))
		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 != "smelting" || !stationMatch(stationDefID, r.Station) || !r.MatchesEntry(itemID) {
			continue
		}
		if !r.HasAllItemsQty(p.CountItem) || p.Level(player.SkillName(r.Skill)) < r.Level {
			continue
		}
		recipes = append(recipes, r)
	}

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

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

	sess.PendingMenu = recipeMenuData(recipes)
	sess.State = net.StateRecipeChoice
	sess.WriteLine("\nWhat would you like to smelt?")
	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) showSmeltMenu(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 != "smelting" || !stationMatch(stationDefID, r.Station) {
			continue
		}
		if seen[r.ID] || !r.HasAllItemsQty(p.CountItem) || p.Level(player.SkillName(r.Skill)) < r.Level {
			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 smelt.")
		return
	}

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

	sess.State = net.StateRecipeChoice
	sess.WriteLine("\nWhat would you like to smelt?")
	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)
}