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

import (
	"fmt"
	"strings"
	"unicode/utf8"

	"thehouseoficarus/internal/color"
	"thehouseoficarus/internal/net"
	"thehouseoficarus/internal/player"
)

func (g *Game) executeSymbol(sess *net.Session, args []string, rawInput string) {
	p := sess.Player
	if len(args) == 0 {
		data, ok := p.MapSymbols[p.RoomID]
		if !ok {
			sess.WriteLine("No custom symbol set for this room.")
			return
		}
		msg := fmt.Sprintf("Custom symbol: %s", data.Char)
		if data.Color != "" {
			spec := color.Parse(data.Color)
			colored := color.Render(g.colorMode(sess), spec, data.Char)
			msg = fmt.Sprintf("Custom symbol: %s (%s)", colored, data.Color)
		}
		sess.WriteLine(msg)
		return
	}

	// ponytail: extract symbol char from rawInput to preserve case, since
	// handleGameCommand lowercases args before dispatch (same pattern as say).
	char := args[0]
	if idx := strings.Index(strings.ToLower(rawInput), "symbol"); idx >= 0 {
		rest := strings.TrimSpace(rawInput[idx+6:])
		if space := strings.Index(rest, " "); space >= 0 {
			char = rest[:space]
		} else {
			char = rest
		}
	}

	cleared := false
	if strings.EqualFold(char, "clear") || strings.EqualFold(char, "remove") {
		delete(p.MapSymbols, p.RoomID)
		if err := g.AccountStore.SaveCharacter(p); err != nil {
			sess.WriteLine("Error saving: " + err.Error())
			return
		}
		sess.WriteLine("Custom symbol cleared for this room.")
		cleared = true
	}

	if !cleared {
		r, size := utf8.DecodeRuneInString(char)
		if size == 0 || size != len(char) || r == utf8.RuneError {
			sess.WriteLine("Usage: symbol <single character> [color...]")
			return
		}

		colorSpec := ""
		if len(args) > 1 {
			colorSpec = strings.Join(args[1:], " ")
			if colorSpec != "" {
				spec := color.Parse(colorSpec)
				if spec.Empty() {
					sess.WriteLine("Invalid color spec. Use a color number like 232 and/or bold/dim/underline.")
					return
				}
			}
		}

		p.MapSymbols[p.RoomID] = player.MapSymbolData{
			Char:  char,
			Color: colorSpec,
		}

		if err := g.AccountStore.SaveCharacter(p); err != nil {
			sess.WriteLine("Error saving: " + err.Error())
			return
		}

		msg := fmt.Sprintf("Map symbol set to: %s", char)
		if colorSpec != "" {
			spec := color.Parse(colorSpec)
			colored := color.Render(g.colorMode(sess), spec, char)
			msg = fmt.Sprintf("Map symbol set to: %s", colored)
		}
		sess.WriteLine(msg)
	}
}