add spin command
Marco Andronaco andronacomarco@gmail.com
Mon, 12 Sep 2022 02:59:27 +0200
3 files changed,
237 insertions(+),
14 deletions(-)
M
Constants.py
→
Constants.py
@@ -25,10 +25,108 @@ 'failed_effect': "Impossibile applicare l'effetto."
}, } +symbols = { + "grape", + "lemon", + "bar", + "seven" +} + +slot_machine_value = { + 1: ("bar", "bar", "bar"), + 2: ("grape", "bar", "bar"), + 3: ("lemon", "bar", "bar"), + 4: ("seven", "bar", "bar"), + 5: ("bar", "grape", "bar"), + 6: ("grape", "grape", "bar"), + 7: ("lemon", "grape", "bar"), + 8: ("seven", "grape", "bar"), + 9: ("bar", "lemon", "bar"), + 10: ("grape", "lemon", "bar"), + 11: ("lemon", "lemon", "bar"), + 12: ("seven", "lemon", "bar"), + 13: ("bar", "seven", "bar"), + 14: ("grape", "seven", "bar"), + 15: ("lemon", "seven", "bar"), + 16: ("seven", "seven", "bar"), + 17: ("bar", "bar", "grape"), + 18: ("grape", "bar", "grape"), + 19: ("lemon", "bar", "grape"), + 20: ("seven", "bar", "grape"), + 21: ("bar", "grape", "grape"), + 22: ("grape", "grape", "grape"), + 23: ("lemon", "grape", "grape"), + 24: ("seven", "grape", "grape"), + 25: ("bar", "lemon", "grape"), + 26: ("grape", "lemon", "grape"), + 27: ("lemon", "lemon", "grape"), + 28: ("seven", "lemon", "grape"), + 29: ("bar", "seven", "grape"), + 30: ("grape", "seven", "grape"), + 31: ("lemon", "seven", "grape"), + 32: ("seven", "seven", "grape"), + 33: ("bar", "bar", "lemon"), + 34: ("grape", "bar", "lemon"), + 35: ("lemon", "bar", "lemon"), + 36: ("seven", "bar", "lemon"), + 37: ("bar", "grape", "lemon"), + 38: ("grape", "grape", "lemon"), + 39: ("lemon", "grape", "lemon"), + 40: ("seven", "grape", "lemon"), + 41: ("bar", "lemon", "lemon"), + 42: ("grape", "lemon", "lemon"), + 43: ("lemon", "lemon", "lemon"), + 44: ("seven", "lemon", "lemon"), + 45: ("bar", "seven", "lemon"), + 46: ("grape", "seven", "lemon"), + 47: ("lemon", "seven", "lemon"), + 48: ("seven", "seven", "lemon"), + 49: ("bar", "bar", "seven"), + 50: ("grape", "bar", "seven"), + 51: ("lemon", "bar", "seven"), + 52: ("seven", "bar", "seven"), + 53: ("bar", "grape", "seven"), + 54: ("grape", "grape", "seven"), + 55: ("lemon", "grape", "seven"), + 56: ("seven", "grape", "seven"), + 57: ("bar", "lemon", "seven"), + 58: ("grape", "lemon", "seven"), + 59: ("lemon", "lemon", "seven"), + 60: ("seven", "lemon", "seven"), + 61: ("bar", "seven", "seven"), + 62: ("grape", "seven", "seven"), + 63: ("lemon", "seven", "seven"), + 64: ("seven", "seven", "seven"), +} + +win_table = { + (3, "seven"): 50, + (3, "bar"): 20, + (3, "lemon"): 10, + (3, "grape"): 5, + + (2, "seven"): 10, + (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 {}{}{}{}{}{}"+ return "localization error {}{}{}{}{}{}" +
A
Games.py
@@ -0,0 +1,119 @@
+from telegram import Update, Dice, InlineKeyboardMarkup, InlineKeyboardButton +from telegram.ext import CallbackContext +from datetime import date +import Constants as c +import time + +lastreset_default = date(1970, 1, 1) +saldo_default = 10000 +bet_default = 50 +slot_emoji = '🎰' + +def set_user_value(context: CallbackContext, key:str, amount): + context.user_data[key] = amount + return amount + +def get_user_value(context: CallbackContext, key:str, default): + try: + return context.user_data[key] + 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) + + if saldo == 0: + lastreset = get_lastreset(context) + today = date.today() + + if lastreset < today: + set_lastreset(context, today) + saldo = set_saldo(context, saldo_default) + + return saldo + +def set_saldo(context: CallbackContext, amount: int): + return set_user_value(context, "saldo", amount) + +def get_bet(context: CallbackContext): + return get_user_value(context, "bet", bet_default) + +def set_lastreset(context: CallbackContext, amount: int): + return set_user_value(context, "lastreset", amount) + +def get_lastreset(context: CallbackContext): + return get_user_value(context, "lastreset", lastreset_default) + +def set_bet(context: CallbackContext, amount: int): + return set_user_value(context, "bet", amount) + +def _spin(context: CallbackContext, id: float, markup: InlineKeyboardMarkup = ""): + + bet = get_bet(context) + saldo = get_saldo(context) + + if saldo < bet: + context.bot.send_message(chat_id=id, text="Saldo esaurito!") + return None + + saldo = set_saldo(context, saldo - 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} + + symbol = max(values_count, key=values_count.get) + + win = bet * c.get_multiplier(count=values_count[symbol], symbol=symbol) + + saldo = set_saldo(context, saldo + win) + + text = "Hai perso..." if win == 0 else "Hai vinto {}€!".format(win / 100) + + text += " Saldo: {}€.".format(saldo / 100) + + time.sleep(2) + context.bot.send_message(chat_id=id, text=text, reply_markup=markup) + return win + +def spin(update: Update, context: CallbackContext): + + bet = get_bet(context) / 100 + + try: + amount = int(context.args[0]) + except (TypeError, IndexError): + amount = 1 + + if amount == 1: + + markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Reroll (-{}€)".format(bet), callback_data="callback_1")]]) + _spin(context=context, id=update.effective_chat.id, markup=markup) + else: + count = 0 + total_win = 0 + for i in range(amount): + win = _spin(context=context, id=update.effective_chat.id, markup="") + + if win is None: + break + + count += 1 + total_win += win + + 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 spin_5(update: Update, context: CallbackContext): + bet = get_bet(context) / 100 + amount = 5 + + markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Altri {} spin (-{}€)".format(amount, bet * amount), callback_data="callback_2")]]) + + _autospin(context=context, id=update.effective_chat.id, amount=5, markup=markup)
M
main.py
→
main.py
@@ -2,6 +2,7 @@ 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 dotenv import load_dotenv load_dotenv()@@ -10,15 +11,10 @@ logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
from io import BytesIO from telegram.error import TelegramError -from telegram.ext import Updater, CallbackContext, CommandHandler, MessageHandler, Filters, PicklePersistence +from telegram.ext import Updater, CallbackContext, CallbackQueryHandler, CommandHandler, MessageHandler, Filters, PicklePersistence from telegram import Update, InlineKeyboardMarkup, InlineKeyboardButton lang = 'us' - -def _get_args(context): - - logging.info(context.args) - return ' '.join(context.args) def _get_message_content(message):@@ -160,7 +156,7 @@ if image is None:
logging.warning("Getting Image failed") raise TelegramError("bad image") - image = _img_to_bio(image) + image = img_to_bio(image) markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("sauce", lang), url=url)]]) update.message.reply_photo(photo=image, parse_mode="markdown", reply_markup=markup)@@ -178,7 +174,7 @@ logging.warning("Getting Image failed")
raise TelegramError("bad image") return - image = _img_to_bio(image) + image = img_to_bio(image) markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("sauce", lang), url=url)]]) update.message.reply_photo(photo=image, parse_mode="markdown", reply_markup=markup)@@ -351,7 +347,7 @@ update.message.reply_photo(photo=image, reply_markup=markup)
def caps(update: Update, context: CallbackContext): - _, reply, _ = _get_reply(update.message.reply_to_message, _get_args(context)) + _, reply, _ = _get_reply(update.message.reply_to_message, ' '.join(context.args)) context.bot.send_message(chat_id=update.effective_chat.id, text=reply.upper()) def unknown(update: Update, context: CallbackContext):@@ -360,8 +356,8 @@
def error_callback(update: Update, context: CallbackContext): try: raise context.error - except TelegramError: - logging.error("TelegramError!!") + except TelegramError as e: + logging.error("TelegramError! " + e) context.bot.send_message(chat_id=update.effective_chat.id, text=l('error', lang)) def _add_effect_handler(dispatcher, command: str, callback):@@ -379,13 +375,13 @@
dispatcher = updater.dispatcher dispatcher.add_error_handler(error_callback) + # commands dispatcher.add_handler(CommandHandler('start', start)) dispatcher.add_handler(CommandHandler('lewd', set_lewd)) dispatcher.add_handler(CommandHandler('caps', caps)) dispatcher.add_handler(CommandHandler('pic', pic)) - dispatcher.add_handler(CommandHandler('raw', raw)) - dispatcher.add_handler(CommandHandler('pilu', pilu)) + # effects _add_effect_handler(dispatcher, 'ttbt', ttbt) _add_effect_handler(dispatcher, 'tt', tt) _add_effect_handler(dispatcher, 'bt', bt)@@ -393,6 +389,16 @@ _add_effect_handler(dispatcher, 'splash', splash)
_add_effect_handler(dispatcher, 'wot', wot) _add_effect_handler(dispatcher, 'text', text) + # games + dispatcher.add_handler(CommandHandler('spin', spin)) + dispatcher.add_handler(CallbackQueryHandler(callback=spin)) + #dispatcher.add_handler(CallbackQueryHandler(callback=autospin)) + + # secrets + dispatcher.add_handler(CommandHandler('raw', raw)) + dispatcher.add_handler(CommandHandler('pilu', pilu)) + + # fallback dispatcher.add_handler(MessageHandler(Filters.command, unknown)) updater.start_polling() updater.idle()