This repository has been archived by the owner on Oct 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
lobby.go
174 lines (161 loc) · 4.02 KB
/
lobby.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
169
170
171
172
173
174
package main
import (
"bufio"
"bytes"
"log"
"net/http"
"os"
"regexp"
"strings"
"sync"
"time"
"github.com/maxsupermanhd/go-wz/lobby"
)
const (
lobbyHistoryMax = 50
)
type LobbyRoomPretty struct {
GameID uint32
GameName string
MapName string
HostName string
Version string
Private bool
Pure bool
MaxPlayers uint32
CurrentPlayers uint32
LastSeen int64
History bool
}
func lobbyRoomPrettyfy(room lobby.LobbyRoom) LobbyRoomPretty {
return LobbyRoomPretty{
room.GameID,
string(room.GameName[:bytes.IndexByte(room.GameName[:], 0)]),
string(room.MapName[:bytes.IndexByte(room.MapName[:], 0)]),
string(room.HostName[:bytes.IndexByte(room.HostName[:], 0)]),
string(room.Version[:bytes.IndexByte(room.Version[:], 0)]),
btoi(room.Private),
btoi(room.Pure),
room.MaxPlayers,
room.CurrentPlayers,
time.Now().Unix(),
false,
}
}
type LobbyResponsePretty struct {
lobby.LobbyResponse
prettyRooms []LobbyRoomPretty
}
func lobbyLookup() (ret LobbyResponsePretty) {
lookup, err := lobby.LobbyLookup()
ret = LobbyResponsePretty{
LobbyResponse: lookup,
prettyRooms: []LobbyRoomPretty{},
}
if err != nil {
log.Printf("Error reading lobby: %s", err)
return
}
for _, v := range lookup.Rooms {
if lobbyIgnores(string(v.HostIP[:bytes.IndexByte(v.HostIP[:], 0)])) {
continue
}
ret.prettyRooms = append(ret.prettyRooms, lobbyRoomPrettyfy(v))
}
return
}
func lobbyPoller() {
lobbyHistory := []LobbyRoomPretty{}
previousLookup := []LobbyRoomPretty{}
for {
lookup := lobbyLookup()
for _, vv := range previousLookup {
found := false
for _, v := range lookup.prettyRooms {
if v.GameID == vv.GameID {
found = true
break
}
}
if !found {
vv.History = true
lobbyHistory = append([]LobbyRoomPretty{vv}, lobbyHistory...)
}
}
if len(lobbyHistory) > lobbyHistoryMax {
lobbyHistory = lobbyHistory[:lobbyHistoryMax]
}
previousLookup = lookup.prettyRooms
LobbyWSHub.clientsLock.Lock()
watchers := len(LobbyWSHub.clients)
LobbyWSHub.clientsLock.Unlock()
WSLobbyUpdateLobby(map[string]any{
"Rooms": append(lookup.prettyRooms, lobbyHistory...),
"MOTD": lookup.MOTD,
"Watching": watchers,
})
time.Sleep(1 * time.Second)
}
}
var (
lobbyLastRequest = map[string]time.Time{}
lobbyLastRequestLock sync.Mutex
lobbyTooManyRequestsTime = int64(7) // seconds
)
func lobbyHandler(w http.ResponseWriter, r *http.Request) {
u := sessionGetUsername(r)
if len(u) > 0 && u != "Flex seal" {
lobbyLastRequestLock.Lock()
l, ok := lobbyLastRequest[u]
lobbyLastRequest[u] = time.Now()
if ok {
if l.Unix()+lobbyTooManyRequestsTime > time.Now().Unix() {
basicLayoutLookupRespond("plainmsg", w, r, map[string]any{"msg": "Too many requests. Please do not spam page refresh. Authorized accounts have automatic lobby refresh every second.", "msgred": true})
lobbyLastRequestLock.Unlock()
return
}
}
lobbyLastRequestLock.Unlock()
}
// s, reqres := RequestHosters()
// var rooms []any
// if s {
// json.Unmarshal([]byte(reqres), &rooms)
// }
// basicLayoutLookupRespond("lobby", w, r, map[string]any{"Lobby": LobbyLookup(), "Hoster": rooms})
lr := lobbyLookup()
basicLayoutLookupRespond("lobby", w, r, map[string]any{"Lobby": map[string]any{
"Rooms": lr.prettyRooms,
"MOTD": lr.MOTD,
}})
}
func lobbyIgnores(ip string) bool {
for _, i := range lobbyIgnoreIPS {
if i.MatchString(ip) {
log.Printf("Lobby ignores %q because %q", ip, i.String())
return true
}
}
return false
}
var lobbyIgnoreIPS []*regexp.Regexp
func loadLobbyIgnores(path string) error {
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
fi, fn, ok := strings.Cut(scanner.Text(), " ")
if !ok || fi == "" {
continue
}
r, err := regexp.Compile(fi)
if err != nil {
log.Printf("Failed to compile regex %q (%s): %s", fi, fn, err)
}
lobbyIgnoreIPS = append(lobbyIgnoreIPS, r)
}
return scanner.Err()
}