all repos — python-meme-bot @ dd64024e9c2abe30460ea3af2586ad2ad9f3f4ba

Telegram Bot that uses PIL to compute light image processing.

Games.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 = 50
  9lastreset_default = date(1970, 1, 1)
 10cash_default = 5000
 11bet_default = 50
 12slot_emoji = '🎰'
 13lang = 'us'
 14
 15def read_arg(context: CallbackContext, default=None, cast=int):
 16    try:
 17        return cast(context.args[0])
 18    except (TypeError, IndexError, ValueError):
 19        return default
 20
 21def set_user_value(context: CallbackContext, key:str, amount):
 22    context.user_data[key] = amount
 23    return amount
 24
 25def get_user_value(context: CallbackContext, key:str, default):
 26    try:
 27        return context.user_data[key]
 28    except KeyError:
 29        print(f"set {key} to {str(default)}")
 30        return set_user_value(context, key, default)
 31
 32def get_cash(context: CallbackContext):
 33    cash = get_user_value(context, "cash", cash_default)
 34    
 35    if cash == 0:
 36        lastreset = get_lastreset(context)
 37        today = date.today()
 38        
 39        if lastreset < today:
 40            set_lastreset(context, today)
 41            cash = set_cash(context, cash_default)
 42
 43    return cash
 44
 45def set_cash(context: CallbackContext, amount: int):
 46    return set_user_value(context, "cash", amount)
 47
 48def get_bet(context: CallbackContext):
 49    return get_user_value(context, "bet", bet_default)
 50
 51def set_lastreset(context: CallbackContext, amount: int):
 52    return set_user_value(context, "lastreset", amount)
 53
 54def get_lastreset(context: CallbackContext):
 55    return get_user_value(context, "lastreset", lastreset_default)
 56    
 57def set_bet(context: CallbackContext, amount: int):
 58    return set_user_value(context, "bet", amount)
 59
 60def _spin(context: CallbackContext, id: float, markup: InlineKeyboardMarkup = ""):
 61
 62    bet = get_bet(context)
 63    cash = get_cash(context)
 64    
 65    if cash < bet:
 66        context.bot.send_message(chat_id=id, text=l("not_enough_cash", lang))
 67        return None
 68    
 69    cash = set_cash(context, cash - bet)
 70    
 71    message = context.bot.send_dice(chat_id=id, emoji=slot_emoji)
 72    
 73    multiplier = get_multiplier(message.dice.value)
 74    
 75    win = bet * multiplier
 76    
 77    cash = set_cash(context, cash + win)
 78        
 79    text = l("you_lost", lang) if win == 0 else l("you_won", lang).format(win / 100)
 80    
 81    text += l("cash_result", lang).format(cash / 100)
 82    
 83    time.sleep(2)
 84    context.bot.send_message(chat_id=id, text=text, reply_markup=markup)
 85    return win
 86
 87def spin(update: Update, context: CallbackContext):
 88    
 89    bet = get_bet(context) / 100
 90    
 91    amount = read_arg(context, 1)
 92    
 93    if amount > 1 and update.effective_chat.type != 'private':
 94        amount = 1
 95        context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", lang))
 96    
 97    if amount == 1:
 98        markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("reroll", lang).format(bet), callback_data="callback_1")]])
 99        _spin(context=context, id=update.effective_chat.id, markup=markup)
100    else:
101        amount = max(1, min(amount, autospin_cap))
102        count = 0
103        total_win = 0
104        for i in range(amount):
105            win = _spin(context=context, id=update.effective_chat.id, markup="")
106            
107            if win is None:
108                break
109            
110            count += 1
111            total_win += win
112        
113        result = l("summary", lang).format(count * bet, total_win / 100)
114        markup = "" #InlineKeyboardMarkup([[InlineKeyboardButton(text="Altri {} spin (-{}€)".format(amount, bet * amount), callback_data="callback_2")]])
115        context.bot.send_message(chat_id=update.effective_chat.id, text=result, reply_markup=markup)
116
117def bet(update: Update, context: CallbackContext):
118    
119    amount = read_arg(context=context, cast=float)
120    
121    if amount is not None:
122        bet = set_bet(context, int(amount * 100))
123    else:
124        bet = get_bet(context)
125    
126    result = l("current_bet", lang).format(c.format_author(update.effective_user), bet / 100)
127    context.bot.send_message(chat_id=update.effective_chat.id, text=result)
128    
129def cash(update: Update, context: CallbackContext):
130    cash = get_cash(context) / 100
131    result = l("current_cash", lang).format(c.format_author(update.effective_user), cash)
132    context.bot.send_message(chat_id=update.effective_chat.id, text=result)
133    
134def get_multiplier(value: int):
135    
136    try:
137        values = c.slot_machine_value[value]
138    except IndexError:
139        values = c.slot_machine_value[5]
140    
141    try:
142        return c.win_table[values]
143    except KeyError:
144        return 0