Пример #1
0
 async def betting_start_command(self, ctx: commands.Context, *argv):
     if len(argv) != 1 or not argv[0].isdigit():
         await ctx.channel.send('參數錯誤: 請使用`!betitng start 賭局id`')
         return
     game_id = int(argv[0])
     game: GamblingGame = GamblingUtil.get_game(game_id)
     if game == None:
         await ctx.channel.send(
             '參數錯誤: 無法找到id 為:{} 的賭盤。請使用`!betitng list`查詢。'.format(game_id))
         return
     if game.creater_id != ctx.author.id:
         await ctx.channel.send('權限錯誤: 這個賭盤不是你創建的!')
         return
     if game.guild_id != ctx.guild.id:
         await ctx.channel.send('權限錯誤: 這個賭盤不是在這裡創建的,創建的伺服為: {}'.format(
             self.bot.get_guild(game.guild_id).name))
         return
     if game.status != Util.GamblingStatus.init:
         await ctx.channel.send('權限錯誤: 這個賭盤的狀態為: {},無法開始。'.format(
             Util.GamblingStatus(game.status).name))
         return
     embed = GamblingEmbed.get_betting_embed(game)
     msg = await ctx.channel.send(embed=embed)
     await msg.pin()
     GamblingUtil.update_game_status(game, Util.GamblingStatus.ready,
                                     ctx.channel.id, msg.id)
    def test_addGamePoolAmount(self):
        original: GamblingGame = GamblingUtil.create_game(
            123, "new_game", 5, ["hello", "hi"], 123)
        GamblingUtil.add_game_pool_amount(original, 300)
        original = GamblingUtil.get_game(original.id)

        assert original.pool == 300
    def test_updateGameStatus(self):
        game: GamblingGame = GamblingUtil.create_game(123, "new_game", 5,
                                                      ["hello", "hi"], 123)
        GamblingUtil.update_game_status(game, Util.GamblingStatus.ready, 123,
                                        456)
        result_game = GamblingUtil.get_game(game.id)

        assert game == result_game
    def test_addBetSuccess(self):
        original: GamblingGame = GamblingUtil.create_game(
            123, "new_game", 5, ["hello", "hi"], 123)
        GamblingUtil.update_game_status(original, Util.GamblingStatus.ready,
                                        123, 456)
        assert GamblingUtil.add_bet(original, 123, 10, 1)

        original: GamblingGame = GamblingUtil.get_game(original.id)
        assert original.pool == 10

        assert GamblingUtil.add_bet(original, 123, 10, 1)
        assert original.pool == 20
Пример #5
0
 async def betting_lock_command(self, ctx: commands.Context, *argv):
     send_target = None
     if ctx.channel == None:
         send_target = ctx.author
     else:
         send_target = ctx.channel
     if len(argv) != 1 or not argv[0].isdigit():
         await send_target.send('參數錯誤: `!betting lock 賭盤id`')
         return
     game_id = int(argv[0])
     game: GamblingGame = GamblingUtil.get_game(game_id)
     if game == None:
         await send_target.send('參數錯誤: 沒有ID為{}的賭盤'.format(game_id))
         return
     if game.creater_id != ctx.author.id:
         await send_target.send('權限錯誤: 你不是創建這個賭盤的人')
         return
     if game.status != Util.GamblingStatus.ready:
         await send_target.send('權限錯誤: 這個賭盤的狀態為:{}'.format(game.status.name)
                                )
         return
     GamblingUtil.update_game_status(game, Util.GamblingStatus.wait)
     await send_target.send('更新賭盤狀態為: {}'.format(
         Util.GamblingStatus.wait.name))
Пример #6
0
    async def betting_end_command(self, ctx: commands.Context, *argv):
        if len(argv) != 2:
            await ctx.channel.send('參數錯誤: `!betting lock 勝利編號 賭盤id`')
            return
        if not argv[1].isdigit() or not argv[0].isdigit():
            await ctx.channel.send('參數錯誤: `!betting lock 勝利編號 賭盤id`')
            return
        game_id = int(argv[1])
        option_index = int(argv[0])
        game: GamblingGame = GamblingUtil.get_game(int(argv[1]))
        if not game:
            await ctx.channel.send('參數錯誤: 找不到id 為{}的賭盤'.format(game_id))
            return
        if game.creater_id != ctx.author.id:
            await ctx.channel.send('權限錯誤: 你不是創建這個賭盤的人')
            return
        if game.status != Util.GamblingStatus.wait:
            await ctx.channel.send('權限錯誤: 這個賭盤的狀態為:{}'.format(game.status.name)
                                   )
            return
        betting_items = json.loads(game.item_list)
        if option_index < 0 or option_index > len(betting_items):
            await ctx.channel.send(f'參數錯誤: `勝利編號 {option_index} 為無效編號`')
            return

        #[第一項總注,第n項總注...,總項總注]
        member_charge_sum = [0] * len(betting_items)
        member_bet = {}
        winning_item = betting_items[option_index]
        bets = GamblingUtil.get_bets(game)
        bet: GamblingBet
        for bet in bets:
            member_charge_sum[bet.item_index] += bet.charge
            member_bet[bet.member_id] = member_bet.get(bet.member_id,
                                                       0) + bet.charge

        for member_id in member_bet:
            member: Member = self.database.get_member(member_id)
            if member == None:
                continue
            token_spent = 0
            if member_bet[member_id].get(winning_item, 0) != 0:
                token_spent = member_bet[member_id][winning_item]
            coin_won = 0
            winning_sum = member_charge_sum[option_index]
            if winning_sum != 0:
                coin_won = int(token_spent / winning_sum * game.base *
                               game.pool)
            user = await self.bot.fetch_user(member_id)
            if user == None:
                await ctx.channel.send('無法找到該id的用戶: {},跳過!')
                continue
            self.database.add_token(member_id, coin_won)
            member = self.database.get_member(member_id)
            await user.send('恭喜獲得{}點🍗, ...結餘:{}'.format(coin_won, member.coin))

        GamblingUtil.update_game_status(game,
                                        Util.GamblingStatus.end,
                                        winning_index=option_index)

        channel = await self.bot.fetch_channel(game.channel_id)
        if channel != None:
            msg = await channel.fetch_message(game.message_id)
            await msg.edit(embed=GamblingEmbed.get_betting_embed(
                self.bot, self.database, game))
            if msg.pinned:
                await msg.unpin()
        await ctx.channel.send('結算成功')