Ejemplo n.º 1
0
 def __init__(self, table_id='', document_id='', game=None):
     self.game = game
     if table_id:
         # ID for CouchDB - quoted and without '/'
         table_id_quoted = quote(table_id, safe='')
         self['_id'] = f'table-{table_id_quoted}'
         Document.__init__(self, self.game.db.database)
         # type is for CouchDB
         self['type'] = 'table'
         # what table?
         self['id'] = table_id_quoted
         self['name'] = table_id
         # default empty
         # quite likely order is about to vanish
         self['order'] = []
         self['players'] = []
         self['players_ready'] = []
         self['locked'] = False
         self['is_debugging'] = False
     elif document_id:
         Document.__init__(self, self.game.db.database, document_id=document_id)
         # get document data from CouchDB
         self.fetch()
     # yes, table_id
     if not self['id'] in self.game.rounds:
         self.add_round()
     self.save()
Ejemplo n.º 2
0
 def __init__(self, player_id='', document_id='', game=None):
     # access to global game
     self.game = game
     if player_id:
         # ID for CouchDB - quoted and without '/', to be transported easily througout HTML and JS
         player_id_quoted = quote(player_id, safe='')
         self['_id'] = f"player-{player_id_quoted}"
         Document.__init__(self, self.game.db.database)
         # ID for flask-login
         self['id'] = player_id_quoted
         # type is for CouchDB
         self['type'] = 'player'
         # name of player - to become somewhat more natural
         self['name'] = player_id
         # password hash
         self['password_hash'] = ''
         # current set of cards
         self['cards'] = []
         # which table player sits on
         self['table'] = ''
         # has admin rights
         self['is_admin'] = False
         # let idle players see player's cards
         self['allows_spectators'] = True
         # # other players to the left, opposite and right of table
         # self['left'] = self['opposite'] = self['right'] = None
         self.save()
     elif document_id:
         Document.__init__(self, self.game.db.database, document_id=document_id)
         # get document data from CouchDB
         self.fetch()
         # id needed for flask-login
         self['id'] = self['_id'].split('player-', 1)[1]
Ejemplo n.º 3
0
 def __init__(self, trick_id='', document_id='', game=None):
     self.game = game
     if trick_id:
         # ID generated from Round object
         self['_id'] = f'trick-{trick_id}'
         Document.__init__(self, self.game.db.database)
         self['type'] = 'trick'
         # initialize
         self.reset()
     elif document_id:
         Document.__init__(self, self.game.db.database, document_id=document_id)
         # get document data from CouchDB
         self.fetch()
Ejemplo n.º 4
0
    def __init__(self, players=[], game=None, round_id='', document_id=''):
        """
        either initialize new round or load it from CouchDB
        """
        self.game = game
        # collection of tricks per round - its number should not exceed cards_per_player
        self.tricks = {}
        if round_id:
            # ID for CouchDB - comes already quoted from table
            self['_id'] = f'round-{round_id}'
            Document.__init__(self, self.game.db.database)
            # type is for CouchDB
            self['type'] = 'round'
            # what table?
            self['id'] = round_id
            # list of the 4 players in round
            self['players'] = []
            # keep track of turns in round
            self['turn_count'] = 0
            # keep track of tricks in round
            self['trick_count'] = 0
            # ID of player which has current turn
            self['current_player'] = ''
            # as default play without '9'-cards
            # should be a property of table but rounds are initialized before tables and this leads to a logical
            # problem some lines later when cards are initialized and there are no tables yet which can be asked
            # for a .with_9 property
            self['with_9'] = False
            # even if not logical too just keep the undo setting here too to keep the table/round-settings together
            self['allow_undo'] = True
            # timestamp as checksum to avoid mess on client side if new cards are dealed
            # every deal gets its own timestamp to make cards belonging together
            self['cards_timestamp'] = 0
            # statistics of current round
            self['stats'] = {'score': {},
                             'tricks': {}}
            # initialize
            self.reset(players=players)
        elif document_id:
            Document.__init__(self, self.game.db.database, document_id=document_id)
            # get document data from CouchDB
            self.fetch()
            # a new card deck for every round
            # decide if the '9'-cards are needed and do not give them to round if not
            if self.with_9:
                self.cards = list(Deck.cards)
            else:
                self.cards = [x.id for x in Deck.cards.values() if x.rank != 'Neun']
            # cards per player depend on playing with '9'-cards or not
            self.cards_per_player = len(self.cards) // 4

        # just make sure tricks exist
        # + 1 due to range counting behaviour
        # no matter if '9'-cards are used just create database entries for all 12 possible tricks
        for trick_number in range(1, 13):
            trick = self.game.tricks.get(f'{self.id}-{trick_number}')
            if trick is None:
                # create trick in CouchDB if it does not exist yet
                self.game.tricks[f'{self.id}-{trick_number}'] = Trick(trick_id=f'{self.id}-{trick_number}',
                                                                      game=self.game)
            # access tricks per trick_count number, not as index starting from 0
            self.tricks[trick_number] = self.game.tricks[f'{self.id}-{trick_number}']