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
|
package game
import (
"fmt"
"os"
"strconv"
"time"
"thehouseoficarus/internal/net"
)
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()
if !g.shutdownActive {
g.shutdownMu.Unlock()
sess.WriteLine("No shutdown in progress.")
return
}
close(g.shutdownCancel)
g.shutdownActive = false
g.shutdownMu.Unlock()
for _, other := range g.Hub.AllSessions() {
if other.Player != nil {
other.WriteLine("[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.shutdownActive {
g.shutdownMu.Unlock()
sess.WriteLine("A shutdown is already in progress.")
return
}
g.shutdownActive = true
g.shutdownCancel = make(chan struct{})
g.shutdownMu.Unlock()
go func() {
remaining := minutes * 60
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for _, other := range g.Hub.AllSessions() {
if other.Player != nil {
other.WriteLine(fmt.Sprintf("[Server] Shutting down in %d minute(s).", minutes))
}
}
for {
select {
case <-ticker.C:
remaining -= 30
if remaining <= 0 {
for _, other := range g.Hub.AllSessions() {
if other.Player != nil {
other.WriteLine("[Server] Shutting down NOW.")
}
}
os.Exit(0)
}
mins := remaining / 60
secs := remaining % 60
if secs == 0 {
for _, other := range g.Hub.AllSessions() {
if other.Player != nil {
other.WriteLine(fmt.Sprintf("[Server] Shutting down in %d minute(s).", mins))
}
}
} else {
for _, other := range g.Hub.AllSessions() {
if other.Player != nil {
other.WriteLine(fmt.Sprintf("[Server] Shutting down in %d minute(s) %d seconds.", mins, secs))
}
}
}
case <-g.shutdownCancel:
return
}
}
}()
sess.WriteLine(fmt.Sprintf("Shutdown scheduled in %d minute(s).", minutes))
}
|