Ejemplo n.º 1
0
 def test_string_from(self):
     maxwidths = [10, 3, 10, 10, 10]
     fields = AsciiTable.fields_from(self.tsv_3_lines[2], self.tsv_delim)
     output = AsciiTable.string_from(maxwidths, fields)
     self.assertEqual(
         output,
         "|            | 0.1 | hit a cell | select cel | .cancellin |")
Ejemplo n.º 2
0
    async def roll(self, dice, display_channel):

        start = dt.utcnow()
        now = dt.utcnow()
        new_dice = Dice(2)
        new_dice.roll()
        roll_m = await display_channel.send(AsciiTable.show_dice(new_dice))
        while (now - start).total_seconds() < self.roll_duration:
            new_dice = Dice(2)
            new_dice.roll()
            await roll_m.edit(content=AsciiTable.show_dice(new_dice))
            await asyncio.sleep(0.2)
            now = dt.utcnow()
        dice.roll()
        await roll_m.edit(content=AsciiTable.show_dice(dice))
        vals = dice.values
        e0 = E.for_number(vals[0])
        e1 = E.for_number(vals[1])
        await roll_m.add_reaction(e0)
        if e0 != e1:
            await roll_m.add_reaction(e1)
        else:
            # doubles
            await roll_m.add_reaction(E.MULTIPLY)
            await roll_m.add_reaction(E.TWO)
        await asyncio.sleep(self.leave_dice_for)
        return dice
Ejemplo n.º 3
0
 def test_linebreak_from(self):
     maxwidths = [10, 3, 10, 10, 10]
     self.assertEqual(
         AsciiTable.linebreak_from(maxwidths),
         '+------------+-----+------------+------------+------------+')
     maxwidths = [7, 3, 7, 7, 7, 7]
     self.assertEqual(
         AsciiTable.linebreak_from(maxwidths),
         '+---------+-----+---------+---------+---------+---------+')
Ejemplo n.º 4
0
 def test_table_width_for(self):
     fieldwidths = [23, 3, 38, 11, 11]
     output = AsciiTable.table_width_for(fieldwidths)
     self.assertLessEqual(output, 120)
     self.assertEqual(output, 102)
     fieldwidths = [10, 3, 10, 10, 10]
     output = AsciiTable.table_width_for(fieldwidths)
     self.assertLessEqual(output, 60)
     self.assertEqual(output, 59)
Ejemplo n.º 5
0
 def test_max_fieldwidths_from(self):
     delim = '\t'
     table_width = 80
     self.assertEqual(
         AsciiTable.max_fieldwidths_from(self.tsv_3_lines, self.tsv_delim),
         [23, 3, 38, 11, 11])
     self.assertEqual(
         AsciiTable.max_fieldwidths_from(self.tsv_3_lines_uneven_len,
                                         self.tsv_delim),
         [23, 3, 38, 11, 11, 21])
Ejemplo n.º 6
0
 def test_fields_from(self):
     delim = '\t'
     input = "	0.1	hit a cell in a current user's booking	select cell	.cancelling"
     self.assertEqual(AsciiTable.fields_from(input, delim), [
         '', '0.1', "hit a cell in a current user's booking", 'select cell',
         '.cancelling'
     ])
     input = ""
     self.assertEqual(AsciiTable.fields_from(input, delim), [''])
     input = "\t"
     self.assertEqual(AsciiTable.fields_from(input, delim), ['', ''])
Ejemplo n.º 7
0
 def test_fit(self):
     max_fieldwidths = [23, 3, 38, 11, 11]
     table_width = 120
     self.assertEqual(AsciiTable.fit(max_fieldwidths, table_width),
                      [23, 3, 38, 11, 11])
     table_width = 60
     self.assertEqual(AsciiTable.fit(max_fieldwidths, table_width),
                      [10, 3, 10, 10, 10])
     max_fieldwidths = [23, 3, 38, 11, 11, 21]
     self.assertEqual(AsciiTable.fit(max_fieldwidths, table_width),
                      [7, 3, 7, 7, 7, 7])
Ejemplo n.º 8
0
 def test_limited_lines(self):
     maxwidths = [10, 3, 10, 10, 10]
     fields = AsciiTable.fields_from(self.tsv_3_lines[2], self.tsv_delim)
     expected = [
         '|            | 0.1 | hit a      | select     | .cancellin |',
         '|            |     | cell in a  | cell       | g          |',
         '|            |     | current    |            |            |',
         "|            |     | user's     |            |            |",
         '|            |     | booking    |            |            |'
     ]
     output = AsciiTable.limited_lines(maxwidths, fields)
     self.assertEqual(output, expected)
Ejemplo n.º 9
0
    def test_table_limited(self):
        table_width = 50
        input = ['']
        self.assertEqual(
            AsciiTable.table_limited(input, self.tsv_delim, table_width),
            '+--+')
        table_width = 80
        #self.maxDiff = None #<- uncomment to see long string differences
        expected = """\
+---------------------+-----+---------------------+-------------+-------------+
| state               | s.e | event               | action      | next state  |
+---------------------+-----+---------------------+-------------+-------------+
| .idle  0 cells      | 0.0 | hit a free cell     | select cell | .booking1   |
| selected            |     |                     |             |             |
+---------------------+-----+---------------------+-------------+-------------+
|                     | 0.1 | hit a cell in a     | select cell | .cancelling |
|                     |     | current user's      |             |             |
|                     |     | booking             |             |             |
+---------------------+-----+---------------------+-------------+-------------+"""
        self.assertEqual(
            AsciiTable.table_limited(self.tsv_3_lines, self.tsv_delim,
                                     table_width), expected)
        #print("\n"+table_limited(input, delim, table_width))

        # verify that uneven number of fields in lines is supported graciosly
        table_width = 80
        expected = """\
+--------------+-----+--------------+-------------+-------------+--------------+
| state        | s.e | event        | action      | next state  |              |
+--------------+-----+--------------+-------------+-------------+--------------+
| .idle  0     | 0.0 | hit a free   | select cell | .booking1   | UNKNOWN      |
| cells        |     | cell         |             |             | FLYING       |
| selected     |     |              |             |             | OBJECT       |
+--------------+-----+--------------+-------------+-------------+--------------+
|              | 0.1 | hit a cell   | select cell | .cancelling |              |
|              |     | in a         |             |             |              |
|              |     | current      |             |             |              |
|              |     | user's       |             |             |              |
|              |     | booking      |             |             |              |
+--------------+-----+--------------+-------------+-------------+--------------+"""
        output = AsciiTable.table_limited(self.tsv_3_lines_uneven_len,
                                          self.tsv_delim, table_width)
        self.assertEqual(output, expected)
Ejemplo n.º 10
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')
Ejemplo n.º 11
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
Ejemplo n.º 12
0
 def test_len_of(self):
     input = ['']
     self.assertEqual(AsciiTable.len_of(input), 0)
     input = ['', '0.1', 'select cell', '.cancelling']
     self.assertEqual(AsciiTable.len_of(input), 25)
Ejemplo n.º 13
0
    def test_splitme(self):
        input = "deselect the selected"
        self.assertEqual(len(input), 21)
        limit = 25
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselect the selected', ''))
        limit = 21
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselect the selected', ''))
        limit = 20
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselect the', ' selected'))
        limit = 15
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselect the', ' selected'))
        limit = 12
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselect', ' the selected'))
        limit = 11
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselect', ' the selected'))
        limit = 9
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselect', ' the selected'))
        limit = 8
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselect', ' the selected'))
        limit = 7
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('deselec', 't the selected'))
        limit = 1
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('d', 'eselect the selected'))
        limit = 0
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('d', 'eselect the selected'))
        limit = -7
        self.assertEqual(AsciiTable.splitme(input, limit),
                         ('d', 'eselect the selected'))

        input = ""
        self.assertEqual(len(input), 0)
        limit = 5
        self.assertEqual(AsciiTable.splitme(input, limit), ('', ''))
        limit = 0
        self.assertEqual(AsciiTable.splitme(input, limit), ('', ''))
        limit = -5
        self.assertEqual(AsciiTable.splitme(input, limit), ('', ''))