-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
303 lines (226 loc) · 9.37 KB
/
Copy pathmain.py
File metadata and controls
303 lines (226 loc) · 9.37 KB
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import asyncio
import datetime
import os
from dotenv import load_dotenv
import random
from aiogram import Bot, Dispatcher, types, filters, F
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.types import Message
from aiogram.fsm.context import FSMContext
from Model.StationsFactory import StationsFactory
from Model.QuestStates import QuestStates
from Model.StationModel import StationCard
import DB
from Config import TextFiles
from Handlers import MainHandler
from keyboards import MainKeyboards
# import background
load_dotenv()
bot = Bot(os.getenv('TEST_TOKEN'))
dp = Dispatcher(bot=bot)
cards = []
@dp.message(filters.Command('finish'))
async def finish_quest(message: Message) -> None:
if message.from_user.id not in [542687360]:
return
DB.set_finished_flag(True)
await message.answer("Закончен")
@dp.message(filters.Command('finish_cancel'))
async def finish_quest_cancel(message: Message) -> None:
if message.from_user.id not in [542687360]:
return
DB.set_finished_flag(False)
await message.answer("Возобновлен")
@dp.message(filters.Command('close'))
async def finish_quest_cancel(message: Message) -> None:
if message.from_user.id not in [542687360]:
return
DB.set_closed_flag(True)
await message.answer("Закрыт")
@dp.message(filters.Command('open'))
async def finish_quest_cancel(message: Message) -> None:
if message.from_user.id not in [542687360]:
return
DB.set_closed_flag(False)
await message.answer("Открыт")
@dp.message(filters.Command('alert'))
async def alert_users(message: Message) -> None:
if message.from_user.id not in [542687360]:
return
try:
users = DB.get_users_list()
for user in users:
try:
await bot.send_message(text=message.html_text.replace('/alert', ''), chat_id=user[0], parse_mode='HTML')
except:
pass
except:
pass
@dp.message(filters.Command('alert_to'))
async def alert_users(message: Message) -> None:
if message.from_user.id not in [542687360]:
return
try:
text = message.html_text.replace('/alert_to', '')
text = text.split('##')
test = '##'.join(text[1:])
await bot.send_message(text=test, chat_id=text[0].strip(), parse_mode='HTML')
except Exception as ex:
await bot.send_message(text=str(ex), chat_id=542687360)
@dp.message(filters.Command('leaders'))
async def alert_users(message: Message) -> None:
if message.from_user.id not in [542687360]:
return
table = [DB.get_oi_leaders_list(),
DB.get_oe_leaders_list(),
DB.get_om_leaders_list(),
DB.get_oo_leaders_list()]
text = ""
for branch in table:
if branch:
text += "\n\n" + branch[0][3] + "\n"
for person in branch:
text += f"<code>{person[0]}</code> @{person[1]} {person[2]}\nбаллы: {person[4]} время: {person[5]} \n\n"
if text:
await message.answer(text=text, parse_mode='HTML')
else:
await message.answer("None")
@dp.message(filters.CommandStart())
async def command_start_handler(message: Message, state: FSMContext) -> None:
await state.set_state(QuestStates.start_state)
if DB.get_closed_flag():
await message.answer(TextFiles.QUEST_CLOSED)
await state.set_state(QuestStates.quest_finished)
return
keyboard = []
prefix = "Привет!"
auth = DB.get_auth(message.chat.id)
if not DB.user_exist(message.from_user.id):
DB.add_user(message.from_user.id, message.from_user.username)
keyboard = MainKeyboards.start_keyboard
elif "" in auth:
keyboard = MainKeyboards.start_keyboard
else:
keyboard = MainKeyboards.continue_rename_keyboard
prefix = f"Рад приветствовать тебя снова, {auth[0]} из {auth[1]}!"
await message.answer(f"{prefix + TextFiles.GREETING}",
reply_markup=keyboard, parse_mode='HTML')
async def drop_inline(call: types.CallbackQuery) -> None:
await bot.edit_message_reply_markup(chat_id=call.from_user.id,
message_id=call.message.message_id,
reply_markup=None)
@dp.callback_query(lambda c: c.data == 'start_quest')
async def start_quest(call: types.CallbackQuery, state: FSMContext) -> None:
await drop_inline(call)
await call.message.answer(TextFiles.FIO_AUTH)
await state.set_state(QuestStates.fio_auth)
@dp.callback_query(lambda c: c.data == 'continue_quest')
async def start_quest(call: types.CallbackQuery, state: FSMContext) -> None:
await drop_inline(call)
await play_quest(call.message, state)
@dp.message(QuestStates.fio_auth)
async def fio_auth(message: Message, state: FSMContext) -> None:
user_id = message.chat.id
try:
print(message.text + ': ' + str(message.chat.id))
DB.set_user_fio(user_id, message.text)
await message.answer("Записано!")
await state.set_state(QuestStates.branch_auth)
await message.answer(text=TextFiles.BRANCH_AUTH, reply_markup=MainKeyboards.branch_keyboard)
except:
await message.reply(text=TextFiles.TYPE_ERROR_MESSAGE)
print(str(message.chat.id) + ' - Шлет что-то странное')
@dp.callback_query(lambda c: c.data in ['ОЭиС', 'ОМЭиКС', 'ОИТ', 'ООПНиПТ'])
async def branch_auth(call: types.CallbackQuery, state: FSMContext) -> None:
await drop_inline(call)
if await state.get_state() == QuestStates.branch_auth:
try:
DB.set_user_branch(call.message.chat.id, call.data)
await play_quest(call.message, state)
except:
pass
async def play_quest(message: Message, state: FSMContext) -> None:
if DB.get_closed_flag():
await message.answer(TextFiles.QUEST_CLOSED)
await state.set_state(QuestStates.quest_finished)
return
if DB.get_finished_flag():
await message.answer(text=TextFiles.QUEST_FINISHED)
await state.set_state(QuestStates.quest_finished)
return
card = get_current_station(message, state)
if card.answer == '':
await message.answer(text=TextFiles.FINISH)
await state.set_state(QuestStates.quest_finished)
return
await message.answer(card.question)
await state.set_state(QuestStates.station_opened)
def get_current_station(message: Message, state: FSMContext) -> StationCard:
user_id = message.chat.id
users_stations = [DB.get_first_group_stations(user_id),
DB.get_second_group_stations(user_id)]
# DB.get_third_group_stations(user_id)]
if len(users_stations[0]) + len(users_stations[1]) == 10:
return StationCard('', '', 0, 0) # complete
for group_id, station in enumerate(users_stations):
if len(station) <= 5:
if len(station) == 0:
return get_random_station(user_id, group_id)
elif len(station) == 5 and station[-1][1] == 0:
pass
elif station[-1][1] == 1:
return cards[group_id][station[-1][0]]
else:
return get_random_station(user_id, group_id)
def get_random_station(user_id: int, group: int) -> StationCard:
while True:
try:
station = random.randint(0, len(cards[group]) - 1)
DB.open_station(user_id, station, group)
return cards[group][station]
except:
pass
def station_existing_check(station: int, user_stations) -> bool:
for i in user_stations:
if station == i[0]:
return True
else:
return False
@dp.message(QuestStates.station_opened)
async def check_answer(message: Message, state: FSMContext):
user_id = message.chat.id
card = get_current_station(message, state)
try:
print(message.text + ': ' + str(message.chat.id))
DB.close_station(user_id, card.id, card.group)
if card.check_answer(message.text):
DB.add_score(user_id, card.group + 1)
DB.add_time(user_id, DB.get_time_delt(user_id, card.id, card.group))
await state.set_state(QuestStates.station_closed)
await message.answer(text=TextFiles.RIGHT_ANSWER, reply_markup=MainKeyboards.continue_keyboard)
except:
await message.reply(text=TextFiles.TYPE_ERROR_MESSAGE)
print(str(message.chat.id) + ' - Шлет что-то странное')
@dp.message()
async def check_answer(message: Message, state: FSMContext):
user_id = message.chat.id
try:
print(message.text + ': ' + str(message.chat.id))
except:
pass
s = await state.get_state()
if s is None:
await bot.send_message(chat_id=user_id, text=TextFiles.BOT_RESTART)
elif s in [QuestStates.start_state, QuestStates.branch_auth, QuestStates.station_closed]:
await message.answer(TextFiles.INVALID_TEXT_ON_BUTTON)
else:
await message.answer(TextFiles.INVALID_TEXT)
async def main() -> None:
global cards
cards = [StationsFactory().get_cards_first_group(),
StationsFactory().get_cards_second_group()]
# StationsFactory().get_cards_third_group()]
await bot.delete_webhook(drop_pending_updates=True)
await dp.start_polling(bot)
if __name__ == '__main__':
asyncio.run(main())