-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
250 lines (222 loc) · 9.82 KB
/
bot.py
File metadata and controls
250 lines (222 loc) · 9.82 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
import telebot
from telebot import types
import config
import logging
bot = telebot.TeleBot(config.TOKEN)
logger = logging.getLogger(__name__)
handler = logging.FileHandler("my_logger.log")
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - \
%(message)s")
handler.setFormatter(formatter)
logger.addHandler(handler)
game_fields = {}
RULES = "\
Còàíäàðòíàÿ êîëîäà êàðò ðàñêëàäûâàåòñÿ â 8 êîëîíîê, ÷åòûðå êîëîíêè ïî 7 \
êàðò è ÷åòûðå — ïî 6. Êàðòû ëåæàò ëèöåâîé ñòîðîíîé ââåðõ.\n\
Ââåðõó ñëåâà 4 ÿ÷åéêè, îíè íàçûâàþòñÿ «äîìîì», ñïðàâà îò íèõ 4 «ñâîáîäíûõ» \
ÿ÷åéêè. Íà ìîìåíò íà÷àëà èãðû âñå îíè ïóñòû.\n\
 èãðå ðàçðåøåíî ïåðåêëàäûâòü âåðõíþþ êàðòó èç êîëîíêè èëè ñâîáîäíîé ÿ÷åéêè\n\
â ëþáóþ äðóãóþ êîëîíêó íà ñëåäóþùóþ ïî ñòàðøèíñòâó êàðòó äðóãîãî öâåòà;\n\
â ëþáóþ îñâîáîäèâùóþñÿ êîëîíêó;\n\
â ëþáóþ ïóñòóþ \"ñâîáîäíóþ\" ÿ÷åéêó;\n\
â \"äîì\", íà÷èíàÿ ñ òóçà è çàêàí÷èâàÿ êîðîë¸ì òîé æå ìàñòè.\n\
Ïàñüÿíñ ñõîäèòñÿ, åñëè óäà¸òñÿ ïåðåìåñòèòü âñþ êîëîäó â «äîì»."
def get_name(message):
name = message.from_user.first_name
if hasattr(message.from_user, "last_name") \
and message.from_user.last_name is not None:
name += u" {}".format(message.from_user.last_name)
if hasattr(message.from_user, "username") \
and message.from_user.username is not None:
name += u" (@{})".format(message.from_user.username)
return name
def logg_info_record(message, text, level):
logger.setLevel(level)
handler.setLevel(level)
logger.info("In the chat {} user {} {}".format(
message.chat.id, get_name(message), text))
def logg_error_record(message, text, level):
logger.setLevel(level)
handler.setLevel(level)
logger.error("In the chat {} user {} {}".format(
message.chat.id, get_name(message), text))
@bot.message_handler(commands=["start"])
def start(message):
logg_info_record(message, "joins the game", logging.INFO)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup.row("Íîâàÿ èãðà", "Óçíàòü ïðàâèëà èãðû")
msg = bot.send_message(message.chat.id,
text="Äîáðî ïîæàëîâàòü â èãðó, {}".format(
get_name(message)), reply_markup=markup)
bot.register_next_step_handler(msg, steps)
def steps(message):
if (message.text=="Íîâàÿ èãðà") | (message.text=="Íà÷àòü èãðó"):
new_game(message)
elif message.text=="Óçíàòü ïðàâèëà èãðû":
first_rules(message)
elif message.text=="Ïðàâèëà èãðû":
rules(message)
elif (message.text=="Âûõîä"):
start(message)
elif (message.text=="Âûáðàòü äðóãóþ êàðòó") | (message.text=="Íàçàä"):
step(message)
elif ((message.text[0]==config.SPADE) | (message.text[0]==config.CLUB) |
(message.text[0]==config.HEART) | (message.text[0]==config.DIAMOND)):
#âîçìîæíî ìîæíî óëó÷ùèòü ýòó ïðîâåðêó
moving(message)
elif (message.text=="Ñâîáîäíàÿ ÿ÷åéêà"):
move_to_free(message)
elif (message.text=="Äîì"):
move_to_home(message)
elif (message.text[1:9]==" Êîëîíêà"):
move_to_column(message)
elif (message.text == "/start"):
nothing = 0
else:
error(message)
def error(message):
chat_id = message.chat.id
logg_error_record(message, "requests incorrect command", logging.ERROR)
msg = bot.send_message(chat_id, text="Êàêàÿ-òî îøèáêà")
bot.register_next_step_handler(msg, steps)
def first_rules(message):
chat_id = message.chat.id
logg_info_record(message, "asks about the rules", logging.INFO)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup.row("Íà÷àòü èãðó", "Âûõîä")
msg = bot.send_message(chat_id, text=RULES, reply_markup=markup)
bot.register_next_step_handler(msg, steps)
def rules(message):
chat_id = message.chat.id
logg_info_record(message, "asked about the rules", logging.INFO)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup.row("Íàçàä")
msg = bot.send_message(chat_id, text=RULES, reply_markup=markup)
bot.register_next_step_handler(msg, steps)
def step(message):
chat_id = message.chat.id
user_id = message.from_user.id
with open(game_fields[chat_id, user_id][0].take_photo(
"{} {}".format(str(chat_id), str(user_id))), "rb") as f:
bot.send_photo(chat_id, photo=f)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
end_of_game = True
moving_cards = game_fields[chat_id, user_id][0].moving_cards_in_frees()
keyboards = []
for elem in moving_cards:
end_of_game = False
suit, number = divmod(elem, 13)
keyboards.append("{}{}".format(config.SUITS[suit],
config.NUMBERS[number]))
markup.row(*[types.KeyboardButton(name) for name in keyboards])
moving_cards = game_fields[chat_id, user_id][0].moving_cards_in_field()
keyboards = []
for elem in moving_cards:
end_of_game = False
suit, number = divmod(elem, 13)
keyboards.append("{}{}".format(config.SUITS[suit],
config.NUMBERS[number]))
if len(keyboards) <= 4:
markup.row(*[types.KeyboardButton(name) for name in keyboards])
else:
markup.row(*[types.KeyboardButton(name)
for name in keyboards[:len(keyboards) // 2]])
markup.row(*[types.KeyboardButton(name)
for name in keyboards[len(keyboards) // 2:]])
if(end_of_game):
finish(message)
else:
markup.row("Íîâàÿ èãðà")
markup.row("Âûõîä", "Ïðàâèëà èãðû")
msg = bot.send_message(chat_id,
text="Êàêóþ êàðòó âû õîòèòå ïåðåëîæèòü?",
reply_markup=markup)
bot.register_next_step_handler(msg, steps)
def finish(message):
chat_id = message.chat.id
logg_info_record(message, "wins the game", logging.INFO)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
markup.row("Íîâàÿ èãðà", "Âûõîä")
msg = bot.send_message(chat_id, text="Ïîçäðàâëÿþ, âû ïîáåäèòåëü!",
reply_markup=markup)
bot.send_sticker(chat_id, data="CAADAgAD-QsAAiMhBQAB9_KHkFe4N40C")
bot.register_next_step_handler(msg, steps)
def new_game(message):
chat_id = message.chat.id
user_id = message.from_user.id
logg_info_record(message, "starts new game", logging.INFO)
game_fields[chat_id, user_id] = [config.Game(), config.Card()]
step(message)
def moving(message):
chat_id = message.chat.id
user_id = message.from_user.id
logg_info_record(message, "choses card {} {}".format(
config.SUITS_STR[config.SUITS_NUM[message.text[0]]],
message.text[1:]), logging.INFO)
markup = types.ReplyKeyboardMarkup(resize_keyboard=True)
suit = message.text[0]
number = config.NUMBERS_NUM[message.text[1:]]
i, j, figure = game_fields[chat_id, user_id][0].find_in_field(suit, number)
if j == -1:
i, figure = game_fields[chat_id, user_id][0].find_in_frees(suit, number)
game_fields[chat_id, user_id][1] = config.Card(i, -1, figure)
else:
game_fields[chat_id, user_id][1] = config.Card(i, j, figure)
keyboards = []
if game_fields[chat_id, user_id][0].is_there_home(suit, number):
keyboards.append("Äîì")
if game_fields[chat_id, user_id][0].is_there_free():
keyboards.append("Ñâîáîäíàÿ ÿ÷åéêà")
markup.row(*[types.KeyboardButton(name) for name in keyboards])
column = game_fields[chat_id, user_id][0].suitable_column(suit, number)
keyboards = []
for elem in column:
keyboards.append("{} Êîëîíêà".format(str(elem)))
markup.row(*[types.KeyboardButton(name) for name in keyboards])
markup.row("Âûáðàòü äðóãóþ êàðòó")
markup.row("Âûõîä", "Ïðàâèëà èãðû")
msg = bot.send_message(chat_id,
text="Êóäà âû õîòèòå ïåðåëîæèòü ýòó êàðòó?\n",
reply_markup=markup)
bot.register_next_step_handler(msg, steps)
def move_to_free(message):
chat_id = message.chat.id
user_id = message.from_user.id
current_suit, current_number = divmod(
game_fields[chat_id, user_id][1].figure, 13)
logg_info_record(message, "moves card {} {} to the open cells".format(
config.SUITS_STR[current_suit], config.NUMBERS[current_number]),
logging.INFO)
game_fields[chat_id, user_id][0].move_to_free(
game_fields[chat_id, user_id][1].row,
game_fields[chat_id, user_id][1].column,
game_fields[chat_id, user_id][1].figure)
step(message)
def move_to_home(message):
chat_id = message.chat.id
user_id = message.from_user.id
current_suit, current_number = divmod(
game_fields[chat_id, user_id][1].figure, 13)
logg_info_record(message, "moves card {} {} to foundations".format(
config.SUITS_STR[current_suit], config.NUMBERS[current_number]),
logging.INFO)
game_fields[chat_id, user_id][0].move_to_home(
game_fields[chat_id, user_id][1].row,
game_fields[chat_id, user_id][1].column,
game_fields[chat_id, user_id][1].figure)
step(message)
def move_to_column(message):
chat_id = message.chat.id
user_id = message.from_user.id
current_suit, current_number = divmod(
game_fields[chat_id, user_id][1].figure, 13)
new_column = int(message.text[0]) - 1
logg_info_record(message, "moves card {} {} to {} column".format(
config.SUITS_STR[current_suit], config.NUMBERS[current_number],
message.text[0]), logging.INFO)
game_fields[chat_id, user_id][0].move_to_column(
game_fields[chat_id, user_id][1].row,
game_fields[chat_id, user_id][1].column,
game_fields[chat_id, user_id][1].figure, new_column)
step(message)
bot.polling()