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, markup: InlineKeyboardMarkup = ""):
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 multiplier = get_multiplier(message.dice.value)
63
64 win = bet * multiplier
65
66 cash = set_cash(context, cash + win)
67
68 text = l("you_lost", context) if win == 0 else l("you_won", context).format(win / 100)
69
70 text += l("cash_result", context).format(cash / 100)
71
72 time.sleep(2)
73 context.bot.send_message(chat_id=id, text=text, reply_markup=markup, disable_notification=True)
74 return win
75
76def spin(update: Update, context: CallbackContext):
77
78 bet = get_bet(context) / 100
79
80 amount = read_arg(context, 1)
81
82 if amount > 1 and update.effective_chat.type != 'private':
83 amount = 1
84 context.bot.send_message(chat_id=update.effective_chat.id, text=l("no_autospin", context))
85
86 if amount == 1:
87 markup = InlineKeyboardMarkup([[InlineKeyboardButton(text=l("reroll", context).format(bet), callback_data="reroll_single")]])
88 _spin(context=context, id=update.effective_chat.id, markup=markup)
89 else:
90 amount = max(1, min(amount, autospin_cap))
91 count = 0
92 total_win = 0
93 for i in range(amount):
94 win = _spin(context=context, id=update.effective_chat.id, markup="")
95
96 if win is None:
97 break
98
99 count += 1
100 total_win += win
101
102 result = l("summary", context).format(count * bet, total_win / 100)
103 markup = "" #InlineKeyboardMarkup([[InlineKeyboardButton(text="Altri {} spin (-{}€)".format(amount, bet * amount), callback_data="callback_2")]])
104 context.bot.send_message(chat_id=update.effective_chat.id, text=result, reply_markup=markup, disable_notification=False)
105
106def bet(update: Update, context: CallbackContext):
107
108 amount = read_arg(context=context, cast=float)
109
110 if amount is not None:
111 bet = set_bet(context, int(amount * 100))
112 else:
113 bet = get_bet(context)
114
115 result = l("current_bet", context).format(c.format_author(update.effective_user), bet / 100)
116 context.bot.send_message(chat_id=update.effective_chat.id, text=result)
117
118def cash(update: Update, context: CallbackContext):
119 cash = get_cash(context) / 100
120
121 if cash < cash_default / 2:
122 lastreset = get_lastreset(context)
123 today = date.today()
124
125 if lastreset < today:
126 set_lastreset(context, today)
127 cash = set_cash(context, cash_default)
128
129 result = l("cash_reset", context).format(c.format_author(update.effective_user), cash / 100)
130 return update.message.reply_text(text=result)
131 else:
132 return update.message.reply_text(text=l("cash_reset_fail", context).format(c.format_author(update.effective_user)))
133
134
135 result = l("current_cash", context).format(c.format_author(update.effective_user), cash)
136 return update.message.reply_text(text=result)
137
138def get_multiplier(value: int):
139
140 try:
141 values = c.slot_machine_value[value]
142 except IndexError:
143 values = c.slot_machine_value[5]
144
145 try:
146 return c.win_table[values]
147 except KeyError:
148 return 0