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
|
package game
// The shutdown command schedules a graceful server shutdown. Signals and
// this command converge on Game.RequestShutdown, so both paths drain work,
// resolve outstanding wagers, and save owned state before transports close.
import (
"fmt"
"strconv"
"time"
"thehouseoficarus/internal/net"
)
// scheduledShutdown carries one countdown run's captured cancel channel, so
// a later run (or cancel) can never close or read a replacement run's
// channel.
type scheduledShutdown struct {
cancel chan struct{}
}
func (g *Game) executeShutdown(sess *net.Session, args []string, rawInput string) {
if !g.checkAdmin(sess) {
sess.WriteLine("Unknown command.")
return
}
if len(args) == 0 {
sess.WriteLine("Usage: shutdown <minutes> or shutdown cancel")
return
}
if args[0] == "cancel" {
g.shutdownMu.Lock()
run := g.shutdownRun
g.shutdownMu.Unlock()
if run == nil {
sess.WriteLine("No shutdown in progress.")
return
}
close(run.cancel)
g.shutdownMu.Lock()
g.shutdownRun = nil
g.shutdownMu.Unlock()
g.broadcastLoggedIn("[Server] Shutdown cancelled.")
return
}
minutes, err := strconv.Atoi(args[0])
if err != nil || minutes <= 0 {
sess.WriteLine("Invalid minutes.")
return
}
g.shutdownMu.Lock()
if g.shutdownRun != nil {
g.shutdownMu.Unlock()
sess.WriteLine("A shutdown is already in progress.")
return
}
run := &scheduledShutdown{cancel: make(chan struct{})}
g.shutdownRun = run
g.shutdownMu.Unlock()
go g.runShutdownCountdown(run, minutes)
sess.WriteLine(fmt.Sprintf("Shutdown scheduled in %d minute(s).", minutes))
}
// runShutdownCountdown announces the countdown and requests the shutdown
// at expiry. Cancellation is handled through the run's captured channel;
// nothing reads a replaceable shared field here.
func (g *Game) runShutdownCountdown(run *scheduledShutdown, minutes int) {
remaining := minutes * 60
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
g.broadcastLoggedIn(fmt.Sprintf("[Server] Shutting down in %d minute(s).", minutes))
for {
select {
case <-ticker.C:
remaining -= 30
if remaining <= 0 {
g.broadcastLoggedIn("[Server] Shutting down NOW.")
if g.shutdownHook != nil {
g.shutdownHook("scheduled shutdown")
}
return
}
mins := remaining / 60
secs := remaining % 60
if secs == 0 {
g.broadcastLoggedIn(fmt.Sprintf("[Server] Shutting down in %d minute(s).", mins))
} else {
g.broadcastLoggedIn(fmt.Sprintf("[Server] Shutting down in %d minute(s) %d seconds.", mins, secs))
}
case <-run.cancel:
return
}
}
}
// broadcastLoggedIn writes one line to every logged-in player. Read-only
// over Hub membership (mutex-protected); writes go through outbound queues.
func (g *Game) broadcastLoggedIn(msg string) {
if g.Hub == nil {
return
}
for _, other := range g.Hub.AllSessions() {
if other.Player != nil {
other.WriteLine(msg)
}
}
}
|