Slot.py (view raw)
1from telegram import Update, Dice, InlineKeyboardMarkup, InlineKeyboardButton
2from telegram.ext import CallbackContext
3from datetime import date
4from Constants import get_localized_string as l
5import Constants as c
6import time
7
8autospin_cap = 50
9lastreset_default = date(1970, 1, 1)
10cash_default = 5000
11bet_default = 50
12slot_emoji = '🎰'
13
14def read_arg(context: CallbackContext, default=None, cast=int):
15 try:
16 return cast(context.args[0].replace(",", ".").replace("€", ""))
17 except (TypeError, IndexError, ValueError):
18 return default
19
20def set_user_value(context: CallbackContext, key:str, amount):
21 context.user_data[key] = amount
22 return amount
23
24def get_user_value(context: CallbackContext, key:str, default):
25 try:
26 return context.user_data[key]
27 except KeyError:
28 #print(f"set {key} to {str(default)}")
29 return set_user_value(context, key, default)
30
31def get_cash(context: CallbackContext):
32 return get_user_value(context, "cash", cash_default)
33
34def set_cash(context: CallbackContext, amount: int):
35 return set_user_value(context, "cash", amount)
36
37def get_bet(context: CallbackContext):
38 return get_user_value(context, "bet", bet_default)
39
40def set_lastreset(context: CallbackContext, amount: int):
41 return set_user_value(context, "lastreset", amount)
42
43def get_lastreset(context: CallbackContext):
44 return get_user_value(context, "lastreset", lastreset_default)
45
46def set_bet(context: CallbackContext, amount: int):
47 return set_user_value(context, "bet", amount)
48
49def _spin(context: CallbackContext, id: float, delay=True):
50
51 bet = get_bet(context)
52 cash = get_cash(context)
53
54 if cash < bet:
55 context.bot.send_message(chat_id=id, text=l("not_enough_cash", context))
56 return None
57
58 cash = set_cash(context, cash - bet)
59
60 message = context.bot.send_dice(chat_id=id, emoji=slot_emoji, disable_notification=True)
61
62 win = bet * get_multiplier(message.dice.value)
63
64 cash = set_cash(context, cash + win)
65
66 if delay:
67 markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("reroll", context).format(bet / 100), callback_data="reroll 1")]])
68 text = l("you_lost", context) if win == 0 else l("you_won", context).format(win / 100)
69 text += l("cash_result", context).format(cash / 100)
70
71 args = {
72 "chat_id": id,
73 "text": text,
74 "reply_markup": markup
75 }
76 context.job_queue.run_once(show_result, 2, context=args, name=str(id))
77 else:
78 message.edit_reply_markup(InlineKeyboardMarkup([[InlineKeyboardButton(text=l("fast_output", context).format(win / 100), callback_data="none")]]))
79 return win
80
81def show_result(context: CallbackContext):
82 con = context.job.context
83 context.bot.send_message(chat_id=con["chat_id"], text=con["text"], reply_markup=con["reply_markup"], disable_notification=True)
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 and update.effective_chat.type != 'private':
92 amount = 1
93 context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", context))
94
95 if amount == 1:
96
97 _spin(context=context, id=update.effective_chat.id)
98 else:
99 autospin(context=context, id=update.effective_chat.id, amount=amount)
100
101def autospin(context: CallbackContext, id: int, amount: int):
102
103 bet = get_bet(context) / 100
104 count = 0
105 total_win = 0
106
107 for i in range(amount):
108
109 win = _spin(context=context, id=id, delay=False)
110
111 if win is None:
112 break
113
114 count += 1
115 total_win += win
116
117 result = l("summary", context).format(count * bet, total_win / 100)
118 markup = InlineKeyboardMarkup([[InlineKeyboardButton(text="Altri {} spin (-{}€)".format(amount, bet * amount), callback_data=f"reroll {amount}")]])
119 context.bot.send_message(chat_id=id, text=result, reply_markup=markup, disable_notification=False)
120
121def bet(update: Update, context: CallbackContext):
122
123 amount = read_arg(context=context, cast=float)
124
125 if amount is not None:
126 bet = set_bet(context, int(amount * 100))
127 else:
128 bet = get_bet(context)
129
130 result = l("current_bet", context).format(c.format_author(update.effective_user), bet / 100)
131 context.bot.send_message(chat_id=update.effective_chat.id, text=result)
132
133def cash(update: Update, context: CallbackContext):
134 cash = get_cash(context) / 100
135
136 if cash < cash_default / 2:
137 lastreset = get_lastreset(context)
138 today = date.today()
139
140 if lastreset < today:
141 set_lastreset(context, today)
142 cash = set_cash(context, cash_default)
143
144 result = l("cash_reset", context).format(c.format_author(update.effective_user), cash / 100)
145 return update.message.reply_text(text=result)
146 else:
147 return update.message.reply_text(text=l("cash_reset_fail", context).format(c.format_author(update.effective_user)))
148
149
150 result = l("current_cash", context).format(c.format_author(update.effective_user), cash)
151 return update.message.reply_text(text=result)
152
153def get_multiplier(value: int):
154
155 try:
156 values = c.slot_machine_value[value]
157 except IndexError:
158 values = c.slot_machine_value[5]
159
160 try:
161 return c.win_table[values]
162 except KeyError:
163 return 0