all repos — python-meme-bot @ 14b60490cbc0253e75af3eca694ddbb97ca2f5c0

Telegram Bot that uses PIL to compute light image processing.

Slot.py (view raw)

  1from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton
  2from telegram.ext import CallbackContext
  3from datetime import date
  4from Constants import get_localized_string as l
  5import Constants as c
  6
  7autospin_cap = 20
  8lastreset_default = date(1970, 1, 1)
  9cash_default = 5000
 10bet_default = 50
 11slot_emoji = '🎰'
 12
 13
 14def read_arg(context: CallbackContext, default=None, cast=int):
 15    try:
 16        return cast(context.args[0].replace(",", ".").replace("€", ""))
 17    except (TypeError, IndexError, ValueError):
 18        return default
 19
 20
 21def set_user_value(context: CallbackContext, key: str, amount):
 22    context.user_data[key] = amount
 23    return amount
 24
 25
 26def get_user_value(context: CallbackContext, key: str, default):
 27    try:
 28        return context.user_data[key]
 29    except KeyError:
 30        # print(f"set {key} to {str(default)}")
 31        return set_user_value(context, key, default)
 32
 33
 34def get_cash(context: CallbackContext):
 35    return get_user_value(context, "cash", cash_default)
 36
 37
 38def set_cash(context: CallbackContext, amount: int):
 39    return set_user_value(context, "cash", amount)
 40
 41
 42def get_bet(context: CallbackContext):
 43    return get_user_value(context, "bet", bet_default)
 44
 45
 46def set_lastreset(context: CallbackContext, amount: int):
 47    return set_user_value(context, "lastreset", amount)
 48
 49
 50def get_lastreset(context: CallbackContext):
 51    return get_user_value(context, "lastreset", lastreset_default)
 52
 53
 54def set_bet(context: CallbackContext, amount: int):
 55    return set_user_value(context, "bet", max(0, amount))
 56
 57
 58async def _spin(context: CallbackContext, id: float, delay=True):
 59    bet = get_bet(context)
 60    cash = get_cash(context)
 61
 62    if cash < bet:
 63        await context.bot.send_message(chat_id=id, text=l("not_enough_cash", context))
 64        return None
 65
 66    cash = set_cash(context, cash - bet)
 67
 68    message = await context.bot.send_dice(chat_id=id, emoji=slot_emoji, disable_notification=True)
 69
 70    multiplier = get_multiplier(message.dice.value)
 71
 72    win = bet * multiplier
 73
 74    cash = set_cash(context, cash + win)
 75
 76    if delay:
 77        markup = InlineKeyboardMarkup(
 78            [[InlineKeyboardButton(text=l("reroll", context).format(bet / 100), callback_data="reroll 1")]])
 79
 80        text = l("you_lost", context) if multiplier == 0 else l("you_won", context).format(win / 100)
 81        if bet != 0:
 82            text += l("cash_result", context).format(cash / 100)
 83
 84        args = {
 85            "chat_id": id,
 86            "text": text,
 87            "reply_markup": markup
 88        }
 89        context.job_queue.run_once(show_result, 2, data=args, name=str(id))
 90    else:
 91        message.edit_reply_markup(InlineKeyboardMarkup(
 92            [[InlineKeyboardButton(text=l("fast_output", context).format(win / 100), callback_data="none")]]))
 93    return win
 94
 95
 96async def show_result(context: CallbackContext):
 97    con = context.job.data
 98    return await context.bot.send_message(chat_id=con["chat_id"], text=con["text"], reply_markup=con["reply_markup"],
 99                                          disable_notification=True)
100
101
102async def spin(update: Update, context: CallbackContext):
103    bet = get_bet(context) / 100
104
105    amount = read_arg(context, 1)
106
107    if amount > 1 and update.effective_chat.type != 'private':
108        amount = 1
109        await context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", context))
110
111    if amount == 1:
112        return await _spin(context=context, id=update.effective_chat.id)
113    else:
114        return await autospin(context=context, id=update.effective_chat.id, amount=amount)
115
116
117async def autospin(context: CallbackContext, id: int, amount: int):
118    bet = get_bet(context) / 100
119    count = 0
120    total_win = 0
121    amount = max(2, min(autospin_cap, amount))
122
123    for i in range(amount):
124
125        win = await _spin(context=context, id=id, delay=False)
126
127        if win is None:
128            break
129
130        count += 1
131        total_win += win
132
133    result = l("summary", context).format(count * bet, total_win / 100)
134    markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Altri {} spin (-{}€)".format(amount, bet * amount),
135                                                         callback_data=f"reroll {amount}")]])
136    return await context.bot.send_message(chat_id=id, text=result, reply_markup=markup, disable_notification=False)
137
138
139async def bet(update: Update, context: CallbackContext):
140    amount = read_arg(context=context, cast=float)
141
142    if amount is not None:
143        bet = set_bet(context, int(amount * 100))
144    else:
145        bet = get_bet(context)
146
147    result = l("current_bet", context).format(c.format_author(update.effective_user), bet / 100)
148    return await context.bot.send_message(chat_id=update.effective_chat.id, text=result)
149
150
151def cash(update: Update, context: CallbackContext):
152    cash = get_cash(context) / 100
153
154    if cash < cash_default / 200:
155        lastreset = get_lastreset(context)
156        today = date.today()
157
158        if lastreset < today:
159            set_lastreset(context, today)
160            cash = set_cash(context, cash_default)
161
162            result = l("cash_reset", context).format(c.format_author(update.effective_user), cash / 100)
163            return update.message.reply_text(text=result)
164        else:
165            return update.message.reply_text(
166                text=l("cash_reset_fail", context).format(c.format_author(update.effective_user), cash))
167
168    result = l("current_cash", context).format(c.format_author(update.effective_user), cash)
169    return update.message.reply_text(text=result)
170
171
172def get_multiplier(value: int):
173    try:
174        values = c.slot_machine_value[value]
175    except IndexError:
176        values = c.slot_machine_value[5]
177
178    try:
179        return c.win_table[values]
180    except KeyError:
181        return 0