moth/pointsd.py

45 lines
1.2 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:
if not (now - 2 < when < now):
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__":
HOST, PORT = "localhost", 9999
server = socketserver.UDPServer((HOST, PORT), MyHandler)
server.serve_forever()