async def mainloop(self):
    while True:
        logger.info('Waiting for players')
        self.current_player = (await self.players.get())
        if self.current_player.ws.closed:
            logger.error('<{}> disconnect while waiting'.format(self.current_player.name))
            continue
        try:
            logger.info('Starting game for <{}>'.format(self.current_player.name))
            self.game.start(self.current_player.name)
            if self.grading:
                game_rec = dict(self.game_properties)
                game_rec['player'] = self.current_player.name
            while self.game.running:
                (await self.game.next_frame())
                (await self.current_player.ws.send(self.game.state))
                if self.viewers:
                    (await asyncio.wait([client.send(self.game.state) for client in self.viewers]))
            (await self.current_player.ws.send(json.dumps({'score': self.game.score})))
            logger.info('Disconnecting <{}>'.format(self.current_player.name))
        except websockets.exceptions.ConnectionClosed as c:
            self.current_player = None
        finally:
            if self.grading:
                game_rec['score'] = self.game.score
                r = requests.post(self.grading, json=game_rec)
            if self.current_player:
                (await self.current_player.ws.close())
async def mainloop(self):
    while True:
        logger.info('Waiting for players')
        self.current_player = (await self.players.get())
        if self.current_player.ws.closed:
            logger.error('<{}> disconnect while waiting'.format(self.current_player.name))
            continue
        try:
            logger.info('Starting game for <{}>'.format(self.current_player.name))
            self.game.start(self.current_player.name)
            if self.grading:
                game_rec = dict(self.game_properties)
                game_rec['player'] = self.current_player.name
            while self.game.running:
                (await self.game.next_frame())
                (await self.current_player.ws.send(self.game.state))
                if self.viewers:
                    (await asyncio.wait([client.send(self.game.state) for client in self.viewers]))
            (await self.current_player.ws.send(json.dumps({'score': self.game.score})))
            logger.info('Disconnecting <{}>'.format(self.current_player.name))
        except websockets.exceptions.ConnectionClosed as c:
            self.current_player = None
        finally:
            if self.grading:
                game_rec['score'] = self.game.score
                r = requests.post(self.grading, json=game_rec)
            if self.current_player:
                (await self.current_player.ws.close())