diff options
| author | historia <[not public]> | 2026-06-20 03:20:01 -0400 |
|---|---|---|
| committer | historia <[not public]> | 2026-06-20 03:20:01 -0400 |
| commit | e4400c5f1e84c18d2126f2cb502ae10685977cbb (patch) | |
| tree | 0523e9d68f1cfdc6f81b862e3068fe12c1a85054 /internal/game/core_production.go | |
| parent | 5ea082ab4e82409aebc889b496f43e52b003d4f7 (diff) | |
| download | thehouseoficarus-e4400c5f1e84c18d2126f2cb502ae10685977cbb.tar.gz | |
refactor: combined all station crafting
Diffstat (limited to 'internal/game/core_production.go')
| -rw-r--r-- | internal/game/core_production.go | 142 |
1 files changed, 75 insertions, 67 deletions
diff --git a/internal/game/core_production.go b/internal/game/core_production.go index 227c4ad..59517d5 100644 --- a/internal/game/core_production.go +++ b/internal/game/core_production.go @@ -18,7 +18,7 @@ import ( 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) + g.cancelBgAction(p) skill := recipe.EffectiveSkill() if skill != "" { @@ -65,13 +65,7 @@ func (g *Game) startProduction(sess *net.Session, p *player.Player, recipe *acti p.ActionState = &ActionState{Type: ActionProducing, TargetName: outputName, Verb: displayVerb} - if g.Hub != nil { - for _, other := range g.Hub.PlayersInRoom(p.RoomID) { - if other != sess && other.Player != nil { - other.WriteLine(g.colorize(other, "broadcast", fmt.Sprintf("\n%s starts %s.", p.Name, displayVerb))) - } - } - } + g.broadcastAction(sess, "\n%s starts %s.", p.Name, displayVerb) } func (g *Game) startProductionFromRecipe(sess *net.Session, p *player.Player, recipe *action.RecipeDef, count int) { @@ -106,14 +100,9 @@ func (g *Game) startProductionFromRecipe(sess *net.Session, p *player.Player, re g.startProduction(sess, p, recipe, actionType, displayVerb, startMsg, endMsg, count) } -func (g *Game) loadProductionRecipe(recipeID string) *action.RecipeDef { - all, err := g.RecipeStore.LoadAll() - if err == nil { - for _, r := range all { - if r.ID == recipeID { - return &r - } - } +func (g *Game) loadRecipe(recipeID string) *action.RecipeDef { + if r := g.RecipeStore.GetByID(recipeID); r != nil { + return r } if strings.HasPrefix(recipeID, "combine_") { itemID := strings.TrimPrefix(recipeID, "combine_") @@ -132,7 +121,7 @@ func (g *Game) advanceProduction(sess *net.Session, p *player.Player) bool { endMsg, _ := p.Action.Data["end_msg"].(string) remaining, _ := p.Action.Data["remaining"].(int) - recipe := g.loadProductionRecipe(recipeID) + recipe := g.loadRecipe(recipeID) if recipe == nil { g.cancelAction(p) return false @@ -350,25 +339,19 @@ func (g *Game) handleRecipeChoice(sess *net.Session, input string) { func (g *Game) menuEntryName(entry map[string]string) string { if rid, ok := entry["recipe_id"]; ok { - all, _ := g.RecipeStore.LoadAll() - for _, r := range all { - if r.ID == rid { - if def, err := g.ItemStore.Load(r.Output); err == nil { - return def.Name - } - return r.Output + if r := g.RecipeStore.GetByID(rid); r != nil { + if def, err := g.ItemStore.Load(r.Output); err == nil { + return def.Name } + return r.Output } } if rid, ok := entry["fletch_recipe_id"]; ok { - all, _ := g.RecipeStore.LoadAll() - for _, r := range all { - if r.ID == rid { - if def, err := g.ItemStore.Load(r.Output); err == nil { - return def.Name - } - return r.Output + if r := g.RecipeStore.GetByID(rid); r != nil { + if def, err := g.ItemStore.Load(r.Output); err == nil { + return def.Name } + return r.Output } } if barID, ok := entry["bar_id"]; ok { @@ -399,12 +382,9 @@ func (g *Game) dispatchMenuEntry(sess *net.Session, entry map[string]string) { } if rid, ok := entry["fletch_recipe_id"]; ok { p := sess.Player - allRecipes, _ := g.RecipeStore.LoadAll() - for _, r := range allRecipes { - if r.ID == rid { - g.startFletchAction(sess, p, &r, 0) - return - } + if r := g.RecipeStore.GetByID(rid); r != nil { + g.startFletchAction(sess, p, r, 0) + return } g.reprompt(sess) return @@ -416,7 +396,7 @@ func (g *Game) dispatchMenuEntry(sess *net.Session, entry map[string]string) { g.reprompt(sess) } -func (g *Game) formatRecipeMaterials(r *action.RecipeDef) string { +func (g *Game) formatMaterials(r *action.RecipeDef) string { var parts []string for _, e := range r.Consume { itemName := e.Items[0] @@ -461,7 +441,7 @@ func (g *Game) showProductionTable(sess *net.Session, p *player.Player, recipes productName = fmt.Sprintf("%s x%d", productName, outputQty) } - matStr := g.formatRecipeMaterials(&r) + matStr := g.formatMaterials(&r) levelStr := fmt.Sprint(r.Level) numStr := fmt.Sprintf("%d", i+1) @@ -524,20 +504,11 @@ func (g *Game) handleProductChoice(sess *net.Session, input string) { input = strings.TrimSpace(input) - allRecipes, err := g.RecipeStore.LoadAll() - if err != nil { - g.reprompt(sess) - return - } - var recipes []action.RecipeDef for _, entry := range menuData { rid := entry["recipe_id"] - for _, r := range allRecipes { - if r.ID == rid { - recipes = append(recipes, r) - break - } + if r := g.RecipeStore.GetByID(rid); r != nil { + recipes = append(recipes, *r) } } @@ -587,10 +558,15 @@ func (g *Game) handleProductChoice(sess *net.Session, input string) { } } -func (g *Game) hasToolType(p *player.Player, toolType string) bool { +func (g *Game) findTool(p *player.Player, toolTypes []string) (toolSpeed float64, toolName string, found bool) { + toolSpeed = -1 if itemID, ok := p.Equipment[object.SlotMainHand]; ok { - if def, err := g.ItemStore.Load(itemID); err == nil && def.ToolType == toolType { - return true + if def, err := g.ItemStore.Load(itemID); err == nil { + for _, t := range toolTypes { + if def.ToolType == t { + return def.ToolSpeed, def.Name, true + } + } } } for i := 0; i < 28; i++ { @@ -598,11 +574,22 @@ func (g *Game) hasToolType(p *player.Player, toolType string) bool { if slot == nil { continue } - if def, err := g.ItemStore.Load(slot.ItemID); err == nil && def.ToolType == toolType { - return true + def, err := g.ItemStore.Load(slot.ItemID) + if err != nil { + continue + } + for _, t := range toolTypes { + if def.ToolType == t { + return def.ToolSpeed, def.Name, true + } } } - return false + return -1, "", false +} + +func (g *Game) hasToolType(p *player.Player, toolType string) bool { + _, _, found := g.findTool(p, []string{toolType}) + return found } type recipeEntry struct { ItemName string @@ -729,6 +716,37 @@ func (g *Game) showMenuTable(sess *net.Session, title string, names []string) { } } +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() +} + +func (g *Game) showRecipeChoice(sess *net.Session, p *player.Player, recipes []action.RecipeDef, emptyMsg, title string, autoOption string) { + if len(recipes) == 0 { + sess.WriteLine(emptyMsg) + return + } + if len(recipes) == 1 { + if autoOption != "" && p.OptionBool(autoOption) { + g.startProductionFromRecipe(sess, p, &recipes[0], 0) + return + } + g.promptHowMany(sess, recipes[0].ID) + return + } + sess.State = net.StateRecipeChoice + sess.PendingMenu = recipeMenuData(recipeDefIDs(recipes)) + var names []string + for i := range recipes { + names = append(names, g.recipeName(sess, &recipes[i])) + } + g.showMenuTable(sess, title, names) +} + func (g *Game) handleHowMany(sess *net.Session, input string) { p := sess.Player recipeID := sess.PendingRecipeID @@ -758,17 +776,7 @@ func (g *Game) handleHowMany(sess *net.Session, input string) { } recipe = g.buildCombineRecipe(itemDef) } else { - all, err := g.RecipeStore.LoadAll() - if err != nil { - g.reprompt(sess) - return - } - for _, r := range all { - if r.ID == recipeID { - recipe = &r - break - } - } + recipe = g.RecipeStore.GetByID(recipeID) } if recipe == nil { |
