-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgsi_server.py
81 lines (61 loc) · 2.39 KB
/
gsi_server.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
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import json
import logger
import payloadparser
import gamestate
import provider
class GSIServer(HTTPServer):
def __init__(self, server_address, token, RequestHandler):
self.provider = provider.Provider()
self.auth_token = token
self.gamestatemanager = gamestate.GameStateManager()
super(GSIServer, self).__init__(server_address, RequestHandler)
self.setup_log_file()
self.payload_parser = payloadparser.PayloadParser()
def setup_log_file(self):
self.log_file = logger.LogFile(time.asctime())
class RequestHandler(BaseHTTPRequestHandler):
def do_POST(self):
length = int(self.headers["Content-Length"])
body = self.rfile.read(length).decode("utf-8")
payload = json.loads(body)
# Ignore unauthenticated payloads
if not self.authenticate_payload(payload):
return None
self.server.log_file.log_event(time.asctime(), payload)
self.server.payload_parser.parse_payload(payload, self.server.gamestatemanager)
self.send_header("Content-type", "text/html")
self.send_response(200)
self.end_headers()
def authenticate_payload(self, payload):
if "auth" in payload and "token" in payload["auth"]:
return payload["auth"]["token"] == server.auth_token
else:
return False
def parse_payload(self, payload):
self.server.log_file.log_event(time.asctime(), payload)
# round_phase = self.get_round_phase(payload)
# if round_phase != self.server.round_phase:
# self.server.round_phase = round_phase
# print('New round phase: %s' % round_phase)
def get_round_phase(self, payload):
if "round" in payload and "phase" in payload["round"]:
return payload["round"]["phase"]
else:
return None
def get_kill(self, payload):
if (
"player" in payload
and "state" in payload["player"]
and "rounds_kills" in payload["player"]["state"]
):
return payload["player"]["rounds_kills"]
else:
return None
def log_message(self, format, *args):
"""
Prevents requests from printing into the console
"""
return
server = GSIServer(("localhost", 3000), "MYTOKENHERE", RequestHandler)