-
Notifications
You must be signed in to change notification settings - Fork 0
/
source.py
256 lines (204 loc) · 6.99 KB
/
source.py
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# Copyright (c) 2019 Adam Dodd
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import socket
import struct
import string
import time
import sys
import sqlite3
import webbrowser
import surf
class SourceServer(object):
"""Base class for Valve Source engine game servers"""
def __init__(self, sID, nick, addr, port):
self._sID = sID
self.nick = nick
self.addr = addr
self.port = port
# Internal variables to determine status
self._pinged = False
self._online = bool()
self._latency = int()
# Variables from ping
self._header = int()
self._protocol = int()
self._name = str()
self._map = str()
self._folder = str()
self._game = str()
self._gameID = int()
self._players = int()
self._max_players = int()
self._bots = int()
self._type = str()
self._env = str()
self._vis = bool()
self._vac = bool()
def __str__(self):
return ("SourceServer:\n-> nick : \"%s\"\n-> addr : \"%s\"\n" + \
"-> port : %d") % (self.nick, self.addr, self.port)
def __repr__(self):
return str(self)
@classmethod
def new(cls, nick, addr, port):
return cls(surf.SurfDb.getNextServerID(), nick, addr, port)
def get(self):
"""Look up server by ID"""
conn = sqlite3.connect("surf.db")
conn.isolation_level = None
cur = conn.cursor()
cur.execute("SELECT `name`, `address`, `port` FROM servers WHERE " \
"`id`=?", (self._sID,))
row = cur.fetchone()
if row == None:
ret = False
else:
self.name = row[0]
self.address = row[1]
self.port = int(row[2])
ret = True
cur.close()
conn.close()
return ret
def insert(self):
"""Insert server (auto ID)"""
conn = sqlite3.connect("surf.db")
conn.isolation_level = None
cur = conn.cursor()
cur.execute("INSERT INTO servers (`id`, `name`, `address`, `port`) " \
"VALUES (?, ?, ?, ?)", (self._sID, self.nick, self.addr,
self.port))
cur.close()
conn.close()
def update(self):
"""Update server by ID"""
conn = sqlite3.connect("surf.db")
conn.isolation_level = None
cur = conn.cursor()
cur.execute("UPDATE servers SET `name`=?, `address`=?, `port`=? " \
"WHERE `id`=?", (self.nick, self.addr, self.port,
self._sID))
cur.close()
conn.close()
def delete(self):
"""Delete server by ID"""
conn = sqlite3.connect("surf.db")
conn.isolation_level = None
cur = conn.cursor()
cur.execute("DELETE FROM servers WHERE `id`=?", (self._sID,))
cur.close()
conn.close()
def _ping(self, timeout):
req = b"\xff\xff\xff\xff\x54\x53\x6f\x75\x72\x63\x65\x20\x45" + \
b"\x6e\x67\x69\x6e\x65\x20\x51\x75\x65\x72\x79\x00"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(timeout)
sock.connect((self.addr, self.port))
t1 = time.time()
sock.sendall(req)
try:
reply = sock.recv(4096)
t2 = time.time()
self._latency = int(round((t2 - t1) * 1000, 0))
except socket.error as e:
reply = None
finally:
sock.close()
return reply
def ping(self, timeout = 0.35, maxAttempts = 2):
attempts = 0
while attempts < maxAttempts:
reply = self._ping(timeout)
if not reply == None:
break
attempts += 1
if reply == None:
self._online = False
else:
self._online = True
self._header = chr(reply[4])
self._protocol = chr(reply[5])
i = 6
while reply[i] != 0:
i += 1
self._name = reply[6:i].decode("utf8")
i += 1
j = i
while reply[i] != 0:
i += 1
self._map = reply[j:i].decode("utf8").lower()
i += 1
j = i
while reply[i] != 0:
i += 1
self._folder = reply[j:i].decode("utf8")
i += 1
j = i
while reply[i] != 0:
i += 1
self._game = reply[j:i].decode("utf8")
i += 1
self._gameID = struct.unpack('<H', reply[i:i + 2])[0]
i += 2
self._players = reply[i]
i += 1
self._max_players = reply[i]
i += 1
self._bots = reply[i]
i += 1
self._type = chr(reply[i])
i += 1
self._env = chr(reply[i])
i += 1
self._vis = bool(reply[i])
i += 1
self._vac = bool(reply[i])
self._pinged = True
def join(self, pingInterval = 0.2):
self.ping()
if self._online and self._players >= self._max_players:
sys.stdout.write("Server \"%s\" on \"%s\" is full (%d/%d)\nListening" \
" for free space" % (self.nick, self._map,
self._players, self._max_players))
sys.stdout.flush()
while self._online and self._players >= self._max_players:
self.ping()
sys.stdout.write(".")
sys.stdout.flush()
time.sleep(pingInterval)
sys.stdout.write("\n")
sys.stdout.flush()
if self._online:
print("Space now available; joining (%d/%d)" % (self._players,
self._max_players))
webbrowser.open("steam://connect/%s:%d" % (self.addr, self.port))
else:
print("Server \"%s\" has gone offline" % (self.nick,))
elif self._online:
print("Joining server \"%s\" on \"%s\" (%d/%d)" % (self.nick,
self._map, self._players, self._max_players))
webbrowser.open("steam://connect/%s:%d" % (self.addr, self.port))
# Consider FancyURLopener() if this doesn't work on some platform?
else:
print("Server \"%s\" is offline" % (self.nick,))
@staticmethod
def pingAll(servers):
for server in servers:
server.ping()