diff options
Diffstat (limited to 'internal/game/action_production.go')
| -rw-r--r-- | internal/game/action_production.go | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/internal/game/action_production.go b/internal/game/action_production.go index 0b8ca45..d4e2274 100644 --- a/internal/game/action_production.go +++ b/internal/game/action_production.go @@ -41,6 +41,58 @@ func (g *Game) promptHowMany(sess *net.Session, recipeID string) { sess.Write("How many (return for all)?: ") } +type recipeSelection struct { + Recipe *action.RecipeDef + Count int +} + +func (g *Game) resolveRecipeByInput(input string, recipes []action.RecipeDef, lastRecipeID string) (sel recipeSelection, errMsg string) { + if input == "" { + if lastRecipeID == "" { + return sel, "never_mind" + } + for i := range recipes { + if recipes[i].ID == lastRecipeID { + sel.Recipe = &recipes[i] + return sel, "" + } + } + return sel, "never_mind" + } + + qty, productName := parseQty(input) + productName = strings.TrimSpace(productName) + + if idx, err := strconv.Atoi(productName); err == nil && idx > 0 && idx <= len(recipes) { + sel.Recipe = &recipes[idx-1] + sel.Count = qty + return sel, "" + } + + matchCount := 0 + for i := range recipes { + outDef, _ := g.ItemStore.Load(recipes[i].Output) + name := recipes[i].Output + if outDef != nil { + name = outDef.Name + } + if world.WordPrefixMatch(productName, name) { + if matchCount == 0 { + sel.Recipe = &recipes[i] + sel.Count = qty + } + matchCount++ + } + } + if matchCount > 1 { + return sel, "ambiguous" + } + if sel.Recipe == nil { + return sel, "never_mind" + } + return sel, "" +} + func (g *Game) showMenuTable(sess *net.Session, title string, names []string) { p := sess.Player.(*player.Player) tbl := &Table{Title: title} @@ -129,6 +181,7 @@ func (g *Game) buildCombineRecipe(def *object.ItemDef) *action.RecipeDef { func (g *Game) startProduction(sess *net.Session, p *player.Player, recipe *action.RecipeDef, actionType, displayVerb, startMsg, endMsg string, count int) { g.CancelAction(p) + g.CancelBackgroundAction(p) if recipe.Skill != "" { skillLevel := p.Level(player.SkillName(recipe.Skill)) |
