def test_getBets(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)

        bets = GamblingUtil.get_bets(original)
        assert len(bets) == 1
Beispiel #2
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('結算成功')
Beispiel #3
0
    def get_betting_embed(bot: commands.Bot, database: KfpDb,
                          game: GamblingGame):
        guild: Guild = bot.get_guild(game.guild_id)
        if guild == None:
            return Embed(title='錯誤',
                         description='無法處理的伺服id: {}'.format(game.guild_id))
        embed = Embed()
        embed.colour = Color(randint(0, 0xffffff))
        embed.title = game.name
        _description = ''
        if game.status == Util.GamblingStatus.ready:
            _description += '請各位輸入`!bet 下注數 下注編號 {}` 開始下注!。\n'.format(game.id)
        if game.status == Util.GamblingStatus.wait:
            _description += '停止下注!'
        _description += '#目前所有選項的期望賠率都是一樣的,有人有想法可以聯絡<@326752816238428164>\n'
        if game.status == Util.GamblingStatus.ready:
            _description += '<@{}>可以輸入`!betting lock {}`來停止下注。'.format(
                game.creater_id, game.id)
        if game.status == Util.GamblingStatus.wait:
            _description += '<@{}>可以輸入`!betting end 勝利編號 {}`來結算賭盤。'.format(
                game.creater_id, game.id)
        embed.description = _description

        betting_items = json.loads(game.item_list)
        #[第一項總注,第n項總注...,總項總注]
        member_charge_sum = [0] * len(betting_items)
        member_bet = {}
        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 i, target_name in enumerate(betting_items):
            if member_charge_sum[i] > 0:
                embed.add_field(name='編號: {} #'.format(i) + target_name,
                                value='賠率: {:.3f}'.format(
                                    float((game.pool / member_charge_sum[i]))),
                                inline=False)
            else:
                embed.add_field(name='編號: {} #'.format(i) + target_name,
                                value='賠率: ?',
                                inline=False)
        embed.add_field(name='賭局id', value=str(game.id))
        embed.add_field(name='獎金池', value=str(game.pool * game.base))
        embed.add_field(name='每注單位', value=str('{}🍗'.format(game.base)))
        embed.add_field(name='刷新速度', value='5s')
        embed.add_field(name='狀態', value=Util.GamblingStatus(game.status).name)
        embed.add_field(name='====我是分隔線====', value='#', inline=False)
        inline_flasg = False
        for member_id in member_bet:
            member = guild.get_member(int(member_id))
            if member == None:
                continue
            value = ''
            for bet_item in member_bet[member_id]:
                if game.status == Util.GamblingStatus.end.value:
                    if bet_item == betting_items[game.winning_index]:
                        value += '{}:得到{}點🍗\n'.format(
                            bet_item, member_bet[member_id][bet_item] /
                            member_charge_sum[game.winning_index] *
                            member_charge_sum[-1] * game.base)
                    else:
                        value += '{}:輸掉{}點🍗拉\n'.format(
                            bet_item,
                            member_bet[member_id][bet_item] * game.base)
                else:
                    value += '{}:{}注\n'.format(bet_item,
                                               member_bet[member_id][bet_item])
            embed.add_field(name=member.display_name,
                            value=value[:-1],
                            inline=inline_flasg)
            if not inline_flasg:
                inline_flasg = True

        return embed