all repos — python-meme-bot @ d7425d4fa8d0c7e35a45f0137f3d3ea9680c9b5f

Telegram Bot that uses PIL to compute light image processing.

add bet and cash commands
Marco Andronaco andronacomarco@gmail.com
Mon, 12 Sep 2022 12:47:00 +0200
commit

d7425d4fa8d0c7e35a45f0137f3d3ea9680c9b5f

parent

40c83258dcc1ec16822e667287b4415380300b69

3 files changed, 81 insertions(+), 57 deletions(-)

jump to
M Constants.pyConstants.py

@@ -25,6 +25,20 @@ 'failed_effect': "Impossibile applicare l'effetto."

}, } +def format_author(user): + + if user.username is not None: + return user.full_name + f" ({user.username})" + return user.full_name + +def get_localized_string(text, lang='us'): + try: + return localization[lang][text] + except KeyError: + logging.error("No text was found.") + return "localization error {}{}{}{}{}{}" + + symbols = { "grape", "lemon",

@@ -110,23 +124,3 @@ (2, "bar"): 5,

(2, "lemon"): 2, (2, "grape"): 1 } - -def get_symbols(value: int): - try: - return slot_machine_value[value] - except IndexError: - return slot_machine_value[50] - -def get_multiplier(count: int, symbol: str): - try: - return win_table[(count, symbol)] - except KeyError as e: - return 0 - -def get_localized_string(text, lang='us'): - try: - return localization[lang][text] - except KeyError: - logging.error("No text was found.") - return "localization error {}{}{}{}{}{}" -
M Games.pyGames.py

@@ -4,10 +4,17 @@ from datetime import date

import Constants as c import time +autospin_cap = 50 lastreset_default = date(1970, 1, 1) -saldo_default = 5000 +cash_default = 5000 bet_default = 50 slot_emoji = '🎰' + +def read_arg(context: CallbackContext, default=None, cast=int): + try: + return cast(context.args[0]) + except (TypeError, IndexError, ValueError): + return default def set_user_value(context: CallbackContext, key:str, amount): context.user_data[key] = amount

@@ -20,21 +27,21 @@ except KeyError:

print(f"set {key} to {str(default)}") return set_user_value(context, key, default) -def get_saldo(context: CallbackContext): - saldo = get_user_value(context, "saldo", saldo_default) +def get_cash(context: CallbackContext): + cash = get_user_value(context, "cash", cash_default) - if saldo == 0: + if cash == 0: lastreset = get_lastreset(context) today = date.today() if lastreset < today: set_lastreset(context, today) - saldo = set_saldo(context, saldo_default) + cash = set_cash(context, cash_default) - return saldo + return cash -def set_saldo(context: CallbackContext, amount: int): - return set_user_value(context, "saldo", amount) +def set_cash(context: CallbackContext, amount: int): + return set_user_value(context, "cash", amount) def get_bet(context: CallbackContext): return get_user_value(context, "bet", bet_default)

@@ -51,28 +58,25 @@

def _spin(context: CallbackContext, id: float, markup: InlineKeyboardMarkup = ""): bet = get_bet(context) - saldo = get_saldo(context) + cash = get_cash(context) - if saldo < bet: - context.bot.send_message(chat_id=id, text="Saldo esaurito!") + if cash < bet: + context.bot.send_message(chat_id=id, text="Saldo insufficiente!") return None - saldo = set_saldo(context, saldo - bet) + cash = set_cash(context, cash - bet) message = context.bot.send_dice(chat_id=id, emoji=slot_emoji) - values = c.get_symbols(message.dice.value) - values_count = {i:values.count(i) for i in values} + multiplier = get_multiplier(message.dice.value) - symbol = max(values_count, key=values_count.get) - - win = bet * c.get_multiplier(count=values_count[symbol], symbol=symbol) + win = bet * multiplier - saldo = set_saldo(context, saldo + win) + cash = set_cash(context, cash + win) - text = "Hai perso..." if win == 0 else "Hai vinto {}€!".format(win / 100) + text = "Hai perso..." if win == 0 else "Hai vinto {:0.2f}€!".format(win / 100) - text += " Saldo: {}€.".format(saldo / 100) + text += " Saldo: {:0.2f}€.".format(cash / 100) time.sleep(2) context.bot.send_message(chat_id=id, text=text, reply_markup=markup)

@@ -82,16 +86,13 @@ def spin(update: Update, context: CallbackContext):

bet = get_bet(context) / 100 - try: - amount = int(context.args[0]) - except (TypeError, IndexError): - amount = 1 + amount = read_arg(context, 1) if amount == 1: - - markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Reroll (-{}€)".format(bet), callback_data="callback_1")]]) + markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Reroll (-{:0.2f}€)".format(bet), callback_data="callback_1")]]) _spin(context=context, id=update.effective_chat.id, markup=markup) else: + amount = min(amount, autospin_cap) count = 0 total_win = 0 for i in range(amount):

@@ -106,3 +107,36 @@

result = "Hai giocato {}€ e vinto un totale di {}€".format(count * bet, total_win / 100) markup = "" #InlineKeyboardMarkup([[InlineKeyboardButton(text="Altri {} spin (-{}€)".format(amount, bet * amount), callback_data="callback_2")]]) context.bot.send_message(chat_id=update.effective_chat.id, text=result, reply_markup=markup) + +def bet(update: Update, context: CallbackContext): + + amount = read_arg(context=context, cast=float) + + if amount is not None: + bet = set_bet(context, int(amount * 100)) + else: + bet = get_bet(context) + + result = "{}, il tuo bet attuale è {:0.2f}€.".format(c.format_author(update.effective_user), bet / 100) + context.bot.send_message(chat_id=update.effective_chat.id, text=result) + +def cash(update: Update, context: CallbackContext): + cash = get_cash(context) / 100 + result = "{}, il tuo saldo attuale è {:0.2f}€.".format(c.format_author(update.effective_user), cash) + context.bot.send_message(chat_id=update.effective_chat.id, text=result) + +def get_multiplier(value: int): + + try: + values = c.slot_machine_value[value] + except IndexError: + values = c.slot_machine_value[50] + + values_count = {i:values.count(i) for i in values} + symbol = max(values_count, key=values_count.get) + count = values_count[symbol] + + try: + return c.win_table[(count, symbol)] + except KeyError: + return 0
M main.pymain.py

@@ -1,8 +1,8 @@

from PIL import Image from Api import get_random_image, rating_normal, rating_lewd from Effects import img_to_bio, tt_bt_effect, bt_effect, splash_effect, wot_effect, text_effect -from Constants import get_localized_string as l -from Games import spin +from Constants import get_localized_string as l, format_author +from Games import spin, bet, cash from dotenv import load_dotenv load_dotenv()

@@ -179,16 +179,10 @@ markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("sauce", lang), url=url)]])

update.message.reply_photo(photo=image, parse_mode="markdown", reply_markup=markup) -def _format_author(user): - - if user.username is not None: - return user.full_name + f" ({user.username})" - return user.full_name - def _get_author(message): if message.forward_from is not None: - return _format_author(message.forward_from) + return format_author(message.forward_from) if message.forward_sender_name is not None: return message.forward_sender_name

@@ -196,7 +190,7 @@

if message.forward_from_chat is not None: return message.forward_from_chat.title + ("" if message.forward_from_chat.username is None else f" ({message.forward_from_chat.username})") - return _format_author(message.from_user) + return format_author(message.from_user) def tt_check(info):

@@ -357,7 +351,7 @@ def error_callback(update: Update, context: CallbackContext):

try: raise context.error except TelegramError as e: - logging.error("TelegramError! " + e) + logging.error("TelegramError! " + str(e)) context.bot.send_message(chat_id=update.effective_chat.id, text=l('error', lang)) def _add_effect_handler(dispatcher, command: str, callback):

@@ -391,6 +385,8 @@ _add_effect_handler(dispatcher, 'text', text)

# games dispatcher.add_handler(CommandHandler('spin', spin)) + dispatcher.add_handler(CommandHandler('bet', bet)) + dispatcher.add_handler(CommandHandler('cash', cash)) dispatcher.add_handler(CallbackQueryHandler(callback=spin)) #dispatcher.add_handler(CallbackQueryHandler(callback=autospin))