all repos — python-meme-bot @ d7425d4fa8d0c7e35a45f0137f3d3ea9680c9b5f

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