def test_getActiveGameInChannel(self):
        original: GamblingGame = GamblingUtil.create_game(
            123, "new_game", 5, ["hello", "hi"], 123)
        GamblingUtil.update_game_status(original, Util.GamblingStatus.ready,
                                        123, 456)

        game = GamblingUtil.get_active_game_in_channel(123, 456)
        assert len(game) == 0
        game = GamblingUtil.get_active_game_in_channel(123, 123)
        assert game == [original]

        another = GamblingUtil.create_game(123, "new_game", 5, ["hello", "hi"],
                                           123)
        GamblingUtil.update_game_status(another, Util.GamblingStatus.ready,
                                        123, 456)
        game = GamblingUtil.get_active_game_in_channel(123, 123)
        assert len(game) == 2
Esempio n. 2
0
    async def betting_bte_command(self, ctx: commands.Context, *argv):
        guild = ctx.guild
        channel = ctx.channel
        ##
        if channel == None:
            await ctx.author.send('請到開啟賭牌的頻道下注!!')
            return
        if guild == None:
            await ctx.author.send('無法處理的伺服器!')
            return
        if not self.database.has_channel(guild.id, channel.id,
                                         Util.ChannelType.AUTO_DELETE):
            await ctx.message.delete()
        flag = False
        if len(argv) < 2:
            flag = True
        elif not argv[0].isdigit() or not argv[1].isdigit():
            flag = True
        if flag:
            await ctx.author.send('參數錯誤: `!bet 加注數量 下注編號 [賭局ID]`')
            return
        bet_amount = int(argv[0])  # 加注數量
        choice_id = int(argv[1])  # 下注編號

        _bettings = GamblingUtil.get_active_game_in_channel(
            guild.id, ctx.channel.id)
        ready_games = []
        game: GamblingGame
        for game in _bettings:
            if game.status == Util.GamblingStatus.ready:
                ready_games.append(game)
        if len(ready_games) == 0:
            await ctx.author.send('參數錯誤: 這個頻道沒有開啟的賭局!')
            return
        if len(ready_games) > 1:
            if len(argv) <= 2:
                tem_betting_list = ''
                for game in ready_games:
                    tem_betting_list += '\n賭局名:{}, id: {}'.format(
                        game.name, game.id)
                await ctx.author.send('這個頻道有複數賭局開啟中\n請指定賭局`!bet 下注數 賭局ID`' +
                                      tem_betting_list)
                return
            if not argv[2].isdigit():
                await ctx.author.send('參數錯誤: 賭局ID必須是數字')
            betting_id = int(argv[2])
            flag = True
            for game in ready_games:
                if betting_id == game.id:
                    flag = False
                    break
            if flag:
                ctx.author.send('參數錯誤: 這個<#{}>沒有ID為: {}的賭局'.format(
                    ctx.channel.id, betting_id))
            ready_games = game
        elif len(ready_games) == 1:
            ready_games = ready_games[0]
        else:
            await ctx.channel.send('未預期的錯誤: <@!326752816238428164>快修阿!')
            return
        game: GamblingGame = ready_games
        if game.status != Util.GamblingStatus.ready:
            await ctx.author.send('權限錯誤: 現在的賭局狀態為: {}不能下注'.format(
                Util.GamblingStatus(game.status).name))
            return
        if bet_amount < 1:
            await ctx.author.send('參數錯誤: 下注🍗不能為此數: {}'.format(bet_amount))
            return
        # 所有可下注選項
        betting_item_list = json.load(game.item_list)
        if not choice_id < len(betting_item_list):
            await ctx.author.send('參數錯誤: 不存在編號: {}'.format(choice_id))
            return
        member = self.database.get_member(ctx.author.id)
        if member == None:
            member = self.database.add_member(ctx.author.id)
        require_amount = bet_amount * game.base
        if member.coin < require_amount:
            await ctx.author.send('道德錯誤: 你的🍗不夠啦! ...剩餘{},下注{}'.format(
                member.coin, require_amount))
            return
        self.database.add_coin(member, -1 * require_amount)
        GamblingUtil.add_bet(game=game,
                             user_id=member.member_id,
                             amount=require_amount,
                             item_index=choice_id)

        await ctx.author.send('你成功對{} 下注了{}點🍗。...餘額為: {}。'.format(
            betting_item_list[choice_id], require_amount, member.coin))