-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
283 lines (217 loc) · 9.47 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import json
import time
from telebot import TeleBot
from telebot.types import InlineKeyboardButton, InlineKeyboardMarkup, ReplyKeyboardMarkup
from random import randint
from users_controller import UsersController
users_controller = UsersController('db/users.json')
config = json.loads(open('config.json', 'r').read())
token = config['token']
bot = TeleBot(token)
@bot.message_handler(commands=['start'])
def start_message(message):
text = message.text.split(' ')
ref = 0
if len(text) == 2:
try:
ref = int(text[1])
except:
pass
username = message.from_user.username
user_id = message.from_user.id
users_controller.add_user(username, user_id, ref)
msg = """*Добро пожаловать в игру Орели и Решка*
У вас есть демо-баланс на 100 едениц, используйте с умом
"""
keyboard = InlineKeyboardMarkup()
buttons = [
[
{'text': 'Инфо', 'callback_data': 'gminfo'},
{'text': 'Информация о профиле', 'callback_data': 'profile_info'}
],
[
{'text': 'Играть', 'callback_data': 'play'},
{'text': 'Пополнить баланс', 'callback_data': 'payment'}
]
]
for row in buttons:
btns = [
InlineKeyboardButton(text=btn['text'], callback_data=btn['callback_data']) for btn in row
]
keyboard.add(*btns)
bot.send_message(message.chat.id, msg, reply_markup=keyboard, parse_mode='Markdown')
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
if call.message:
data = call.data
args = data.split('/')
if args[0] == 'gminfo':
msg = """*Орел и Решка*
MVP проекта для Лиса
"""
keyboard = InlineKeyboardMarkup()
buttons = [
[
{'text': 'Назад', 'callback_data': 'main_menu'},
]
]
for row in buttons:
btns = [
InlineKeyboardButton(text=btn['text'], callback_data=btn['callback_data']) for btn in row
]
keyboard.add(*btns)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=msg, parse_mode='Markdown', reply_markup=keyboard)
elif args[0] == 'main_menu':
msg = """*Добро пожаловать в игру Орели и Решка*
У вас есть демо-баланс на 100 едениц, используйте с умом
"""
keyboard = InlineKeyboardMarkup()
buttons = [
[
{'text': 'Инфо', 'callback_data': 'gminfo'},
{'text': 'Информация о профиле', 'callback_data': 'profile_info'}
],
[
{'text': 'Играть', 'callback_data': 'play'},
{'text': 'Пополнить баланс', 'callback_data': 'payment'}
]
]
for row in buttons:
btns = [
InlineKeyboardButton(text=btn['text'], callback_data=btn['callback_data']) for btn in row
]
keyboard.add(*btns)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=msg, parse_mode='Markdown', reply_markup=keyboard)
elif args[0] == 'profile_info':
user = users_controller.find_user_by_user_id(call.from_user.id)
msg = "*Username*: %s\n" % (user['username'])
msg += "*User ID*: %i\n" % (user['user_id'])
msg += "*Balance*: %f\n" % (user['balance'])
msg += "*Ref ID*: %i\n" % (user['ref'])
keyboard = InlineKeyboardMarkup()
buttons = [
[
{'text': 'Назад', 'callback_data': 'main_menu'},
],
[
{'text': 'Пополнить баланс', 'callback_data': 'payment'}
]
]
for row in buttons:
btns = [
InlineKeyboardButton(text=btn['text'], callback_data=btn['callback_data']) for btn in row
]
keyboard.add(*btns)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=msg, parse_mode='Markdown', reply_markup=keyboard)
elif args[0] == 'payment':
msg = 'Попление баланса в разработке'
keyboard = InlineKeyboardMarkup()
buttons = [
[
{'text': 'Назад', 'callback_data': 'main_menu'},
]
]
for row in buttons:
btns = [
InlineKeyboardButton(text=btn['text'], callback_data=btn['callback_data']) for btn in row
]
keyboard.add(*btns)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=msg, parse_mode='Markdown', reply_markup=keyboard)
elif args[0] == 'play':
msg = 'Сделайте ставку:'
keyboard = InlineKeyboardMarkup()
buttons = [
[
{'text': '100', 'callback_data': 'st/100'},
{'text': '200', 'callback_data': 'st/200'},
],
[
{'text': '300', 'callback_data': 'st/300'},
{'text': '400', 'callback_data': 'st/400'},
],
[
{'text': 'Назад', 'callback_data': 'main_menu'},
]
]
for row in buttons:
btns = [
InlineKeyboardButton(text=btn['text'], callback_data=btn['callback_data']) for btn in row
]
keyboard.add(*btns)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=msg, parse_mode='Markdown', reply_markup=keyboard)
elif args[0] == 'st':
msg = ''
buttons = []
user = users_controller.find_user_by_user_id(call.from_user.id)
st = int(args[1])
if st > user['balance']:
msg = 'Недостаточно средтсв'
buttons = [
[
{'text': 'Назад', 'callback_data': 'play'},
]
]
else:
msg = 'Ваша ставка: ' + args[1]
msg += 'Выберите исход:'
buttons = [
[
{'text': 'Орел', 'callback_data': 'select/' + args[1] + '/0'},
{'text': 'Решка', 'callback_data': 'select/' + args[1] + '/1'},
],
[
{'text': 'Назад', 'callback_data': 'play'},
]
]
keyboard = InlineKeyboardMarkup()
for row in buttons:
btns = [
InlineKeyboardButton(text=btn['text'], callback_data=btn['callback_data']) for btn in row
]
keyboard.add(*btns)
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=msg, parse_mode='Markdown', reply_markup=keyboard)
elif args[0] == 'select':
user = users_controller.find_user_by_user_id(call.from_user.id)
print(user)
exodus = randint(0, 100)
msg = ''
st = int(args[1])
select = int(args[2])
print(user['username'], st, select, exodus)
if exodus > 50:
print('q')
if select == 1:
msg += 'Вы выиграли'
balance = user['balance'] + st
users_controller.change_balance(call.from_user.id, balance)
else:
msg += 'Вы проиграли'
balance = user['balance'] - st
users_controller.change_balance(call.from_user.id, balance)
else:
print('qq')
if select == 0:
msg += 'Вы выиграли'
balance = user['balance'] + st
users_controller.change_balance(call.from_user.id, balance)
else:
msg += 'Вы проиграли'
balance = user['balance'] - st
users_controller.change_balance(call.from_user.id, balance)
print('pre keyboard')
keyboard = InlineKeyboardMarkup()
print('pre buttons')
buttons = [
[
{'text': 'Назад', 'callback_data': 'main_menu'},
]
]
print('pre adding buttons')
for row in buttons:
btns = [
InlineKeyboardButton(text=btn['text'], callback_data=btn['callback_data']) for btn in row
]
keyboard.add(*btns)
print('pre edit')
bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=msg, parse_mode='Markdown', reply_markup=keyboard)
bot.infinity_polling()