示例#1
0
    async def show(self, bot, dice, shooter_id, table, first_roll,
                   display_channel):
        # set pace based on test mode
        self.timeout = 6.0
        self.roll_duration = 0.0
        self.nap = 0.4
        self.leave_dice_for = 2.0
        if bot.TEST_MODE or table.empty:
            self.timeout = 2.0
            self.roll_duration = 0.0
            self.nap = 0.1
            self.leave_dice_for = 1.0
        if shooter_id:
            user = await bot.fetch_user(shooter_id)
            bets_in = T.bold("Bets are IN!")
            wait_str = T.block_quote(f"Waiting {self.timeout:.0f} seconds for "
                                     f"{user.display_name} to `roll`...")
            waiting = await display_channel.send(f'{bets_in}\n{wait_str}')
        else:
            await display_channel.send(f'No shooter. Auto-rolling...')
            return await self.roll(dice, display_channel)

        def check(m):
            if m.author.id != shooter_id:
                return False
            return True if self.__class__.get_roll_command(m) else False

        try:
            resp = await bot.wait_for('message',
                                      check=check,
                                      timeout=self.timeout)
            roll_cmd = self.__class__.get_roll_command(resp)
            if not roll_cmd:
                raise SceneException(
                    f"Unexpected roll command: {resp.content}")

            # add some extra flavor if the shooter blew on the dice
            if roll_cmd == self.__class__.BLOW_CMDS[0]:
                await resp.add_reaction(E.BLOW)
                await resp.add_reaction(E.DIE)

            # full banter only happens when the player initiates the roll.
            banter = self.banter(table.point, True)
            await display_channel.send(banter)

        except asyncio.TimeoutError:
            async with display_channel.typing():
                await waiting.add_reaction(E.HOURGLASS)
                await asyncio.sleep(self.nap)
                new_content = waiting.content + " nevermind, I can do it for you."
                await waiting.edit(content=new_content)
            await asyncio.sleep(self.nap)
            banter = self.banter(table.point, False)
            await display_channel.send(banter)
        finally:
            await asyncio.sleep(3 * self.nap)
            return await self.roll(dice, display_channel)
示例#2
0
 async def report_exception(
     self,
     exception: Exception
 ):
     exception_str = T.block_quote(
         f'{T.inline_mono(type(exception))}\n{str(exception)}')
     await self.display_channel.send(
         f'Sorry to show you this, but could you let Andy know he has an '
         f'unhandled exception:\n{exception_str}',
     )
示例#3
0
    def make_table(self, table, allowed_bets, time_remaining):
        ascii_table = AsciiTable.from_table(table)

        place_bets = T.bold("Place your bets!")
        place_bets += f" You have {time_remaining:.0f} "
        if time_remaining < self.timeout:
            place_bets += "more seconds to bet..."
        else:
            place_bets += "seconds."

        valid_bets = ", ".join([T.inline_mono(b) for b in allowed_bets])
        valid_bets = T.block_quote("Valid bets are: " + valid_bets)
        return (f'{ascii_table}\n' f'{place_bets}\n' f'{valid_bets}\n')
示例#4
0
    async def show(self, bot, table, allowed_bet_types, display_channel,
                   dealer):

        if bot.TEST_MODE:
            self.timeout = 2.5
        else:
            if table.empty:
                self.timeout = 5.0

        allowed_bets = [bt.cmd_name for bt in allowed_bet_types]

        if not allowed_bets:
            no_bets = T.block_quote("No allowed bets right now!")
            ascii_table = AsciiTable.from_table(table)
            await display_channel.send(f'{ascii_table}\n' f'{no_bets}\n')
            return []

        def check(m):
            tokens = m.content.lower().strip().split()
            bet_type = tokens[0]
            if bet_type == 'roll':
                if m.channel == display_channel:
                    msg = random.choice(self.wait_messages)
                    asyncio.get_event_loop().create_task(m.reply(msg))
            if bet_type not in allowed_bets:
                return False
            try:
                amount = tokens[1]
                float(amount)
                return True
            except Exception:
                return False
            return False

        bet_msg = await display_channel.send(
            self.make_table(table, allowed_bets, self.timeout))

        timeout = self.timeout
        start = dt.utcnow()
        while True:
            try:
                left = timeout - (dt.utcnow() - start).total_seconds()
                left = max(left, self.time_increase_per_bet)
                left = min(left, self.timeout)
                if left < 0.0:
                    raise asyncio.TimeoutError()
                m = await bot.wait_for('message', check=check, timeout=left)
                tokens = m.content.lower().strip().split()
                bet_type = tokens[0]
                amount = float(tokens[1])
                u_id = m.author.id

                bet = None
                for bt in allowed_bet_types:
                    if bt.cmd_name == bet_type:
                        bet = bt.bet_class(amount, u_id)
                        break
                if not bet:
                    raise ValueError("No bet class found")
                player = table.player_for(u_id)
                if not player:
                    player = table.create_player(u_id, m.author.display_name)
                try:
                    table.sit(player.id)
                except AlreadyExists:
                    pass
                left = timeout - (dt.utcnow() - start).total_seconds()
                left = max(left, self.time_increase_per_bet)
                left = min(left, self.timeout)
                await self.handle_bet(bet, m, player, dealer, bet_msg, table,
                                      allowed_bets, left)
            except asyncio.TimeoutError:
                await bet_msg.add_reaction(E.HOURGLASS)
                break
        return self.new_bets