-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
168 lines (131 loc) · 3.01 KB
/
main.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"fmt"
"log/slog"
"os"
"os/exec"
"strings"
"time"
"github.com/miscord-dev/palog/pkg/palrcon"
)
var (
rconEndpoint = os.Getenv("RCON_ENDPOINT")
rconPassword = os.Getenv("RCON_PASSWORD")
intervalRaw = os.Getenv("INTERVAL")
interval time.Duration
timeoutRaw = os.Getenv("TIMEOUT")
timeout time.Duration
uconvLatin = os.Getenv("UCONV_LATIN") != "false"
)
func init() {
var err error
if timeoutRaw == "" {
timeoutRaw = "1s"
}
timeout, err = time.ParseDuration(timeoutRaw)
if err != nil {
slog.Error("failed to parse timeout", "error", err)
os.Exit(1)
}
if intervalRaw == "" {
intervalRaw = "5s"
}
interval, err = time.ParseDuration(intervalRaw)
if err != nil {
slog.Error("failed to parse interval", "error", err)
os.Exit(1)
}
}
func runUconvLatin(s string) string {
var out strings.Builder
cmd := exec.Command("uconv", "-x", "latin")
cmd.Stdin = strings.NewReader(s)
cmd.Stderr = os.Stderr
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
slog.Error("failed to run uconv", "error", err)
return s
}
return out.String()
}
func escapeString(s string) string {
if uconvLatin {
s = runUconvLatin(s)
}
s = strings.ReplaceAll(s, " ", "_")
s = strings.TrimSpace(s)
runes := []rune(s)
for i := range runes {
b := []byte(string(runes[i]))
if len(b) != 1 {
runes[i] = '*'
}
}
return string(runes)
}
func main() {
palRCON := palrcon.NewPalRCON(rconEndpoint, rconPassword)
palRCON.SetTimeout(timeout)
var prev map[string]palrcon.Player
makeMap := func(players []palrcon.Player) map[string]palrcon.Player {
m := make(map[string]palrcon.Player)
for _, player := range players {
if player.PlayerUID == "00000000" {
continue
}
m[player.PlayerUID] = player
}
return m
}
retriedBoarcast := func(message string) error {
message = escapeString(message)
var err error
for i := 0; i < 10; i++ {
err = palRCON.Broadcast(message)
if err != nil {
slog.Error("failed to broadcast", "error", err)
continue
}
return nil
}
return fmt.Errorf("failed to broadcast: %w", err)
}
for {
{
players, err := palRCON.GetPlayers()
if err != nil {
slog.Error("failed to get players", "error", err)
goto NEXT
}
slog.Debug("Current players", "players", players)
playersMap := makeMap(players)
if prev == nil {
prev = playersMap
goto NEXT
}
for _, player := range playersMap {
if _, ok := prev[player.PlayerUID]; !ok {
err := retriedBoarcast(fmt.Sprintf("joined:%s", player.Name))
if err != nil {
slog.Error("failed to broadcast", "error", err)
continue
}
slog.Info("Player joined", "player", player)
}
}
for _, player := range prev {
if _, ok := playersMap[player.PlayerUID]; !ok {
slog.Info("Player left", "player", player)
err := retriedBoarcast(fmt.Sprintf("left:%s", player.Name))
if err != nil {
slog.Error("failed to broadcast", "error", err)
}
}
}
prev = playersMap
}
NEXT:
time.Sleep(interval)
}
}