all repos — python-meme-bot @ e11e33a9f3ccc4aec435d101110c5a6c19d86faf

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