-
Notifications
You must be signed in to change notification settings - Fork 6
/
serverComms.py
151 lines (115 loc) · 4.58 KB
/
serverComms.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
#!/usr/bin/python3
import asyncio
import json
import os
from datetime import datetime
from dotenv import load_dotenv
from ftplib import FTP
async def start_udp_listener():
loop = asyncio.get_event_loop()
return await loop.create_datagram_endpoint(lambda: InhouseServerProtocol(), local_addr=('0.0.0.0', 16353))
def main_watcher():
loop = asyncio.get_event_loop()
coro = start_udp_listener()
transport, _ = loop.run_until_complete(coro)
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
transport.close()
loop.close()
def main():
global FTP_USER
global FTP_PASSWD
global FTP_SERVER
load_dotenv()
FTP_USER = os.getenv('FTP_USER')
FTP_PASSWD = os.getenv('FTP_PASSWD')
FTP_SERVER = os.getenv('FTP_SERVER')
main_watcher()
def getLastGameLogs():
global FTP_USER
global FTP_PASSWD
global FTP_SERVER
if os.path.exists('prevlog.json'):
with open('prevlog.json', 'r') as f:
prevlog = json.load(f)
else:
prevlog = []
ftp = FTP(FTP_SERVER, user=FTP_USER, passwd=FTP_PASSWD)
ftp.cwd('/tfc/logs')
logFiles = ftp.nlst('-t') # get list of logs by time
logFiles.reverse() # sort descending
firstLog = None
secondLog = None
for logFile in logFiles:
if ".log" not in logFile:
continue
if 'logFiles' in prevlog and logFile in prevlog['logFiles']:
print("already parsed the latest log")
return
# check the size, should be >100kB
if int(ftp.size(logFile)) > 100000:
logModified = datetime.strptime(ftp.voidcmd("MDTM %s" % logFile).split()[-1], '%Y%m%d%H%M%S')
if firstLog is None:
firstLog = (logFile, logModified)
continue
# otherwise, verify that there was another round played at least <60 minutes within the last found log
if (firstLog[1] - logModified).total_seconds() < 3600:
secondLog = (logFile, logModified)
# if secondLog is not populated, this is probably the first pickup of the day; abort
break
# abort if we didn't find a log
if firstLog is None or secondLog is None:
return
ftp.retrbinary("RETR %s" % firstLog[0], open('logs/%s' % firstLog[0], 'wb').write)
ftp.retrbinary("RETR %s" % secondLog[0], open('logs/%s' % secondLog[0], 'wb').write)
hampalyze = 'curl -X POST -F logs[]=@%s -F logs[]=@%s http://app.hampalyzer.com/api/parseGame' % ('logs/'+secondLog[0], 'logs/'+firstLog[0])
output = os.popen(hampalyze).read()
print(output)
status = json.loads(output)
if 'success' in status:
site = "http://app.hampalyzer.com" + status['success']['path']
print("Parsed logs available: %s" % site)
with open('prevlog.json', 'w') as f:
prevlog = { 'site': site, 'logFiles': [ firstLog[0], secondLog[0] ] }
json.dump(prevlog, f)
else:
print('error parsing logs: %s' % output)
class InhouseServerProtocol:
def connection_made(self, transport):
self.transport = transport
def datagram_received(self, data, addr):
message = data.decode()
print('received %r from %s' % (message, addr))
message_parts = message.split("@")
if message_parts[0] != "BOT_MSG":
return
if message_parts[1] == "IRC":
print("message inhouse! {}" % message)
if message_parts[1] == "MAP":
with open('prevmaps.json', 'r') as f:
prevmaps = json.load(f)
curmap = prevmaps[-1]
self.send_message("MAP", curmap, addr)
if message_parts[1] == "RS":
with open('prevmaps.json', 'r') as f:
prevmaps = json.load(f)
curmap = prevmaps[-1]
self.send_message("RS", curmap, addr)
if message_parts[1] == "TEAMS":
with open('prevteams.json', 'r') as f:
prevteams = json.load(f)
self.send_message("TEAMS", ', '.join(prevteams[:4]), addr)
self.send_message("TEAMS", ', '.join(prevteams[4:]), addr)
if message_parts[1] == "END":
getLastGameLogs()
if message_parts[1] == "TIMELEFT":
with open('timeleft.json', 'w') as f:
json.dump({ 'timeleft': message_parts[-1] }, f)
def send_message(self, msg_type, message, addr):
data = ("BOT_MSG@%s@%s" % (msg_type, message)).encode()
self.transport.sendto(data, (addr[0], 16354)) # bot only listens on this port
if __name__ == "__main__":
main()