-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathonlineCount.go
144 lines (120 loc) · 2.8 KB
/
onlineCount.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
package main
import (
"log"
"net"
"sync"
"time"
"github.com/PaulSonOfLars/gotgbot/v2"
)
const (
OfflineCheckInterval = time.Second * 2 // Частая проверка когда офлайн
OnlineCheckInterval = time.Minute * 5 // Редкая проверка когда онлайн но пусто
)
var (
onlinePlayers = struct {
sync.RWMutex
players map[string]bool
}{
players: make(map[string]bool),
}
serverStatus struct {
sync.RWMutex
isOnline bool
lastCheck time.Time
}
)
func addPlayer(nickname string) {
onlinePlayers.Lock()
onlinePlayers.players[nickname] = true
onlinePlayers.Unlock()
}
func removePlayer(nickname string) {
onlinePlayers.Lock()
delete(onlinePlayers.players, nickname)
if len(onlinePlayers.players) == 0 {
go updateServerStatus()
}
onlinePlayers.Unlock()
}
func getOnlinePlayers() []string {
onlinePlayers.RLock()
defer onlinePlayers.RUnlock()
players := make([]string, 0, len(onlinePlayers.players))
for player := range onlinePlayers.players {
players = append(players, player)
}
return players
}
///////////////////////////////////////////////////////////////////////////////
func updateOnlineMessage() {
if cfg.OnlineMessageID == 0 {
return
}
players := getOnlinePlayers()
var msg string
if len(players) == 0 {
msg = "Online: 0"
} else {
msg = "Online: "
for i, player := range players {
if i > 0 {
msg += ", "
}
msg += player
}
}
if !serverStatus.isOnline {
msg = "Offline"
}
// TODO 20 msg per minute in groups?
_, _, err := bot.EditMessageText(msg, &gotgbot.EditMessageTextOpts{
ChatId: cfg.OnlineMessageChatID,
MessageId: cfg.OnlineMessageID,
})
if err != nil {
log.Printf("Failed to update online message: %v", err)
}
}
///////////////////////////////////////////////////////////////////////////////
func updateServerStatus() {
dialer := net.Dialer{
Timeout: DialTimeout,
LocalAddr: &net.TCPAddr{
IP: net.ParseIP(ProxyBind),
},
}
currentStatus := false
conn, err := dialer.Dial("tcp", cfg.MinecraftServer)
if err == nil {
conn.Close()
currentStatus = true
}
serverStatus.Lock()
defer serverStatus.Unlock()
if serverStatus.isOnline != currentStatus {
serverStatus.isOnline = currentStatus
serverStatus.lastCheck = time.Now()
if currentStatus {
log.Println("Server is now ONLINE")
bot.SendMessage(cfg.AdminID, "🟢 Server is online.", nil)
} else {
log.Println("Server is now OFFLINE")
bot.SendMessage(cfg.AdminID, "🔴 Server is now OFFLINE!", nil)
}
updateOnlineMessage()
}
}
func startServerStatusChecker() {
ticker := time.NewTicker(OfflineCheckInterval)
defer ticker.Stop()
for {
if serverStatus.isOnline {
if time.Since(serverStatus.lastCheck) > OnlineCheckInterval {
updateServerStatus()
}
} else {
updateServerStatus()
}
<-ticker.C
}
}