all repos — python-meme-bot @ 06f4f2d1be1acacaaf84179a7a8ad0d2b78ef739

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
 49async def _spin(context: CallbackContext, id: float, delay=True):
 50
 51    bet = get_bet(context)
 52    cash = get_cash(context)
 53    
 54    if cash < bet:
 55        await 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 = await 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, data=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
 85async def show_result(context: CallbackContext):
 86    con = context.job.data
 87    return await context.bot.send_message(chat_id=con["chat_id"], text=con["text"], reply_markup=con["reply_markup"], disable_notification=True)
 88    
 89async def 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        await context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", context))
 98    
 99    if amount == 1:
100        return await _spin(context=context, id=update.effective_chat.id)
101    else:
102        return await autospin(context=context, id=update.effective_chat.id, amount=amount)
103    
104async def 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 = await _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    return await context.bot.send_message(chat_id=id, text=result, reply_markup=markup, disable_notification=False)
124
125async def 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    return await context.bot.send_message(chat_id=update.effective_chat.id, text=result)
136    
137def cash(update: Update, context: CallbackContext):
138    
139    cash = get_cash(context) / 100
140    
141    if cash < cash_default / 200:
142        lastreset = get_lastreset(context)
143        today = date.today()
144        
145        if lastreset < today:
146            set_lastreset(context, today)
147            cash = set_cash(context, cash_default)
148            
149            result = l("cash_reset", context).format(c.format_author(update.effective_user), cash / 100)
150            return update.message.reply_text(text=result)
151        else:
152            return update.message.reply_text(text=l("cash_reset_fail", context).format(c.format_author(update.effective_user), cash))
153    
154    
155    result = l("current_cash", context).format(c.format_author(update.effective_user), cash)
156    return update.message.reply_text(text=result)
157    
158def get_multiplier(value: int):
159    
160    try:
161        values = c.slot_machine_value[value]
162    except IndexError:
163        values = c.slot_machine_value[5]
164    
165    try:
166        return c.win_table[values]
167    except KeyError:
168        return 0