From 6204868a18dc17426928b98b2a45004da7180c72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Thu, 19 May 2016 20:52:50 +0200 Subject: [PATCH 01/20] separate game logic from bot interface, introduce exceptions instead of boolean returns, remove repetitive code, begin unit tests, improve docstrings, update to python-telegram-bot==4.1.1, add ponyorm settings classes (unused) --- bot.py | 500 ++++++++++++++++++++++---------------- card.py | 10 +- chat_setting.py | 20 ++ database.py | 23 ++ deck.py | 21 +- errors.py | 37 +++ game.py | 15 +- game_manager.py | 115 +++++---- player.py | 30 ++- results.py | 21 +- test/test.py | 41 ---- test/test_game_manager.py | 73 ++++++ test/test_player.py | 158 ++++++++++++ user_setting.py | 30 +++ 14 files changed, 755 insertions(+), 339 deletions(-) create mode 100644 chat_setting.py create mode 100644 database.py create mode 100644 errors.py delete mode 100644 test/test.py create mode 100644 test/test_game_manager.py create mode 100644 test/test_player.py create mode 100644 user_setting.py diff --git a/bot.py b/bot.py index 1fffc37..2f87297 100644 --- a/bot.py +++ b/bot.py @@ -32,8 +32,16 @@ from telegram.utils.botan import Botan from game_manager import GameManager from credentials import TOKEN, BOTAN_TOKEN from start_bot import start_bot -from results import * -from utils import * +from results import (add_call_bluff, add_choose_color, add_draw, add_gameinfo, + add_no_game, add_not_started, add_other_cards, add_pass, + add_card) +from utils import display_name +import card as c +from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError, + NotEnoughPlayersError, DeckEmptyError) +from database import db_session + +TIMEOUT = 2.5 logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', @@ -83,8 +91,9 @@ source_text = ("This bot is Free Software and licensed under the AGPL. " @run_async def send_async(bot, *args, **kwargs): + """Send a message asynchronously""" if 'timeout' not in kwargs: - kwargs['timeout'] = 2.5 + kwargs['timeout'] = TIMEOUT try: bot.sendMessage(*args, **kwargs) @@ -94,8 +103,9 @@ def send_async(bot, *args, **kwargs): @run_async def answer_async(bot, *args, **kwargs): + """Answer an inline query asynchronously""" if 'timeout' not in kwargs: - kwargs['timeout'] = 2.5 + kwargs['timeout'] = TIMEOUT try: bot.answerInlineQuery(*args, **kwargs) @@ -104,89 +114,97 @@ def answer_async(bot, *args, **kwargs): def error(bot, update, error): - """ Simple error handler """ + """Simple error handler""" logger.exception(error) def new_game(bot, update): - """ Handler for the /new command """ + """Handler for the /new command""" chat_id = update.message.chat_id + if update.message.chat.type == 'private': help(bot, update) + else: game = gm.new_game(update.message.chat) game.owner = update.message.from_user send_async(bot, chat_id, text="Created a new game! Join the game with /join " "and start the game with /start") + if botan: botan.track(update.message, 'New games') def join_game(bot, update): - """ Handler for the /join command """ - chat_id = update.message.chat_id + """Handler for the /join command""" + chat = update.message.chat + if update.message.chat.type == 'private': help(bot, update) - else: - try: - game = gm.chatid_games[chat_id][-1] - if not game.open: - send_async(bot, chat_id, text="The lobby is closed") - return - except (KeyError, IndexError): - pass + return - joined = gm.join_game(chat_id, update.message.from_user) - if joined: - send_async(bot, chat_id, - text="Joined the game", - reply_to_message_id=update.message.message_id) - elif joined is None: - send_async(bot, chat_id, - text="No game is running at the moment. " - "Create a new game with /new", - reply_to_message_id=update.message.message_id) - else: - send_async(bot, chat_id, - text="You already joined the game. Start the game " - "with /start", - reply_to_message_id=update.message.message_id) + try: + gm.join_game(update.message.from_user, chat) + + except LobbyClosedError: + send_async(bot, chat.id, text="The lobby is closed") + + except NoGameInChatError: + send_async(bot, chat.id, + text="No game is running at the moment. " + "Create a new game with /new", + reply_to_message_id=update.message.message_id) + + except AlreadyJoinedError: + send_async(bot, chat.id, + text="You already joined the game. Start the game " + "with /start", + reply_to_message_id=update.message.message_id) + else: + send_async(bot, chat.id, + text="Joined the game", + reply_to_message_id=update.message.message_id) def leave_game(bot, update): - """ Handler for the /leave command """ - chat_id = update.message.chat_id + """Handler for the /leave command""" + chat = update.message.chat user = update.message.from_user - players = gm.userid_players.get(user.id, list()) - for player in players: - if player.game.chat.id == chat_id: - game = player.game - break - else: - send_async(bot, chat_id, text="You are not playing in a game in " + + player = gm.player_for_user_in_chat(user, chat) + + if player is None: + send_async(bot, chat.id, text="You are not playing in a game in " "this group.", reply_to_message_id=update.message.message_id) return + game = player.game user = update.message.from_user - if len(game.players) < 3: - gm.end_game(chat_id, user) - send_async(bot, chat_id, text="Game ended!") + try: + gm.leave_game(user, chat) + + except NoGameInChatError: + send_async(bot, chat.id, text="You are not playing in a game in " + "this group.", + reply_to_message_id=update.message.message_id) + + except NotEnoughPlayersError: + gm.end_game(chat, user) + send_async(bot, chat.id, text="Game ended!") + else: - if gm.leave_game(user, chat_id): - send_async(bot, chat_id, - text="Okay. Next Player: " + - display_name(game.current_player.user), - reply_to_message_id=update.message.message_id) - else: - send_async(bot, chat_id, text="You are not playing in a game in " - "this group.", - reply_to_message_id=update.message.message_id) + send_async(bot, chat.id, + text="Okay. Next Player: " + + display_name(game.current_player.user), + reply_to_message_id=update.message.message_id) +@run_async def select_game(bot, update): + """Handler for callback queries to select the current game""" chat_id = int(update.callback_query.data) user_id = update.callback_query.from_user.id @@ -196,8 +214,9 @@ def select_game(bot, update): gm.userid_current[user_id] = player break else: - send_async(bot, update.callback_query.message.chat_id, - text="Game not found :(") + bot.sendMessage(update.callback_query.message.chat_id, + text="Game not found.", + timeout=TIMEOUT) return back = [[InlineKeyboardButton(text='Back to last group', @@ -206,7 +225,8 @@ def select_game(bot, update): bot.answerCallbackQuery(update.callback_query.id, text="Please switch to the group you selected!", show_alert=False, - timeout=2.5) + timeout=TIMEOUT) + bot.editMessageText(chat_id=update.callback_query.message.chat_id, message_id=update.callback_query.message.message_id, text="Selected group: %s\n" @@ -215,87 +235,115 @@ def select_game(bot, update): % gm.userid_current[user_id].game.chat.title, reply_markup=InlineKeyboardMarkup(back), parse_mode=ParseMode.HTML, - timeout=2.5) + timeout=TIMEOUT) def status_update(bot, update): - """ Remove player from game if user leaves the group """ + """Remove player from game if user leaves the group""" + chat = update.message.chat if update.message.left_chat_member: try: - chat_id = update.message.chat_id user = update.message.left_chat_member except KeyError: return - if gm.leave_game(user, chat_id): - send_async(bot, chat_id, text="Removing %s from the game" + try: + gm.leave_game(user, chat) + except NoGameInChatError: + pass + except NotEnoughPlayersError: + gm.end_game(chat, user) + send_async(bot, chat.id, text="Game ended!") + else: + send_async(bot, chat.id, text="Removing %s from the game" % display_name(user)) def start_game(bot, update, args): - """ Handler for the /start command """ + """Handler for the /start command""" if update.message.chat.type != 'private': - # Show the first card - chat_id = update.message.chat_id + chat = update.message.chat + try: - game = gm.chatid_games[chat_id][-1] + game = gm.chatid_games[chat.id][-1] except (KeyError, IndexError): - send_async(bot, chat_id, text="There is no game running in this " + send_async(bot, chat.id, text="There is no game running in this " "chat. Create a new one with /new") return - if game.current_player is None or \ - game.current_player is game.current_player.next: - send_async(bot, chat_id, text="At least two players must /join " + if game.started: + send_async(bot, chat.id, text="The game has already started") + + elif len(game.players) < 2: + send_async(bot, chat.id, text="At least two players must /join " "the game before you can start it") - elif game.started: - send_async(bot, chat_id, text="The game has already started") + else: game.play_card(game.last_card) game.started = True - bot.sendSticker(chat_id, - sticker=c.STICKERS[str(game.last_card)], - timeout=2.5) - send_async(bot, chat_id, - text="First player: %s\n" - "Use /close to stop people from joining the game." - % display_name(game.current_player.user)) + + @run_async + def send_first(): + """Send the first card and player""" + + bot.sendSticker(chat.id, + sticker=c.STICKERS[str(game.last_card)], + timeout=TIMEOUT) + + bot.sendMessage(chat.id, + text="First player: %s\n" + "Use /close to stop people from joining " + "the game." + % display_name(game.current_player.user), + timeout=TIMEOUT) + + send_first() + elif len(args) and args[0] == 'select': players = gm.userid_players[update.message.from_user.id] groups = list() for player in players: - groups.append([InlineKeyboardButton(text=player.game.chat.title, - callback_data= - str(player.game.chat.id))]) + title = player.game.chat.title + + if player is gm.userid_current[update.message.from_user.id]: + title = '- %s -' % player.game.chat.title + + groups.append( + [InlineKeyboardButton(text=title, + callback_data=str(player.game.chat.id))] + ) + send_async(bot, update.message.chat_id, - text='Please select the group you want to play in. ', + text='Please select the group you want to play in.', reply_markup=InlineKeyboardMarkup(groups)) + else: help(bot, update) def close_game(bot, update): - """ Handler for the /close command """ - chat_id = update.message.chat_id + """Handler for the /close command""" + chat = update.message.chat user = update.message.from_user - games = gm.chatid_games.get(chat_id) + games = gm.chatid_games.get(chat.id) if not games: - send_async(bot, chat_id, text="There is no running game") + send_async(bot, chat.id, text="There is no running game in this chat.") return game = games[-1] if game.owner.id == user.id: game.open = False - send_async(bot, chat_id, text="Closed the lobby. " + send_async(bot, chat.id, text="Closed the lobby. " "No more players can join this game.") return + else: - send_async(bot, chat_id, + send_async(bot, chat.id, text="Only the game creator (%s) can do that" % game.owner.first_name, reply_to_message_id=update.message.message_id) @@ -303,118 +351,115 @@ def close_game(bot, update): def open_game(bot, update): - """ Handler for the /open command """ - chat_id = update.message.chat_id + """Handler for the /open command""" + chat = update.message.chat user = update.message.from_user - games = gm.chatid_games.get(chat_id) + games = gm.chatid_games.get(chat.id) if not games: - send_async(bot, chat_id, text="There is no running game") + send_async(bot, chat.id, text="There is no running game in this chat.") return game = games[-1] if game.owner.id == user.id: game.open = True - send_async(bot, chat_id, text="Opened the lobby. " + send_async(bot, chat.id, text="Opened the lobby. " "New players may /join the game.") return else: - send_async(bot, chat_id, - text="Only the game creator (%s) can do that" + send_async(bot, chat.id, + text="Only the game creator (%s) can do that." % game.owner.first_name, reply_to_message_id=update.message.message_id) return def skip_player(bot, update): - """ Handler for the /skip command """ - chat_id = update.message.chat_id + """Handler for the /skip command""" + chat = update.message.chat user = update.message.from_user - games = gm.chatid_games.get(chat_id) - players = gm.userid_players.get(user.id) - if not games: - send_async(bot, chat_id, text="There is no running game") + player = gm.player_for_user_in_chat(user, chat) + if not player: + send_async(bot, chat.id, text="You are not playing in a game in this " + "chat.") return - if not players: - send_async(bot, chat_id, text="You are not playing") - return + game = player.game + skipped_player = game.current_player + next_player = game.current_player.next - for game in games: - for player in players: - if player in game.players: - started = game.current_player.turn_started - now = datetime.now() - delta = (now - started).seconds + started = skipped_player.turn_started + now = datetime.now() + delta = (now - started).seconds - if delta < game.current_player.waiting_time: - send_async(bot, chat_id, - text="Please wait %d seconds" - % (game.current_player.waiting_time - - delta), - reply_to_message_id= - update.message.message_id) - return + if delta < skipped_player.waiting_time: + send_async(bot, chat.id, + text="Please wait %d seconds" + % (skipped_player.waiting_time - delta), + reply_to_message_id=update.message.message_id) - elif game.current_player.waiting_time > 0: - game.current_player.anti_cheat += 1 - game.current_player.waiting_time -= 30 - game.current_player.cards.append(game.deck.draw()) - send_async(bot, chat_id, - text="Waiting time to skip this player has " - "been reduced to %d seconds.\n" - "Next player: %s" - % (game.current_player.waiting_time, - display_name( - game.current_player.next.user))) - game.turn() - return + elif skipped_player.waiting_time > 0: + skipped_player.anti_cheat += 1 + skipped_player.waiting_time -= 30 + try: + skipped_player.draw() + except DeckEmptyError: + pass - elif len(game.players) > 2: - send_async(bot, chat_id, - text="%s was skipped four times in a row " - "and has been removed from the game.\n" - "Next player: %s" - % (display_name(game.current_player.user), - display_name( - game.current_player.next.user))) + send_async(bot, chat.id, + text="Waiting time to skip this player has " + "been reduced to %d seconds.\n" + "Next player: %s" + % (skipped_player.waiting_time, + display_name(next_player.user))) + game.turn() - gm.leave_game(game.current_player.user, chat_id) - return - else: - send_async(bot, chat_id, - text="%s was skipped four times in a row " - "and has been removed from the game.\n" - "The game ended." - % display_name(game.current_player.user)) + else: + try: + gm.leave_game(skipped_player.user, chat) + send_async(bot, chat.id, + text="%s was skipped four times in a row " + "and has been removed from the game.\n" + "Next player: %s" + % (display_name(skipped_player.user), + display_name(next_player.user))) - gm.end_game(chat_id, game.current_player.user) - return + except NotEnoughPlayersError: + send_async(bot, chat.id, + text="%s was skipped four times in a row " + "and has been removed from the game.\n" + "The game ended." + % display_name(skipped_player.user)) + + gm.end_game(chat.id, skipped_player.user) def help(bot, update): - """ Handler for the /help command """ + """Handler for the /help command""" send_async(bot, update.message.chat_id, text=help_text, parse_mode=ParseMode.HTML, disable_web_page_preview=True) def source(bot, update): - """ Handler for the /help command """ + """Handler for the /help command""" send_async(bot, update.message.chat_id, text=source_text, parse_mode=ParseMode.HTML, disable_web_page_preview=True) def news(bot, update): - """ Handler for the /news command """ + """Handler for the /news command""" send_async(bot, update.message.chat_id, text="All news here: https://telegram.me/unobotupdates", disable_web_page_preview=True) def reply_to_query(bot, update): - """ Builds the result list for inline queries and answers to the client """ + """ + Handler for inline queries. + Builds the result list for inline queries and answers to the client. + """ results = list() playable = list() switch = None @@ -429,9 +474,11 @@ def reply_to_query(bot, update): else: if not game.started: add_not_started(results) + elif user_id == game.current_player.user.id: if game.choosing_color: add_choose_color(results) + add_other_cards(playable, player, results, game) else: if not player.drew: add_draw(player, results) @@ -443,19 +490,18 @@ def reply_to_query(bot, update): add_call_bluff(results) playable = player.playable_cards() - added_ids = list() + added_ids = list() # Duplicates are not allowed for card in sorted(player.cards): - add_play_card(game, card, results, - can_play=(card in playable and + add_card(game, card, results, + can_play=(card in playable and str(card) not in added_ids)) added_ids.append(str(card)) - if False or game.choosing_color: - add_other_cards(playable, player, results, game) elif user_id != game.current_player.user.id or not game.started: for card in sorted(player.cards): - add_play_card(game, card, results, can_play=False) + add_card(game, card, results, can_play=False) + else: add_gameinfo(game, results) @@ -470,13 +516,16 @@ def reply_to_query(bot, update): def process_result(bot, update): - """ Check the players actions and act accordingly """ + """ + Handler for chosen inline results. + Checks the players actions and acts accordingly. + """ try: user = update.chosen_inline_result.from_user player = gm.userid_current[user.id] game = player.game result_id = update.chosen_inline_result.result_id - chat_id = game.chat.id + chat = game.chat except KeyError: return @@ -491,103 +540,130 @@ def process_result(bot, update): elif len(result_id) == 36: # UUID result return elif int(anti_cheat) != last_anti_cheat: - send_async(bot, chat_id, + send_async(bot, chat.id, text="Cheat attempt by %s" % display_name(player.user)) return elif result_id == 'call_bluff': - reset_waiting_time(bot, chat_id, player) - do_call_bluff(bot, chat_id, game, player) + reset_waiting_time(bot, player) + do_call_bluff(bot, player) elif result_id == 'draw': - reset_waiting_time(bot, chat_id, player) - do_draw(game, player) + reset_waiting_time(bot, player) + do_draw(player) elif result_id == 'pass': game.turn() elif result_id in c.COLORS: game.choose_color(result_id) else: - reset_waiting_time(bot, chat_id, player) - do_play_card(bot, chat_id, game, player, result_id, user) + reset_waiting_time(bot, player) + do_play_card(bot, player, result_id) - if game in gm.chatid_games.get(chat_id, list()): - send_async(bot, chat_id, text="Next player: " + + if game in gm.chatid_games.get(chat.id, list()): + send_async(bot, chat.id, text="Next player: " + display_name(game.current_player.user)) -def reset_waiting_time(bot, chat_id, player): +def reset_waiting_time(bot, player): + """Resets waiting time for a player and sends a notice to the group""" + chat = player.game.chat + if player.waiting_time < 90: player.waiting_time = 90 - send_async(bot, chat_id, text="Waiting time for %s has been reset to " + send_async(bot, chat.id, text="Waiting time for %s has been reset to " "90 seconds" % display_name(player.user)) -def do_play_card(bot, chat_id, game, player, result_id, user): +def do_play_card(bot, player, result_id): + """Plays the selected card and sends an update to the group if needed""" card = c.from_str(result_id) - game.play_card(card) - player.cards.remove(card) + player.play(card) + game = player.game + chat = game.chat + user = player.user + if game.choosing_color: - send_async(bot, chat_id, text="Please choose a color") + send_async(bot, chat.id, text="Please choose a color") + if len(player.cards) == 1: - send_async(bot, chat_id, text="UNO!") + send_async(bot, chat.id, text="UNO!") + if len(player.cards) == 0: - send_async(bot, chat_id, text="%s won!" % user.first_name) - if len(game.players) < 3: - send_async(bot, chat_id, text="Game ended!") - gm.end_game(chat_id, user) - else: - gm.leave_game(user, chat_id) + send_async(bot, chat.id, text="%s won!" % user.first_name) + try: + gm.leave_game(user, chat) + except NotEnoughPlayersError: + send_async(bot, chat.id, text="Game ended!") + gm.end_game(chat, user) if botan: botan.track(Message(randint(1, 1000000000), user, datetime.now(), - Chat(chat_id, 'group')), + Chat(chat.id, 'group')), 'Played cards') -def do_draw(game, player): +def do_draw(bot, player): + """Does the drawing""" + game = player.game draw_counter_before = game.draw_counter - for n in range(game.draw_counter or 1): - player.cards.append(game.deck.draw()) - game.draw_counter = 0 - player.drew = True + + try: + player.draw() + except DeckEmptyError: + send_async(bot, player.game.chat.id, + text="There are no more cards in the deck.") + if (game.last_card.value == c.DRAW_TWO or game.last_card.special == c.DRAW_FOUR) and \ draw_counter_before > 0: game.turn() -def do_call_bluff(bot, chat_id, game, player): +def do_call_bluff(bot, player): + """Handles the bluff calling""" + game = player.game + chat = game.chat + if player.prev.bluffing: - send_async(bot, chat_id, text="Bluff called! Giving %d cards to %s" + send_async(bot, chat.id, text="Bluff called! Giving %d cards to %s" % (game.draw_counter, player.prev.user.first_name)) - for i in range(game.draw_counter): - player.prev.cards.append(game.deck.draw()) + + try: + player.prev.draw() + except DeckEmptyError: + send_async(bot, player.game.chat.id, + text="There are no more cards in the deck.") + else: - send_async(bot, chat_id, text="%s didn't bluff! Giving %d cards to %s" + game.draw_counter += 2 + send_async(bot, chat.id, text="%s didn't bluff! Giving %d cards to %s" % (player.prev.user.first_name, - game.draw_counter + 2, + game.draw_counter, player.user.first_name)) - for i in range(game.draw_counter + 2): - player.cards.append(game.deck.draw()) - game.draw_counter = 0 + try: + player.draw() + except DeckEmptyError: + send_async(bot, player.game.chat.id, + text="There are no more cards in the deck.") + game.turn() # Add all handlers to the dispatcher and run the bot -dp.addHandler(InlineQueryHandler(reply_to_query)) -dp.addHandler(ChosenInlineResultHandler(process_result)) -dp.addHandler(CallbackQueryHandler(select_game)) -dp.addHandler(CommandHandler('start', start_game, pass_args=True)) -dp.addHandler(CommandHandler('new', new_game)) -dp.addHandler(CommandHandler('join', join_game)) -dp.addHandler(CommandHandler('leave', leave_game)) -dp.addHandler(CommandHandler('open', open_game)) -dp.addHandler(CommandHandler('close', close_game)) -dp.addHandler(CommandHandler('skip', skip_player)) -dp.addHandler(CommandHandler('help', help)) -dp.addHandler(CommandHandler('source', source)) -dp.addHandler(CommandHandler('news', news)) -dp.addHandler(MessageHandler([Filters.status_update], status_update)) -dp.addErrorHandler(error) +dp.add_handler(InlineQueryHandler(reply_to_query)) +dp.add_handler(ChosenInlineResultHandler(process_result)) +dp.add_handler(CallbackQueryHandler(select_game)) +dp.add_handler(CommandHandler('start', start_game, pass_args=True)) +dp.add_handler(CommandHandler('new', new_game)) +dp.add_handler(CommandHandler('join', join_game)) +dp.add_handler(CommandHandler('leave', leave_game)) +dp.add_handler(CommandHandler('open', open_game)) +dp.add_handler(CommandHandler('close', close_game)) +dp.add_handler(CommandHandler('skip', skip_player)) +dp.add_handler(CommandHandler('help', help)) +dp.add_handler(CommandHandler('source', source)) +dp.add_handler(CommandHandler('news', news)) +dp.add_handler(MessageHandler([Filters.status_update], status_update)) +dp.add_error_handler(error) start_bot(u) u.idle() diff --git a/card.py b/card.py index aa3b303..a291e45 100644 --- a/card.py +++ b/card.py @@ -180,9 +180,7 @@ STICKERS_GREY = { class Card(object): - """ - This class represents a card. - """ + """This class represents an UNO card""" def __init__(self, color, value, special=None): self.color = color @@ -205,16 +203,16 @@ class Card(object): return '%s%s' % (COLOR_ICONS[self.color], self.value.capitalize()) def __eq__(self, other): - """ Needed for sorting the cards """ + """Needed for sorting the cards""" return str(self) == str(other) def __lt__(self, other): - """ Needed for sorting the cards """ + """Needed for sorting the cards""" return str(self) < str(other) def from_str(string): - """ Decode a Card object from a string """ + """Decodes a Card object from a string""" if string not in SPECIALS: color, value = string.split('_') return Card(color, value) diff --git a/chat_setting.py b/chat_setting.py new file mode 100644 index 0000000..baccfee --- /dev/null +++ b/chat_setting.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python3 +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +pass diff --git a/database.py b/database.py new file mode 100644 index 0000000..30b2e54 --- /dev/null +++ b/database.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python3 +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +from pony.orm import Database, db_session, Optional, Required, Set, PrimaryKey + +# Database singleton +db = Database() diff --git a/deck.py b/deck.py index 0d3b82c..0fda198 100644 --- a/deck.py +++ b/deck.py @@ -18,9 +18,11 @@ from random import shuffle +import logging + import card as c from card import Card -import logging +from errors import DeckEmptyError class Deck(object): @@ -45,22 +47,25 @@ class Deck(object): self.shuffle() def shuffle(self): - """ Shuffle the deck """ + """Shuffles the deck""" self.logger.debug("Shuffling Deck") shuffle(self.cards) def draw(self): - """ Draw a card from this deck """ + """Draws a card from this deck""" try: card = self.cards.pop() self.logger.debug("Drawing card " + str(card)) return card except IndexError: - while len(self.graveyard): - self.cards.append(self.graveyard.pop()) - self.shuffle() - return self.draw() + if len(self.graveyard): + while len(self.graveyard): + self.cards.append(self.graveyard.pop()) + self.shuffle() + return self.draw() + else: + raise DeckEmptyError() def dismiss(self, card): - """ All played cards should be returned into the deck """ + """Returns a card to the deck""" self.graveyard.append(card) diff --git a/errors.py b/errors.py new file mode 100644 index 0000000..ac0575a --- /dev/null +++ b/errors.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +class NoGameInChatError(Exception): + pass + + +class AlreadyJoinedError(Exception): + pass + + +class LobbyClosedError(Exception): + pass + + +class NotEnoughPlayersError(Exception): + pass + + +class DeckEmptyError(Exception): + pass diff --git a/game.py b/game.py index 0e4e632..169e4fc 100644 --- a/game.py +++ b/game.py @@ -48,6 +48,7 @@ class Game(object): @property def players(self): + """Returns a list of all players in this game""" players = list() if not self.current_player: return players @@ -61,18 +62,23 @@ class Game(object): return players def reverse(self): - """ Reverse the direction of play """ + """Reverses the direction of game""" self.reversed = not self.reversed def turn(self): - """ Mark the turn as over and change the current player """ + """Marks the turn as over and change the current player""" self.logger.debug("Next Player") self.current_player = self.current_player.next self.current_player.drew = False self.current_player.turn_started = datetime.now() + self.choosing_color = False def play_card(self, card): - """ Play a card and trigger its effects """ + """ + Plays a card and triggers its effects. + Should be called only from Player.play or on game start to play the + first card + """ self.deck.dismiss(self.last_card) self.last_card = card @@ -100,7 +106,6 @@ class Game(object): self.choosing_color = True def choose_color(self, color): - """ Carries out the color choosing and turns the game """ + """Carries out the color choosing and turns the game""" self.last_card.color = color self.turn() - self.choosing_color = False diff --git a/game_manager.py b/game_manager.py index 4e3ca4b..1f87f28 100644 --- a/game_manager.py +++ b/game_manager.py @@ -21,6 +21,8 @@ import logging from game import Game from player import Player +from errors import (AlreadyJoinedError, LobbyClosedError, NoGameInChatError, + NotEnoughPlayersError) class GameManager(object): @@ -38,7 +40,7 @@ class GameManager(object): """ chat_id = chat.id - self.logger.info("Creating new game with id " + str(chat_id)) + self.logger.debug("Creating new game in chat " + str(chat_id)) game = Game(chat) if chat_id not in self.chatid_games: @@ -47,13 +49,17 @@ class GameManager(object): self.chatid_games[chat_id].append(game) return game - def join_game(self, chat_id, user): + def join_game(self, user, chat): """ Create a player from the Telegram user and add it to the game """ - self.logger.info("Joining game with id " + str(chat_id)) + self.logger.info("Joining game with id " + str(chat.id)) + try: - game = self.chatid_games[chat_id][-1] + game = self.chatid_games[chat.id][-1] except (KeyError, IndexError): - return None + raise NoGameInChatError() + + if not game.open: + raise LobbyClosedError() if user.id not in self.userid_players: self.userid_players[user.id] = list() @@ -61,76 +67,81 @@ class GameManager(object): players = self.userid_players[user.id] # Don not re-add a player and remove the player from previous games in - # this chat + # this chat, if he is in one of them for player in players: if player in game.players: - return False + raise AlreadyJoinedError() else: - self.leave_game(user, chat_id) + try: + self.leave_game(user, chat) + except NoGameInChatError: + pass player = Player(game, user) players.append(player) self.userid_current[user.id] = player - return True - def leave_game(self, user, chat_id): + def leave_game(self, user, chat): """ Remove a player from its current game """ - try: - players = self.userid_players[user.id] - games = self.chatid_games[chat_id] - for player in players: - for game in games: - if player in game.players: - if player is game.current_player: - game.turn() + player = self.player_for_user_in_chat(user, chat) + players = self.userid_players.get(user.id, list()) - player.leave() - players.remove(player) + if not player: + raise NoGameInChatError - # If this is the selected game, switch to another - if self.userid_current[user.id] is player: - if len(players): - self.userid_current[user.id] = players[0] - else: - del self.userid_current[user.id] - return True + game = player.game + + if len(game.players) < 3: + raise NotEnoughPlayersError() + + if player is game.current_player: + game.turn() + + player.leave() + players.remove(player) + + # If this is the selected game, switch to another + if self.userid_current.get(user.id, None) is player: + if players: + self.userid_current[user.id] = players[0] else: - return False + del self.userid_current[user.id] + del self.userid_players[user.id] - except KeyError: - return False - - def end_game(self, chat_id, user): + def end_game(self, chat, user): """ End a game """ - self.logger.info("Game in chat " + str(chat_id) + " ended") - players = self.userid_players[user.id] - games = self.chatid_games[chat_id] - the_game = None + self.logger.info("Game in chat " + str(chat.id) + " ended") # Find the correct game instance to end - for player in players: - for game in games: - if player in game.players: - the_game = game - break - if the_game: - break - else: - return + player = self.player_for_user_in_chat(user, chat) - for player in the_game.players: + if not player: + raise NoGameInChatError + + game = player.game + + # Clear game + for player_in_game in game.players: this_users_players = self.userid_players[player.user.id] - this_users_players.remove(player) - if len(this_users_players) is 0: + this_users_players.remove(player_in_game) + + if this_users_players: + self.userid_current[player.user.id] = this_users_players[0] + else: del self.userid_players[player.user.id] del self.userid_current[player.user.id] - else: - self.userid_current[player.user.id] = this_users_players[0] - self.chatid_games[chat_id].remove(the_game) - return + self.chatid_games[chat.id].remove(game) + + def player_for_user_in_chat(self, user, chat): + players = self.userid_players.get(user.id, list()) + for player in players: + if player.game.chat.id == chat.id: + return player + else: + return None diff --git a/player.py b/player.py index a8531ff..db24a09 100644 --- a/player.py +++ b/player.py @@ -58,7 +58,7 @@ class Player(object): self.waiting_time = 90 def leave(self): - """ Leave the current game """ + """Removes player from the game and closes the gap in the list""" if self.next is self: return @@ -100,8 +100,23 @@ class Player(object): else: self._next = player + def draw(self): + """Draws 1+ cards from the deck, depending on the draw counter""" + _amount = self.game.draw_counter or 1 + + for i in range(_amount): + self.cards.append(self.game.deck.draw()) + + self.game.draw_counter = 0 + self.drew = True + + def play(self, card): + """Plays a card and removes it from hand""" + self.cards.remove(card) + self.game.play_card(card) + def playable_cards(self): - """ Returns a list of the cards this player can play right now """ + """Returns a list of the cards this player can play right now""" playable = list() last = self.game.last_card @@ -115,7 +130,7 @@ class Player(object): # You may only play a +4 if you have no cards of the correct color self.bluffing = False for card in cards: - if self.card_playable(card, playable): + if self._card_playable(card): self.logger.debug("Matching!") playable.append(card) @@ -127,8 +142,8 @@ class Player(object): return playable - def card_playable(self, card, playable): - """ Check a single card if it can be played """ + def _card_playable(self, card): + """Check a single card if it can be played""" is_playable = True last = self.game.last_card @@ -149,9 +164,8 @@ class Player(object): (card.special == c.CHOOSE or card.special == c.DRAW_FOUR): self.logger.debug("Can't play colorchooser on another one") is_playable = False - elif not last.color or card in playable: - self.logger.debug("Last card has no color or the card was " - "already added to the list") + elif not last.color: + self.logger.debug("Last card has no color") is_playable = False return is_playable diff --git a/results.py b/results.py index 6da172d..674e0eb 100644 --- a/results.py +++ b/results.py @@ -17,6 +17,8 @@ # along with this program. If not, see . +"""Defines helper functions to build the inline result list""" + from uuid import uuid4 from telegram import InlineQueryResultArticle, InputTextMessageContent, \ @@ -27,6 +29,7 @@ from utils import * def add_choose_color(results): + """Add choose color options""" for color in c.COLORS: results.append( InlineQueryResultArticle( @@ -40,6 +43,7 @@ def add_choose_color(results): def add_other_cards(playable, player, results, game): + """Add hand cards when choosing colors""" if not playable: playable = list() @@ -61,13 +65,15 @@ def add_other_cards(playable, player, results, game): def player_list(game): + """Generate list of player strings""" players = list() for player in game.players: - add_player(player, players) + player.user.first_name + " (%d cards)" % len(player.cards) return players def add_no_game(results): + """Add text result if user is not playing""" results.append( InlineQueryResultArticle( "nogame", @@ -81,6 +87,7 @@ def add_no_game(results): def add_not_started(results): + """Add text result if the game has not yet started""" results.append( InlineQueryResultArticle( "nogame", @@ -92,6 +99,7 @@ def add_not_started(results): def add_draw(player, results): + """Add option to draw""" results.append( Sticker( "draw", sticker_file_id=c.STICKERS['option_draw'], @@ -103,6 +111,7 @@ def add_draw(player, results): def add_gameinfo(game, results): + """Add option to show game info""" players = player_list(game) results.append( @@ -119,6 +128,7 @@ def add_gameinfo(game, results): def add_pass(results): + """Add option to pass""" results.append( Sticker( "pass", sticker_file_id=c.STICKERS['option_pass'], @@ -128,6 +138,7 @@ def add_pass(results): def add_call_bluff(results): + """Add option to call a bluff""" results.append( Sticker( "call_bluff", @@ -138,7 +149,8 @@ def add_call_bluff(results): ) -def add_play_card(game, card, results, can_play): +def add_card(game, card, results, can_play): + """Add an option that represents a card""" players = player_list(game) if can_play: @@ -156,8 +168,3 @@ def add_play_card(game, card, results, can_play): "Players: " + " -> ".join(players))) ) - -def add_player(itplayer, players): - players.append(itplayer.user.first_name + " (%d cards)" - % len(itplayer.cards)) - diff --git a/test/test.py b/test/test.py deleted file mode 100644 index 3c18722..0000000 --- a/test/test.py +++ /dev/null @@ -1,41 +0,0 @@ -import unittest -from game import Game -from player import Player - - -class Test(unittest.TestCase): - - game = None - - def setUp(self): - self.game = Game() - - def test_insert(self): - p0 = Player(self.game, "Player 0") - p1 = Player(self.game, "Player 1") - p2 = Player(self.game, "Player 2") - - self.assertEqual(p0, p2.next) - self.assertEqual(p1, p0.next) - self.assertEqual(p2, p1.next) - - self.assertEqual(p0.prev, p2) - self.assertEqual(p1.prev, p0) - self.assertEqual(p2.prev, p1) - - def test_reverse(self): - p0 = Player(self.game, "Player 0") - p1 = Player(self.game, "Player 1") - p2 = Player(self.game, "Player 2") - self.game.reverse() - p3 = Player(self.game, "Player 3") - - self.assertEqual(p0, p3.next) - self.assertEqual(p1, p2.next) - self.assertEqual(p2, p0.next) - self.assertEqual(p3, p1.next) - - self.assertEqual(p0, p2.prev) - self.assertEqual(p1, p3.prev) - self.assertEqual(p2, p1.prev) - self.assertEqual(p3, p0.prev) diff --git a/test/test_game_manager.py b/test/test_game_manager.py new file mode 100644 index 0000000..e06882e --- /dev/null +++ b/test/test_game_manager.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +import unittest + +from telegram import User, Chat + +from game_manager import GameManager +from errors import AlreadyJoinedError, LobbyClosedError, NoGameInChatError, \ + NotEnoughPlayersError + + +class Test(unittest.TestCase): + + game = None + + def setUp(self): + self.gm = GameManager() + + self.chat0 = Chat(0, 'group') + self.chat1 = Chat(1, 'group') + self.chat2 = Chat(2, 'group') + + self.user0 = User(0, 'user0') + self.user1 = User(1, 'user1') + self.user2 = User(2, 'user2') + + def test_new_game(self): + g0 = self.gm.new_game(self.chat0) + g1 = self.gm.new_game(self.chat1) + + self.assertListEqual(self.gm.chatid_games[0], [g0]) + self.assertListEqual(self.gm.chatid_games[1], [g1]) + + def test_join_game(self): + + self.assertRaises(NoGameInChatError, + self.gm.join_game, + *(self.user0, self.chat0)) + + g0 = self.gm.new_game(self.chat0) + + self.gm.join_game(self.user0, self.chat0) + self.assertEqual(len(g0.players), 1) + + self.gm.join_game(self.user1, self.chat0) + self.assertEqual(len(g0.players), 2) + + g0.open = False + self.assertRaises(LobbyClosedError, + self.gm.join_game, + *(self.user2, self.chat0)) + + g0.open = True + self.assertRaises(AlreadyJoinedError, + self.gm.join_game, + *(self.user1, self.chat0)) diff --git a/test/test_player.py b/test/test_player.py new file mode 100644 index 0000000..e65a13f --- /dev/null +++ b/test/test_player.py @@ -0,0 +1,158 @@ +#!/usr/bin/env python3 +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +import unittest + +from game import Game +from player import Player +import card as c + + +class Test(unittest.TestCase): + + game = None + + def setUp(self): + self.game = Game(None) + + def test_insert(self): + p0 = Player(self.game, "Player 0") + p1 = Player(self.game, "Player 1") + p2 = Player(self.game, "Player 2") + + self.assertEqual(p0, p2.next) + self.assertEqual(p1, p0.next) + self.assertEqual(p2, p1.next) + + self.assertEqual(p0.prev, p2) + self.assertEqual(p1.prev, p0) + self.assertEqual(p2.prev, p1) + + def test_reverse(self): + p0 = Player(self.game, "Player 0") + p1 = Player(self.game, "Player 1") + p2 = Player(self.game, "Player 2") + self.game.reverse() + p3 = Player(self.game, "Player 3") + + self.assertEqual(p0, p3.next) + self.assertEqual(p1, p2.next) + self.assertEqual(p2, p0.next) + self.assertEqual(p3, p1.next) + + self.assertEqual(p0, p2.prev) + self.assertEqual(p1, p3.prev) + self.assertEqual(p2, p1.prev) + self.assertEqual(p3, p0.prev) + + def test_leave(self): + p0 = Player(self.game, "Player 0") + p1 = Player(self.game, "Player 1") + p2 = Player(self.game, "Player 2") + + p1.leave() + + self.assertEqual(p0, p2.next) + self.assertEqual(p2, p0.next) + + def test_draw(self): + p = Player(self.game, "Player 0") + + deck_before = len(self.game.deck.cards) + top_card = self.game.deck.cards[-1] + + p.draw() + + self.assertEqual(top_card, p.cards[-1]) + self.assertEqual(deck_before, len(self.game.deck.cards) + 1) + + def test_draw_two(self): + p = Player(self.game, "Player 0") + + deck_before = len(self.game.deck.cards) + self.game.draw_counter = 2 + + p.draw() + + self.assertEqual(deck_before, len(self.game.deck.cards) + 2) + + def test_playable_cards_simple(self): + p = Player(self.game, "Player 0") + + self.game.last_card = c.Card(c.RED, '5') + + p.cards = [c.Card(c.RED, '0'), c.Card(c.RED, '5'), c.Card(c.BLUE, '0'), + c.Card(c.GREEN, '5'), c.Card(c.GREEN, '8')] + + expected = [c.Card(c.RED, '0'), c.Card(c.RED, '5'), + c.Card(c.GREEN, '5')] + + self.assertListEqual(p.playable_cards(), expected) + + def test_playable_cards_on_draw_two(self): + p = Player(self.game, "Player 0") + + self.game.last_card = c.Card(c.RED, c.DRAW_TWO) + self.game.draw_counter = 2 + + p.cards = [c.Card(c.RED, c.DRAW_TWO), c.Card(c.RED, '5'), + c.Card(c.BLUE, '0'), c.Card(c.GREEN, '5'), + c.Card(c.GREEN, c.DRAW_TWO)] + + expected = [c.Card(c.RED, c.DRAW_TWO), c.Card(c.GREEN, c.DRAW_TWO)] + + self.assertListEqual(p.playable_cards(), expected) + + def test_playable_cards_on_draw_four(self): + p = Player(self.game, "Player 0") + + self.game.last_card = c.Card(c.RED, None, c.DRAW_FOUR) + self.game.draw_counter = 4 + + p.cards = [c.Card(c.RED, c.DRAW_TWO), c.Card(c.RED, '5'), + c.Card(c.BLUE, '0'), c.Card(c.GREEN, '5'), + c.Card(c.GREEN, c.DRAW_TWO), + c.Card(None, None, c.DRAW_FOUR), + c.Card(None, None, c.CHOOSE)] + + expected = list() + + self.assertListEqual(p.playable_cards(), expected) + + def test_bluffing(self): + p = Player(self.game, "Player 0") + + self.game.last_card = c.Card(c.RED, '1') + + p.cards = [c.Card(c.RED, c.DRAW_TWO), c.Card(c.RED, '5'), + c.Card(c.BLUE, '0'), c.Card(c.GREEN, '5'), + c.Card(c.RED, '5'), c.Card(c.GREEN, c.DRAW_TWO), + c.Card(None, None, c.DRAW_FOUR), + c.Card(None, None, c.CHOOSE)] + + p.playable_cards() + self.assertTrue(p.bluffing) + + p.cards = [c.Card(c.BLUE, '1'), c.Card(c.GREEN, '1'), + c.Card(c.GREEN, c.DRAW_TWO), + c.Card(None, None, c.DRAW_FOUR), + c.Card(None, None, c.CHOOSE)] + + p.playable_cards() + self.assertFalse(p.bluffing) diff --git a/user_setting.py b/user_setting.py new file mode 100644 index 0000000..54e2054 --- /dev/null +++ b/user_setting.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +from database import db, Optional, Required, PrimaryKey + + +class UserSetting(db.Entity): + + id = PrimaryKey(int, auto=False) # Telegram User ID + lang = Optional(str, default='en') # The language setting for this user + stats = Optional(bool, default=False) # Opt-in to keep game statistics + first_places = Optional(int, default=0) # Nr. of games won in first place + games_played = Optional(int, default=0) # Nr. of games completed + cards_played = Optional(int, default=0) # Nr. of cards played From c9f7c09a462e171a814a635f33bbc7f904e3214e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Thu, 19 May 2016 20:57:32 +0200 Subject: [PATCH 02/20] update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7958e48..c0dcf2f 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Telegram Bot that allows you to play the popular card game UNO via inline querie To run the bot yourself, you will need: - Python (tested with 3.4 and 3.5) -- The [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot) module version 4.0.3 +- The [python-telegram-bot](https://github.com/python-telegram-bot/python-telegram-bot) module version 4.1.1 Get a bot token from [@BotFather](http://telegram.me/BotFather), place it in `credentials.py` and run the bot with `python3 bot.py` From 424219d825458c2d5fbb32240ef3850910b4265f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Thu, 19 May 2016 21:28:04 +0200 Subject: [PATCH 03/20] fix end_game --- game_manager.py | 8 +++++--- test/test_game_manager.py | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/game_manager.py b/game_manager.py index 1f87f28..c26e150 100644 --- a/game_manager.py +++ b/game_manager.py @@ -127,16 +127,18 @@ class GameManager(object): # Clear game for player_in_game in game.players: - this_users_players = self.userid_players[player.user.id] + this_users_players = self.userid_players[player_in_game.user.id] this_users_players.remove(player_in_game) if this_users_players: self.userid_current[player.user.id] = this_users_players[0] else: - del self.userid_players[player.user.id] - del self.userid_current[player.user.id] + del self.userid_players[player_in_game.user.id] + del self.userid_current[player_in_game.user.id] self.chatid_games[chat.id].remove(game) + if not self.chatid_games[chat.id]: + del self.chatid_games[chat.id] def player_for_user_in_chat(self, user, chat): players = self.userid_players.get(user.id, list()) diff --git a/test/test_game_manager.py b/test/test_game_manager.py index e06882e..ab9c913 100644 --- a/test/test_game_manager.py +++ b/test/test_game_manager.py @@ -71,3 +71,37 @@ class Test(unittest.TestCase): self.assertRaises(AlreadyJoinedError, self.gm.join_game, *(self.user1, self.chat0)) + + def test_leave_game(self): + g0 = self.gm.new_game(self.chat0) + + self.gm.join_game(self.user0, self.chat0) + self.gm.join_game(self.user1, self.chat0) + + self.assertRaises(NotEnoughPlayersError, + self.gm.leave_game, + *(self.user1, self.chat0)) + + self.gm.join_game(self.user2, self.chat0) + self.gm.leave_game(self.user0, self.chat0) + + self.assertRaises(NoGameInChatError, + self.gm.leave_game, + *(self.user0, self.chat0)) + + def test_end_game(self): + g0 = self.gm.new_game(self.chat0) + + self.gm.join_game(self.user0, self.chat0) + self.gm.join_game(self.user1, self.chat0) + + self.assertEqual(len(self.gm.userid_players[0]), 1) + + g1 = self.gm.new_game(self.chat0) + self.gm.join_game(self.user2, self.chat0) + + self.gm.end_game(self.chat0, self.user0) + self.assertEqual(len(self.gm.chatid_games[0]), 1) + + self.gm.end_game(self.chat0, self.user2) + self.assertFalse(0 in self.gm.chatid_games) From 9936d973738b259f9455670e02403915d3ce365f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Thu, 19 May 2016 21:29:07 +0200 Subject: [PATCH 04/20] update test_end_game --- test/test_game_manager.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test_game_manager.py b/test/test_game_manager.py index ab9c913..7cdb0f7 100644 --- a/test/test_game_manager.py +++ b/test/test_game_manager.py @@ -105,3 +105,6 @@ class Test(unittest.TestCase): self.gm.end_game(self.chat0, self.user2) self.assertFalse(0 in self.gm.chatid_games) + self.assertFalse(0 in self.gm.userid_players) + self.assertFalse(1 in self.gm.userid_players) + self.assertFalse(2 in self.gm.userid_players) From 204b0578104a139b230b59353540dccb347b7255 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Thu, 19 May 2016 23:15:46 +0200 Subject: [PATCH 05/20] add encoding --- bot.py | 1 + card.py | 1 + chat_setting.py | 1 + database.py | 1 + deck.py | 1 + errors.py | 1 + game.py | 1 + game_manager.py | 1 + player.py | 1 + results.py | 1 + start_bot.py | 1 + test/test_game_manager.py | 1 + test/test_player.py | 1 + user_setting.py | 1 + utils.py | 1 + 15 files changed, 15 insertions(+) diff --git a/bot.py b/bot.py index 2f87297..ea4f180 100644 --- a/bot.py +++ b/bot.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/card.py b/card.py index a291e45..6a0496a 100644 --- a/card.py +++ b/card.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/chat_setting.py b/chat_setting.py index baccfee..c9cfc0c 100644 --- a/chat_setting.py +++ b/chat_setting.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/database.py b/database.py index 30b2e54..38b5cca 100644 --- a/database.py +++ b/database.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/deck.py b/deck.py index 0fda198..436a323 100644 --- a/deck.py +++ b/deck.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/errors.py b/errors.py index ac0575a..632e25a 100644 --- a/errors.py +++ b/errors.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/game.py b/game.py index 169e4fc..63c5edb 100644 --- a/game.py +++ b/game.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/game_manager.py b/game_manager.py index c26e150..a505ade 100644 --- a/game_manager.py +++ b/game_manager.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/player.py b/player.py index db24a09..a9b37bf 100644 --- a/player.py +++ b/player.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/results.py b/results.py index 674e0eb..5a0ff0a 100644 --- a/results.py +++ b/results.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/start_bot.py b/start_bot.py index e38fca2..a9730e7 100644 --- a/start_bot.py +++ b/start_bot.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/test/test_game_manager.py b/test/test_game_manager.py index 7cdb0f7..944833b 100644 --- a/test/test_game_manager.py +++ b/test/test_game_manager.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/test/test_player.py b/test/test_player.py index e65a13f..2e516f0 100644 --- a/test/test_player.py +++ b/test/test_player.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/user_setting.py b/user_setting.py index 54e2054..f44bd82 100644 --- a/user_setting.py +++ b/user_setting.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke diff --git a/utils.py b/utils.py index 7d327a8..6035b58 100644 --- a/utils.py +++ b/utils.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +# -*- coding: utf-8 -*- # # Telegram bot to play UNO in group chats # Copyright (c) 2016 Jannes Höke From 8af8852d051805ac134f9b4b1ee76878b8e361bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Thu, 19 May 2016 23:18:05 +0200 Subject: [PATCH 06/20] initial translation support --- .gitignore | 2 +- i18n/messages.in | 2 + i18n/messages.pot | 313 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 316 insertions(+), 1 deletion(-) create mode 100644 i18n/messages.in create mode 100644 i18n/messages.pot diff --git a/.gitignore b/.gitignore index 50992e9..2215d04 100644 --- a/.gitignore +++ b/.gitignore @@ -47,7 +47,7 @@ coverage.xml # Translations *.mo -*.pot +# *.pot # Django stuff: *.log diff --git a/i18n/messages.in b/i18n/messages.in new file mode 100644 index 0000000..9021290 --- /dev/null +++ b/i18n/messages.in @@ -0,0 +1,2 @@ +bot.py +results.py diff --git a/i18n/messages.pot b/i18n/messages.pot new file mode 100644 index 0000000..9d8deab --- /dev/null +++ b/i18n/messages.pot @@ -0,0 +1,313 @@ +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +#: bot.py:224 +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: mau_mau_bot 0.1\n" +"Report-Msgid-Bugs-To: uno@jhoeke.de\n" +"POT-Creation-Date: 2016-05-19 22:38+0200\n" +"PO-Revision-Date: 2016-05-19 22:38+0200\n" +"Last-Translator: Jannes Höke \n" +"Language-Team: en \n" +"Language: en\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=CHARSET\n" +"Content-Transfer-Encoding: 8bit\n" + + +#: bot.py:60 +msgid "Follow these steps:\n" +"\n" +"1. Add this bot to a group\n" +"2. In the group, start a new game with /new or join an already running game " +"with /join\n" +"3. After at least two players have joined, start the game with /start\n" +"4. Type @mau_mau_bot into your chat box and hit space, " +"or click the via @mau_mau_bot text next to messages. You will " +"see your cards (some greyed out), any extra options like drawing, and a ?" +" to see the current game state. The greyed out cards are those " +"you can not play at the moment. Tap an option to execute the selected " +"action.\n" +"Players can join the game at any time. To leave a game, use /leave. If a " +"player takes more than 90 seconds to play, you can use /skip to skip that " +"player.\n" +"\n" +"Other commands (only game creator):\n" +"/close - Close lobby\n" +"/open - Open lobby\n" +"\n" +"Experimental: Play in multiple groups at the same time. Press the " +"Current game: ... button and select the group you want to play " +"a card in.\n" +"If you enjoy this bot, rate me, join the update channel and buy an UNO card game.\n" +msgstr "" + +#: bot.py:88 +msgid "This bot is Free Software and licensed under the AGPL. The code is available " +"here: \n" +"https://github.com/jh0ker/mau_mau_bot" +msgstr "" + +#: bot.py:133 +msgid "Created a new game! Join the game with /join and start the game with /start" +msgstr "" + +#: bot.py:152 +msgid "The lobby is closed" +msgstr "" + +#: bot.py:156 +msgid "No game is running at the moment. Create a new game with /new" +msgstr "" + +#: bot.py:162 +msgid "You already joined the game. Start the game with /start" +msgstr "" + +#: bot.py:167 +msgid "Joined the game" +msgstr "" + +#: bot.py:179 bot.py:191 +msgid "You are not playing in a game in this group." +msgstr "" + +#: bot.py:197 bot.py:258 bot.py:595 +msgid "Game ended!" +msgstr "" + +#: bot.py:201 +msgid "Okay. Next Player: " +msgstr "" + +#: bot.py:219 +msgid "Game not found." +msgstr "" + +#: bot.py:223 +msgid "Back to last group" +msgstr "" + +#: bot.py:227 +msgid "Please switch to the group you selected!" +msgstr "" + +#: bot.py:233 +#, python-format +msgid "Selected group: ${group}\n" +"Make sure that you switch to the correct group!" +msgstr "" + +#: bot.py:260 +#, python-format +msgid "Removing ${name} from the game" +msgstr "" + + +#: bot.py:273 +msgid "There is no game running in this chat. Create a new one with /new" +msgstr "" + +#: bot.py:278 +msgid "The game has already started" +msgstr "" + +#: bot.py:281 +msgid "At least two players must /join the game before you can start it" +msgstr "" + + +#: bot.py:297 +#, python-format +msgid "First player: ${name}\n" +"Use /close to stop people from joining the game." +msgstr "" + +#: bot.py:321 +msgid "Please select the group you want to play in." +msgstr "" + + +#: bot.py:335 bot.py:361 +msgid "There is no running game in this chat." +msgstr "" + +#: bot.py:342 +msgid "Closed the lobby. No more players can join this game." +msgstr "" + +#: bot.py:348 bot.py:373 +#, python-format +msgid "Only the game creator (${name}) can do that." +msgstr "" + +#: bot.py:368 +msgid "Opened the lobby. New players may /join the game." +msgstr "" + +#: bot.py:386 +msgid "You are not playing in a game in this chat." +msgstr "" + +#: bot.py:400 +#, python-format +msgid "Please wait ${time} seconds" +msgstr "" + +#: bot.py:413 +#, python-format +msgid "Waiting time to skip this player has been reduced to ${time} seconds.\n" +"Next player: ${name}" +msgstr "" + +#: bot.py:424 +#, python-format +msgid "${name1} was skipped four times in a row and has been removed from the game.\n" +"Next player: ${name2}" +msgstr "" + +#: bot.py:432 +#, python-format +msgid "${name} was skipped four times in a row and has been removed from the game.\n" +"The game ended." +msgstr "" + +#: bot.py:455 +msgid "All news here: https://telegram.me/unobotupdates" +msgstr "" + +#: bot.py:513 +#, python-format +msgid "Current game: %s" +msgstr "" + +#: bot.py:533 +msgid "Selected result: " +msgstr "" + +#: bot.py:545 +#, python-format +msgid "Cheat attempt by %s" +msgstr "" + +#: bot.py:562 +msgid "Next player: " +msgstr "" + +#: bot.py:572 +#, python-format +msgid "Waiting time for ${name} has been reset to 90 seconds" +msgstr "" + +#: bot.py:577 +msgid "Plays the selected card and sends an update to the group if needed" +msgstr "" + +#: bot.py:585 +msgid "Please choose a color" +msgstr "" + +#: bot.py:588 +msgid "UNO!" +msgstr "" + +#: bot.py:591 +#, python-format +msgid "${name} won!" +msgstr "" + +#: bot.py:601 +msgid "Played cards" +msgstr "" + +#: bot.py:613 bot.py:635 bot.py:647 +msgid "There are no more cards in the deck." +msgstr "" + +#: bot.py:627 +#, python-format +msgid "Bluff called! Giving ${number} cards to ${name}" +msgstr "" + +#: bot.py:639 +#, python-format +msgid "${name1} didn't bluff! Giving ${number} cards to ${name2}" +msgstr "" + +#: results.py:38 +msgid "Choose Color" +msgstr "" + +#: results.py:56 +msgid "Cards (tap for game state):" +msgstr "" + +#: results.py:60 results.py:123 results.py:165 +msgid "Current player: " +msgstr "" + +#: results.py:61 results.py:124 results.py:167 +msgid "\n" +"Last card: " +msgstr "" + +#: results.py:62 results.py:125 results.py:168 +msgid "\n" +"Players: " +msgstr "" + + +#: results.py:72 +#, python-format +msgid " (${number} cards)" +msgstr "" + +#: results.py:81 +msgid "You are not playing" +msgstr "" + +#: results.py:83 +msgid "Not playing right now. Use /new to start a game or /join to join the current " +"game in this group" +msgstr "" + +#: results.py:95 +msgid "The game wasn't started yet" +msgstr "" + +#: results.py:97 +msgid "Start the game with /start" +msgstr "" + +#: results.py:108 +#, python-format +msgid "Drawing one card" +msgid_plural "Drawing ${number} cards" +msgstr "" + +#: results.py:136 +msgid "Pass" +msgstr "" + +#: results.py:148 +msgid "I'm calling your bluff!" +msgstr "" + From 2316ab8a1c2288b6fd84e8619899470cf740c4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Fri, 20 May 2016 17:53:01 +0200 Subject: [PATCH 07/20] pot formatting --- i18n/messages.pot | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/i18n/messages.pot b/i18n/messages.pot index 9d8deab..920a156 100644 --- a/i18n/messages.pot +++ b/i18n/messages.pot @@ -27,7 +27,7 @@ msgstr "" "Language-Team: en \n" "Language: en\n" "MIME-Version: 1.0\n" -"Content-Type: text/plain; charset=CHARSET\n" +"Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" @@ -301,7 +301,8 @@ msgstr "" #, python-format msgid "Drawing one card" msgid_plural "Drawing ${number} cards" -msgstr "" +msgstr[0] "" +msgstr[1] "" #: results.py:136 msgid "Pass" From 0dcd1f6cdcb855e0b4c9d40e75259f78d32ede08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Fri, 20 May 2016 17:55:08 +0200 Subject: [PATCH 08/20] use regular formatting --- i18n/messages.pot | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/i18n/messages.pot b/i18n/messages.pot index 920a156..deb829e 100644 --- a/i18n/messages.pot +++ b/i18n/messages.pot @@ -112,13 +112,13 @@ msgstr "" #: bot.py:233 #, python-format -msgid "Selected group: ${group}\n" +msgid "Selected group: {group}\n" "Make sure that you switch to the correct group!" msgstr "" #: bot.py:260 #, python-format -msgid "Removing ${name} from the game" +msgid "Removing {name} from the game" msgstr "" @@ -137,7 +137,7 @@ msgstr "" #: bot.py:297 #, python-format -msgid "First player: ${name}\n" +msgid "First player: {name}\n" "Use /close to stop people from joining the game." msgstr "" @@ -156,7 +156,7 @@ msgstr "" #: bot.py:348 bot.py:373 #, python-format -msgid "Only the game creator (${name}) can do that." +msgid "Only the game creator ({name}) can do that." msgstr "" #: bot.py:368 @@ -169,24 +169,24 @@ msgstr "" #: bot.py:400 #, python-format -msgid "Please wait ${time} seconds" +msgid "Please wait {time} seconds" msgstr "" #: bot.py:413 #, python-format -msgid "Waiting time to skip this player has been reduced to ${time} seconds.\n" -"Next player: ${name}" +msgid "Waiting time to skip this player has been reduced to {time} seconds.\n" +"Next player: {name}" msgstr "" #: bot.py:424 #, python-format -msgid "${name1} was skipped four times in a row and has been removed from the game.\n" -"Next player: ${name2}" +msgid "{name1} was skipped four times in a row and has been removed from the game.\n" +"Next player: {name2}" msgstr "" #: bot.py:432 #, python-format -msgid "${name} was skipped four times in a row and has been removed from the game.\n" +msgid "{name} was skipped four times in a row and has been removed from the game.\n" "The game ended." msgstr "" @@ -214,7 +214,7 @@ msgstr "" #: bot.py:572 #, python-format -msgid "Waiting time for ${name} has been reset to 90 seconds" +msgid "Waiting time for {name} has been reset to 90 seconds" msgstr "" #: bot.py:577 @@ -231,7 +231,7 @@ msgstr "" #: bot.py:591 #, python-format -msgid "${name} won!" +msgid "{name} won!" msgstr "" #: bot.py:601 @@ -244,12 +244,12 @@ msgstr "" #: bot.py:627 #, python-format -msgid "Bluff called! Giving ${number} cards to ${name}" +msgid "Bluff called! Giving {number} cards to {name}" msgstr "" #: bot.py:639 #, python-format -msgid "${name1} didn't bluff! Giving ${number} cards to ${name2}" +msgid "{name1} didn't bluff! Giving {number} cards to {name2}" msgstr "" #: results.py:38 @@ -277,7 +277,7 @@ msgstr "" #: results.py:72 #, python-format -msgid " (${number} cards)" +msgid " ({number} cards)" msgstr "" #: results.py:81 @@ -300,7 +300,7 @@ msgstr "" #: results.py:108 #, python-format msgid "Drawing one card" -msgid_plural "Drawing ${number} cards" +msgid_plural "Drawing {number} cards" msgstr[0] "" msgstr[1] "" From aee310ec9ca8ea841fe1e6799ee58364d9fd3de1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Fri, 20 May 2016 18:34:27 +0200 Subject: [PATCH 09/20] handle empty decks on player join --- bot.py | 17 ++++++++++++++++- player.py | 13 ++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/bot.py b/bot.py index ea4f180..b1dd093 100644 --- a/bot.py +++ b/bot.py @@ -30,6 +30,9 @@ from telegram.ext import Updater, InlineQueryHandler, \ from telegram.ext.dispatcher import run_async from telegram.utils.botan import Botan +from flufl.i18n import registry +from flufl.i18n import PackageStrategy + from game_manager import GameManager from credentials import TOKEN, BOTAN_TOKEN from start_bot import start_bot @@ -41,6 +44,7 @@ import card as c from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError, NotEnoughPlayersError, DeckEmptyError) from database import db_session +import i18n TIMEOUT = 2.5 @@ -49,6 +53,10 @@ logging.basicConfig( level=logging.DEBUG) logger = logging.getLogger(__name__) +strategy = PackageStrategy('uno', i18n) +application = registry.register(strategy) +_ = application._ + gm = GameManager() u = Updater(token=TOKEN, workers=32) dp = u.dispatcher @@ -162,6 +170,13 @@ def join_game(bot, update): text="You already joined the game. Start the game " "with /start", reply_to_message_id=update.message.message_id) + + except DeckEmptyError: + send_async(bot, chat.id, + text="There are not enough cards left in the deck for new " + "players to join.", + reply_to_message_id=update.message.message_id) + else: send_async(bot, chat.id, text="Joined the game", @@ -439,7 +454,7 @@ def skip_player(bot, update): def help(bot, update): """Handler for the /help command""" - send_async(bot, update.message.chat_id, text=help_text, + send_async(bot, update.message.chat_id, text=_(help_text), parse_mode=ParseMode.HTML, disable_web_page_preview=True) diff --git a/player.py b/player.py index a9b37bf..904ce76 100644 --- a/player.py +++ b/player.py @@ -22,6 +22,7 @@ import logging from datetime import datetime import card as c +from errors import DeckEmptyError class Player(object): @@ -38,6 +39,15 @@ class Player(object): self.user = user self.logger = logging.getLogger(__name__) + try: + for i in range(7): + self.cards.append(self.game.deck.draw()) + except DeckEmptyError: + for card in self.cards: + self.game.deck.dismiss(card) + + raise + # Check if this player is the first player in this game. if game.current_player: self.next = game.current_player @@ -49,9 +59,6 @@ class Player(object): self._prev = self game.current_player = self - for i in range(7): - self.cards.append(self.game.deck.draw()) - self.bluffing = False self.drew = False self.anti_cheat = 0 From a39fa85b3bf104288211498f9d323e74a73bcc93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Fri, 20 May 2016 18:35:21 +0200 Subject: [PATCH 10/20] more translation supprt --- i18n/__init__.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 i18n/__init__.py diff --git a/i18n/__init__.py b/i18n/__init__.py new file mode 100644 index 0000000..e69de29 From d5b76c5c1263b2e65267a3600b210a03d7eb8b6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 21 May 2016 18:55:03 +0200 Subject: [PATCH 11/20] fix result list generation --- results.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/results.py b/results.py index 5a0ff0a..aae1aef 100644 --- a/results.py +++ b/results.py @@ -67,10 +67,9 @@ def add_other_cards(playable, player, results, game): def player_list(game): """Generate list of player strings""" - players = list() - for player in game.players: - player.user.first_name + " (%d cards)" % len(player.cards) - return players + return [player.user.first_name + " (%d cards)" % len(player.cards) + for player in game.players] + def add_no_game(results): From 73365f49fcf55a4c2676b60551bed38fcd7c8ee9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 21 May 2016 18:55:53 +0200 Subject: [PATCH 12/20] draw method reset draw counter on empty deck --- player.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/player.py b/player.py index 904ce76..45954f1 100644 --- a/player.py +++ b/player.py @@ -112,11 +112,16 @@ class Player(object): """Draws 1+ cards from the deck, depending on the draw counter""" _amount = self.game.draw_counter or 1 - for i in range(_amount): - self.cards.append(self.game.deck.draw()) + try: + for i in range(_amount): + self.cards.append(self.game.deck.draw()) - self.game.draw_counter = 0 - self.drew = True + except DeckEmptyError: + raise + + finally: + self.game.draw_counter = 0 + self.drew = True def play(self, card): """Plays a card and removes it from hand""" From a02813477a39808a8d35e1dc4289dc1ec070cdc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 21 May 2016 18:56:27 +0200 Subject: [PATCH 13/20] more stable first-card drawing --- game.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/game.py b/game.py index 63c5edb..cbfc575 100644 --- a/game.py +++ b/game.py @@ -37,12 +37,10 @@ class Game(object): def __init__(self, chat): self.chat = chat - self.deck = Deck() - self.last_card = self.deck.draw() + self.last_card = None - while self.last_card.special: - self.deck.dismiss(self.last_card) - self.deck.shuffle() + while not self.last_card or self.last_card.special: + self.deck = Deck() self.last_card = self.deck.draw() self.logger = logging.getLogger(__name__) From becc7e28dcbef4dee705c08018a6d2aa151a34c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 21 May 2016 18:56:58 +0200 Subject: [PATCH 14/20] fix broken method call --- bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.py b/bot.py index b1dd093..0de2661 100644 --- a/bot.py +++ b/bot.py @@ -564,7 +564,7 @@ def process_result(bot, update): do_call_bluff(bot, player) elif result_id == 'draw': reset_waiting_time(bot, player) - do_draw(player) + do_draw(bot, player) elif result_id == 'pass': game.turn() elif result_id in c.COLORS: From 5ece46527a30ae116a2e1cb9b16ff75793a583d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sat, 21 May 2016 21:41:38 +0200 Subject: [PATCH 15/20] locales are working, added de_DE locale --- bot.py | 211 +++++++------- database.py | 14 + i18n/messages.in | 2 - {i18n => locales}/__init__.py | 0 locales/de_DE/LC_MESSAGES/unobot.po | 352 ++++++++++++++++++++++++ i18n/messages.pot => locales/unobot.pot | 54 ++-- results.py | 69 +++-- utils.py | 8 + 8 files changed, 540 insertions(+), 170 deletions(-) delete mode 100644 i18n/messages.in rename {i18n => locales}/__init__.py (100%) create mode 100644 locales/de_DE/LC_MESSAGES/unobot.po rename i18n/messages.pot => locales/unobot.pot (89%) diff --git a/bot.py b/bot.py index 0de2661..9e513b2 100644 --- a/bot.py +++ b/bot.py @@ -30,9 +30,6 @@ from telegram.ext import Updater, InlineQueryHandler, \ from telegram.ext.dispatcher import run_async from telegram.utils.botan import Botan -from flufl.i18n import registry -from flufl.i18n import PackageStrategy - from game_manager import GameManager from credentials import TOKEN, BOTAN_TOKEN from start_bot import start_bot @@ -43,8 +40,9 @@ from utils import display_name import card as c from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError, NotEnoughPlayersError, DeckEmptyError) -from database import db_session -import i18n +from database import db_session, user_locale +from utils import _ + TIMEOUT = 2.5 @@ -53,10 +51,6 @@ logging.basicConfig( level=logging.DEBUG) logger = logging.getLogger(__name__) -strategy = PackageStrategy('uno', i18n) -application = registry.register(strategy) -_ = application._ - gm = GameManager() u = Updater(token=TOKEN, workers=32) dp = u.dispatcher @@ -91,7 +85,7 @@ help_text = ("Follow these steps:\n\n" "" "rate me, join the " "update channel" - " and buy an UNO card game.\n") + " and buy an UNO card game.") source_text = ("This bot is Free Software and licensed under the AGPL. " "The code is available here: \n" @@ -127,6 +121,7 @@ def error(bot, update, error): logger.exception(error) +@user_locale def new_game(bot, update): """Handler for the /new command""" chat_id = update.message.chat_id @@ -138,13 +133,14 @@ def new_game(bot, update): game = gm.new_game(update.message.chat) game.owner = update.message.from_user send_async(bot, chat_id, - text="Created a new game! Join the game with /join " - "and start the game with /start") + text=_("Created a new game! Join the game with /join " + "and start the game with /start")) if botan: botan.track(update.message, 'New games') +@user_locale def join_game(bot, update): """Handler for the /join command""" chat = update.message.chat @@ -157,32 +153,33 @@ def join_game(bot, update): gm.join_game(update.message.from_user, chat) except LobbyClosedError: - send_async(bot, chat.id, text="The lobby is closed") + send_async(bot, chat.id, text=_("The lobby is closed")) except NoGameInChatError: send_async(bot, chat.id, - text="No game is running at the moment. " - "Create a new game with /new", + text=_("No game is running at the moment. " + "Create a new game with /new"), reply_to_message_id=update.message.message_id) except AlreadyJoinedError: send_async(bot, chat.id, - text="You already joined the game. Start the game " - "with /start", + text=_("You already joined the game. Start the game " + "with /start"), reply_to_message_id=update.message.message_id) except DeckEmptyError: send_async(bot, chat.id, - text="There are not enough cards left in the deck for new " - "players to join.", + text=_("There are not enough cards left in the deck for new " + "players to join."), reply_to_message_id=update.message.message_id) else: send_async(bot, chat.id, - text="Joined the game", + text=_("Joined the game"), reply_to_message_id=update.message.message_id) +@user_locale def leave_game(bot, update): """Handler for the /leave command""" chat = update.message.chat @@ -191,8 +188,8 @@ def leave_game(bot, update): player = gm.player_for_user_in_chat(user, chat) if player is None: - send_async(bot, chat.id, text="You are not playing in a game in " - "this group.", + send_async(bot, chat.id, text=_("You are not playing in a game in " + "this group."), reply_to_message_id=update.message.message_id) return @@ -203,18 +200,18 @@ def leave_game(bot, update): gm.leave_game(user, chat) except NoGameInChatError: - send_async(bot, chat.id, text="You are not playing in a game in " - "this group.", + send_async(bot, chat.id, text=_("You are not playing in a game in " + "this group."), reply_to_message_id=update.message.message_id) except NotEnoughPlayersError: gm.end_game(chat, user) - send_async(bot, chat.id, text="Game ended!") + send_async(bot, chat.id, text=_("Game ended!")) else: send_async(bot, chat.id, - text="Okay. Next Player: " + - display_name(game.current_player.user), + text=_("Okay. Next Player: {name}").format( + name=display_name(game.current_player.user)), reply_to_message_id=update.message.message_id) @@ -231,29 +228,30 @@ def select_game(bot, update): break else: bot.sendMessage(update.callback_query.message.chat_id, - text="Game not found.", + text=_("Game not found."), timeout=TIMEOUT) return - back = [[InlineKeyboardButton(text='Back to last group', + back = [[InlineKeyboardButton(text=_("Back to last group"), switch_inline_query='')]] bot.answerCallbackQuery(update.callback_query.id, - text="Please switch to the group you selected!", + text=_("Please switch to the group you selected!"), show_alert=False, timeout=TIMEOUT) bot.editMessageText(chat_id=update.callback_query.message.chat_id, message_id=update.callback_query.message.message_id, - text="Selected group: %s\n" - "Make sure that you switch to the correct " - "group!" - % gm.userid_current[user_id].game.chat.title, + text=_("Selected group: {group}\n" + "Make sure that you switch to the correct " + "group!").format( + group=gm.userid_current[user_id].game.chat.title), reply_markup=InlineKeyboardMarkup(back), parse_mode=ParseMode.HTML, timeout=TIMEOUT) +@user_locale def status_update(bot, update): """Remove player from game if user leaves the group""" chat = update.message.chat @@ -270,12 +268,13 @@ def status_update(bot, update): pass except NotEnoughPlayersError: gm.end_game(chat, user) - send_async(bot, chat.id, text="Game ended!") + send_async(bot, chat.id, text=_("Game ended!")) else: - send_async(bot, chat.id, text="Removing %s from the game" - % display_name(user)) + send_async(bot, chat.id, text=_("Removing {name} from the game") + .format(name=display_name(user))) +@user_locale def start_game(bot, update, args): """Handler for the /start command""" @@ -285,16 +284,18 @@ def start_game(bot, update, args): try: game = gm.chatid_games[chat.id][-1] except (KeyError, IndexError): - send_async(bot, chat.id, text="There is no game running in this " - "chat. Create a new one with /new") + send_async(bot, chat.id, + text=_("There is no game running in this chat. Create " + "a new one with /new")) return if game.started: - send_async(bot, chat.id, text="The game has already started") + send_async(bot, chat.id, text=_("The game has already started")) elif len(game.players) < 2: - send_async(bot, chat.id, text="At least two players must /join " - "the game before you can start it") + send_async(bot, chat.id, + text=_("At least two players must /join the game " + "before you can start it")) else: game.play_card(game.last_card) @@ -309,10 +310,12 @@ def start_game(bot, update, args): timeout=TIMEOUT) bot.sendMessage(chat.id, - text="First player: %s\n" - "Use /close to stop people from joining " - "the game." - % display_name(game.current_player.user), + text=_("First player: {name}\n" + "Use /close to stop people from " + "joining the game.") + .format( + name=display_name(game.current_player.user) + ), timeout=TIMEOUT) send_first() @@ -333,13 +336,14 @@ def start_game(bot, update, args): ) send_async(bot, update.message.chat_id, - text='Please select the group you want to play in.', + text=_('Please select the group you want to play in.'), reply_markup=InlineKeyboardMarkup(groups)) else: help(bot, update) +@user_locale def close_game(bot, update): """Handler for the /close command""" chat = update.message.chat @@ -347,25 +351,27 @@ def close_game(bot, update): games = gm.chatid_games.get(chat.id) if not games: - send_async(bot, chat.id, text="There is no running game in this chat.") + send_async(bot, chat.id, + text=_("There is no running game in this chat.")) return game = games[-1] if game.owner.id == user.id: game.open = False - send_async(bot, chat.id, text="Closed the lobby. " - "No more players can join this game.") + send_async(bot, chat.id, text=_("Closed the lobby. " + "No more players can join this game.")) return else: send_async(bot, chat.id, - text="Only the game creator (%s) can do that" - % game.owner.first_name, + text=_("Only the game creator ({name}) can do that") + .format(name=game.owner.first_name), reply_to_message_id=update.message.message_id) return +@user_locale def open_game(bot, update): """Handler for the /open command""" chat = update.message.chat @@ -373,24 +379,26 @@ def open_game(bot, update): games = gm.chatid_games.get(chat.id) if not games: - send_async(bot, chat.id, text="There is no running game in this chat.") + send_async(bot, chat.id, + text=_("There is no running game in this chat.")) return game = games[-1] if game.owner.id == user.id: game.open = True - send_async(bot, chat.id, text="Opened the lobby. " - "New players may /join the game.") + send_async(bot, chat.id, text=_("Opened the lobby. " + "New players may /join the game.")) return else: send_async(bot, chat.id, - text="Only the game creator (%s) can do that." - % game.owner.first_name, + text=_("Only the game creator ({name}) can do that") + .format(name=game.owner.first_name), reply_to_message_id=update.message.message_id) return +@user_locale def skip_player(bot, update): """Handler for the /skip command""" chat = update.message.chat @@ -398,8 +406,8 @@ def skip_player(bot, update): player = gm.player_for_user_in_chat(user, chat) if not player: - send_async(bot, chat.id, text="You are not playing in a game in this " - "chat.") + send_async(bot, chat.id, + text=_("You are not playing in a game in this chat.")) return game = player.game @@ -412,8 +420,8 @@ def skip_player(bot, update): if delta < skipped_player.waiting_time: send_async(bot, chat.id, - text="Please wait %d seconds" - % (skipped_player.waiting_time - delta), + text=_("Please wait {time} seconds") + .format(time=(skipped_player.waiting_time - delta)), reply_to_message_id=update.message.message_id) elif skipped_player.waiting_time > 0: @@ -425,52 +433,56 @@ def skip_player(bot, update): pass send_async(bot, chat.id, - text="Waiting time to skip this player has " - "been reduced to %d seconds.\n" - "Next player: %s" - % (skipped_player.waiting_time, - display_name(next_player.user))) + text=_("Waiting time to skip this player has " + "been reduced to {time} seconds.\n" + "Next player: {name}") + .format(time=skipped_player.waiting_time, + name=display_name(next_player.user))) game.turn() else: try: gm.leave_game(skipped_player.user, chat) send_async(bot, chat.id, - text="%s was skipped four times in a row " - "and has been removed from the game.\n" - "Next player: %s" - % (display_name(skipped_player.user), - display_name(next_player.user))) + text=_("{name1} was skipped four times in a row " + "and has been removed from the game.\n" + "Next player: {name2}") + .format(name1=display_name(skipped_player.user), + name2=display_name(next_player.user))) except NotEnoughPlayersError: send_async(bot, chat.id, - text="%s was skipped four times in a row " - "and has been removed from the game.\n" - "The game ended." - % display_name(skipped_player.user)) + text=_("{name} was skipped four times in a row " + "and has been removed from the game.\n" + "The game ended.") + .format(name=display_name(skipped_player.user))) gm.end_game(chat.id, skipped_player.user) +@user_locale def help(bot, update): """Handler for the /help command""" send_async(bot, update.message.chat_id, text=_(help_text), parse_mode=ParseMode.HTML, disable_web_page_preview=True) +@user_locale def source(bot, update): """Handler for the /help command""" - send_async(bot, update.message.chat_id, text=source_text, + send_async(bot, update.message.chat_id, text=_(source_text), parse_mode=ParseMode.HTML, disable_web_page_preview=True) +@user_locale def news(bot, update): """Handler for the /news command""" send_async(bot, update.message.chat_id, - text="All news here: https://telegram.me/unobotupdates", + text=_("All news here: https://telegram.me/unobotupdates"), disable_web_page_preview=True) +@user_locale def reply_to_query(bot, update): """ Handler for inline queries. @@ -525,12 +537,13 @@ def reply_to_query(bot, update): result.id += ':%d' % player.anti_cheat if players and game and len(players) > 1: - switch = 'Current game: %s' % game.chat.title + switch = _('Current game: {game}').format(game=game.chat.title) answer_async(bot, update.inline_query.id, results, cache_time=0, switch_pm_text=switch, switch_pm_parameter='select') +@user_locale def process_result(bot, update): """ Handler for chosen inline results. @@ -557,7 +570,8 @@ def process_result(bot, update): return elif int(anti_cheat) != last_anti_cheat: send_async(bot, chat.id, - text="Cheat attempt by %s" % display_name(player.user)) + text=_("Cheat attempt by {name}") + .format(name=display_name(player.user))) return elif result_id == 'call_bluff': reset_waiting_time(bot, player) @@ -574,8 +588,9 @@ def process_result(bot, update): do_play_card(bot, player, result_id) if game in gm.chatid_games.get(chat.id, list()): - send_async(bot, chat.id, text="Next player: " + - display_name(game.current_player.user)) + send_async(bot, chat.id, + text=_("Next player: {name}") + .format(name=display_name(game.current_player.user))) def reset_waiting_time(bot, player): @@ -584,8 +599,9 @@ def reset_waiting_time(bot, player): if player.waiting_time < 90: player.waiting_time = 90 - send_async(bot, chat.id, text="Waiting time for %s has been reset to " - "90 seconds" % display_name(player.user)) + send_async(bot, chat.id, + text=_("Waiting time for {name} has been reset to 90 " + "seconds").format(name=display_name(player.user))) def do_play_card(bot, player, result_id): @@ -597,17 +613,18 @@ def do_play_card(bot, player, result_id): user = player.user if game.choosing_color: - send_async(bot, chat.id, text="Please choose a color") + send_async(bot, chat.id, text=_("Please choose a color")) if len(player.cards) == 1: - send_async(bot, chat.id, text="UNO!") + send_async(bot, chat.id, text=_("UNO!")) if len(player.cards) == 0: - send_async(bot, chat.id, text="%s won!" % user.first_name) + send_async(bot, chat.id, + text=_("{name} won!").format(name=user.first_name)) try: gm.leave_game(user, chat) except NotEnoughPlayersError: - send_async(bot, chat.id, text="Game ended!") + send_async(bot, chat.id, text=_("Game ended!")) gm.end_game(chat, user) if botan: @@ -625,7 +642,7 @@ def do_draw(bot, player): player.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text="There are no more cards in the deck.") + text=_("There are no more cards in the deck.")) if (game.last_card.value == c.DRAW_TWO or game.last_card.special == c.DRAW_FOUR) and \ @@ -639,27 +656,27 @@ def do_call_bluff(bot, player): chat = game.chat if player.prev.bluffing: - send_async(bot, chat.id, text="Bluff called! Giving %d cards to %s" - % (game.draw_counter, - player.prev.user.first_name)) + send_async(bot, chat.id, + text=_("Bluff called! Giving 4 cards to {name}") + .format(name=player.prev.user.first_name)) try: player.prev.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text="There are no more cards in the deck.") + text=_("There are no more cards in the deck.")) else: game.draw_counter += 2 - send_async(bot, chat.id, text="%s didn't bluff! Giving %d cards to %s" - % (player.prev.user.first_name, - game.draw_counter, - player.user.first_name)) + send_async(bot, chat.id, + text="{name1} didn't bluff! Giving 6 cards to {name2}" + .format(name1=player.prev.user.first_name, + name2=player.user.first_name)) try: player.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text="There are no more cards in the deck.") + text=_("There are no more cards in the deck.")) game.turn() diff --git a/database.py b/database.py index 38b5cca..509dbbe 100644 --- a/database.py +++ b/database.py @@ -18,7 +18,21 @@ # along with this program. If not, see . +from functools import wraps + from pony.orm import Database, db_session, Optional, Required, Set, PrimaryKey +from utils import _ + # Database singleton db = Database() + + +def user_locale(func): + @wraps(func) + def wrapped(*pargs, **kwargs): + _.push('de_DE') # TODO: Get user locale from Database + result = func(*pargs, **kwargs) + _.pop() + return result + return wrapped diff --git a/i18n/messages.in b/i18n/messages.in deleted file mode 100644 index 9021290..0000000 --- a/i18n/messages.in +++ /dev/null @@ -1,2 +0,0 @@ -bot.py -results.py diff --git a/i18n/__init__.py b/locales/__init__.py similarity index 100% rename from i18n/__init__.py rename to locales/__init__.py diff --git a/locales/de_DE/LC_MESSAGES/unobot.po b/locales/de_DE/LC_MESSAGES/unobot.po new file mode 100644 index 0000000..63bc3d7 --- /dev/null +++ b/locales/de_DE/LC_MESSAGES/unobot.po @@ -0,0 +1,352 @@ +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# Jannes Höke , 2016. +# +#: bot.py:224 +msgid "" +msgstr "" +"Project-Id-Version: mau_mau_bot 0.1\n" +"Report-Msgid-Bugs-To: uno@jhoeke.de\n" +"POT-Creation-Date: 2016-05-19 22:38+0200\n" +"PO-Revision-Date: 2016-05-21 21:16+0200\n" +"Last-Translator: Jannes Höke \n" +"Language-Team: Deutsch \n" +"Language: de_DE\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n > 1);\n" +"X-Generator: Gtranslator 2.91.6\n" + +#: bot.py:60 +msgid "" +"Follow these steps:\n" +"\n" +"1. Add this bot to a group\n" +"2. In the group, start a new game with /new or join an already running game " +"with /join\n" +"3. After at least two players have joined, start the game with /start\n" +"4. Type @mau_mau_bot into your chat box and hit space, " +"or click the via @mau_mau_bot text next to messages. You will " +"see your cards (some greyed out), any extra options like drawing, and a ?" +" to see the current game state. The greyed out cards are those " +"you can not play at the moment. Tap an option to execute the selected " +"action.\n" +"Players can join the game at any time. To leave a game, use /leave. If a " +"player takes more than 90 seconds to play, you can use /skip to skip that " +"player.\n" +"\n" +"Other commands (only game creator):\n" +"/close - Close lobby\n" +"/open - Open lobby\n" +"\n" +"Experimental: Play in multiple groups at the same time. Press the " +"Current game: ... button and select the group you want to play " +"a card in.\n" +"If you enjoy this bot, rate me, join the update channel and buy an UNO card game." +msgstr "" +"Folge den folgenden Schritten:\n" +"\n" +"1. Füge diesen Bot einer Gruppe hinzu\n" +"2. In einer Gruppe kannst du mit /new ein neues Spiel erstellen und mit /" +"join einem bestehenden Spiel beitreten\n" +"3. Nachdem mindestens zwei Spieler beigetreten sind, starte das Spiel mit /" +"start\n" +"3. Gib @mau_mau_bot in deine Chatbox ein und drücke die " +"Leertaste, oder tippe auf den via @mau_mau_bot-Text " +"neben oder über den Nachrichten. Du siehst deine Karten (einige in grau), " +"zusätzliche Optionen wie z. B. Ziehen, und ein ? um den Infos über " +"das laufende Spiel anzuzeigen. Die grauen Karten kannst du gerade " +"nicht spielen. Tippe eine der Optionen oder Karten an, um diese " +"Aktion auszuführen bzw. die Karte zu spielen. \n" +"Spieler können dem Spiel jederzeit beitreten. Um das Spiel zu verlassen, " +"benutze /leave. Wenn ein Spieler länger als 90 Sekunden braucht, kannst du " +"ihn mit /skip überspringen.\n" +"\n" +"Weitere Kommandos (nur Spiel-Ersteller):\n" +"/close - Lobby schließen\n" +"/open - Lobby öffnen\n" +"\n" +"Experimentell: Spiele in mehreren Gruppen gleichzeitig. Um die " +"Gruppe, in der du deine Karte spielen willst, auszuwählen, tippe auf den " +"Aktuelles Spiel: ...-Button.\n" +"Wenn dir dieser Bot gefällt, bewerte ihn, tritt dem News-Channel bei und kaufe ein UNO Kartenspiel." + +#: bot.py:88 +msgid "" +"This bot is Free Software and licensed under the AGPL. The code is available " +"here: \n" +"https://github.com/jh0ker/mau_mau_bot" +msgstr "" +"Dieser Bot ist Freie Software und lizenziert unter der AGPL. Der Quellcode " +"ist hier verfügbar:\n" +"https://github.com/jh0ker/mau_mau_bot" + +#: bot.py:133 +msgid "" +"Created a new game! Join the game with /join and start the game with /start" +msgstr "" +"Neues Spiel erstellt! Tritt dem Spiel mit /join bei und starte es mit /start" + +#: bot.py:152 +msgid "The lobby is closed" +msgstr "Die Lobby ist geschlossen" + +#: bot.py:156 +msgid "No game is running at the moment. Create a new game with /new" +msgstr "Zur Zeit läuft kein Spiel. Erstelle ein neues mit /new" + +#: bot.py:162 +msgid "You already joined the game. Start the game with /start" +msgstr "Du bist dem Spiel bereits beigetreten. Starte es mit /start" + +#: bot.py:167 +msgid "Joined the game" +msgstr "Spiel beigetreten" + +#: bot.py:179 bot.py:191 +msgid "You are not playing in a game in this group." +msgstr "Du spielst in keinem Spiel in dieser Gruppe." + +#: bot.py:197 bot.py:258 bot.py:595 +msgid "Game ended!" +msgstr "Spiel beendet!" + +#: bot.py:201 +msgid "Okay. Next Player: {name}" +msgstr "Okay. Nächster Spieler: {name}" + +#: bot.py:219 +msgid "Game not found." +msgstr "Spiel nicht gefunden." + +#: bot.py:223 +msgid "Back to last group" +msgstr "Zurück zur letzten Gruppe" + +#: bot.py:227 +msgid "Please switch to the group you selected!" +msgstr "Bitte wechsele zu der Gruppe, die du gewählt hast!" + +#: bot.py:233 +#, python-format +msgid "" +"Selected group: {group}\n" +"Make sure that you switch to the correct group!" +msgstr "" +"Ausgewählte Gruppe: {group}\n" +"Stell sicher, dass du in die richtige Gruppe wechselst!" + +#: bot.py:260 +#, python-format +msgid "Removing {name} from the game" +msgstr "Entferne {name} aus dem Spiel" + +#: bot.py:273 +msgid "There is no game running in this chat. Create a new one with /new" +msgstr "" +"In dieser Gruppe gibt es kein laufendes Spiel. Erstelle ein neues mit /new" + +#: bot.py:278 +msgid "The game has already started" +msgstr "Das Spiel hat bereits begonnen" + +#: bot.py:281 +msgid "At least two players must /join the game before you can start it" +msgstr "" +"Es müssen mindestens zwei Spieler dem Spiel beitreten, bevor du es starten " +"kannst" + +#: bot.py:297 +#, python-format +msgid "" +"First player: {name}\n" +"Use /close to stop people from joining the game." +msgstr "" +"Erster Spieler: {name}\n" +"Benutze /close, um zu verhindern, dass weitere Spieler beitreten." + +#: bot.py:321 +msgid "Please select the group you want to play in." +msgstr "Bitte wähle die Gruppe, in der du spielen willst." + +#: bot.py:335 bot.py:361 +msgid "There is no running game in this chat." +msgstr "In dieser Gruppe läuft gerade kein Spiel." + +#: bot.py:342 +msgid "Closed the lobby. No more players can join this game." +msgstr "" +"Lobby geschlossen. Diesem Spiel können keine weiteren Spieler beitreten." + +#: bot.py:348 bot.py:373 +#, python-format +msgid "Only the game creator ({name}) can do that." +msgstr "Dies kann nur der Ersteller des Spiels ({name}) tun." + +#: bot.py:368 +msgid "Opened the lobby. New players may /join the game." +msgstr "Lobby geöffnet. Neue Spieler können nun beitreten." + +#: bot.py:386 +msgid "You are not playing in a game in this chat." +msgstr "Du spielst kein Spiel in dieser Gruppe." + +#: bot.py:400 +#, python-format +msgid "Please wait {time} seconds" +msgstr "Bitte warte {time} Sekunden" + +#: bot.py:413 +#, python-format +msgid "" +"Waiting time to skip this player has been reduced to {time} seconds.\n" +"Next player: {name}" +msgstr "" +"Die Wartezeit um diesen Spieler zu überspringen wurde auf {time} Sekunden " +"reduziert.\n" +"Nächster Spieler: {name}" + +#: bot.py:424 +#, python-format +msgid "" +"{name1} was skipped four times in a row and has been removed from the game.\n" +"Next player: {name2}" +msgstr "" +"{name1} wurde vier Mal hintereinander übersprungen und daher aus dem Spiel " +"entfernt.\n" +"Nächster Spieler: {name2}" + +#: bot.py:432 +#, python-format +msgid "" +"{name} was skipped four times in a row and has been removed from the game.\n" +"The game ended." +msgstr "" +"{name1} wurde vier Mal hintereinander übersprungen und daher aus dem Spiel " +"entfernt.\n" +"Das Spiel wurde beendet." + +#: bot.py:455 +msgid "All news here: https://telegram.me/unobotupdates" +msgstr "Alle News hier: https://telegram.me/unobotupdates" + +#: bot.py:513 +#, python-format +msgid "Current game: %s" +msgstr "Aktuelles Spiel: {game}" + +#: bot.py:545 +#, python-format +msgid "Cheat attempt by %s" +msgstr "{name} hat versucht zu schummeln!" + +#: bot.py:562 +msgid "Next player: " +msgstr "Nächster Spieler: {name}" + +#: bot.py:572 +#, python-format +msgid "Waiting time for {name} has been reset to 90 seconds" +msgstr "Die Wartezeit für {name} wurde auf 90 Sekunden zurückgesetzt." + +#: bot.py:585 +msgid "Please choose a color" +msgstr "Bitte wähle eine Farbe" + +#: bot.py:591 +#, python-format +msgid "{name} won!" +msgstr "{name} hat gewonnen!" + +#: bot.py:613 bot.py:635 bot.py:647 +msgid "There are no more cards in the deck." +msgstr "Es sind keine Karten mehr im Deck." + +#: bot.py:627 +#, python-format +msgid "Bluff called! Giving 4 cards to {name}" +msgstr "Bluff gecalled! {name} bekommt 4 Karten." + +#: bot.py:639 +#, python-format +msgid "{name1} didn't bluff! Giving 6 cards to {name2}" +msgstr "{name1} hat nicht geblufft! {name2} bekommt 6 Karten." + +#: results.py:38 +msgid "Choose Color" +msgstr "Wähle Farbe" + +#: results.py:56 +msgid "Cards (tap for game state):" +msgstr "Karten (tippe für Spielinfo):" + +#: results.py:60 results.py:123 results.py:165 +msgid "Current player: {name}" +msgstr "Aktueller Spieler: {name}" + +#: results.py:61 results.py:124 results.py:167 +msgid "Last card: {card}" +msgstr "Letzte Karte: {card}" + +#: results.py:62 results.py:125 results.py:168 +msgid "Players: {player_list}" +msgstr "Spieler: {player_list}" + +#: results.py:72 +#, python-format +msgid "{name} ({number} cards)" +msgstr "{name} ({number} Karten)" + +#: results.py:81 +msgid "You are not playing" +msgstr "Du spielst gerade nicht" + +#: results.py:83 +msgid "" +"Not playing right now. Use /new to start a game or /join to join the current " +"game in this group" +msgstr "" +"Du spielst gerade nicht. Benutze /new um ein neues Spiel zu starten oder /" +"join, um einem bestehenden Spiel beizutreten." + +#: results.py:95 +msgid "The game wasn't started yet" +msgstr "Das Spiel wurde noch nicht gestartet." + +#: results.py:97 +msgid "Start the game with /start" +msgstr "Starte das Spiel mit /start" + +#: results.py:108 +#, python-format +msgid "Drawing 1 card" +msgstr "Zieht 1 Karte" + +msgid "Drawing {number} cards" +msgstr "Zieht {number} Karten" + +#: results.py:136 +msgid "Pass" +msgstr "Passe" + +#: results.py:148 +msgid "I'm calling your bluff!" +msgstr "Ich glaube du bluffst!" diff --git a/i18n/messages.pot b/locales/unobot.pot similarity index 89% rename from i18n/messages.pot rename to locales/unobot.pot index deb829e..71b93a8 100644 --- a/i18n/messages.pot +++ b/locales/unobot.pot @@ -25,10 +25,10 @@ msgstr "" "PO-Revision-Date: 2016-05-19 22:38+0200\n" "Last-Translator: Jannes Höke \n" "Language-Team: en \n" -"Language: en\n" +"Language: en_US\n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=utf-8\n" -"Content-Transfer-Encoding: 8bit\n" +"Content-Transfer-Encoding: utf-8\n" #: bot.py:60 @@ -57,7 +57,7 @@ msgid "Follow these steps:\n" "a card in.\n" "If you enjoy this bot, rate me, join the update channel and buy an UNO card game.\n" +"unobotupdates\">update channel and buy an UNO card game." msgstr "" #: bot.py:88 @@ -95,7 +95,7 @@ msgid "Game ended!" msgstr "" #: bot.py:201 -msgid "Okay. Next Player: " +msgid "Okay. Next Player: {name}" msgstr "" #: bot.py:219 @@ -196,20 +196,16 @@ msgstr "" #: bot.py:513 #, python-format -msgid "Current game: %s" -msgstr "" - -#: bot.py:533 -msgid "Selected result: " +msgid "Current game: {group}" msgstr "" #: bot.py:545 #, python-format -msgid "Cheat attempt by %s" +msgid "Cheat attempt by {name}" msgstr "" #: bot.py:562 -msgid "Next player: " +msgid "Next player: {name}" msgstr "" #: bot.py:572 @@ -217,39 +213,27 @@ msgstr "" msgid "Waiting time for {name} has been reset to 90 seconds" msgstr "" -#: bot.py:577 -msgid "Plays the selected card and sends an update to the group if needed" -msgstr "" - #: bot.py:585 msgid "Please choose a color" msgstr "" -#: bot.py:588 -msgid "UNO!" -msgstr "" - #: bot.py:591 #, python-format msgid "{name} won!" msgstr "" -#: bot.py:601 -msgid "Played cards" -msgstr "" - #: bot.py:613 bot.py:635 bot.py:647 msgid "There are no more cards in the deck." msgstr "" #: bot.py:627 #, python-format -msgid "Bluff called! Giving {number} cards to {name}" +msgid "Bluff called! Giving 4 cards to {name}" msgstr "" #: bot.py:639 #, python-format -msgid "{name1} didn't bluff! Giving {number} cards to {name2}" +msgid "{name1} didn't bluff! Giving 6 cards to {name2}" msgstr "" #: results.py:38 @@ -261,23 +245,20 @@ msgid "Cards (tap for game state):" msgstr "" #: results.py:60 results.py:123 results.py:165 -msgid "Current player: " +msgid "Current player: {name}" msgstr "" #: results.py:61 results.py:124 results.py:167 -msgid "\n" -"Last card: " +msgid "Last card: {card}" msgstr "" #: results.py:62 results.py:125 results.py:168 -msgid "\n" -"Players: " +msgid "Players: {player_list}" msgstr "" - #: results.py:72 #, python-format -msgid " ({number} cards)" +msgid "{name} ({number} cards)" msgstr "" #: results.py:81 @@ -299,10 +280,11 @@ msgstr "" #: results.py:108 #, python-format -msgid "Drawing one card" -msgid_plural "Drawing {number} cards" -msgstr[0] "" -msgstr[1] "" +msgid "Drawing 1 card" +msgstr "" + +msgid "Drawing {number} cards" +msgstr "" #: results.py:136 msgid "Pass" diff --git a/results.py b/results.py index aae1aef..69c81e9 100644 --- a/results.py +++ b/results.py @@ -26,7 +26,7 @@ from telegram import InlineQueryResultArticle, InputTextMessageContent, \ InlineQueryResultCachedSticker as Sticker import card as c -from utils import * +from utils import display_color, display_name, list_subtract, _ def add_choose_color(results): @@ -35,7 +35,7 @@ def add_choose_color(results): results.append( InlineQueryResultArticle( id=color, - title="Choose Color", + title=_("Choose Color"), description=display_color(color), input_message_content= InputTextMessageContent(display_color(color)) @@ -48,40 +48,34 @@ def add_other_cards(playable, player, results, game): if not playable: playable = list() - players = player_list(game) - results.append( InlineQueryResultArticle( "hand", - title="Cards (tap for game state):", + title=_("Cards (tap for game state):"), description=', '.join([repr(card) for card in list_subtract(player.cards, playable)]), - input_message_content=InputTextMessageContent( - "Current player: " + display_name(game.current_player.user) + - "\n" + - "Last card: " + repr(game.last_card) + "\n" + - "Players: " + " -> ".join(players)) + input_message_content=game_info(game) ) ) def player_list(game): """Generate list of player strings""" - return [player.user.first_name + " (%d cards)" % len(player.cards) + return ["{name} ({number} cards)" + .format(name=player.user.first_name, number=len(player.cards)) for player in game.players] - def add_no_game(results): """Add text result if user is not playing""" results.append( InlineQueryResultArticle( "nogame", - title="You are not playing", + title=_("You are not playing"), input_message_content= - InputTextMessageContent('Not playing right now. Use /new to start ' - 'a game or /join to join the current game ' - 'in this group') + InputTextMessageContent(_('Not playing right now. Use /new to ' + 'start a game or /join to join the ' + 'current game in this group')) ) ) @@ -91,38 +85,37 @@ def add_not_started(results): results.append( InlineQueryResultArticle( "nogame", - title="The game wasn't started yet", + title=_("The game wasn't started yet"), input_message_content= - InputTextMessageContent('Start the game with /start') + InputTextMessageContent(_('Start the game with /start')) ) ) def add_draw(player, results): """Add option to draw""" + n = player.game.draw_counter or 1 + results.append( Sticker( "draw", sticker_file_id=c.STICKERS['option_draw'], input_message_content= - InputTextMessageContent('Drawing %d card(s)' - % (player.game.draw_counter or 1)) + InputTextMessageContent(_('Drawing 1 card') + if n == 1 else + _('Drawing {number} cards') + .format(number=n)) ) ) def add_gameinfo(game, results): """Add option to show game info""" - players = player_list(game) results.append( Sticker( "gameinfo", sticker_file_id=c.STICKERS['option_info'], - input_message_content=InputTextMessageContent( - "Current player: " + display_name(game.current_player.user) + - "\n" + - "Last card: " + repr(game.last_card) + "\n" + - "Players: " + " -> ".join(players)) + input_message_content=game_info(game) ) ) @@ -132,7 +125,7 @@ def add_pass(results): results.append( Sticker( "pass", sticker_file_id=c.STICKERS['option_pass'], - input_message_content=InputTextMessageContent('Pass') + input_message_content=InputTextMessageContent(_('Pass')) ) ) @@ -144,14 +137,13 @@ def add_call_bluff(results): "call_bluff", sticker_file_id=c.STICKERS['option_bluff'], input_message_content= - InputTextMessageContent("I'm calling your bluff!") + InputTextMessageContent(_("I'm calling your bluff!")) ) ) def add_card(game, card, results, can_play): """Add an option that represents a card""" - players = player_list(game) if can_play: results.append( @@ -160,11 +152,18 @@ def add_card(game, card, results, can_play): else: results.append( Sticker(str(uuid4()), sticker_file_id=c.STICKERS_GREY[str(card)], - input_message_content=InputTextMessageContent( - "Current player: " + display_name( - game.current_player.user) + - "\n" + - "Last card: " + repr(game.last_card) + "\n" + - "Players: " + " -> ".join(players))) + input_message_content=game_info(game)) ) + +def game_info(game): + players = player_list(game) + return InputTextMessageContent( + _("Current player: {name}") + .format(name=display_name(game.current_player.user)) + + "\n" + + _("Last card: {card}").format(card=repr(game.last_card)) + + "\n" + + _("Players: {player_list}") + .format(player_list=" -> ".join(players)) + ) \ No newline at end of file diff --git a/utils.py b/utils.py index 6035b58..cb4b998 100644 --- a/utils.py +++ b/utils.py @@ -18,7 +18,15 @@ # along with this program. If not, see . +from flufl.i18n import registry +from flufl.i18n import PackageStrategy + from telegram import Emoji +import locales + +strategy = PackageStrategy('unobot', locales) +application = registry.register(strategy) +_ = application._ def list_subtract(list1, list2): From cddf13dc5dbdc75f345973d4aba75a4c2fd2e652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sun, 22 May 2016 03:13:05 +0200 Subject: [PATCH 16/20] add __ function to translate complete stack, add dummy decorators to pull locales from db --- bot.py | 96 +++++++++++++++++++++++++++++++++++------------------ database.py | 14 -------- results.py | 12 +++---- utils.py | 28 ++++++++++++++++ 4 files changed, 97 insertions(+), 53 deletions(-) diff --git a/bot.py b/bot.py index 9e513b2..0566d21 100644 --- a/bot.py +++ b/bot.py @@ -20,6 +20,7 @@ import logging from datetime import datetime +from functools import wraps from random import randint from telegram import ParseMode, Message, Chat, InlineKeyboardMarkup, \ @@ -40,8 +41,7 @@ from utils import display_name import card as c from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError, NotEnoughPlayersError, DeckEmptyError) -from database import db_session, user_locale -from utils import _ +from utils import _, __ TIMEOUT = 2.5 @@ -92,6 +92,32 @@ source_text = ("This bot is Free Software and licensed under the AGPL. " "https://github.com/jh0ker/mau_mau_bot") +def user_locale(func): + @wraps(func) + def wrapped(*pargs, **kwargs): + _.push('de_DE') # TODO: Get user locale from Database + result = func(*pargs, **kwargs) + _.pop() + return result + return wrapped + + +def game_locales(func): + @wraps(func) + def wrapped(*pargs, **kwargs): + num_locales = 0 + for loc in ('en_US', 'de_DE'): # TODO: Get user locales from Database + _.push(loc) + num_locales += 1 + + result = func(*pargs, **kwargs) + + for i in range(num_locales): + _.pop() + return result + return wrapped + + @run_async def send_async(bot, *args, **kwargs): """Send a message asynchronously""" @@ -169,8 +195,8 @@ def join_game(bot, update): except DeckEmptyError: send_async(bot, chat.id, - text=_("There are not enough cards left in the deck for new " - "players to join."), + text=_("There are not enough cards left in the deck for " + "new players to join."), reply_to_message_id=update.message.message_id) else: @@ -206,11 +232,11 @@ def leave_game(bot, update): except NotEnoughPlayersError: gm.end_game(chat, user) - send_async(bot, chat.id, text=_("Game ended!")) + send_async(bot, chat.id, text=__("Game ended!")) else: send_async(bot, chat.id, - text=_("Okay. Next Player: {name}").format( + text=__("Okay. Next Player: {name}").format( name=display_name(game.current_player.user)), reply_to_message_id=update.message.message_id) @@ -251,7 +277,7 @@ def select_game(bot, update): timeout=TIMEOUT) -@user_locale +@game_locales def status_update(bot, update): """Remove player from game if user leaves the group""" chat = update.message.chat @@ -268,12 +294,13 @@ def status_update(bot, update): pass except NotEnoughPlayersError: gm.end_game(chat, user) - send_async(bot, chat.id, text=_("Game ended!")) + send_async(bot, chat.id, text=__("Game ended!")) else: - send_async(bot, chat.id, text=_("Removing {name} from the game") + send_async(bot, chat.id, text=__("Removing {name} from the game") .format(name=display_name(user))) +@game_locales @user_locale def start_game(bot, update, args): """Handler for the /start command""" @@ -310,9 +337,9 @@ def start_game(bot, update, args): timeout=TIMEOUT) bot.sendMessage(chat.id, - text=_("First player: {name}\n" - "Use /close to stop people from " - "joining the game.") + text=__("First player: {name}\n" + "Use /close to stop people from " + "joining the game.") .format( name=display_name(game.current_player.user) ), @@ -398,6 +425,7 @@ def open_game(bot, update): return +@game_locales @user_locale def skip_player(bot, update): """Handler for the /skip command""" @@ -433,9 +461,9 @@ def skip_player(bot, update): pass send_async(bot, chat.id, - text=_("Waiting time to skip this player has " - "been reduced to {time} seconds.\n" - "Next player: {name}") + text=__("Waiting time to skip this player has " + "been reduced to {time} seconds.\n" + "Next player: {name}") .format(time=skipped_player.waiting_time, name=display_name(next_player.user))) game.turn() @@ -444,17 +472,17 @@ def skip_player(bot, update): try: gm.leave_game(skipped_player.user, chat) send_async(bot, chat.id, - text=_("{name1} was skipped four times in a row " - "and has been removed from the game.\n" - "Next player: {name2}") + text=__("{name1} was skipped four times in a row " + "and has been removed from the game.\n" + "Next player: {name2}") .format(name1=display_name(skipped_player.user), name2=display_name(next_player.user))) except NotEnoughPlayersError: send_async(bot, chat.id, - text=_("{name} was skipped four times in a row " - "and has been removed from the game.\n" - "The game ended.") + text=__("{name} was skipped four times in a row " + "and has been removed from the game.\n" + "The game ended.") .format(name=display_name(skipped_player.user))) gm.end_game(chat.id, skipped_player.user) @@ -482,6 +510,7 @@ def news(bot, update): disable_web_page_preview=True) +@game_locales @user_locale def reply_to_query(bot, update): """ @@ -543,6 +572,7 @@ def reply_to_query(bot, update): switch_pm_text=switch, switch_pm_parameter='select') +@game_locales @user_locale def process_result(bot, update): """ @@ -570,7 +600,7 @@ def process_result(bot, update): return elif int(anti_cheat) != last_anti_cheat: send_async(bot, chat.id, - text=_("Cheat attempt by {name}") + text=__("Cheat attempt by {name}") .format(name=display_name(player.user))) return elif result_id == 'call_bluff': @@ -589,7 +619,7 @@ def process_result(bot, update): if game in gm.chatid_games.get(chat.id, list()): send_async(bot, chat.id, - text=_("Next player: {name}") + text=__("Next player: {name}") .format(name=display_name(game.current_player.user))) @@ -600,8 +630,8 @@ def reset_waiting_time(bot, player): if player.waiting_time < 90: player.waiting_time = 90 send_async(bot, chat.id, - text=_("Waiting time for {name} has been reset to 90 " - "seconds").format(name=display_name(player.user))) + text=__("Waiting time for {name} has been reset to 90 " + "seconds").format(name=display_name(player.user))) def do_play_card(bot, player, result_id): @@ -616,15 +646,15 @@ def do_play_card(bot, player, result_id): send_async(bot, chat.id, text=_("Please choose a color")) if len(player.cards) == 1: - send_async(bot, chat.id, text=_("UNO!")) + send_async(bot, chat.id, text="UNO!") if len(player.cards) == 0: send_async(bot, chat.id, - text=_("{name} won!").format(name=user.first_name)) + text=__("{name} won!").format(name=user.first_name)) try: gm.leave_game(user, chat) except NotEnoughPlayersError: - send_async(bot, chat.id, text=_("Game ended!")) + send_async(bot, chat.id, text=__("Game ended!")) gm.end_game(chat, user) if botan: @@ -642,7 +672,7 @@ def do_draw(bot, player): player.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text=_("There are no more cards in the deck.")) + text=__("There are no more cards in the deck.")) if (game.last_card.value == c.DRAW_TWO or game.last_card.special == c.DRAW_FOUR) and \ @@ -657,26 +687,26 @@ def do_call_bluff(bot, player): if player.prev.bluffing: send_async(bot, chat.id, - text=_("Bluff called! Giving 4 cards to {name}") + text=__("Bluff called! Giving 4 cards to {name}") .format(name=player.prev.user.first_name)) try: player.prev.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text=_("There are no more cards in the deck.")) + text=__("There are no more cards in the deck.")) else: game.draw_counter += 2 send_async(bot, chat.id, - text="{name1} didn't bluff! Giving 6 cards to {name2}" + text=__("{name1} didn't bluff! Giving 6 cards to {name2}") .format(name1=player.prev.user.first_name, name2=player.user.first_name)) try: player.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text=_("There are no more cards in the deck.")) + text=__("There are no more cards in the deck.")) game.turn() diff --git a/database.py b/database.py index 509dbbe..38b5cca 100644 --- a/database.py +++ b/database.py @@ -18,21 +18,7 @@ # along with this program. If not, see . -from functools import wraps - from pony.orm import Database, db_session, Optional, Required, Set, PrimaryKey -from utils import _ - # Database singleton db = Database() - - -def user_locale(func): - @wraps(func) - def wrapped(*pargs, **kwargs): - _.push('de_DE') # TODO: Get user locale from Database - result = func(*pargs, **kwargs) - _.pop() - return result - return wrapped diff --git a/results.py b/results.py index 69c81e9..47fa31b 100644 --- a/results.py +++ b/results.py @@ -26,7 +26,7 @@ from telegram import InlineQueryResultArticle, InputTextMessageContent, \ InlineQueryResultCachedSticker as Sticker import card as c -from utils import display_color, display_name, list_subtract, _ +from utils import display_color, display_name, list_subtract, _, __ def add_choose_color(results): @@ -61,7 +61,7 @@ def add_other_cards(playable, player, results, game): def player_list(game): """Generate list of player strings""" - return ["{name} ({number} cards)" + return [_("{name} ({number} cards)") .format(name=player.user.first_name, number=len(player.cards)) for player in game.players] @@ -100,9 +100,9 @@ def add_draw(player, results): Sticker( "draw", sticker_file_id=c.STICKERS['option_draw'], input_message_content= - InputTextMessageContent(_('Drawing 1 card') + InputTextMessageContent(__('Drawing 1 card') if n == 1 else - _('Drawing {number} cards') + __('Drawing {number} cards') .format(number=n)) ) ) @@ -125,7 +125,7 @@ def add_pass(results): results.append( Sticker( "pass", sticker_file_id=c.STICKERS['option_pass'], - input_message_content=InputTextMessageContent(_('Pass')) + input_message_content=InputTextMessageContent(__('Pass')) ) ) @@ -137,7 +137,7 @@ def add_call_bluff(results): "call_bluff", sticker_file_id=c.STICKERS['option_bluff'], input_message_content= - InputTextMessageContent(_("I'm calling your bluff!")) + InputTextMessageContent(__("I'm calling your bluff!")) ) ) diff --git a/utils.py b/utils.py index cb4b998..66fa333 100644 --- a/utils.py +++ b/utils.py @@ -18,6 +18,8 @@ # along with this program. If not, see . +import logging + from flufl.i18n import registry from flufl.i18n import PackageStrategy @@ -27,6 +29,32 @@ import locales strategy = PackageStrategy('unobot', locales) application = registry.register(strategy) _ = application._ +logger = logging.getLogger(__name__) + + +def __(string): + """Translates text into all locales on the stack""" + translations = list() + locales = list() + + while True: + translation = _(string) + + if translation not in translations: + translations.append(translation) + + l = _.code + _.pop() + + if l is None: + break + else: + locales.append(l) + + for l in reversed(locales): + _.push(l) + + return '\n'.join(translations) # TODO def list_subtract(list1, list2): From 6c610c1aeba11f4d3036634ead2b2ca6f461631f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sun, 22 May 2016 14:45:51 +0200 Subject: [PATCH 17/20] settings UI added, save locale to database --- bot.py | 164 ++++------------------------ locales/de_DE/LC_MESSAGES/unobot.po | 36 ++++++ locales/unobot.pot | 36 ++++++ settings.py | 104 ++++++++++++++++++ shared_vars.py | 37 +++++++ simple_commands.py | 83 ++++++++++++++ user_setting.py | 7 +- utils.py | 68 ++++++++++++ 8 files changed, 388 insertions(+), 147 deletions(-) create mode 100644 settings.py create mode 100644 shared_vars.py create mode 100644 simple_commands.py diff --git a/bot.py b/bot.py index 0566d21..e1bdefa 100644 --- a/bot.py +++ b/bot.py @@ -20,19 +20,14 @@ import logging from datetime import datetime -from functools import wraps from random import randint from telegram import ParseMode, Message, Chat, InlineKeyboardMarkup, \ InlineKeyboardButton -from telegram.ext import Updater, InlineQueryHandler, \ - ChosenInlineResultHandler, CommandHandler, MessageHandler, Filters, \ - CallbackQueryHandler +from telegram.ext import InlineQueryHandler, ChosenInlineResultHandler, \ + CommandHandler, MessageHandler, Filters, CallbackQueryHandler from telegram.ext.dispatcher import run_async -from telegram.utils.botan import Botan -from game_manager import GameManager -from credentials import TOKEN, BOTAN_TOKEN from start_bot import start_bot from results import (add_call_bluff, add_choose_color, add_draw, add_gameinfo, add_no_game, add_not_started, add_other_cards, add_pass, @@ -41,111 +36,18 @@ from utils import display_name import card as c from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError, NotEnoughPlayersError, DeckEmptyError) -from utils import _, __ +from utils import _, __, send_async, answer_async, user_locale, game_locales, \ + error, TIMEOUT +from shared_vars import botan, gm, updater, dispatcher +import simple_commands, settings -TIMEOUT = 2.5 logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG) logger = logging.getLogger(__name__) -gm = GameManager() -u = Updater(token=TOKEN, workers=32) -dp = u.dispatcher - -botan = False -if BOTAN_TOKEN: - botan = Botan(BOTAN_TOKEN) - -help_text = ("Follow these steps:\n\n" - "1. Add this bot to a group\n" - "2. In the group, start a new game with /new or join an already" - " running game with /join\n" - "3. After at least two players have joined, start the game with" - " /start\n" - "4. Type @mau_mau_bot into your chat box and hit " - "space, or click the via @mau_mau_bot text " - "next to messages. You will see your cards (some greyed out), " - "any extra options like drawing, and a ? to see the " - "current game state. The greyed out cards are those you " - "can not play at the moment. Tap an option to execute " - "the selected action.\n" - "Players can join the game at any time. To leave a game, " - "use /leave. If a player takes more than 90 seconds to play, " - "you can use /skip to skip that player.\n\n" - "Other commands (only game creator):\n" - "/close - Close lobby\n" - "/open - Open lobby\n\n" - "Experimental: Play in multiple groups at the same time. " - "Press the Current game: ... button and select the " - "group you want to play a card in.\n" - "If you enjoy this bot, " - "" - "rate me, join the " - "update channel" - " and buy an UNO card game.") - -source_text = ("This bot is Free Software and licensed under the AGPL. " - "The code is available here: \n" - "https://github.com/jh0ker/mau_mau_bot") - - -def user_locale(func): - @wraps(func) - def wrapped(*pargs, **kwargs): - _.push('de_DE') # TODO: Get user locale from Database - result = func(*pargs, **kwargs) - _.pop() - return result - return wrapped - - -def game_locales(func): - @wraps(func) - def wrapped(*pargs, **kwargs): - num_locales = 0 - for loc in ('en_US', 'de_DE'): # TODO: Get user locales from Database - _.push(loc) - num_locales += 1 - - result = func(*pargs, **kwargs) - - for i in range(num_locales): - _.pop() - return result - return wrapped - - -@run_async -def send_async(bot, *args, **kwargs): - """Send a message asynchronously""" - if 'timeout' not in kwargs: - kwargs['timeout'] = TIMEOUT - - try: - bot.sendMessage(*args, **kwargs) - except Exception as e: - error(None, None, e) - - -@run_async -def answer_async(bot, *args, **kwargs): - """Answer an inline query asynchronously""" - if 'timeout' not in kwargs: - kwargs['timeout'] = TIMEOUT - - try: - bot.answerInlineQuery(*args, **kwargs) - except Exception as e: - error(None, None, e) - - -def error(bot, update, error): - """Simple error handler""" - logger.exception(error) - @user_locale def new_game(bot, update): @@ -487,29 +389,6 @@ def skip_player(bot, update): gm.end_game(chat.id, skipped_player.user) - -@user_locale -def help(bot, update): - """Handler for the /help command""" - send_async(bot, update.message.chat_id, text=_(help_text), - parse_mode=ParseMode.HTML, disable_web_page_preview=True) - - -@user_locale -def source(bot, update): - """Handler for the /help command""" - send_async(bot, update.message.chat_id, text=_(source_text), - parse_mode=ParseMode.HTML, disable_web_page_preview=True) - - -@user_locale -def news(bot, update): - """Handler for the /news command""" - send_async(bot, update.message.chat_id, - text=_("All news here: https://telegram.me/unobotupdates"), - disable_web_page_preview=True) - - @game_locales @user_locale def reply_to_query(bot, update): @@ -712,21 +591,18 @@ def do_call_bluff(bot, player): # Add all handlers to the dispatcher and run the bot -dp.add_handler(InlineQueryHandler(reply_to_query)) -dp.add_handler(ChosenInlineResultHandler(process_result)) -dp.add_handler(CallbackQueryHandler(select_game)) -dp.add_handler(CommandHandler('start', start_game, pass_args=True)) -dp.add_handler(CommandHandler('new', new_game)) -dp.add_handler(CommandHandler('join', join_game)) -dp.add_handler(CommandHandler('leave', leave_game)) -dp.add_handler(CommandHandler('open', open_game)) -dp.add_handler(CommandHandler('close', close_game)) -dp.add_handler(CommandHandler('skip', skip_player)) -dp.add_handler(CommandHandler('help', help)) -dp.add_handler(CommandHandler('source', source)) -dp.add_handler(CommandHandler('news', news)) -dp.add_handler(MessageHandler([Filters.status_update], status_update)) -dp.add_error_handler(error) +dispatcher.add_handler(InlineQueryHandler(reply_to_query)) +dispatcher.add_handler(ChosenInlineResultHandler(process_result)) +dispatcher.add_handler(CallbackQueryHandler(select_game)) +dispatcher.add_handler(CommandHandler('start', start_game, pass_args=True)) +dispatcher.add_handler(CommandHandler('new', new_game)) +dispatcher.add_handler(CommandHandler('join', join_game)) +dispatcher.add_handler(CommandHandler('leave', leave_game)) +dispatcher.add_handler(CommandHandler('open', open_game)) +dispatcher.add_handler(CommandHandler('close', close_game)) +dispatcher.add_handler(CommandHandler('skip', skip_player)) +dispatcher.add_handler(MessageHandler([Filters.status_update], status_update)) +dispatcher.add_error_handler(error) -start_bot(u) -u.idle() +start_bot(updater) +updater.idle() diff --git a/locales/de_DE/LC_MESSAGES/unobot.po b/locales/de_DE/LC_MESSAGES/unobot.po index 63bc3d7..89a0232 100644 --- a/locales/de_DE/LC_MESSAGES/unobot.po +++ b/locales/de_DE/LC_MESSAGES/unobot.po @@ -350,3 +350,39 @@ msgstr "Passe" #: results.py:148 msgid "I'm calling your bluff!" msgstr "Ich glaube du bluffst!" + +#: settings.py:39 +msgid "Please edit your settings in a private chat with the bot." +msgstr "Bitte ändere deine Einstellungen in einem privaten Chat mit dem Bot." + +#: settings.py:49 +msgid "Enable statistics" +msgstr "Statistiken aktivieren" + +#: settings.py:51 +msgid "Delete all statistics" +msgstr "Alle Statistiken löschen" + +#: settings.py:53 +msgid "Language" +msgstr "Sprache" + +#: settings.py:54 +msgid "Settings" +msgstr "Einstellungen" + +#: settings.py:68 +msgid "Enabled statistics!" +msgstr "Statistiken aktiviert!" + +#: settings.py:70 +msgid "Select locale" +msgstr "Bitte Sprache auswählen" + +#: settings.py:81 +msgid "Deleted and disabled statistics!" +msgstr "Alle Statistiken gelöscht und deaktiviert!" + +#: settings.py:94 +msgid "Set locale!" +msgstr "Sprache gesetzt!" diff --git a/locales/unobot.pot b/locales/unobot.pot index 71b93a8..2742045 100644 --- a/locales/unobot.pot +++ b/locales/unobot.pot @@ -294,3 +294,39 @@ msgstr "" msgid "I'm calling your bluff!" msgstr "" +#: settings.py:39 +msgid "Please edit your settings in a private chat with the bot." +msgstr "" + +#: settings.py:49 +msgid "Enable statistics" +msgstr "" + +#: settings.py:51 +msgid "Delete all statistics" +msgstr "" + +#: settings.py:53 +msgid "Language" +msgstr "" + +#: settings.py:54 +msgid "Settings" +msgstr "" + +#: settings.py:68 +msgid "Enabled statistics!" +msgstr "" + +#: settings.py:70 +msgid "Select locale" +msgstr "" + +#: settings.py:81 +msgid "Deleted and disabled statistics!" +msgstr "" + +#: settings.py:94 +msgid "Set locale!" +msgstr "" + diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..b98600d --- /dev/null +++ b/settings.py @@ -0,0 +1,104 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +import logging + +from telegram import ReplyKeyboardMarkup, Emoji +from telegram.ext import CommandHandler, RegexHandler + +from utils import send_async +from user_setting import UserSetting +from utils import _, user_locale +from shared_vars import dispatcher + +available_locales = [['en_US', 'de_DE']] + +@user_locale +def show_settings(bot, update): + chat = update.message.chat + + if update.message.chat.type != 'private': + send_async(bot, chat.id, + text=_("Please edit your settings in a private chat with " + "the bot.")) + return + + us = UserSetting.get(id=update.message.from_user.id) + + if not us: + us = UserSetting(id=update.message.from_user.id) + + if not us.stats: + stats = Emoji.BAR_CHART + ' ' + _("Enable statistics") + else: + stats = Emoji.CROSS_MARK + ' ' + _("Delete all statistics") + + kb = [[stats], [Emoji.EARTH_GLOBE_EUROPE_AFRICA + ' ' + _("Language")]] + send_async(bot, chat.id, text=Emoji.WRENCH + ' ' + _("Settings"), + reply_markup=ReplyKeyboardMarkup(keyboard=kb, + one_time_keyboard=True)) + + +@user_locale +def kb_select(bot, update, groups): + chat = update.message.chat + user = update.message.from_user + option = groups[0] + + if option == Emoji.BAR_CHART: + us = UserSetting.get(id=user.id) + us.stats = True + send_async(bot, chat.id, text=_("Enabled statistics!")) + + elif option == Emoji.EARTH_GLOBE_EUROPE_AFRICA: + send_async(bot, chat.id, text=_("Select locale"), + reply_markup=ReplyKeyboardMarkup(keyboard=available_locales, + one_time_keyboard=True)) + + elif option == Emoji.CROSS_MARK: + us = UserSetting.get(id=user.id) + us.stats = False + us.first_places = 0 + us.games_played = 0 + us.cards_played = 0 + send_async(bot, chat.id, text=_("Deleted and disabled statistics!")) + + +@user_locale +def locale_select(bot, update, groups): + chat = update.message.chat + user = update.message.from_user + option = groups[0] + + if option in [locale for row in available_locales for locale in row]: + us = UserSetting.get(id=user.id) + us.lang = option + _.push(option) + send_async(bot, chat.id, text=_("Set locale!")) + _.pop() + + +dispatcher.add_handler(CommandHandler('settings', show_settings)) +dispatcher.add_handler(RegexHandler('^([' + Emoji.BAR_CHART + + Emoji.EARTH_GLOBE_EUROPE_AFRICA + + Emoji.CROSS_MARK + ']) .+$', + kb_select, pass_groups=True)) +dispatcher.add_handler(RegexHandler(r'^(\w\w_\w\w)$', + locale_select, pass_groups=True)) diff --git a/shared_vars.py b/shared_vars.py new file mode 100644 index 0000000..ceda668 --- /dev/null +++ b/shared_vars.py @@ -0,0 +1,37 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + + +from telegram.ext import Updater +from telegram.utils.botan import Botan + +from game_manager import GameManager +from database import db +from credentials import TOKEN, BOTAN_TOKEN + +db.bind('sqlite', 'uno.sqlite3', create_db=True) +db.generate_mapping(create_tables=True) + +gm = GameManager() +updater = Updater(token=TOKEN, workers=32) +dispatcher = updater.dispatcher + +botan = False +if BOTAN_TOKEN: + botan = Botan(BOTAN_TOKEN) diff --git a/simple_commands.py b/simple_commands.py new file mode 100644 index 0000000..5fb9d13 --- /dev/null +++ b/simple_commands.py @@ -0,0 +1,83 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +# +# Telegram bot to play UNO in group chats +# Copyright (c) 2016 Jannes Höke +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from telegram import ParseMode +from telegram.ext import CommandHandler + +from utils import _, send_async, user_locale +from shared_vars import dispatcher + +help_text = ("Follow these steps:\n\n" + "1. Add this bot to a group\n" + "2. In the group, start a new game with /new or join an already" + " running game with /join\n" + "3. After at least two players have joined, start the game with" + " /start\n" + "4. Type @mau_mau_bot into your chat box and hit " + "space, or click the via @mau_mau_bot text " + "next to messages. You will see your cards (some greyed out), " + "any extra options like drawing, and a ? to see the " + "current game state. The greyed out cards are those you " + "can not play at the moment. Tap an option to execute " + "the selected action.\n" + "Players can join the game at any time. To leave a game, " + "use /leave. If a player takes more than 90 seconds to play, " + "you can use /skip to skip that player.\n\n" + "Other commands (only game creator):\n" + "/close - Close lobby\n" + "/open - Open lobby\n\n" + "Experimental: Play in multiple groups at the same time. " + "Press the Current game: ... button and select the " + "group you want to play a card in.\n" + "If you enjoy this bot, " + "" + "rate me, join the " + "update channel" + " and buy an UNO card game.") + +source_text = ("This bot is Free Software and licensed under the AGPL. " + "The code is available here: \n" + "https://github.com/jh0ker/mau_mau_bot") + + +@user_locale +def help(bot, update): + """Handler for the /help command""" + send_async(bot, update.message.chat_id, text=_(help_text), + parse_mode=ParseMode.HTML, disable_web_page_preview=True) + + +@user_locale +def source(bot, update): + """Handler for the /help command""" + send_async(bot, update.message.chat_id, text=_(source_text), + parse_mode=ParseMode.HTML, disable_web_page_preview=True) + + +@user_locale +def news(bot, update): + """Handler for the /news command""" + send_async(bot, update.message.chat_id, + text=_("All news here: https://telegram.me/unobotupdates"), + disable_web_page_preview=True) + + +dispatcher.add_handler(CommandHandler('help', help)) +dispatcher.add_handler(CommandHandler('source', source)) +dispatcher.add_handler(CommandHandler('news', news)) diff --git a/user_setting.py b/user_setting.py index f44bd82..5eb1a58 100644 --- a/user_setting.py +++ b/user_setting.py @@ -18,14 +18,15 @@ # along with this program. If not, see . -from database import db, Optional, Required, PrimaryKey +from database import db, Optional, Required, PrimaryKey, db_session class UserSetting(db.Entity): - id = PrimaryKey(int, auto=False) # Telegram User ID + id = PrimaryKey(int, auto=False, size=64) # Telegram User ID lang = Optional(str, default='en') # The language setting for this user stats = Optional(bool, default=False) # Opt-in to keep game statistics first_places = Optional(int, default=0) # Nr. of games won in first place games_played = Optional(int, default=0) # Nr. of games completed - cards_played = Optional(int, default=0) # Nr. of cards played + cards_played = Optional(int, default=0) # Nr. of cards played total + use_keyboards = Optional(bool, default=False) # Use keyboards (unused) diff --git a/utils.py b/utils.py index 66fa333..1d0676f 100644 --- a/utils.py +++ b/utils.py @@ -19,18 +19,24 @@ import logging +from functools import wraps from flufl.i18n import registry from flufl.i18n import PackageStrategy from telegram import Emoji +from telegram.ext.dispatcher import run_async import locales +from database import db_session +from user_setting import UserSetting strategy = PackageStrategy('unobot', locales) application = registry.register(strategy) _ = application._ logger = logging.getLogger(__name__) +TIMEOUT = 2.5 + def __(string): """Translates text into all locales on the stack""" @@ -85,3 +91,65 @@ def display_color(color): return Emoji.GREEN_HEART + " Green" if color == "y": return Emoji.YELLOW_HEART + " Yellow" + + +def error(bot, update, error): + """Simple error handler""" + logger.exception(error) + + +@run_async +def send_async(bot, *args, **kwargs): + """Send a message asynchronously""" + if 'timeout' not in kwargs: + kwargs['timeout'] = TIMEOUT + + try: + bot.sendMessage(*args, **kwargs) + except Exception as e: + error(None, None, e) + + +@run_async +def answer_async(bot, *args, **kwargs): + """Answer an inline query asynchronously""" + if 'timeout' not in kwargs: + kwargs['timeout'] = TIMEOUT + + try: + bot.answerInlineQuery(*args, **kwargs) + except Exception as e: + error(None, None, e) + + +def user_locale(func): + @wraps(func) + @db_session + def wrapped(bot, update, *pargs, **kwargs): + with db_session: + us = UserSetting.get(id=update.message.from_user.id) + if us: + _.push(us.lang) + else: + _.push('en_US') + result = func(bot, update, *pargs, **kwargs) + _.pop() + return result + return wrapped + + +def game_locales(func): + @wraps(func) + @db_session + def wrapped(*pargs, **kwargs): + num_locales = 0 + for loc in ('en_US', 'de_DE'): # TODO: Get user locales from Database + _.push(loc) + num_locales += 1 + + result = func(*pargs, **kwargs) + + for i in range(num_locales): + _.pop() + return result + return wrapped \ No newline at end of file From 005445c4ddaa1872ce4b5e993fc26c4503e0b5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sun, 22 May 2016 14:47:26 +0200 Subject: [PATCH 18/20] add database file to gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 2215d04..49decb9 100644 --- a/.gitignore +++ b/.gitignore @@ -63,3 +63,6 @@ target/ # PyCharm .idea + +# Database file +uno.sqlite3 From 4cdffffa5f4d213719735f86681a76538ab2f8a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sun, 22 May 2016 17:02:27 +0200 Subject: [PATCH 19/20] Optional multi-translations --- bot.py | 130 +++++++++++++++++++++------- game.py | 1 + game_manager.py | 2 + locales/de_DE/LC_MESSAGES/unobot.po | 39 ++++++--- locales/unobot.pot | 23 ++++- results.py | 15 ++-- simple_commands.py | 6 +- utils.py | 87 ++++++++++++++----- 8 files changed, 229 insertions(+), 74 deletions(-) diff --git a/bot.py b/bot.py index e1bdefa..d3b1ea2 100644 --- a/bot.py +++ b/bot.py @@ -41,6 +41,7 @@ from utils import _, __, send_async, answer_async, user_locale, game_locales, \ from shared_vars import botan, gm, updater, dispatcher import simple_commands, settings +from simple_commands import help logging.basicConfig( @@ -134,11 +135,11 @@ def leave_game(bot, update): except NotEnoughPlayersError: gm.end_game(chat, user) - send_async(bot, chat.id, text=__("Game ended!")) + send_async(bot, chat.id, text=__("Game ended!", game.translate)) else: send_async(bot, chat.id, - text=__("Okay. Next Player: {name}").format( + text=__("Okay. Next Player: {name}", game.translate).format( name=display_name(game.current_player.user)), reply_to_message_id=update.message.message_id) @@ -185,20 +186,20 @@ def status_update(bot, update): chat = update.message.chat if update.message.left_chat_member: - try: - user = update.message.left_chat_member - except KeyError: - return + user = update.message.left_chat_member try: gm.leave_game(user, chat) + game = gm.player_for_user_in_chat(user, chat).game + except NoGameInChatError: pass except NotEnoughPlayersError: gm.end_game(chat, user) - send_async(bot, chat.id, text=__("Game ended!")) + send_async(bot, chat.id, text=__("Game ended!", game.translate)) else: - send_async(bot, chat.id, text=__("Removing {name} from the game") + send_async(bot, chat.id, text=__("Removing {name} from the game", + game.translate) .format(name=display_name(user))) @@ -230,6 +231,13 @@ def start_game(bot, update, args): game.play_card(game.last_card) game.started = True + first_message = ( + __("First player: {name}\n" + "Use /close to stop people from joining the game.\n" + "Enable multi-translations with /enable_translations", + game.translate) + .format(name=display_name(game.current_player.user))) + @run_async def send_first(): """Send the first card and player""" @@ -239,12 +247,7 @@ def start_game(bot, update, args): timeout=TIMEOUT) bot.sendMessage(chat.id, - text=__("First player: {name}\n" - "Use /close to stop people from " - "joining the game.") - .format( - name=display_name(game.current_player.user) - ), + text=first_message, timeout=TIMEOUT) send_first() @@ -327,6 +330,63 @@ def open_game(bot, update): return +@user_locale +def enable_translations(bot, update): + """Handler for the /enable_translations command""" + chat = update.message.chat + user = update.message.from_user + games = gm.chatid_games.get(chat.id) + + if not games: + send_async(bot, chat.id, + text=_("There is no running game in this chat.")) + return + + game = games[-1] + + if game.owner.id == user.id: + game.translate = True + send_async(bot, chat.id, text=_("Enabled multi-translations. " + "Disable with /disable_translations")) + return + + else: + send_async(bot, chat.id, + text=_("Only the game creator ({name}) can do that") + .format(name=game.owner.first_name), + reply_to_message_id=update.message.message_id) + return + + +@user_locale +def disable_translations(bot, update): + """Handler for the /disable_translations command""" + chat = update.message.chat + user = update.message.from_user + games = gm.chatid_games.get(chat.id) + + if not games: + send_async(bot, chat.id, + text=_("There is no running game in this chat.")) + return + + game = games[-1] + + if game.owner.id == user.id: + game.translate = False + send_async(bot, chat.id, text=_("Disabled multi-translations. " + "Enable them again with " + "/enable_translations")) + return + + else: + send_async(bot, chat.id, + text=_("Only the game creator ({name}) can do that") + .format(name=game.owner.first_name), + reply_to_message_id=update.message.message_id) + return + + @game_locales @user_locale def skip_player(bot, update): @@ -365,7 +425,7 @@ def skip_player(bot, update): send_async(bot, chat.id, text=__("Waiting time to skip this player has " "been reduced to {time} seconds.\n" - "Next player: {name}") + "Next player: {name}", game.translate) .format(time=skipped_player.waiting_time, name=display_name(next_player.user))) game.turn() @@ -376,7 +436,7 @@ def skip_player(bot, update): send_async(bot, chat.id, text=__("{name1} was skipped four times in a row " "and has been removed from the game.\n" - "Next player: {name2}") + "Next player: {name2}", game.translate) .format(name1=display_name(skipped_player.user), name2=display_name(next_player.user))) @@ -384,11 +444,12 @@ def skip_player(bot, update): send_async(bot, chat.id, text=__("{name} was skipped four times in a row " "and has been removed from the game.\n" - "The game ended.") + "The game ended.", game.translate) .format(name=display_name(skipped_player.user))) gm.end_game(chat.id, skipped_player.user) + @game_locales @user_locale def reply_to_query(bot, update): @@ -420,10 +481,10 @@ def reply_to_query(bot, update): add_draw(player, results) else: - add_pass(results) + add_pass(results, game) if game.last_card.special == c.DRAW_FOUR and game.draw_counter: - add_call_bluff(results) + add_call_bluff(results, game) playable = player.playable_cards() added_ids = list() # Duplicates are not allowed @@ -479,7 +540,7 @@ def process_result(bot, update): return elif int(anti_cheat) != last_anti_cheat: send_async(bot, chat.id, - text=__("Cheat attempt by {name}") + text=__("Cheat attempt by {name}", game.translate) .format(name=display_name(player.user))) return elif result_id == 'call_bluff': @@ -498,7 +559,7 @@ def process_result(bot, update): if game in gm.chatid_games.get(chat.id, list()): send_async(bot, chat.id, - text=__("Next player: {name}") + text=__("Next player: {name}", game.translate) .format(name=display_name(game.current_player.user))) @@ -510,7 +571,8 @@ def reset_waiting_time(bot, player): player.waiting_time = 90 send_async(bot, chat.id, text=__("Waiting time for {name} has been reset to 90 " - "seconds").format(name=display_name(player.user))) + "seconds", player.game.translate) + .format(name=display_name(player.user))) def do_play_card(bot, player, result_id): @@ -529,11 +591,12 @@ def do_play_card(bot, player, result_id): if len(player.cards) == 0: send_async(bot, chat.id, - text=__("{name} won!").format(name=user.first_name)) + text=__("{name} won!", game.translate) + .format(name=user.first_name)) try: gm.leave_game(user, chat) except NotEnoughPlayersError: - send_async(bot, chat.id, text=__("Game ended!")) + send_async(bot, chat.id, text=__("Game ended!", game.translate)) gm.end_game(chat, user) if botan: @@ -551,7 +614,8 @@ def do_draw(bot, player): player.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text=__("There are no more cards in the deck.")) + text=__("There are no more cards in the deck.", + game.translate)) if (game.last_card.value == c.DRAW_TWO or game.last_card.special == c.DRAW_FOUR) and \ @@ -566,26 +630,30 @@ def do_call_bluff(bot, player): if player.prev.bluffing: send_async(bot, chat.id, - text=__("Bluff called! Giving 4 cards to {name}") + text=__("Bluff called! Giving 4 cards to {name}", + game.translate) .format(name=player.prev.user.first_name)) try: player.prev.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text=__("There are no more cards in the deck.")) + text=__("There are no more cards in the deck.", + game.translate)) else: game.draw_counter += 2 send_async(bot, chat.id, - text=__("{name1} didn't bluff! Giving 6 cards to {name2}") + text=__("{name1} didn't bluff! Giving 6 cards to {name2}", + game.translate) .format(name1=player.prev.user.first_name, name2=player.user.first_name)) try: player.draw() except DeckEmptyError: send_async(bot, player.game.chat.id, - text=__("There are no more cards in the deck.")) + text=__("There are no more cards in the deck.", + game.translate)) game.turn() @@ -600,6 +668,10 @@ dispatcher.add_handler(CommandHandler('join', join_game)) dispatcher.add_handler(CommandHandler('leave', leave_game)) dispatcher.add_handler(CommandHandler('open', open_game)) dispatcher.add_handler(CommandHandler('close', close_game)) +dispatcher.add_handler(CommandHandler('enable_translations', + enable_translations)) +dispatcher.add_handler(CommandHandler('disable_translations', + disable_translations)) dispatcher.add_handler(CommandHandler('skip', skip_player)) dispatcher.add_handler(MessageHandler([Filters.status_update], status_update)) dispatcher.add_error_handler(error) diff --git a/game.py b/game.py index cbfc575..f25726a 100644 --- a/game.py +++ b/game.py @@ -34,6 +34,7 @@ class Game(object): started = False owner = None open = True + translate = False def __init__(self, chat): self.chat = chat diff --git a/game_manager.py b/game_manager.py index a505ade..0013cba 100644 --- a/game_manager.py +++ b/game_manager.py @@ -77,6 +77,8 @@ class GameManager(object): self.leave_game(user, chat) except NoGameInChatError: pass + except NotEnoughPlayersError: + self.end_game(chat, user) player = Player(game, user) diff --git a/locales/de_DE/LC_MESSAGES/unobot.po b/locales/de_DE/LC_MESSAGES/unobot.po index 89a0232..9ce018d 100644 --- a/locales/de_DE/LC_MESSAGES/unobot.po +++ b/locales/de_DE/LC_MESSAGES/unobot.po @@ -18,7 +18,7 @@ #: bot.py:224 msgid "" msgstr "" -"Project-Id-Version: mau_mau_bot 0.1\n" +"Project-Id-Version: uno_bot 0.1\n" "Report-Msgid-Bugs-To: uno@jhoeke.de\n" "POT-Creation-Date: 2016-05-19 22:38+0200\n" "PO-Revision-Date: 2016-05-21 21:16+0200\n" @@ -32,8 +32,7 @@ msgstr "" "X-Generator: Gtranslator 2.91.6\n" #: bot.py:60 -msgid "" -"Follow these steps:\n" +msgid "Follow these steps:\n" "\n" "1. Add this bot to a group\n" "2. In the group, start a new game with /new or join an already running game " @@ -49,9 +48,13 @@ msgid "" "player takes more than 90 seconds to play, you can use /skip to skip that " "player.\n" "\n" +"Language and other settings: /settings\n" "Other commands (only game creator):\n" "/close - Close lobby\n" "/open - Open lobby\n" +"/enable_translations - Translate relevant texts into all " +"languages spoken in a game\n" +"/disable_translations - Use English for those texts\n" "\n" "Experimental: Play in multiple groups at the same time. Press the " "Current game: ... button and select the group you want to play " @@ -78,9 +81,13 @@ msgstr "" "benutze /leave. Wenn ein Spieler länger als 90 Sekunden braucht, kannst du " "ihn mit /skip überspringen.\n" "\n" +"Sprache und andere Einstellungen: /settings\n" "Weitere Kommandos (nur Spiel-Ersteller):\n" "/close - Lobby schließen\n" "/open - Lobby öffnen\n" +"/enable_translations - Übersetze relevante Texte in alle im Spiel gesprochenen" +" Sprachen\n" +"/disable_translations - Verwende Englisch für diese Texte\n" "\n" "Experimentell: Spiele in mehreren Gruppen gleichzeitig. Um die " "Gruppe, in der du deine Karte spielen willst, auszuwählen, tippe auf den " @@ -170,15 +177,14 @@ msgstr "Das Spiel hat bereits begonnen" #: bot.py:281 msgid "At least two players must /join the game before you can start it" -msgstr "" -"Es müssen mindestens zwei Spieler dem Spiel beitreten, bevor du es starten " -"kannst" +msgstr "Es müssen mindestens zwei Spieler dem Spiel beitreten, bevor du es " +"starten kannst" #: bot.py:297 -#, python-format -msgid "" -"First player: {name}\n" -"Use /close to stop people from joining the game." +#, python-format, fuzzy +msgid "First player: {name}\n" +"Use /close to stop people from joining the game.\n" +"Enable multi-translations with /enable_translations" msgstr "" "Erster Spieler: {name}\n" "Benutze /close, um zu verhindern, dass weitere Spieler beitreten." @@ -201,6 +207,17 @@ msgstr "" msgid "Only the game creator ({name}) can do that." msgstr "Dies kann nur der Ersteller des Spiels ({name}) tun." +#: bot.py:349 +#, python-format +msgid "Enabled multi-translations. Disable with /disable_translations" +msgstr "Multi-Übersetzungen aktiviert. Deaktivieren mit /disable_translations" + +#: bot.py:377 +#, python-format +msgid "Disabled multi-translations. Enable them again with /enable_translations" +msgstr "Multi-Übersetzungen deaktiviert. Aktiviere sie wieder mit " +"/enable_translations" + #: bot.py:368 msgid "Opened the lobby. New players may /join the game." msgstr "Lobby geöffnet. Neue Spieler können nun beitreten." @@ -259,7 +276,7 @@ msgid "Cheat attempt by %s" msgstr "{name} hat versucht zu schummeln!" #: bot.py:562 -msgid "Next player: " +msgid "Next player: {name}" msgstr "Nächster Spieler: {name}" #: bot.py:572 diff --git a/locales/unobot.pot b/locales/unobot.pot index 2742045..0005894 100644 --- a/locales/unobot.pot +++ b/locales/unobot.pot @@ -19,7 +19,7 @@ #, fuzzy msgid "" msgstr "" -"Project-Id-Version: mau_mau_bot 0.1\n" +"Project-Id-Version: uno_bot 0.1\n" "Report-Msgid-Bugs-To: uno@jhoeke.de\n" "POT-Creation-Date: 2016-05-19 22:38+0200\n" "PO-Revision-Date: 2016-05-19 22:38+0200\n" @@ -32,6 +32,7 @@ msgstr "" #: bot.py:60 +#, fuzzy msgid "Follow these steps:\n" "\n" "1. Add this bot to a group\n" @@ -48,10 +49,13 @@ msgid "Follow these steps:\n" "player takes more than 90 seconds to play, you can use /skip to skip that " "player.\n" "\n" +"Language and other settings: /settings\n" "Other commands (only game creator):\n" "/close - Close lobby\n" "/open - Open lobby\n" -"\n" +"/enable_translations - Translate relevant texts into all " +"languages spoken in a game\n" +"/disable_translations - Use English for those texts\n\n" "Experimental: Play in multiple groups at the same time. Press the " "Current game: ... button and select the group you want to play " "a card in.\n" @@ -136,9 +140,10 @@ msgstr "" #: bot.py:297 -#, python-format +#, python-format, fuzzy msgid "First player: {name}\n" -"Use /close to stop people from joining the game." +"Use /close to stop people from joining the game.\n" +"Enable multi-translations with /enable_translations" msgstr "" #: bot.py:321 @@ -159,6 +164,16 @@ msgstr "" msgid "Only the game creator ({name}) can do that." msgstr "" +#: bot.py:349 +#, python-format +msgid "Enabled multi-translations. Disable with /disable_translations" +msgstr "" + +#: bot.py:377 +#, python-format +msgid "Disabled multi-translations. Enable them again with /enable_translations" +msgstr "" + #: bot.py:368 msgid "Opened the lobby. New players may /join the game." msgstr "" diff --git a/results.py b/results.py index 47fa31b..722656d 100644 --- a/results.py +++ b/results.py @@ -100,9 +100,10 @@ def add_draw(player, results): Sticker( "draw", sticker_file_id=c.STICKERS['option_draw'], input_message_content= - InputTextMessageContent(__('Drawing 1 card') + InputTextMessageContent(__('Drawing 1 card', player.game.translate) if n == 1 else - __('Drawing {number} cards') + __('Drawing {number} cards', + player.game.translate) .format(number=n)) ) ) @@ -120,24 +121,26 @@ def add_gameinfo(game, results): ) -def add_pass(results): +def add_pass(results, game): """Add option to pass""" results.append( Sticker( "pass", sticker_file_id=c.STICKERS['option_pass'], - input_message_content=InputTextMessageContent(__('Pass')) + input_message_content=InputTextMessageContent(__('Pass', + game.translate)) ) ) -def add_call_bluff(results): +def add_call_bluff(results, game): """Add option to call a bluff""" results.append( Sticker( "call_bluff", sticker_file_id=c.STICKERS['option_bluff'], input_message_content= - InputTextMessageContent(__("I'm calling your bluff!")) + InputTextMessageContent(__("I'm calling your bluff!", + game.translate)) ) ) diff --git a/simple_commands.py b/simple_commands.py index 5fb9d13..51c0770 100644 --- a/simple_commands.py +++ b/simple_commands.py @@ -39,9 +39,13 @@ help_text = ("Follow these steps:\n\n" "Players can join the game at any time. To leave a game, " "use /leave. If a player takes more than 90 seconds to play, " "you can use /skip to skip that player.\n\n" + "Language and other settings: /settings\n" "Other commands (only game creator):\n" "/close - Close lobby\n" - "/open - Open lobby\n\n" + "/open - Open lobby\n" + "/enable_translations - Translate relevant texts into all " + "languages spoken in a game\n" + "/disable_translations - Use English for those texts\n\n" "Experimental: Play in multiple groups at the same time. " "Press the Current game: ... button and select the " "group you want to play a card in.\n" diff --git a/utils.py b/utils.py index 1d0676f..9d93152 100644 --- a/utils.py +++ b/utils.py @@ -29,6 +29,7 @@ from telegram.ext.dispatcher import run_async import locales from database import db_session from user_setting import UserSetting +from shared_vars import gm strategy = PackageStrategy('unobot', locales) application = registry.register(strategy) @@ -38,27 +39,28 @@ logger = logging.getLogger(__name__) TIMEOUT = 2.5 -def __(string): +def __(string, multi_translate): """Translates text into all locales on the stack""" translations = list() locales = list() - while True: - translation = _(string) - - if translation not in translations: - translations.append(translation) - - l = _.code + if not multi_translate: + _.push('en_US') + translations.append(_(string)) _.pop() - if l is None: - break - else: - locales.append(l) + else: + while _.code: + translation = _(string) - for l in reversed(locales): - _.push(l) + if translation not in translations: + translations.append(translation) + + locales.append(_.code) + _.pop() + + for l in reversed(locales): + _.push(l) return '\n'.join(translations) # TODO @@ -126,12 +128,16 @@ def user_locale(func): @wraps(func) @db_session def wrapped(bot, update, *pargs, **kwargs): + user, chat = _user_chat_from_update(update) + with db_session: - us = UserSetting.get(id=update.message.from_user.id) + us = UserSetting.get(id=user.id) + if us: _.push(us.lang) else: _.push('en_US') + result = func(bot, update, *pargs, **kwargs) _.pop() return result @@ -141,15 +147,50 @@ def user_locale(func): def game_locales(func): @wraps(func) @db_session - def wrapped(*pargs, **kwargs): - num_locales = 0 - for loc in ('en_US', 'de_DE'): # TODO: Get user locales from Database - _.push(loc) - num_locales += 1 + def wrapped(bot, update, *pargs, **kwargs): + user, chat = _user_chat_from_update(update) + player = gm.player_for_user_in_chat(user, chat) + locales = list() - result = func(*pargs, **kwargs) + if player: + for player in player.game.players: + us = UserSetting.get(id=player.user.id) - for i in range(num_locales): + if us: + loc = us.lang + else: + loc = 'en_US' + + if loc in locales: + continue + + _.push(loc) + locales.append(loc) + + result = func(bot, update, *pargs, **kwargs) + + for i in locales: _.pop() return result - return wrapped \ No newline at end of file + return wrapped + + +def _user_chat_from_update(update): + + try: + user = update.message.from_user + chat = update.message.chat + except (NameError, AttributeError): + try: + user = update.inline_query.from_user + chat = gm.userid_current[user.id].game.chat + except KeyError: + chat = None + except (NameError, AttributeError): + try: + user = update.chosen_inline_result.from_user + chat = gm.userid_current[user.id].game.chat + except (NameError, AttributeError): + chat = None + + return user, chat From ba47f4c19ef4d400da9319b08056cdc7dd30eb4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannes=20H=C3=B6ke?= Date: Sun, 22 May 2016 19:21:51 +0200 Subject: [PATCH 20/20] final version? --- bot.py | 21 +++++++++++++++- game.py | 1 + locales/de_DE/LC_MESSAGES/unobot.po | 37 +++++++++++++++++++++++++++++ locales/unobot.pot | 35 +++++++++++++++++++++++++++ results.py | 7 +++--- settings.py | 1 + simple_commands.py | 23 ++++++++++++++++++ utils.py | 26 ++++++++++++++++---- 8 files changed, 142 insertions(+), 9 deletions(-) diff --git a/bot.py b/bot.py index d3b1ea2..700ed97 100644 --- a/bot.py +++ b/bot.py @@ -32,6 +32,7 @@ from start_bot import start_bot from results import (add_call_bluff, add_choose_color, add_draw, add_gameinfo, add_no_game, add_not_started, add_other_cards, add_pass, add_card) +from user_setting import UserSetting from utils import display_name import card as c from errors import (NoGameInChatError, LobbyClosedError, AlreadyJoinedError, @@ -474,7 +475,7 @@ def reply_to_query(bot, update): elif user_id == game.current_player.user.id: if game.choosing_color: - add_choose_color(results) + add_choose_color(results, game) add_other_cards(playable, player, results, game) else: if not player.drew: @@ -583,6 +584,12 @@ def do_play_card(bot, player, result_id): chat = game.chat user = player.user + us = UserSetting.get(id=user.id) + if not us: + us = UserSetting(id=user.id) + + us.cards_played += 1 + if game.choosing_color: send_async(bot, chat.id, text=_("Please choose a color")) @@ -593,10 +600,22 @@ def do_play_card(bot, player, result_id): send_async(bot, chat.id, text=__("{name} won!", game.translate) .format(name=user.first_name)) + + if us.stats: + us.games_played += 1 + + if game.players_won is 0: + us.first_places += 1 + try: gm.leave_game(user, chat) except NotEnoughPlayersError: send_async(bot, chat.id, text=__("Game ended!", game.translate)) + + us2 = UserSetting.get(id=game.current_player.next.user.id) + if us2 and us2.stats: + us2.games_played += 1 + gm.end_game(chat, user) if botan: diff --git a/game.py b/game.py index f25726a..5308fac 100644 --- a/game.py +++ b/game.py @@ -35,6 +35,7 @@ class Game(object): owner = None open = True translate = False + players_won = 0 def __init__(self, chat): self.chat = chat diff --git a/locales/de_DE/LC_MESSAGES/unobot.po b/locales/de_DE/LC_MESSAGES/unobot.po index 9ce018d..2e72d95 100644 --- a/locales/de_DE/LC_MESSAGES/unobot.po +++ b/locales/de_DE/LC_MESSAGES/unobot.po @@ -403,3 +403,40 @@ msgstr "Alle Statistiken gelöscht und deaktiviert!" #: settings.py:94 msgid "Set locale!" msgstr "Sprache gesetzt!" + + +#: simple_commands.py +msgid "You did not enable statistics. Use /settings in " +"a private chat with the bot to enable them." +msgstr "Du hast die Spiel-Statistiken nicht aktiviert. Aktiviere sie, mit dem " +"/settings-Kommando in einem privaten Chat mit dem Bot." + +#: simple_commands.py +msgid "{number} games played" +msgstr "{number} gespielte Spiele" + +#: simple_commands.py +msgid "{number} first places" +msgstr "{number}x 1. Platz" + +#: simple_commands.py +msgid "{number} cards played" +msgstr "{number} gespielte Karten" + + +#: utils.py +msgid "{emoji} Green" +msgstr "{emoji} Grün" + +#: utils.py +msgid "{emoji} Red" +msgstr "{emoji} Rot" + +#: utils.py +msgid "{emoji} Blue" +msgstr "{emoji} Blau" + +#: utils.py +msgid "{emoji} Yellow" +msgstr "{emoji} Gelb" + diff --git a/locales/unobot.pot b/locales/unobot.pot index 0005894..1d00e72 100644 --- a/locales/unobot.pot +++ b/locales/unobot.pot @@ -345,3 +345,38 @@ msgstr "" msgid "Set locale!" msgstr "" + +#: simple_commands.py +msgid "You did not enable statistics. Use /settings in " +"a private chat with the bot to enable them." +msgstr "" + +#: simple_commands.py +msgid "{number} games played" +msgstr "" + +#: simple_commands.py +msgid "{number} first places" +msgstr "" + +#: simple_commands.py +msgid "{number} cards played" +msgstr "" + + +#: utils.py +msgid "{emoji} Green" +msgstr "" + +#: utils.py +msgid "{emoji} Red" +msgstr "" + +#: utils.py +msgid "{emoji} Blue" +msgstr "" + +#: utils.py +msgid "{emoji} Yellow" +msgstr "" + diff --git a/results.py b/results.py index 722656d..5d98be7 100644 --- a/results.py +++ b/results.py @@ -26,10 +26,11 @@ from telegram import InlineQueryResultArticle, InputTextMessageContent, \ InlineQueryResultCachedSticker as Sticker import card as c -from utils import display_color, display_name, list_subtract, _, __ +from utils import display_color, display_color_group, display_name, \ + list_subtract, _, __ -def add_choose_color(results): +def add_choose_color(results, game): """Add choose color options""" for color in c.COLORS: results.append( @@ -38,7 +39,7 @@ def add_choose_color(results): title=_("Choose Color"), description=display_color(color), input_message_content= - InputTextMessageContent(display_color(color)) + InputTextMessageContent(display_color_group(color, game)) ) ) diff --git a/settings.py b/settings.py index b98600d..aa4a1c3 100644 --- a/settings.py +++ b/settings.py @@ -30,6 +30,7 @@ from shared_vars import dispatcher available_locales = [['en_US', 'de_DE']] + @user_locale def show_settings(bot, update): chat = update.message.chat diff --git a/simple_commands.py b/simple_commands.py index 51c0770..2cb99f3 100644 --- a/simple_commands.py +++ b/simple_commands.py @@ -20,6 +20,7 @@ from telegram import ParseMode from telegram.ext import CommandHandler +from user_setting import UserSetting from utils import _, send_async, user_locale from shared_vars import dispatcher @@ -82,6 +83,28 @@ def news(bot, update): disable_web_page_preview=True) +@user_locale +def stats(bot, update): + user = update.message.from_user + us = UserSetting.get(id=user.id) + if not us or not us.stats: + send_async(bot, update.message.chat_id, + text=_("You did not enable statistics. Use /settings in " + "a private chat with the bot to enable them.")) + else: + stats_text = list() + stats_text.append( + _("{number} games played").format(number=us.games_played)) + stats_text.append( + _("{number} first places").format(number=us.first_places)) + stats_text.append( + _("{number} cards played").format(number=us.cards_played)) + + send_async(bot, update.message.chat_id, + text='\n'.join(stats_text)) + + dispatcher.add_handler(CommandHandler('help', help)) dispatcher.add_handler(CommandHandler('source', source)) dispatcher.add_handler(CommandHandler('news', news)) +dispatcher.add_handler(CommandHandler('stats', stats)) diff --git a/utils.py b/utils.py index 9d93152..af32d83 100644 --- a/utils.py +++ b/utils.py @@ -62,7 +62,7 @@ def __(string, multi_translate): for l in reversed(locales): _.push(l) - return '\n'.join(translations) # TODO + return '\n'.join(translations) def list_subtract(list1, list2): @@ -86,13 +86,29 @@ def display_name(user): def display_color(color): """ Convert a color code to actual color name """ if color == "r": - return Emoji.HEAVY_BLACK_HEART + " Red" + return _("{emoji} Red").format(emoji=Emoji.HEAVY_BLACK_HEART) if color == "b": - return Emoji.BLUE_HEART + " Blue" + return _("{emoji} Blue").format(emoji=Emoji.BLUE_HEART) if color == "g": - return Emoji.GREEN_HEART + " Green" + return _("{emoji} Green").format(emoji=Emoji.GREEN_HEART) if color == "y": - return Emoji.YELLOW_HEART + " Yellow" + return _("{emoji} Yellow").format(emoji=Emoji.YELLOW_HEART) + + +def display_color_group(color, game): + """ Convert a color code to actual color name """ + if color == "r": + return __("{emoji} Red", game.translate).format( + emoji=Emoji.HEAVY_BLACK_HEART) + if color == "b": + return __("{emoji} Blue", game.translate).format( + emoji=Emoji.BLUE_HEART) + if color == "g": + return __("{emoji} Green", game.translate).format( + emoji=Emoji.GREEN_HEART) + if color == "y": + return __("{emoji} Yellow", game.translate).format( + emoji=Emoji.YELLOW_HEART) def error(bot, update, error):