better multithreading and autospin
Marco Andronaco andronacomarco@gmail.com
Tue, 13 Sep 2022 00:25:39 +0200
3 files changed,
60 insertions(+),
29 deletions(-)
M
Constants.py
→
Constants.py
@@ -29,6 +29,9 @@ 'no_autospin': "Sorry, multiple spins are disabled in group chats.",
'current_language': "Current language: {}.\nChoices: {}\nTo change it, type \"/lang <code>\" or use one of the buttons below.", 'invalid_language': "Invalid language.", 'language_set': "Language set: {}", + 'none_callback': "This button does nothing.", + 'fast_output': "Win: {:0.2f}$", + 'repeat_autospin': "Spin {} times again (-{:0.2f}$)" }, 'it': { 'name': "Italiano",@@ -56,6 +59,9 @@ 'no_autospin': "Gli spin multipli sono disabilitati nelle chat di gruppo.",
'current_language': "Lingua attuale: {}.\nAltre lingue: {}\nPer cambiarla, scrivi \"/lang <codice>\" o usa uno dei tasti qui sotto.", 'invalid_language': "Questa lingua non esiste.", 'language_set': "Lingua impostata: {}", + 'none_callback': "Questo tasto non fa nulla.", + 'fast_output': "Vinto: {:0.2f}€", + 'repeat_autospin': "Altri {} spin (-{:0.2f}€)" }, } langs = localization.keys()
M
Slot.py
→
Slot.py
@@ -46,7 +46,7 @@
def set_bet(context: CallbackContext, amount: int): return set_user_value(context, "bet", amount) -def _spin(context: CallbackContext, id: float, markup: InlineKeyboardMarkup = ""): +def _spin(context: CallbackContext, id: float, delay=True): bet = get_bet(context) cash = get_cash(context)@@ -59,20 +59,29 @@ cash = set_cash(context, cash - bet)
message = context.bot.send_dice(chat_id=id, emoji=slot_emoji, disable_notification=True) - multiplier = get_multiplier(message.dice.value) - - win = bet * multiplier + win = bet * get_multiplier(message.dice.value) cash = set_cash(context, cash + win) - - text = l("you_lost", context) if win == 0 else l("you_won", context).format(win / 100) - text += l("cash_result", context).format(cash / 100) - - time.sleep(2) - context.bot.send_message(chat_id=id, text=text, reply_markup=markup, disable_notification=True) + if delay: + markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("reroll", context).format(bet / 100), callback_data="reroll 1")]]) + text = l("you_lost", context) if win == 0 else l("you_won", context).format(win / 100) + text += l("cash_result", context).format(cash / 100) + + args = { + "chat_id": id, + "text": text, + "reply_markup": markup + } + context.job_queue.run_once(show_result, 2, context=args, name=str(id)) + else: + message.edit_reply_markup(InlineKeyboardMarkup([[InlineKeyboardButton(text=l("fast_output", context).format(win / 100), callback_data="none")]])) return win +def show_result(context: CallbackContext): + con = context.job.context + context.bot.send_message(chat_id=con["chat_id"], text=con["text"], reply_markup=con["reply_markup"], disable_notification=True) + def spin(update: Update, context: CallbackContext): bet = get_bet(context) / 100@@ -84,24 +93,30 @@ amount = 1
context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", context)) if amount == 1: - markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("reroll", context).format(bet), callback_data="reroll_single")]]) - _spin(context=context, id=update.effective_chat.id, markup=markup) + + _spin(context=context, id=update.effective_chat.id) else: - amount = max(1, min(amount, autospin_cap)) - 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 + autospin(context=context, id=update.effective_chat.id, amount=amount) + +def autospin(context: CallbackContext, id: int, amount: int): + + bet = get_bet(context) / 100 + count = 0 + total_win = 0 + + for i in range(amount): + + win = _spin(context=context, id=id, delay=False) + + if win is None: + break - result = l("summary", context).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, disable_notification=False) + count += 1 + total_win += win + + result = l("summary", context).format(count * bet, total_win / 100) + markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Altri {} spin (-{}€)".format(amount, bet * amount), callback_data=f"reroll {amount}")]]) + context.bot.send_message(chat_id=id, text=result, reply_markup=markup, disable_notification=False) def bet(update: Update, context: CallbackContext):
M
main.py
→
main.py
@@ -2,7 +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, format_author, format_lang, langs, get_lang, lang_markup -from Slot import spin, bet, cash +from Slot import spin, autospin, bet, cash from dotenv import load_dotenv load_dotenv()@@ -379,10 +379,18 @@ dispatcher.add_handler(MessageHandler(Filters.caption(update=[f"/{command}"]), callback))
def keyboard_handler(update: Update, context: CallbackContext): query = update.callback_query + data = query.data + + if data.startswith("reroll"): + amount = int(data.split(" ")[1]) + + if amount == 1: + return spin(update, context) + return autospin(context, update.effective_chat.id, amount) match query.data: - case "reroll_single": - spin(update, context) + case "none": + return query.answer(l("none_callback", context)) case "set_lang_en": lang = "en" _set_lang(update, context, lang)@@ -406,6 +414,8 @@ #store_user_data=False
)) dispatcher = updater.dispatcher + #dispatcher.workers = 8 + dispatcher.add_error_handler(error_callback) dispatcher.add_handler(CallbackQueryHandler(callback=keyboard_handler))