Ejemplo n.º 1
0
    async def current(self, ctx: commands.Context):
        """Lists your current marble match

        Examples:
            - `$current`

        **Arguments**

        - `<ctx>` The context used to send confirmations.

        """
        logger.debug(f'current: {ctx}, {ctx.author}')

        # Gets player_id from ctx.author and gets match_id for player
        player_id = du.get_id_by_member(ctx, DbHandler.db_cnc, ctx.author)
        match_id = database_operation.find_match_by_player_id(DbHandler.db_cnc, player_id)
        logger.debug(f'player_id: {player_id}, match_id: {match_id}')

        # Checks if match_id is an actual id
        if not match_id:
            logger.debug('match_id is zero')
            await du.code_message(ctx, 'No current match')
            return
        # Gets match_info to display back to the user
        match_info = ma.get_match(ctx, match_id)  # database_operation.get_match_info_by_id(DbHandler.db_cnc, match_id)
        logger.debug(f'match_info: {match_info}')

        # Get Accounts of both participants
        player_info1 = acc.get_account_from_db(ctx, DbHandler.db_cnc, match_info.challenger.id)  # database_operation.get_player_info(DbHandler.db_cnc, match_info[3])
        player_info2 = acc.get_account_from_db(ctx, DbHandler.db_cnc, match_info.recipient.id)  # database_operation.get_player_info(DbHandler.db_cnc, match_info[4])
        logger.debug(f'player_info1: {player_info1}, player_info2: {player_info2}')

        await du.code_message(ctx, f'Match between {player_info1.nickname} and '
                                   f'{player_info2.nickname} for {match_info.amount} marbles'
                                   f'\nMatch ID: {match_info.id}')
Ejemplo n.º 2
0
    async def close_current_match(self, ctx: commands.Context):
        """Closes your active marble match

        Examples:
            - `$close`

        **Arguments**

        - `<ctx>` The context used to send confirmations

        """
        logger.debug(f'close: {ctx}, {ctx.author}')

        # Gets player_id and match_id, to close the current match
        player_id = du.get_id_by_member(ctx, DbHandler.db_cnc, ctx.author)
        match_id = database_operation.find_match_by_player_id(DbHandler.db_cnc, player_id)
        logger.debug(f'player_id: {player_id}, match_id: {match_id}')

        # Gets match_info to return marbles back to participants
        match_info = ma.get_match(ctx, match_id)  # database_operation.get_match_info_by_id(DbHandler.db_cnc, match_id)
        logger.debug(f'match_info: {match_info}')

        player2_refund = False

        # Checks if match is accepted by participant2
        if match_info.accepted:  # Match is accepted
            # Gets participant2's Account to change marbles
            player2 = acc.get_account_from_db(ctx, DbHandler.db_cnc, match_info.recipient.id)
            logger.debug(f'player2: {player2}')
            # Checks if player2 is 0, then returns if 0
            if not player2:
                logger.debug(f'Unable to get participant2 Account')
                await du.code_message(ctx, 'Unable to get participant2\'s account')
                return
            # Sets flag for player2 to be refunded amount to true
            player2_refund = True
            logger.debug(f'player2_refund: {player2_refund}')

        # Get participant1's Account to refund player for match amount
        player1 = acc.get_account_from_db(ctx, DbHandler.db_cnc, match_info.challenger.id)
        # Check if player1 is 0, then returns if 0
        if not player1:
            logger.debug('Unable to get participant1 Account')
            await du.code_message(ctx, 'Unable to get participant1\'s account')
            return

        # Deletes the match and all the bets on the match by id, checks if write was successful and returns message
        if not database_operation.delete_match(DbHandler.db_cnc, match_id):
            logger.debug('Unable to delete match')
            await du.code_message(ctx, 'Unable to delete match', 3)
            return

        # Refunds players marbles, checks if player2 flag is true to refund player2
        player1.marbles += match_info.amount
        if player2_refund:
            player2.marbles += match_info.amount

        database_operation.delete_bet_by_match_id(DbHandler.db_cnc, match_id)
        await du.code_message(ctx, f'Closed match {match_id}.')
Ejemplo n.º 3
0
def process_bets(ctx, match: matches.Match) -> bool:
    """Processes all the bets for a given match
    """
    logger.debug(f'process_bets: {match}')

    # Check if match is a history match
    if not match.is_history:
        logger.error(f"Passed a match that isn't history to process_bets")
        return False

    # Get all bets for match, validate that it's not empty
    bet_data = database_operation.get_bet_info_match_all(
        DbHandler.db_cnc, match.id)
    if not len(bet_data):
        logger.debug(f"No bets placed on this match")
        return False

    # Create list of bets to be filled and processed
    bets = []
    for bet in bet_data:
        # Check if bet and match.id are equal
        if bet[2] == match.id:
            logger.debug(f'Valid bet({bet[0]})')

        # Get bettor account and bet target account, validate
        bettor = account.get_account_from_db(ctx, DbHandler.db_cnc, bet[3])
        bet_target = account.get_account_from_db(ctx, DbHandler.db_cnc, bet[4])
        if not bettor or not bet_target:
            logger.error(f'Unable to get accounts from bet')
            raise exception.UnableToRead(class_='Account', attribute='account')

        # Create bet to append onto bets
        bet_temp = Bet(bet[0], bet[1], match, bettor, bet_target, match.winner)
        logger.debug(f'bet_temp: {bet_temp}')
        if not bet_temp:
            logger.error(f'Unable to create bet')
            raise exception.UnableToWrite(class_='Bet', attribute='bet')
        bets.append(bet_temp)

    # Check if bets has been propagated
    if not len(bets):
        logger.error(f'bets was not propagated')
        return False

    # Total marble count
    marble_pot = 0

    # Total of loser marbles and losers
    loser_count = 0
    loser_pot = 0

    # Total of winner marbles and winners
    winner_count = 0
    winner_pot = 0

    # Calculations that need all bets
    for process_bet in bets:
        # Check if bet is winner
        if process_bet.is_winner():
            # Increment winner count and add bet amount to winner pool
            winner_count += 1
            winner_pot += process_bet.amount
        else:
            # Increment loser count and add bet amount to loser pool
            loser_count += 1
            loser_pot += process_bet.amount
        # Increment total marbles
        marble_pot += process_bet.amount

    logger.debug(f'marble_pot: {marble_pot}, '
                 f'loser: sum({loser_pot}),count({loser_count}), '
                 f'winner: sum({winner_pot}),count({winner_count})')

    # Calculate winner_pot_ratio and winnings per bet
    for calculate_bets in bets:
        if calculate_bets.is_winner():
            # Ratio of your bet in winner_pot
            winner_pot_ratio = calculate_bets.amount / winner_pot
            logger.debug(f'pot_ratio: {winner_pot_ratio}')

            # If loser pot* winner_pot_ratio < 1: return bet amount + one marble
            if (loser_pot * winner_pot_ratio) < 1:
                bet_returns = calculate_bets.amount + 1
            else:
                bet_returns = calculate_bets.amount + (loser_pot *
                                                       winner_pot_ratio)

            # If no losers, house matches bet and returns double amount
            if loser_count == 0:
                bet_returns = calculate_bets.amount * 2

            logger.debug(f'user: {calculate_bets.bettor.nickname}'
                         f'winner_pot_ratio: {winner_pot_ratio}'
                         f'bet_returns: {bet_returns}')

            # Add bet_returns to bettors marbles
            calculate_bets.bettor.marbles += bet_returns

        # Update calculate_bet to prep for making into history
        calculate_bets.bet_time = datetime.datetime.utcnow()
        calculate_bets.is_history = True
        # Delete bet
        calculate_bets.delete_bet()
        # Make bet history
        calculate_bets.create_history()

    return True
Ejemplo n.º 4
0
def get_bet(ctx: commands.Context,
            bet_id: int,
            history: bool = False) -> Union[Bet, int]:
    """Returns a Bet for Bet with bet_id

    **Arguments**
    - `<ctx>` Context used to get members and other information
    - `<bet_id>` Id of the bet to get
    - `<history>` Used to specify if you'd like to get a bet from bet_history or bets

    """
    logger.debug(f'get_bet: {bet_id}, {history}')

    # Check if ctx.channel is dm, return zero if true
    if isinstance(ctx.channel, discord.DMChannel):
        logger.error('ctx channel is dm, get_bet not allowed in dms')
        raise exception.DiscordDM

    # Declare bet_info to be filled later with a tuple of bet data from database
    bet_info = 0

    # Checks history to decide which table to get bet_info from
    if history:
        bet_info = database_operation.get_bet_history_info(
            DbHandler.db_cnc, bet_id)
    else:
        bet_info = database_operation.get_bet_info(DbHandler.db_cnc, bet_id)
    logger.debug(f'bet_info: {bet_info}')

    # Check if bet_info is zero, if true return. Is non zero when filled with data
    if not bet_info:
        logger.error(f'bet_info is zero')
        return 0

    # Get match from id in bet_info and validate
    match = matches.get_match(ctx, bet_info[2])
    logger.debug(f'match: {match}')
    if not match:
        logger.error('match is zero')
        raise exception.UnableToRead(class_='Match', attribute='match')

    # Get bettor from bet_info and validate
    bettor = account.get_account_from_db(ctx, DbHandler.db_cnc, bet_info[3])
    logger.debug(f'bettor: {bettor}')
    if not bettor:
        logger.error('bettor is zero')
        raise exception.UnableToRead(class_='Account', attribute='account')

    # Get bet_target from bet_info and validate
    bet_target = account.get_account_from_db(ctx, DbHandler.db_cnc,
                                             bet_info[4])
    logger.debug(f'bet_target: {bet_target}')
    if not bet_target:
        logger.error('bet_target is zero')
        raise exception.UnableToRead(class_='Account', attribute='account')

    # Check history to get data specific to history matches
    if history:
        logger.debug('history bet')

        # Get winner from bet_info and validate
        winner = account.get_account_from_db(ctx, DbHandler.db_cnc,
                                             bet_info[5])
        logger.debug(f'winner: {winner}')
        if not winner:
            logger.error('winner is zero')
            raise exception.UnableToRead(class_='Account', attribute='account')

        # Create Bet with bet_info data
        bet = Bet(bet_info[0], bet_info[1], match, bettor, bet_target, winner,
                  bet_info[6])
    else:
        bet = Bet(bet_info[0], bet_info[1], match, bettor, bet_target)
    logger.debug(f'bet: {bet}')

    return bet
Ejemplo n.º 5
0
def get_bet_all(ctx: commands.Context,
                user: account.Account,
                user2: account.Account = None,
                history: bool = False) -> Union[list, int]:
    """Returns list of all Bets with user

    **Arguments**

    - `<ctx>` Context used to get information
    - `<user>` User who's bet you wish to get
    - `<user2>` User who you wanna search for as well
    - `<history>` Used to get either match history or active matches

    """
    logger.debug(f'bets.get_bet_all: {user}, {user2}, {history}')

    # Get all bets with user.id, check if bets is valid
    if history:
        bets = database_operation.get_bet_history_info_all(
            DbHandler.db_cnc, user.id)
    else:
        bets = database_operation.get_bet_info_all(DbHandler.db_cnc, user.id)
    if not bets:
        logger.error('bets is zero')
        return 0

    # Create bet list to return
    bet_list = []

    for bet in bets:
        # Get bettor and check if valid
        bettor = account.get_account_from_db(ctx, DbHandler.db_cnc, bet[3])
        logger.debug(f'bettor: {bettor}')
        if not bettor:
            logger.error('Unable to get bettor account')
            raise exception.UnableToRead(class_='Account', attribute='account')
        # Get bet_target and check if valid
        bet_target = account.get_account_from_db(ctx, DbHandler.db_cnc, bet[4])
        logger.debug(f'bet_target: {bet_target}')
        if not bet_target:
            logger.error('Unable to get bet_target account')
            raise exception.UnableToRead(class_='Account', attribute='account')
        # Get match and check if valid
        match = matches.get_match(ctx, bet[2], history)
        logger.debug(f'match: {match}')
        if not match:
            logger.error('Unable to get match')
            raise exception.UnableToRead(class_='Match', attribute='match')

        # Create match depending on if history is true or not
        if history:
            logger.debug('history is true')

            # Get winner and check if valid
            winner = account.get_account_from_db(ctx, DbHandler.db_cnc, bet[5])
            logger.debug(f'winner: {winner}')
            if not winner:
                logger.error('Unable to get winner account')
                raise exception.UnableToRead(class_='Account',
                                             attribute='account')

            append_bet = Bet(bet[0], bet[1], match, bettor, bet_target, winner,
                             bet[6], True)
        else:
            append_bet = Bet(bet[0], bet[1], match, bettor, bet_target)

        logger.debug(f'append_bet: {append_bet}')

        # Check if append_bet is filled
        if append_bet:
            bet_list.append(append_bet)

    # Check if list has been propagated, return 0 otherwise
    if not len(bet_list):
        logger.debug('bet_list length is zero')
        return 0

    # Return bets
    return bet_list
Ejemplo n.º 6
0
def get_match(ctx: commands.Context,
              match_id: int,
              history: bool = False) -> Union[Match, int]:
    """Returns Match for Match with match_id

    **Arguments**

    - `<ctx>` Context used to get members and other information
    - `<match_id>` Id of the match to get
    - `<history>` Used to specify if you'd like to get a match from match_history or matches

    """
    logger.debug(f'get_match: {match_id}, {history}')

    # Check if ctx.channel is dm, return zero if true
    if isinstance(ctx.channel, discord.DMChannel):
        logger.error('ctx channel is dm, get_match not allowed in dms')
        raise exception.DiscordDM

    # Declare match_info to be filled later with tuple of match data from database
    match_info = 0

    # Checks history to decide which table to get match_info from
    if history:
        match_info = database_operation.get_match_history_info(
            DbHandler.db_cnc, match_id)
    else:
        match_info = database_operation.get_match_info_by_id(
            DbHandler.db_cnc, match_id)
    logger.debug(f'match_info: {match_info}')

    # Checks if match_info is int, if true return. Is tuple when filled with data
    if isinstance(match_info, int):
        logger.error(f'match_info was type int')
        return 0

    # Check history to get data specific to history matches
    if history:
        # Get Account of challenger
        challenger = acc.get_account_from_db(ctx, DbHandler.db_cnc,
                                             match_info[2])
        logger.debug(f'challenger: {challenger}')
        # Checks if challenger is int, if true return 0
        if isinstance(challenger, int):
            logger.error('challenger is type int')
            raise exception.UnableToRead(class_='Account', attribute='account')

        # Get Account of recipient
        recipient = acc.get_account_from_db(ctx, DbHandler.db_cnc,
                                            match_info[3])
        logger.debug(f'recipient: {recipient}')
        # Check if recipient is int, if true return 0
        if isinstance(recipient, int):
            logger.error('recipient is type int')
            raise exception.UnableToRead(class_='Account', attribute='account')

        # Get Account for winner
        winner = acc.get_account_from_db(ctx, DbHandler.db_cnc, match_info[4])
        logger.debug(f'winner: {winner}')
        # Checks if winner is int, if true return 0
        if isinstance(winner, int):
            logger.error('winner is type int')
            raise exception.UnableToRead(class_='Account', attribute='account')

        # Create Match with match_info data
        match = Match(match_info[0], match_info[1], True, challenger,
                      recipient, True, winner, match_info[5], match_info[6],
                      match_info[7], True)
        logger.debug(f'match: {match}')
        # Checks if match is type int, if true return 0
        if isinstance(match, int):
            logger.error('match is type int')
            raise exception.UnableToWrite(class_='Match', attribute='match')

        # Return match
        return match
    else:
        # Get Account of challenger
        challenger = acc.get_account_from_db(ctx, DbHandler.db_cnc,
                                             match_info[3])
        logger.debug(f'challenger: {challenger}')
        # Checks if challenger is int, if true return 0
        if isinstance(challenger, int):
            logger.error('challenger is type int')
            raise exception.UnableToRead(class_='Account', attribute='account')

        # Get Account of recipient
        recipient = acc.get_account_from_db(ctx, DbHandler.db_cnc,
                                            match_info[4])
        logger.debug(f'recipient: {recipient}')
        # Check if recipient is int, if true return 0
        if isinstance(recipient, int):
            logger.error('recipient is type int')
            raise exception.UnableToRead(class_='Account', attribute='account')
        # Create match with match_info data
        match = Match(match_info[0],
                      match_info[1],
                      match_info[2],
                      challenger,
                      recipient,
                      match_info[5],
                      _game=match_info[6],
                      _format=match_info[7])

        # Checks if match is type int, if true return 0
        if isinstance(match, int):
            logger.error('match is type int')
            raise exception.UnableToWrite(class_='Match', attribute='match')

        # Return match
        return match
Ejemplo n.º 7
0
def get_matches_all(ctx,
                    user: acc.Account,
                    user2: acc.Account = None,
                    history: bool = False) -> Union[list, int]:
    """Returns list of all Matches with user

    **Arguments**

    - `<ctx>` Context used to get information
    - `<user>` User who's matches you wish to get
    - `<user2>` User who you wanna search for as well
    - `<history>` Used to get either match history or active matches

    """
    logger.debug(f'get_matches_all: {user}, {user2}, {history}')

    # Get all matches with user.id
    if history:
        matches = database_operation.get_match_history_info_all(
            DbHandler.db_cnc, user.id)
    else:
        matches = database_operation.get_match_info_all(
            DbHandler.db_cnc, user.id)
    logger.debug(f'matches: {matches}')

    # Check if matches is valid
    if not matches:
        logger.error('matches is zero')
        if history:
            raise exception.UnableToRead(attribute='matches')
        else:
            raise exception.UnableToRead(attribute='matches_history')

    # Create match list to return
    match_list = []

    # Loop through matches and create matches to return
    for match in matches:
        # Get challenger Account and check if valid
        if history:
            challenger_id = match[2]
        else:
            challenger_id = match[4]
        challenger = acc.get_account_from_db(ctx, DbHandler.db_cnc,
                                             challenger_id)
        logger.debug(f'challenger: {challenger}')
        if not challenger:
            logger.error('Unable to get challenger acc')
            raise exception.UnableToRead(class_='Account', attribute='account')

        # Get recipient Account and check if valid
        if history:
            recipient_id = match[3]
        else:
            recipient_id = match[4]
        recipient = acc.get_account_from_db(ctx, DbHandler.db_cnc,
                                            recipient_id)
        logger.debug(f'recipient: {recipient}')
        if not recipient:
            logger.error('Unable to get challenger acc')
            raise exception.UnableToRead(class_='Account', attribute='account')

        # Check if user2 is not none, to change search to games with user2
        if user2 is not None:
            logger.debug('user2 is not None')
            if challenger.id == user2.id or recipient.id == user2.id:
                pass
            else:
                continue

        # Create match and check if valid before appending to list
        if history:
            logger.debug('history is true')
            # Get winner Account and check if valid
            winner = acc.get_account_from_db(ctx, DbHandler.db_cnc, match[4])
            logger.debug(f'winner: {winner}')
            if not winner:
                logger.error('Unable to get winner account')
                raise exception.UnableToRead(class_='Account',
                                             attribute='account')
            append_match = Match(match[0], match[1], True, challenger,
                                 recipient, True, winner, match[5], match[6],
                                 match[7], True)

        else:
            append_match = Match(match[0],
                                 match[1],
                                 match[2],
                                 challenger,
                                 recipient,
                                 match[5],
                                 _game=match[6],
                                 _format=match[7])

        logger.debug(f'append_match: {append_match}')
        if isinstance(append_match, Match):
            match_list.append(append_match)

    # Check if list has been propagated, return 0 if not
    if not len(match_list):
        logger.debug('match_list length is zero')
        return 0

    # Return matches
    return match_list