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