-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
348 lines (276 loc) · 11.7 KB
/
main.py
File metadata and controls
348 lines (276 loc) · 11.7 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
import time
import telepot
from telepot.loop import MessageLoop
import sys
import hanabi
import draw
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
class ChatGame:
def __init__(self, chat_id, admin):
self.game = None
self.admin = admin
self.player_to_user = {}
self.user_to_message = {}
self.current_action = ''
self.chat_id = chat_id
class BotServer(object):
def __init__(self, token):
self.bot = telepot.Bot(token)
self.token = token
self.games = {}
server = None
def add_player(server, chat_id, user_id, name, allow_repeated_players=False):
if chat_id not in server.games:
server.bot.sendMessage(chat_id, "No game created for this chat")
return
player_to_user = server.games[chat_id].player_to_user
user_to_message = server.games[chat_id].user_to_message
if not allow_repeated_players and user_id in player_to_user.values():
return
# if user_id in server.user_to_chat_id.keys():
# server.bot.sendMessage(chat_id, "You already joined the game")
# return
if len(player_to_user) >= 4:
server.bot.sendMessage(chat_id, "There are already 4 players in the game.")
return
if name in player_to_user:
name += '_' + str(len(player_to_user))
server.bot.sendMessage(chat_id, name + " joined")
player_to_user[name] = user_id
user_to_message[user_id] = None
# server.user_to_chat_id[user_id] = chat_id
def send_game_views(bot, chat_game, last_player=''):
for name, user_id in chat_game.player_to_user.items():
# TODO: Send directly generated image, without write to disk.
filename = str(user_id) + '.png'
draw.draw_board_state(chat_game.game, name, filename)
try:
with open(filename, 'rb') as image:
bot.sendPhoto(user_id, image)
except Exception as ex:
print(ex)
pass
def start_game(server, chat_id, user_id):
if chat_id not in server.games:
server.bot.sendMessage(chat_id, "No game created for this chat")
return
if user_id != server.games[chat_id].admin:
server.bot.sendMessage(chat_id, "You cannot start this game")
player_to_user = server.games[chat_id].player_to_user
if len(server.games[chat_id].player_to_user) < 2:
server.bot.sendMessage(chat_id, "Too few players")
return
players = []
for name in player_to_user.keys():
players.append(name)
server.games[chat_id].game = hanabi.Game(players)
server.bot.sendMessage(chat_id, "Game started with players " + str(players))
# send a view to all the players
chat_game = server.games[chat_id]
send_game_views(server.bot, chat_game)
server.bot.sendMessage(chat_id, "Game started!")
return
def edit_message(chat_game, bot, chat_id, message="", keyboard=None, delete=False):
edited = telepot.message_identifier(chat_game.user_to_message[chat_id])
if delete:
bot.deleteMessage(edited)
else:
bot.editMessageText(edited, message, reply_markup=keyboard)
def send_keyboard(bot, chat_id, keyboard_type):
chat_game = server.games[chat_id]
player = hanabi.get_active_player_name(chat_game.game)
user_id = chat_game.player_to_user[player]
if keyboard_type == "action":
keyboard = [[
InlineKeyboardButton(text='Discard', callback_data='discard|' + str(chat_id)),
InlineKeyboardButton(text='Play', callback_data='play|' + str(chat_id)),
]]
if chat_game.game.hints > 0:
keyboard[0].append(InlineKeyboardButton(text='Hint', callback_data='hint|' + str(chat_id)))
keyboard = InlineKeyboardMarkup(inline_keyboard=keyboard)
if chat_game.user_to_message[user_id] is not None:
edit_message(chat_game, bot, user_id, player + ", choose an action", keyboard)
else:
chat_game.user_to_message[user_id] = bot.sendMessage(user_id, player + ", it's your turn", reply_markup=keyboard)
elif keyboard_type in ['play', 'discard']:
game = chat_game.game
active_player = game.players[game.active_player]
player_hand = chat_game.game.hands[active_player]
options = []
for i, card in enumerate(player_hand):
info = ''
if card.is_color_known:
info += card.color + ' '
if card.is_value_known:
info += str(card.value) + ' '
info = info.strip()
if info == '':
info = ' '
options.append(InlineKeyboardButton(text=info, callback_data=str(i+1)+ '|' + str(chat_id)))
back = [InlineKeyboardButton(text='Back', callback_data='back|' + str(chat_id))]
keyboard = InlineKeyboardMarkup(inline_keyboard=[options, back])
edit_message(chat_game, bot, user_id, "Choose card to " + keyboard_type, keyboard)
elif keyboard_type == "player":
players = chat_game.game.players
options = []
for p in players:
if p != player:
options.append(InlineKeyboardButton(text=p, callback_data=p + '|' + str(chat_id)))
back = [InlineKeyboardButton(text='Back', callback_data='back|' + str(chat_id))]
keyboard = InlineKeyboardMarkup(inline_keyboard=[options, back])
edit_message(chat_game, bot, user_id, "Choose a player to hint", keyboard=keyboard)
elif keyboard_type == "info":
# TODO: ugly keyboard on desktop
colors = []
values = []
for c in hanabi.colors:
colors.append(InlineKeyboardButton(text=c, callback_data=c + '|' + str(chat_id)))
for i in range(1, 6):
values.append(InlineKeyboardButton(text=str(i), callback_data=str(i) + '|' + str(chat_id)))
back = [InlineKeyboardButton(text='Back', callback_data='back|' + str(chat_id))]
keyboard = InlineKeyboardMarkup(inline_keyboard=[colors, values, back])
edit_message(chat_game, bot, user_id, "Choose information to hint", keyboard=keyboard)
def restart_turn(chat_id):
chat_game = server.games[chat_id]
chat_game.current_action = ''
send_keyboard(server.bot, chat_id, "action")
def handle_game_ending(bot, chat_game):
send_game_views(bot, chat_game)
chat_id = chat_game.chat_id
game = chat_game.game
filename = str(chat_id) + '.png'
draw.draw_board_state(chat_game.game, '', filename)
try:
with open(filename, 'rb') as image:
bot.sendPhoto(chat_id, image)
except Exception as ex:
print(ex)
score = hanabi.get_score(game)
for name, user_id in chat_game.player_to_user.items():
bot.sendMessage(user_id, "The game ended with score " + str(score))
bot.sendMessage(chat_id, "The game ended with score " + str(score))
bot.sendMessage(chat_id, "Send /restart to play again")
chat_game.game = None
return
def complete_processed_action(bot, chat_id, last_player):
# check game ending
chat_game = server.games[chat_id]
if hanabi.check_state(chat_game.game) != 0:
handle_game_ending(bot, chat_game)
return
send_game_views(bot, chat_game)
chat_game.current_action = ''
next_player = hanabi.get_active_player_name(chat_game.game)
send_keyboard(server.bot, chat_id, "action")
def handle_keyboard_response(msg):
try:
query_id, from_id, data = telepot.glance(msg, flavor='callback_query')
except:
print("[ERROR]", msg)
return
user_id = int(msg['from']['id'])
chat_id = int(msg['message']['chat']['id'])
if data == 'join':
add_player(server, chat_id, user_id, msg['from']['first_name'])
return
data, chat_id = data.split('|')
chat_id = int(chat_id)
chat_game = server.games.get(chat_id, None)
if not chat_game: return
game = chat_game.game
if not game: return
active_player = hanabi.get_active_player_name(game)
active_user_id = chat_game.player_to_user[active_player]
if user_id != active_user_id: return
# perform action
if chat_game.current_action in ["discard", "play"] or chat_game.current_action.strip().startswith('hint '):
chat_game.current_action += ' ' + data
success = hanabi.perform_action(game, active_player, chat_game.current_action)
if success:
edit_message(chat_game, server.bot, user_id, delete=True)
chat_game.user_to_message[active_user_id] = None
complete_processed_action(server.bot, chat_id, active_player)
else:
restart_turn(chat_id)
if chat_game.current_action == 'hint':
chat_game.current_action += ' ' + data
send_keyboard(server.bot, chat_id, "info")
if data == 'back':
send_keyboard(server.bot, chat_id, "action")
if data == 'discard':
if chat_game.current_action != '': return False
chat_game.current_action = "discard"
send_keyboard(server.bot, chat_id, "discard")
return True
if data == 'play':
if chat_game.current_action != '': return False
chat_game.current_action = "play"
send_keyboard(server.bot, chat_id, "play")
return True
if data == 'hint':
if chat_game.current_action != '': return False
chat_game.current_action = "hint"
if len(chat_game.player_to_user) == 2:
i = 1 - game.active_player
chat_game.current_action += ' ' + game.players[i]
send_keyboard(server.bot, chat_id, "info")
else:
send_keyboard(server.bot, chat_id, "player")
return True
def handle_message(message_object):
content_type, chat_type, chat_id = telepot.glance(message_object)
user_id = int(message_object['from']['id'])
if content_type != 'text':
return
text = message_object['text'].split('@')[0].strip()
data = message_object.get('callback_data', None)
chat_id = int(chat_id)
if data:
print('DATA', data)
if text == '/new_game':
server.games[chat_id] = ChatGame(chat_id, admin=user_id)
keyboard = [[
InlineKeyboardButton(text='Join', callback_data='join'),
]]
keyboard = InlineKeyboardMarkup(inline_keyboard=keyboard)
server.bot.sendMessage(chat_id, "A new game has been created", reply_markup=keyboard)
return
elif text == '/end_game':
edit_message(server.games[chat_id], server.bot, user_id, "The game ended.")
del server.games[chat_id]
return
elif text in ['/start', '/restart']:
start_game(server, chat_id, user_id)
elif text.startswith("/test"):
try:
_, n = text.split(' ')
except ValueError:
n = 4
server.games[chat_id] = ChatGame(chat_id, admin=user_id)
server.bot.sendMessage(chat_id, "A new game has been created.")
test_players = ['gabriele', 'giacomo', 'fabrizio', 'caio']
for name in test_players[:int(n)]:
add_player(server, chat_id, user_id, name, allow_repeated_players=True)
start_game(server, chat_id, user_id)
else:
return
chat_game = server.games[chat_id]
game = chat_game.game
if not game:
return
active_player = hanabi.get_active_player_name(chat_game.game)
active_user_id = chat_game.player_to_user[active_player]
if user_id == active_user_id:
restart_turn(chat_id)
else:
server.bot.sendMessage(chat_id, "Wait for your turn.")
def main(token):
global server
server = BotServer(token)
print ('*** Telegram bot started ***')
print (' Now listening...')
MessageLoop(server.bot, {'chat': handle_message, 'callback_query': handle_keyboard_response}).run_as_thread()
while 1:
time.sleep(10)
if __name__ == '__main__':
main(sys.argv[1])