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

import (
	"fmt"
	"strings"

	"thehouseoficarus/internal/net"
)

func (g *Game) executeGlobal(sess *net.Session, args []string, rawInput string) {
	if len(args) == 0 {
		sess.WriteLine("Usage: global <message>  — sends a message to everyone on the world.")
		return
	}

	msgStart := strings.Index(strings.ToLower(rawInput), "global ") + 7
	if msgStart >= 7 && msgStart < len(rawInput) {
		g.doGlobal(sess, rawInput[msgStart:])
	} else {
		g.doGlobal(sess, strings.Join(args, " "))
	}
}

func (g *Game) doGlobal(sess *net.Session, msg string) {
	p := sess.Player

	for _, other := range g.Hub.AllSessions() {
		if other.Player == nil {
			continue
		}
		other.WriteLine(fmt.Sprintf("%s%s: %s",
			g.colorize(other, "global", "[Global] "),
			g.colorize(other, "player_name", p.Name),
			g.colorize(other, "global", msg)))
	}
}