From d6ab0e93a64525ce34f89e26d5272efbac513c32 Mon Sep 17 00:00:00 2001 From: historia <[not public]> Date: Thu, 11 Jun 2026 20:41:06 -0400 Subject: feat: big map and options changes --- internal/game/cmd_option.go | 116 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 internal/game/cmd_option.go (limited to 'internal/game/cmd_option.go') diff --git a/internal/game/cmd_option.go b/internal/game/cmd_option.go new file mode 100644 index 0000000..49085db --- /dev/null +++ b/internal/game/cmd_option.go @@ -0,0 +1,116 @@ +package game + +import ( + "fmt" + "strconv" + "strings" + + "thirdcollapse/internal/net" + "thirdcollapse/internal/player" +) + +func (g *Game) doOption(sess *net.Session, input string) { + p := sess.Player.(*player.Player) + + if input == "" { + sess.WriteLine("") + sess.WriteLine(fmt.Sprintf(" %-14s %-10s %-22s %s", "Option", "Value", "Valid", "Description")) + for _, def := range player.OptionDefs { + val := formatOptionValue(p, &def) + valid := formatValidValues(&def) + sess.WriteLine(fmt.Sprintf(" %-14s %-10s %-22s %s", def.Name, val, valid, def.Description)) + } + return + } + + parts := strings.Fields(input) + name := strings.ToLower(parts[0]) + def := player.GetOptionDef(name) + if def == nil { + sess.WriteLine(fmt.Sprintf("\nUnknown option: %s", name)) + return + } + + if len(parts) == 1 { + val := formatOptionValue(p, def) + valid := formatValidValues(def) + sess.WriteLine(fmt.Sprintf("\n%s = %s [%s]", def.Name, val, valid)) + sess.WriteLine(fmt.Sprintf(" %s", def.Description)) + return + } + + value := strings.ToLower(parts[1]) + parsed, ok := parseOptionValue(def, value) + if !ok { + valid := formatValidValues(def) + sess.WriteLine(fmt.Sprintf("\nInvalid value for %s: %s [%s]", def.Name, value, valid)) + return + } + + if p.Options == nil { + p.Options = make(map[string]any) + } + p.Options[def.Name] = parsed + g.AccountStore.SaveCharacter(p) + sess.WriteLine(fmt.Sprintf("\n%s set to %s.", def.Name, formatOptionValue(p, def))) +} + +func formatOptionValue(p *player.Player, def *player.OptionDef) string { + switch def.Type { + case player.OptBool: + if p.OptionBool(def.Name) { + return "on" + } + return "off" + case player.OptString: + return p.OptionString(def.Name) + case player.OptInt: + return strconv.Itoa(p.OptionInt(def.Name)) + } + return "" +} + +func formatValidValues(def *player.OptionDef) string { + switch def.Type { + case player.OptBool: + return "on/off" + case player.OptString: + if len(def.ValidValues) > 0 { + return strings.Join(def.ValidValues, "/") + } + return "string" + case player.OptInt: + return "num" + } + return "" +} + +func parseOptionValue(def *player.OptionDef, input string) (any, bool) { + switch def.Type { + case player.OptBool: + switch input { + case "on", "true", "yes", "1": + return true, true + case "off", "false", "no", "0": + return false, true + } + return nil, false + case player.OptString: + if len(def.ValidValues) == 0 { + return input, true + } + for _, v := range def.ValidValues { + if strings.EqualFold(v, input) { + return v, true + } + } + return nil, false + case player.OptInt: + n, err := strconv.Atoi(input) + if err != nil { + return nil, false + } + return n, true + } + return nil, false +} -- cgit v1.2.3