moth/pointsd.py

44 lines
1.1 KiB
Python
Raw Normal View History

2009-08-20 12:51:47 -06:00
#! /usr/bin/env python3
import socketserver
import struct
import points
import time
acked = points.Storage('scores.dat')
class MyHandler(socketserver.BaseRequestHandler):
def respond(self, when, txt):
peer = self.request[1]
resp = points.encode_response(when, txt)
peer.sendto(resp, self.client_address)
def handle(self):
global acked
now = int(time.time())
data = self.request[0]
peer = self.request[1]
try:
req = points.decode_request(data)
except ValueError as e:
return self.respond(now, str(e))
when, cat, team, score = req
# Replays can happen legitimately.
if not req in acked:
2009-08-20 16:20:44 -06:00
if not (now - 2 < when <= now):
2009-08-20 12:51:47 -06:00
resp = points.encode_response(when, 'Your clock is off')
peer.sendto(resp, self.client_address)
return
acked.add(req)
resp = points.encode_response(when, 'OK')
peer.sendto(resp, self.client_address)
if __name__ == "__main__":
2009-08-20 12:57:06 -06:00
server = socketserver.UDPServer(('', 6667), MyHandler)
2009-08-20 12:51:47 -06:00
server.serve_forever()