Skip to content

Commit f8527bf

Browse files
committed
Merge pull request #48 from d-Rickyy-b/bugfixes
Bugfixes
2 parents cd822db + f378ae7 commit f8527bf

File tree

3 files changed

+82
-77
lines changed

3 files changed

+82
-77
lines changed

game/blackJack.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
import logging
33

44
from telegram.keyboardbutton import KeyboardButton
5-
from telegram.replykeyboardremove import ReplyKeyboardRemove
65
from telegram.replykeyboardmarkup import ReplyKeyboardMarkup
6+
from telegram.replykeyboardremove import ReplyKeyboardRemove
77

88
from database.statistics import add_game_played, set_game_won
99
from game.dealer import Dealer
@@ -22,15 +22,16 @@ class BlackJack(object):
2222
MAX_PLAYERS = 5
2323

2424
# Adds Player to the Game
25-
def add_player(self, user_id, first_name, message_id, silent=None):
25+
def add_player(self, user_id, first_name, message_id, silent=False):
2626
if not self.game_running:
2727
if self.get_index_by_user_id(user_id) is None and len(self.players) < self.MAX_PLAYERS:
2828
self.logger.debug("Adding user '" + first_name + "' to players.")
2929
player = Player(user_id, first_name, self.deck)
3030
self.players.append(player)
3131
self.join_message_ids.append(message_id)
3232

33-
if silent is None or silent is False:
33+
if silent is False:
34+
# When the parameter 'silent' is not set, a message will be sent.
3435
# TODO When game is multiplayer then print current players?
3536
self.send_message(self.chat_id, translate("playerJoined", self.lang_id).format(first_name), message_id=message_id, game_id=self.__game_id)
3637
else:
@@ -132,7 +133,7 @@ def dealers_turn(self):
132133
if self.game_type == self.PRIVATE_CHAT:
133134
text += translate("gameBegins", self.lang_id) + "\n"
134135

135-
text += "\n*" + translate("dealersCards", self.lang_id) + "*\n\n" + self.deck.get_card_name(card) + ", | -- |"
136+
text += "\n*{}*\n\n{}, | -- |".format(translate("dealersCards", self.lang_id), self.deck.get_card_name(card))
136137
self.send_message(self.chat_id, text, parse_mode="Markdown", reply_markup=self.keyboard_running)
137138
else:
138139
output_text = translate("croupierDrew", self.lang_id) + "\n\n"
@@ -150,25 +151,28 @@ def dealers_turn(self):
150151
output_text += " , " + self.deck.get_card_name(card)
151152
i += 1
152153

153-
output_text += "\n\n" + translate("cardvalueDealer", self.lang_id) + " " + str(self.dealer.get_cardvalue())
154+
output_text += "\n\n{} {}".format(translate("cardvalueDealer", self.lang_id), self.dealer.get_cardvalue())
154155
self.send_message(self.chat_id, output_text, parse_mode="Markdown", reply_markup=self.keyboard_running)
155156
self.evaluation()
156157

157158
def start_game(self, message_id=None):
158-
if ((self.game_type == self.GROUP_CHAT or self.game_type == self.MULTIPLAYER_GAME) and len(self.players) > 1) or self.game_type == self.PRIVATE_CHAT:
159-
if not self.game_running:
160-
self.game_running = True
159+
if not self.game_running:
160+
if ((self.game_type == self.GROUP_CHAT or self.game_type == self.MULTIPLAYER_GAME) and len(self.players) > 1) or self.game_type == self.PRIVATE_CHAT:
161+
self.game_running = True
161162

162-
for player in self.players:
163-
add_game_played(player.user_id)
163+
for player in self.players:
164+
add_game_played(player.user_id)
164165

165-
if self.game_type == self.GROUP_CHAT or self.game_type == self.MULTIPLAYER_GAME:
166-
self.send_message(self.chat_id, translate("gameBegins", self.lang_id) + "\n" + translate("gameBegins2", self.lang_id) + "\n\n" + self.get_player_overview(), game_id=self.__game_id)
166+
if self.game_type == self.GROUP_CHAT or self.game_type == self.MULTIPLAYER_GAME:
167+
self.send_message(self.chat_id, translate("gameBegins", self.lang_id) + "\n" + translate("gameBegins2", self.lang_id) + "\n\n" + self.get_player_overview(), game_id=self.__game_id)
167168

168-
self.dealers_turn()
169-
self.give_player_one()
169+
self.dealers_turn()
170+
self.give_player_one()
171+
else:
172+
self.send_message(self.chat_id, translate("notEnoughPlayers", self.lang_id), message_id=message_id, game_id=self.__game_id)
170173
else:
171-
self.send_message(self.chat_id, translate("notEnoughPlayers", self.lang_id), message_id=message_id, game_id=self.__game_id)
174+
#TODO Game already running
175+
pass
172176

173177
def evaluation(self):
174178
list_21 = []

gamehandler.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,14 @@ def add_game(self, blackjackgame):
2525
self.GameList.append(blackjackgame)
2626

2727
def get_index_by_chatid(self, chat_id):
28-
index = 0
29-
for game in self.GameList:
28+
for index, game in enumerate(self.GameList):
3029
if game.chat_id == chat_id:
3130
return index
3231
else:
3332
for player in game.players:
3433
if player.user_id == chat_id:
3534
return index
36-
index += 1
35+
3736
return None
3837

3938
def get_game_by_chatid(self, chat_id):

main.py

Lines changed: 61 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import logging
44
import re
55

6+
from telegram import ReplyKeyboardRemove
67
from telegram.ext import Updater, CommandHandler, CallbackQueryHandler, MessageHandler, Filters
78
from telegram.inline.inlinekeyboardbutton import InlineKeyboardButton
89
from telegram.inline.inlinekeyboardmarkup import InlineKeyboardMarkup
9-
from telegram import ReplyKeyboardRemove
1010

1111
from database.db_wrapper import DBwrapper
1212
from 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-
10772
def 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+
207207
def 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

231231
def 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

264264
def 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
280280
def 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):
333334
hide_handler = CommandHandler('hide', hide_cmd)
334335
stats_handler = CommandHandler('stats', stats_cmd)
335336
language_handler = CommandHandler('language', language_cmd)
336-
callback_handler = CallbackQueryHandler(callback_eval)
337337
comment_handler = CommandHandler('comment', comment_cmd)
338-
cancel_handler = CommandHandler(get_translations_of_string("cancel"), cancel)
338+
callback_handler = CallbackQueryHandler(callback_eval)
339339
answer_handler = CommandHandler('answer', answer)
340340

341-
game_command_handler = MessageHandler(Filters.all, game_commands)
341+
game_command_handler = MessageHandler(Filters.text, game_commands)
342342

343343
mp_handler = CommandHandler('multiplayer', multiplayer)
344344
join_sec = CommandHandler('join_secret', join_secret)
345345

346-
dispatcher.add_handler(callback_handler)
347-
dispatcher.add_handler(language_handler)
348346
dispatcher.add_handler(start_handler)
349347
dispatcher.add_handler(stop_handler)
350-
dispatcher.add_handler(answer_handler)
348+
dispatcher.add_handler(hide_handler)
351349
dispatcher.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+
352355
dispatcher.add_handler(mp_handler)
353356
dispatcher.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
357359
dispatcher.add_handler(game_command_handler)
358360

359361
updater.start_polling()

0 commit comments

Comments
 (0)