all repos — python-meme-bot @ 6cb1f2a6c6b0fef96d4e08b11833b2b4b55f71d9

Telegram Bot that uses PIL to compute light image processing.

Slot.py (view raw)

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