33import logging
44import re
55
6+ from telegram import ReplyKeyboardRemove
67from telegram .ext import Updater , CommandHandler , CallbackQueryHandler , MessageHandler , Filters
78from telegram .inline .inlinekeyboardbutton import InlineKeyboardButton
89from telegram .inline .inlinekeyboardmarkup import InlineKeyboardMarkup
9- from telegram import ReplyKeyboardRemove
1010
1111from database .db_wrapper import DBwrapper
1212from database .statistics import get_user_stats
@@ -69,41 +69,6 @@ def start_cmd(bot, update):
6969 game .start_game ()
7070
7171
72- def multiplayer (bot , update ):
73- chat_id = update .message .chat_id
74- user_id = update .message .from_user .id
75- message_id = update .message .message_id
76- first_name = update .message .from_user .first_name
77- # last_name = update.message.from_user.last_name
78- # username = update.message.from_user.username
79- db = DBwrapper .get_instance ()
80-
81- game_index = game_handler .get_index_by_chatid (chat_id )
82- if game_index is None :
83- logger .debug ("Creating a game" )
84- lang_id = db .get_lang_id (user_id )
85- game_id = game_handler .generate_id ()
86- bj = BlackJack (chat_id , user_id , lang_id , first_name , game_handler , message_id , send_mp_message ,
87- multiplayer = True , game_id = game_id )
88- game_handler .add_game (bj )
89- send_message (chat_id , "Your game_id: " + bj .get_game_id ())
90- else :
91- logger .debug ("Game already existing" )
92-
93-
94- def join_secret (bot , update ):
95- user_id = update .message .from_user .id
96- message_id = update .message .message_id
97- first_name = update .message .from_user .first_name
98- text = update .message .text
99- game_id = text .split (' ' )[1 ]
100-
101- print ("ID: " + game_id )
102- game = game_handler .get_game_by_id (game_id )
103- game .add_player (user_id , first_name , message_id )
104- # TODO send message that user joined
105-
106-
10772def stop_cmd (bot , update ):
10873 user_id = update .message .from_user .id
10974 state_handler = StateHandler .get_instance ()
@@ -115,7 +80,7 @@ def stop_cmd(bot, update):
11580 game_handler .gl_remove (chat_id )
11681
11782
118- def help_def (bot , update ):
83+ def help_cmd (bot , update ):
11984 pass
12085
12186
@@ -186,7 +151,7 @@ def comment_cmd(bot, update):
186151 user .set_state (UserState .COMMENTING )
187152
188153
189- def cancel (bot , update ):
154+ def cancel_cmd (bot , update ):
190155 user_id = update .effective_user .id
191156 message_id = update .effective_message .message_id
192157 callback_query_id = update .callback_query .id
@@ -204,6 +169,41 @@ def cancel(bot, update):
204169 bot .answerCallbackQuery (callback_query_id = callback_query_id , text = translate ("cancelledMessage" , lang_id ))
205170
206171
172+ def multiplayer (bot , update ):
173+ chat_id = update .message .chat_id
174+ user_id = update .message .from_user .id
175+ message_id = update .message .message_id
176+ first_name = update .message .from_user .first_name
177+ # last_name = update.message.from_user.last_name
178+ # username = update.message.from_user.username
179+ db = DBwrapper .get_instance ()
180+
181+ game_index = game_handler .get_index_by_chatid (chat_id )
182+ if game_index is None :
183+ logger .debug ("Creating a game" )
184+ lang_id = db .get_lang_id (user_id )
185+ game_id = game_handler .generate_id ()
186+ bj = BlackJack (chat_id , user_id , lang_id , first_name , game_handler , message_id , send_mp_message ,
187+ multiplayer = True , game_id = game_id )
188+ game_handler .add_game (bj )
189+ bot .sendMessage (chat_id , "Your game_id: {}" .format (bj .get_game_id ()))
190+ else :
191+ logger .debug ("Game already existing" )
192+
193+
194+ def join_secret (bot , update ):
195+ user_id = update .message .from_user .id
196+ message_id = update .message .message_id
197+ first_name = update .message .from_user .first_name
198+ text = update .message .text
199+ game_id = text .split (' ' )[1 ]
200+
201+ print ("ID: " + game_id )
202+ game = game_handler .get_game_by_id (game_id )
203+ game .add_player (user_id , first_name , message_id )
204+ # TODO send message that user joined
205+
206+
207207def answer (bot , update ):
208208 sender_id = update .message .from_user .id
209209 reply_to_message = update .message .reply_to_message
@@ -223,9 +223,9 @@ def answer(bot, update):
223223 except :
224224 return
225225
226- answer = translate ("answerFromDev" , db .get_lang_id (user_id )) + " \n \n " + text
227- send_message ( user_id , answer )
228- send_message ( sender_id , "Message sent!" )
226+ answer_text = "{} \n \n {}" . format ( translate ("answerFromDev" , db .get_lang_id (user_id )), text )
227+ bot . sendMessage ( chat_id = user_id , text = answer_text )
228+ bot . sendMessage ( chat_id = sender_id , text = "Message sent!" )
229229
230230
231231def mentions (bot , update ):
@@ -258,7 +258,7 @@ def callback_eval(bot, update):
258258 language_cmd (bot , update )
259259
260260 elif query_data == "cancel_comment" :
261- cancel (bot , update )
261+ cancel_cmd (bot , update )
262262
263263
264264def send_message (chat_id , text , message_id = None , parse_mode = None , reply_markup = None , game_id = None ):
@@ -280,10 +280,11 @@ def send_mp_message(chat_id, text, message_id=None, parse_mode=None, reply_marku
280280def game_commands (bot , update ):
281281 text = update .message .text
282282 chat_id = update .message .chat_id
283- user_id = update .message .from_user .id
284- first_name = update .message .from_user .first_name
285- last_name = update .message .from_user .last_name
286- username = update .message .from_user .username
283+ user = update .effective_user
284+ user_id = user .id
285+ first_name = user .first_name
286+ last_name = user .last_name
287+ username = user .username
287288 db = DBwrapper .get_instance ()
288289 lang_id = db .get_lang_id (user_id )
289290
@@ -292,11 +293,11 @@ def game_commands(bot, update):
292293
293294 if user .get_state () == UserState .COMMENTING :
294295 # User wants to comment!
295- send_message (chat_id , translate ("userComment" , lang_id ))
296+ bot . sendMessage (chat_id , text = translate ("userComment" , lang_id ))
296297 for admin_id in db .get_admins ():
297- send_message ( admin_id ,
298- "New comment: \n \n {} \n \n {} | {} | {} | @{} | {}" . format ( text , user_id , first_name , last_name ,
299- username , lang_id ) )
298+ admin_message = "New comment: \n \n {} \n \n {} | {} | {} | @{} | {}" . format ( text , user_id , first_name , last_name ,
299+ username , lang_id )
300+ bot . sendMessage ( admin_id , text = admin_message )
300301
301302 user .set_state (UserState .IDLE )
302303 return
@@ -333,27 +334,28 @@ def get_translations_of_string(string):
333334hide_handler = CommandHandler ('hide' , hide_cmd )
334335stats_handler = CommandHandler ('stats' , stats_cmd )
335336language_handler = CommandHandler ('language' , language_cmd )
336- callback_handler = CallbackQueryHandler (callback_eval )
337337comment_handler = CommandHandler ('comment' , comment_cmd )
338- cancel_handler = CommandHandler ( get_translations_of_string ( "cancel" ), cancel )
338+ callback_handler = CallbackQueryHandler ( callback_eval )
339339answer_handler = CommandHandler ('answer' , answer )
340340
341- game_command_handler = MessageHandler (Filters .all , game_commands )
341+ game_command_handler = MessageHandler (Filters .text , game_commands )
342342
343343mp_handler = CommandHandler ('multiplayer' , multiplayer )
344344join_sec = CommandHandler ('join_secret' , join_secret )
345345
346- dispatcher .add_handler (callback_handler )
347- dispatcher .add_handler (language_handler )
348346dispatcher .add_handler (start_handler )
349347dispatcher .add_handler (stop_handler )
350- dispatcher .add_handler (answer_handler )
348+ dispatcher .add_handler (hide_handler )
351349dispatcher .add_handler (stats_handler )
350+ dispatcher .add_handler (language_handler )
351+ dispatcher .add_handler (comment_handler )
352+ dispatcher .add_handler (callback_handler )
353+ dispatcher .add_handler (answer_handler )
354+
352355dispatcher .add_handler (mp_handler )
353356dispatcher .add_handler (join_sec )
354- dispatcher .add_handler (comment_handler )
355- dispatcher .add_handler (cancel_handler )
356- dispatcher .add_handler (hide_handler )
357+
358+ # Should always be the last handler to add -> Fallback if no command found
357359dispatcher .add_handler (game_command_handler )
358360
359361updater .start_polling ()
0 commit comments