all repos — python-meme-bot @ 881781f58f4949319ef6afcbd6421c43e8a6ae24

Telegram Bot that uses PIL to compute light image processing.

python_meme_bot/slot.py (view raw)

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