Exemplo n.º 1
0
    def _playerBuyin(self, thread_id, author_id):
        if thread_id not in self.tables:
            return self.sendMessage(BUYIN_FAILURE_NOT_INIT,
                                    thread_id=thread_id,
                                    thread_type=ThreadType.GROUP)

        table = self.tables[thread_id]
        if author_id in table:
            return self.sendMessage(BUYIN_FAILURE_ALREADY_IN_GAME,
                                    thread_id=thread_id,
                                    thread_type=ThreadType.GROUP)

        if len(table.all_players) >= 9:
            return self.sendMessage(BUYIN_FAILURE_MAXIMUM_PLAYERS,
                                    thread_id=thread_id,
                                    thread_type=ThreadType.GROUP)

        args = CONNECTION, 'players', 'fbid', author_id
        if not sqlmeths.getasdict(*args):
            return self.sendMessage(BUYIN_FAILURE_NOT_SIGNED_UP,
                                    thread_id=thread_id,
                                    thread_type=ThreadType.GROUP)

        player = self.__initializePlayer(thread_id, author_id)
        table += [player]

        self.sendMessage(BUYIN_SUCCESS.format(player.name, player.money),
                         thread_id=thread_id,
                         thread_type=ThreadType.GROUP)
Exemplo n.º 2
0
    def _requestMoney(self, thread_id):
        args = CONNECTION, 'players', 'fbid', thread_id
        pl_data = sqlmeths.getasdict(*args)

        if not pl_data:
            return self.sendMessage(NOT_SIGNED_UP_ERROR, thread_id=thread_id)

        timestamp = timemeths.formatted_timestamp()
        diff = timemeths.get_time_diff(timestamp, pl_data['timestamp'])

        hours_waited = diff['days'] * 24 + diff['hours']
        if hours_waited < MONEY_WAITING_PERIOD:
            remainder = timemeths.get_time_remainder(timestamp,
                                                     pl_data['timestamp'],
                                                     MONEY_WAITING_PERIOD)
            to_wait = ', '.join(
                str(remainder[timeframe]) + ' ' + timeframe
                for timeframe in remainder if remainder[timeframe])
            return self.sendMessage(REQUEST_MONEY_FAILURE.format(to_wait),
                                    thread_id=thread_id)

        pl_data['timestamp'] = timestamp
        pl_data['money'] += MONEY_ADD_PER_PERIOD
        sqlmeths.update(CONNECTION, 'players', pl_data, {'fbid': thread_id})
        self.sendMessage(REQUEST_MONEY_SUCCESS.format(MONEY_ADDED_PER_REQUEST),
                         thread_id=thread_id)
Exemplo n.º 3
0
 def resolve(self):
     pl_data = sqlmeths.getasdict(CONNECTION, 'players', 'fbid', self.fb_id)
     tblmoney_data = sqlmeths.getasdicts(CONNECTION, 'playermoneys',
     'tblid', 'money', {'fbid': self.fb_id})
     pl_data['money'] += tblmoney_data[self.table_id]
     self.money = 0
     sqlmeths.deletefromtable(CONNECTION, 'tablemoneys',
     {'fbid': self.fb_id, 'tblid': self.table_id})
     sqlmeths.update(CONNECTION, 'players', pl_data, {'fbid': self.fb_id})
Exemplo n.º 4
0
    def __refillPlayerMoney(self, player):
        args = CONNECTION, 'players', 'fbid', player_id
        pl_data = sqlmeths.getasdict(*args)

        money = pl_data['money']
        money_to_fill = DEFAULT_BUYIN - player.money

        filled = min(money, money_to_fill)
        player.money += filled
        pl_data['money'] -= filled
Exemplo n.º 5
0
    def _showUserMoney(self, thread_id):
        args = CONNECTION, 'players', 'fbid', thread_id
        pl_data = sqlmeths.getasdict(*args)

        if not pl_data:
            return self.sendMessage(NOT_SIGNED_UP_ERROR, thread_id=thread_id)

        self.sendMessage(
            SHOW_USER_MONEY_SUCCESS.format(pl_data['money']),
            thread_id=thread_id,
        )
Exemplo n.º 6
0
    def __resolvePlayer(self, player):
        args = CONNECTION, 'players', 'fbid', player.id
        pl_data = sqlmeths.getasdict(*args)
        tbl_data = sqlmeths.getasdicts(CONNECTION, 'tablemoneys', 'tblid',
                                       'money', {'fbid': player.id})

        pl_data['money'] += tbl_data[player.table_id]
        player.money = 0

        sqlmeths.deletefromtable(CONNECTION, 'tablemoneys', {
            'fbid': player.id,
            'tblid': player.table_id
        })
        sqlmeths.update(CONNECTION, 'players', pl_data, {'fbid': player.id})
Exemplo n.º 7
0
    def __init__(self, name: str, fb_id: str, table_id: str):

        self.name = name
        self.fb_id = fb_id
        self.table_id = table_id

        db_data = sqlmeths.getasdict(CONNECTION, 'players', 'fbid', fb_id)
        money = TABLE_MONEY if db_data['money'] >= TABLE_MONEY else db_data['money']
        db_data['money'] -= money
        sqlmeths.insert(CONNECTION, 'tablemoneys',
        fbid=fb_id, tblid=table_id, money=money)
        sqlmeths.update(CONNECTION, 'players', db_data, {'fbid': fb_id})

        super().__init__(self.name, money)
        self.id = fb_id
Exemplo n.º 8
0
    def refill_money(self):
        if self.money >= TABLE_MONEY:
            return False

        file_data = sqlmeths.getasdict(CONNECTION, 'players', 'fbid', self.fb_id)
        __money = file_data['money']
        money_to_fill = TABLE_MONEY - self.money

        if __money >= money_to_fill:
            self.money += money_to_fill
            file_data['money'] -= money_to_fill
            return money_to_fill
        else:
            self.money += __money
            file_data['money'] = 0
            return __money
Exemplo n.º 9
0
    def __initializePlayer(self, thread_id, p_id):
        args = CONNECTION, 'players', 'fbid', p_id
        db_data = sqlmeths.getasdict(*args)

        money = (DEFAULT_BUYIN
                 if db_data['money'] >= DEFAULT_BUYIN else db_data['money'])

        db_data['money'] -= money
        sqlmeths.insert(CONNECTION,
                        'tablemoneys',
                        fbid=p_id,
                        tblid=thread_id,
                        money=money)
        sqlmeths.update(CONNECTION, 'players', db_data, {'fbid': p_id})

        user_info = self.fetchUserInfo(p_id)[p_id]
        return Player(thread_id, p_id, user_info.name, money)
Exemplo n.º 10
0
    def _signUpPlayer(self, thread_id):
        args = CONNECTION, 'players', 'fbid', thread_id
        sql_data = sqlmeths.getasdict(*args)

        if sql_data:
            return self.sendMessage(PLAYER_SIGN_UP_FAILURE,
                                    thread_id=thread_id)

        user_info = self.fetchUserInfo(thread_id)
        user_info = user_info[thread_id]
        sqlmeths.insert(CONNECTION,
                        'players',
                        name=user_info.name,
                        fbid=thread_id,
                        money=PLAYER_STARTING_MONEY,
                        timestamp=timemeths.formatted_timestamp())
        self.sendMessage(PLAYER_SIGN_UP_SUCCESS.format(user_info.name,
                                                       PLAYER_STARTING_MONEY,
                                                       DOC),
                         thread_id=thread_id)
Exemplo n.º 11
0
        id integer PRIMARY KEY,
        fbid text NOT NULL,
        name text NOT NULL,
        money integer,
        timestamp text
    )
    '''
    tablemoneys_sql = '''
    CREATE TABLE IF NOT EXISTS tablemoneys(
        id integer PRIMARY KEY,
        fbid text NOT NULL,
        tblid text NOT NULL,
        money integer
    )
    '''
    sqlmeths.executesql(CONNECTION, players_sql, tablemoneys_sql)

    # this has to be done before every app rerun
    # in case anyone left the money on some table
    for fbid in sqlmeths.getcol(CONNECTION, 'players', 'fbid'):
        fbid = fbid[0]
        leftovers = sqlmeths.getasdicts(CONNECTION, 'tablemoneys', 'tblid',
                                        'money', {'fbid': fbid})
        playerdata = sqlmeths.getasdict(CONNECTION, 'players', 'fbid', fbid)
        playerdata['money'] += sum(leftovers.values())
        sqlmeths.update(CONNECTION, 'players', playerdata, {'fbid': fbid})
    sqlmeths.emptytable(CONNECTION, 'tablemoneys')

    dealer = Dealer(USERNAME, PASSWORD)
    dealer.listen()
Exemplo n.º 12
0
    def onMessage(self, author_id, message, thread_id, **kwargs):
        message = message.lower()
        if author_id == self.uid or (message not in STATEMENTS and not message.startswith('raise ')):
            return None

        game = self.games[thread_id] if thread_id in self.games else None

        # messages sent from a group (game manipulation messages)
        if kwargs['thread_type'] == ThreadType.GROUP and message in GAME_MANIPULATION_STATEMENTS:

            if message == INITIALIZE_GAME:
                if not game:
                    self.games[thread_id] = FbPokerGame(PlayerGroup([]), BIG_BLIND, thread_id)
                    self.sendMessage('This group was initialized as a poker table',
                    thread_id = thread_id, thread_type = ThreadType.GROUP)
                else:
                    self.sendMessage("This group is already initialized as a poker table",
                    thread_id = thread_id, thread_type = ThreadType.GROUP)

            elif message == START_GAME:
                if not game:
                    self.sendMessage('This group had not yet been initialized as a poker table',
                    thread_id = thread_id, thread_type = ThreadType.GROUP)
                elif game and game.round:
                    self.sendMessage('Rounds are already being played on this table',
                    thread_id = thread_id, thread_type = ThreadType.GROUP)
                elif game and not game.round:
                    if not game.is_ok():
                        self.sendMessage('Not enough players have bought into the game. To buy in, type "buy in"',
                        thread_id = thread_id, thread_type = ThreadType.GROUP)
                    else:
                        self.sendMessage(f'A series of rounds will start. End them by typing "{TOGGLE_LAST_ROUND}"',
                        thread_id = thread_id, thread_type = ThreadType.GROUP)
                        game.new_round()

            elif message == BUY_IN:
                if not game:
                    self.sendMessage('This group had not yet been initialized as a poker table',
                    thread_id = thread_id, thread_type = ThreadType.GROUP)
                elif game.players['fb_id', author_id]:
                    self.sendMessage('''You are already playing in this game.
                    If you wish to refill your money use "{REFILL_TABLE_MONEY}" statement''',
                    thread_id = thread_id, thread_type = ThreadType.GROUP)
                elif len(game.all_players) >= 9:
                    self.sendMessage('There is already a maximum amount of players playing on this table',
                    thread_id = thread_id, thread_type = ThreadType.GROUP)
                else:
                    user_info = self.fetchUserInfo(author_id)[author_id]
                    try:
                        player = FbPlayer(user_info.name, user_info.uid, thread_id)
                        game.on_player_join(player)
                        self.sendMessage(user_info.name +
                        ' has bought into the game with ' + str(player.money),
                        thread_id = thread_id, thread_type = ThreadType.GROUP)
                    except AssertionError:
                        self.sendMessage('You have not signed up yet',
                        thread_id = thread_id, thread_type = ThreadType.GROUP)
                        return None

            # from now on everything requires for a game to be played and that a player in that game wrote the message
            player = game.all_players['fb_id', author_id] if game else None
            if not player:
                return None

            if message == TOGGLE_LAST_ROUND:
                if game.round:
                    game.round.exit_after_this = not game.round.exit_after_this
                    _send = 'game will end after this round' if game.round.exit_after_this else 'game will continue'
                    self.sendMessage(_send, thread_id = thread_id,
                    thread_type = ThreadType.GROUP)

            # player wants to get all money out of the table (player has to be folded or rounds not played)
            elif message == LEAVE_TABLE:
                self.sendMessage(player.name + ' has left the game',
                thread_id = thread_id, thread_type = ThreadType.GROUP)
                game.on_player_leave(player)
                player.resolve()

            # player wants to refill his money on the playing table
            elif message == REFILL_TABLE_MONEY:
                if not game.round or player not in game.round.players or player.is_folded:
                    money_filled = player.refill_money()
                    if money_filled is not False:
                        self.sendMessage('Money successfully refilled by ' +
                        str(money_filled) + ' to ' + str(player.money),
                        thread_id = thread_id, thread_type = ThreadType.GROUP)

            # player requested to see the money in a specific game he is playing (THIS SHOULD BE GONE when a better solution arises)
            elif message == SHOW_USER_TABLE_MONEY:
                self.sendMessage('You Have ' + str(player.money),
                thread_id = thread_id, thread_type = ThreadType.GROUP)

        # message within active round was sent (game continuation)
        elif game and game.round:
            # message was sent from current player in game round
            if author_id == game.round.current_player.fb_id:
                game.round.process_action(message) # game continuation

        # messages sent privately to the dealer (data requests / data modification requests)
        elif kwargs['thread_type'] == ThreadType.USER and message in PRIVATE_STATEMETNS:
            sql_data = sqlmeths.getasdict(CONNECTION, 'players', 'fbid', thread_id)

            # player wants to be signed up
            if message == INITIALIZE_PLAYER:
                if sql_data:
                    self.sendMessage('You are already in the database',
                    thread_id = thread_id)
                else:
                    user_info = self.fetchUserInfo(author_id)[author_id]
                    sqlmeths.insert(CONNECTION, 'players',
                        name = user_info.name,
                        fbid = thread_id,
                        money = PLAYER_STARTING_MONEY,
                        timestamp = timemeths.formatted_timestamp()
                    )
                    self.sendMessage(f'''Welcome {user_info.name}!\n''' +
                    f'''{PLAYER_STARTING_MONEY} was added to your account.\n''' +
                    f'''For more info about the game check the documentation\n''' +
                    f'''{DOCUMENTATION_URL}''',
                    thread_id = thread_id)

            # if the author is not inside the database
            # notify the author and end the execution
            elif not sql_data:
                return self.sendMessage(f'''You are not in the database,
                type "{INITIALIZE_PLAYER}" to be added''',
                thread_id = thread_id)

            # player requested to see the money from the database
            if message == SHOW_USER_MONEY:
                self.sendMessage(f"You have {sql_data['money']} left",
                thread_id = author_id, thread_type = ThreadType.USER)

            elif message == REQUEST_FOR_MONEY:
                timestamp = timemeths.formatted_timestamp()
                diff = timemeths.get_time_diff(timestamp, sql_data['timestamp'])

                if diff['days'] or diff['hours'] >= MONEY_WAITING_PERIOD:
                    sql_data['timestamp'] = timestamp
                    sql_data['money'] += MONEY_ADD_PER_PERIOD
                    sqlmeths.update(CONNECTION, 'players', sql_data, {'fbid': thread_id})
                    self.sendMessage(str(MONEY_ADD_PER_PERIOD) + " successfully added",
                    thread_id = thread_id)
                else:
                    remainder = timemeths.get_time_remainder(timestamp, data['timestamp'], MONEY_WAITING_PERIOD)
                    to_wait = ', '.join([str(remainder[timeframe]) + ' ' + timeframe for timeframe in remainder if remainder[timeframe]])
                    self.sendMessage("Money Can Be Requested in " + to_wait,
                    thread_id = thread_id)
Exemplo n.º 13
0
    timestamp text
)
'''
tablemoneys_sql = '''
CREATE TABLE IF NOT EXISTS tablemoneys(
    id integer PRIMARY KEY,
    fbid integer NOT NULL,
    tblid integer NOT NULL,
    money integer
)
'''
sqlmeths.executesql(CONNECTION, players_sql, tablemoneys_sql)

# this has to be done before every app rerun
# in case anyone left the money on some table
for fbid in sqlmeths.getcol(CONNECTION, 'players', 'fbid'):
    fbid = fbid[0]
    leftovers = sqlmeths.getasdicts(CONNECTION, 'tablemoneys', 'tblid',
    'money', {'fbid': fbid})
    plyrdata = sqlmeths.getasdict(CONNECTION, 'players', 'fbid', fbid)
    plyrdata['money'] += sum(leftovers.values())
    sqlmeths.update(CONNECTION, 'players', plyrdata, {'fbid': fbid})
sqlmeths.emptytable(CONNECTION, 'tablemoneys')

# game continuation
if __name__ == '__main__':
    DEALER_MAIL = input('Dealer email: ')
    DEALER_PASSWORD = input('Dealer password: ')
    DEALER = Dealer(DEALER_MAIL, DEALER_PASSWORD)
    DEALER.listen()