def update_ratings(self, color): points = {self.color: 1.0, self.opponent.color: 0.0}.get(color, 0.5) mine = utils.loads(self.factory.db[self.name]) others = utils.loads(self.factory.db[self.opponent.name]) mine['rating'], others['rating'] = utils.elo(mine['rating'], others['rating'], points) self.factory.db[self.name] = utils.dumps(mine) self.factory.db[self.opponent.name] = utils.dumps(others)
def done(self, board, result): """Called when the game has ended""" rating = int(utils.loads(self.factory.db[self.name])['rating']) self.send({'action': utils.play.DONE, 'board': board, 'result': result, 'rating': rating})
def opponents(self, request): """Propose opponents for the player""" # We're currently using only AI bots, human players support will # get added later opponents = [{'name': name, 'rating': int(desc['rating']), 'type': 'AI'} for name, desc in ((name, utils.loads(desc)) for name, desc in self.factory.db.iteritems()) if desc['type'] == 'AI'] self.send({'action': utils.play.OPPONENTS, 'opponents': opponents})
def auth(self, request): try: user = str(request['user']) desc = utils.loads(self.factory.db[user]) # TODO: maybe add check for human players trying to auth as bots? if desc['password'] == str(request['password']): self.state = AUTHENTICATED self.name = user self.send({'action': utils.auth.AUTH, 'rating': int(round(desc['rating']))}) else: self.send({'action': utils.auth.BADPASSWORD}) except KeyError: self.send({'action': utils.auth.USERNOTFOUND})
def stringReceived(self, string): request = utils.loads(string) try: self.dispatch[request['action']](request) except KeyError: self.send({'action': utils.general.NOTIMPLEMENTED})
def stringReceived(self, response): response = utils.loads(response) self.dispatch[response['action']](response)