cardstories part 14: cancel and autoplay

The –game-timeout option was added to specify that a game left unattended during more than 24h should either be canceled or proceed to the next state. At present the author of the game is required to act to go from the invitation state to the vote state and then to the complete state. The same functions used for the timeout could be used to automate these steps.

game cancelation

The new cancel function was added. It sets the game state to cancel

yield self.service.db.runOperation("UPDATE games SET state = 'canceled' WHERE id = ?", [ self.get_id() ])

and removes all the players:

yield self.cancelInvitations()
yield self.service.db.runOperation("DELETE FROM player2game WHERE game_id = ?", [ self.get_id() ])

from the database. It then notifies the clients interested in the game, before removing the list of players from the in core representation:

 self.destroy() # notify before altering the in core representation
 self.invited = []
 self.players = []

The service.py holding all games is notified of the destruction of the game and removes it from its registry.

    def game_notify(self, args, game_id):
        if args == None:
            del self.games[game_id]
            return False

players leaving the game

When the game is canceled, all players are forced to leave the game. The implementation has been designed to allow a later use by the user, should (s)he decide to leave a game:

    @defer.inlineCallbacks
    def leave_api(self, args):
        self.service.required(args, 'leave', 'player_id')
        player_ids = args['player_id']
        deleted = yield self.leave(player_ids)
        self.touch()
        defer.returnValue({'deleted': deleted})

It is not yet exposed in the webservice because the semantic is still uncertain. When removing the users as a side effect of canceling a game, the touch function is only called once to reduce the number of notifications.

automated state change

The core of the game cancellation is in the state_change function that either moves to the next state, if possible, or cancel the game if it is not possible. If the game is in the invitation state, it can go to the voting state if there are at least three players involved. If the game is in the voting state it can go to the complete state if there has been at least two votes. The automation of the game would require slightly different conditions, for instance moving to the voting state only when all participants picked a card.

This entry was posted in cardstories. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>