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: kb = [[locale + ' - ' + descr] for locale, descr in sorted(available_locales.items())] send_async(bot, chat.id, text=_("Select locale"), reply_markup=ReplyKeyboardMarkup(keyboard=kb, 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!"))
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) player.play(card) game = player.game chat = game.chat user = player.user last = player.game.last_card us = UserSetting.get(id=user.id) if not us: us = UserSetting(id=user.id) if us.stats: us.cards_played += 1 if game.choosing_color: if len(player.cards) >= 1: send_async(bot, chat.id, text=_("Please choose a color")) if len(player.cards) == 1: send_async(bot, chat.id, text="UNO!") if len(player.cards) == 0: send_async(bot, chat.id, text=__("{name} won!", multi=game.translate).format(name=user.first_name)) if us.stats: us.games_played += 1 if game.players_won is 0: us.first_places += 1 game.players_won += 1 game.players_won_list.append(player.prev.user.id) try: if last.special == c.CHOOSE or last.special == c.DRAW_FOUR: game.last_card.color = random.choice(c.COLORS) send_async(bot, chat.id, text=__( "Color randomly choosen to: {col}", multi=game.translate).format( col=c.COLOR_ICONS[game.last_card.color])) gm.leave_game(user, chat) except NotEnoughPlayersError: send_async(bot, chat.id, text=__("Game ended!", multi=game.translate)) us2 = UserSetting.get(id=game.current_player.user.id) if us2 and us2.stats: us2.games_played += 1 us2.last_places += 1 gm.end_game(chat, user)
def kb_select(bot, update, groups): chat = update.message.chat user = update.message.from_user option = groups[0] if option == '📊': us = UserSetting.get(id=user.id) us.stats = True send_async(bot, chat.id, text=_("Enabled statistics!")) elif option == '🌍': kb = [[locale + ' - ' + descr] for locale, descr in sorted(available_locales.items())] send_async(bot, chat.id, text=_("Select locale"), reply_markup=ReplyKeyboardMarkup(keyboard=kb, one_time_keyboard=True)) elif option == '❌': 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!"))
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) player.play(card) game = player.game chat = game.chat user = player.user us = UserSetting.get(id=user.id) if not us: us = UserSetting(id=user.id) if us.stats: us.cards_played += 1 if game.choosing_color: send_async(bot, chat.id, text=__("Please choose a color", multi=game.translate)) if len(player.cards) == 15: send_async(bot, chat.id, text="Amazon khulgya, wew") if len(player.cards) == 13: send_async(bot, chat.id, text="AliExpress khulgya, wew") if len(player.cards) == 11: send_async(bot, chat.id, text="Mall khulgya, wew") if len(player.cards) == 9: send_async(bot, chat.id, text="Dukaan khulgya, wew") if len(player.cards) == 7: send_async(bot, chat.id, text="Dukaan soonTM") if len(player.cards) == 1: send_async(bot, chat.id, text="UNO | Is chutiye ka ek hi card bach gaya, maro!") if len(player.cards) == 0: send_async(bot, chat.id, text=__("{name} jeet gaya bc!", multi=game.translate) .format(name=user.first_name)) if us.stats: us.games_played += 1 if game.players_won is 0: us.first_places += 1 game.players_won += 1 try: gm.leave_game(user, chat) except NotEnoughPlayersError: send_async(bot, chat.id, text=__("Game ended!", multi=game.translate)) us2 = UserSetting.get(id=game.current_player.user.id) if us2 and us2.stats: us2.games_played += 1 gm.end_game(chat, 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) player.play(card) game = player.game chat = game.chat user = player.user us = UserSetting.get(id=user.id) if not us: us = UserSetting(id=user.id) if us.stats: us.cards_played += 1 if game.choosing_color: send_async(bot, chat.id, text=__("Please choose a color", multi=game.translate)) if game.current_player.user.id == bot.id: print('escolheu') color = c.COLORS[random.randint(0, 3)] game.choose_color(color) send_async(bot, chat.id, text=display_color_group(color, game)) if len(player.cards) == 1: send_async(bot, chat.id, text="UNO!") if len(player.cards) == 0: send_async(bot, chat.id, text=__("{name} won!", multi=game.translate).format(name=user.first_name)) if us.stats: us.games_played += 1 if game.players_won is 0: us.first_places += 1 game.players_won += 1 try: gm.leave_game(user, chat) except NotEnoughPlayersError: send_async(bot, chat.id, text=__("Game ended!", multi=game.translate)) us2 = UserSetting.get(id=game.current_player.user.id) if us2 and us2.stats: us2.games_played += 1 gm.end_game(chat, 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) player.play(card) game = player.game chat = game.chat user = player.user us = UserSetting.get(id=user.id) if not us: us = UserSetting(id=user.id) if us.stats: us.cards_played += 1 if game.choosing_color: send_async(bot, chat.id, text=_("Please choose a color")) if len(player.cards) == 1: send_async(bot, chat.id, text="UNO!") if len(player.cards) == 0: send_async(bot, chat.id, text=__("{name} won!", multi=game.translate).format(name=user.first_name)) send_async(bot, chat.id, text=__("Game ended! Flawless Victory!")) if us.stats: # us.games_played += 1 if game.players_won is 0: us.first_places += 1 if game.mode == 'one': gm.end_game(chat, user) else: game.players_won += 1 try: if game.mode != 'one': gm.leave_game(user, chat) except NotEnoughPlayersError: send_async(bot, chat.id, text=__("Game ended!", multi=game.translate)) us2 = UserSetting.get(id=game.current_player.user.id) if us2 and us2.stats: # us2.games_played += 1 us2.last_places += 1 gm.end_game(chat, 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) player.play(card) game = player.game chat = game.chat user = player.user us = UserSetting.get(id=user.id) if not us: us = UserSetting(id=user.id) if us.stats: us.cards_played += 1 if game.choosing_color: send_async(bot, chat.id, text=_("Please choose a color")) if len(player.cards) == 1: send_async(bot, chat.id, text="UNO!") if len(player.cards) == 0: send_async(bot, chat.id, text=__("{name} won!", multi=game.translate).format(name=user.first_name)) if us.stats: us.games_played += 1 if game.players_won is 0: us.first_places += 1 game.players_won += 1 try: gm.leave_game(user, chat) except NotEnoughPlayersError: send_async(bot, chat.id, text=__("Game ended!", multi=game.translate)) us2 = UserSetting.get(id=game.current_player.user.id) if us2 and us2.stats: us2.games_played += 1 gm.end_game(chat, user) if botan: random_int = random.randrange(1, 999999999) botan.track( Message(random_int, user, datetime.now(), Chat(chat.id, 'group')), 'Played cards')
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) player.play(card) game = player.game chat = game.chat user = player.user us = UserSetting.get(id=user.id) if not us: us = UserSetting(id=user.id) if us.stats: us.cards_played += 1 if game.choosing_color: send_async(bot, chat.id, text=_("Please choose a color")) if len(player.cards) == 1: send_async(bot, chat.id, text="UNO!") if len(player.cards) == 0: send_async(bot, chat.id, text=__("{name} won!", multi=game.translate) .format(name=user.first_name)) if us.stats: us.games_played += 1 if game.players_won is 0: us.first_places += 1 game.players_won += 1 try: gm.leave_game(user, chat) except NotEnoughPlayersError: send_async(bot, chat.id, text=__("Game ended!", multi=game.translate)) us2 = UserSetting.get(id=game.current_player.user.id) if us2 and us2.stats: us2.games_played += 1 gm.end_game(chat, user) if botan: botan.track(Message(randint(1, 1000000000), user, datetime.now(), Chat(chat.id, 'group')), 'Played cards')
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))
def wrapped(bot, update, *pargs, **kwargs): user, chat = _user_chat_from_update(update) player = gm.player_for_user_in_chat(user, chat) locales = list() if player: for player in player.game.players: us = UserSetting.get(id=player.user.id) if us and us.lang != 'en': loc = us.lang else: loc = 'en_US' if loc in locales: continue _.push(loc) locales.append(loc) result = func(bot, update, *pargs, **kwargs) while _.code: _.pop() return result
def wrapped(bot, update, *pargs, **kwargs): user, chat = _user_chat_from_update(update) player = gm.player_for_user_in_chat(user, chat) locales = list() if player: for player in player.game.players: us = UserSetting.get(id=player.user.id) if us and us.lang != 'pt': loc = us.lang else: loc = 'en_US' if loc in locales: continue _.push(loc) locales.append(loc) result = func(bot, update, *pargs, **kwargs) while _.code: _.pop() return result
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() n = us.games_played stats_text.append( _("{number} game played", "{number} games played", n).format(number=n) ) n = us.first_places stats_text.append( _("{number} first place", "{number} first places", n).format(number=n) ) n = us.cards_played stats_text.append( _("{number} card played", "{number} cards played", n).format(number=n) ) send_async(bot, update.message.chat_id, text='\n'.join(stats_text))
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() n = us.games_played stats_text.append( _("{number} game played", "{number} games played", n).format(number=n)) n = us.first_places stats_text.append( _("{number} first place", "{number} first places", n).format(number=n)) n = us.cards_played stats_text.append( _("{number} card played", "{number} cards played", n).format(number=n)) send_async(bot, update.message.chat_id, text='\n'.join(stats_text))
def locale_select(bot, update, groups): chat = update.message.chat user = update.message.from_user option = groups[0] if option in available_locales: us = UserSetting.get(id=user.id) us.lang = option _.push(option) send_async(bot, chat.id, text=_("Set locale!")) _.pop()
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!"))
def wrapped(bot, update, *pargs, **kwargs): user, chat = _user_chat_from_update(update) with db_session: us = UserSetting.get(id=user.id) if us and us.lang != 'en': _.push(us.lang) else: _.push('en_US') result = func(bot, update, *pargs, **kwargs) _.pop() return result
def wrapped(bot, update, *pargs, **kwargs): user = _user_chat_from_update(update)[0] with db_session: us = UserSetting.get(id=user.id) if us and us.lang != 'pt': _.push(us.lang) else: _.push('en_US') result = func(bot, update, *pargs, **kwargs) _.pop() return result
def join_game(bot, update): #Start DB us = UserSetting.get(id=update.message.from_user.id) if not us: us = UserSetting(id=update.message.from_user.id) #Start DB """Handler for the /join command""" chat = update.message.chat if update.message.chat.type == 'private': help_handler(bot, update) return 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) 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"), reply_to_message_id=update.message.message_id)
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)) try: us = UserSetting.get(id=user.id) if not us: us = UserSetting(id=user.id) us.stats = True game = self.chatid_games[chat.id][-1] except (KeyError, IndexError): raise NoGameInChatError() if not game.open: raise LobbyClosedError() if user.id not in self.userid_players: self.userid_players[user.id] = list() us.games_played += 1 players = self.userid_players[user.id] # Don not re-add a player and remove the player from previous games in # this chat, if he is in one of them for player in players: if player in game.players: raise AlreadyJoinedError() try: self.leave_game(user, chat) except NoGameInChatError: pass except NotEnoughPlayersError: self.end_game(chat, user) if user.id not in self.userid_players: self.userid_players[user.id] = list() players = self.userid_players[user.id] player = Player(game, user) if game.started: player.draw_first_hand() players.append(player) self.userid_current[user.id] = player
def close_game(bot, update): #Start DB us = UserSetting.get(id=update.message.from_user.id) if not us: us = UserSetting(id=update.message.from_user.id) #Start DB """Handler for the /close 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 user.id in game.owner: game.open = False 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 ({name}) and admin can do that." ).format(name=game.starter.first_name), reply_to_message_id=update.message.message_id) return
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 = '📊' + ' ' + _("Enable statistics") else: stats = '❌' + ' ' + _("Delete all statistics") kb = [[stats], ['🌍' + ' ' + _("Language")]] send_async(bot, chat.id, text='🔧' + ' ' + _("Settings"), reply_markup=ReplyKeyboardMarkup(keyboard=kb, one_time_keyboard=True))
def new_game(bot, update): #Start DB us = UserSetting.get(id=update.message.from_user.id) if not us: us = UserSetting(id=update.message.from_user.id) #Start DB """Handler for the /new command""" chat_id = update.message.chat_id if update.message.chat.type == 'private': help_handler(bot, update) else: if update.message.chat_id in gm.remind_dict: for user in gm.remind_dict[update.message.chat_id]: send_async( bot, user, text=_("A new game has been started in {title}").format( title=update.message.chat.title)) del gm.remind_dict[update.message.chat_id] game = gm.new_game(update.message.chat) game.starter = update.message.from_user game.owner.append(update.message.from_user.id) game.mode = DEFAULT_GAMEMODE send_async(bot, chat_id, text=_("Created a new game! Join the game with /join " "and start the game with /start"))
_(attributions), 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) @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() n = us.games_played stats_text.append( _("{number} game played", "{number} games played", n).format(number=n) ) n = us.first_places
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) player.play(card) game = player.game chat = game.chat user = player.user us = UserSetting.get(id=user.id) if not us: us = UserSetting(id=user.id) if us.stats: us.cards_played += 1 if game.choosing_color: send_async(bot, chat.id, text=_("Please choose a color")) if len(player.cards) == 1: send_async(bot, chat.id, text="UNO!") if len(player.cards) == 0: if game.mode != 'score': send_async( bot, chat.id, text=__("{name} won!", multi=game.translate).format(name=user.first_name)) if game.mode == "score": game.add_score(player) score = sum([n[0] for n in game.last_round_score]) bot.sendMessage( chat.id, text=__("Added {pt} point", "Added {pt} points", score).format(pt=score) + "\n" + ("Points to win: {score}").format(score=game.win_score) + "\n\n" + ("Current standings:\n") + "\n".join([ "{no}. {name}: {pt}".format( no=i + 1, name=x[0].user.first_name, pt=x[1]) for i, x in enumerate( sorted(game.get_scores(), key=lambda x: x[1], reverse=True)) ]) + "\n\nCard Opponents Players:\n" + " | ".join([ "{cl} {vl} (+{pt})".format( cl=c.COLOR_ICONS[n[1].color or 'x'], vl=n[1].value or n[1].special, pt=n[0]).replace('draw_four', '+4').replace( 'draw', '+2').replace('colorchooser', 'Color Chooser').replace( 'skip', 'Skip').replace( 'reverse', 'Reverse') for n in sorted(game.last_round_score, key=lambda x: x[0]) ])) players_cache = game.players highest = sorted(zip(players_cache, map(game.get_score, players_cache)), key=lambda x: x[1])[-1] if highest[1] >= game.win_score: if game.mode == 'score': send_async(bot, chat.id, text=__("Game ended! {name} won the game!", multi=game.translate).format( name=highest[0].user.first_name)) if us.stats: us.first_places += 1 for player in players_cache: us_ = UserSetting.get(id=player.user.id) if not us: us_ = UserSetting(id=player.user.id) us_.games_played += 1 gm.end_game(chat, user) else: sleep(5) game.reset_cards() game.new_round() bot.sendSticker(chat.id, sticker=c.STICKERS[str(game.last_card)], timeout=TIMEOUT) return # If game mode is "score", 'do_play_card' should stop here if us.stats: us.games_played += 1 if game.players_won is 0: us.first_places += 1 game.players_won += 1 if game.mode != "score": send_async(bot, chat.id, text=__("Game ended!", multi=game.translate)) gm.end_game(chat, user)
def start_game(bot, update, args, job_queue): #Start DB us = UserSetting.get(id=update.message.from_user.id) if not us: us = UserSetting(id=update.message.from_user.id) #Start DB """Handler for the /start command""" if update.message.chat.type != 'private': chat = update.message.chat 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")) return if game.started: send_async(bot, chat.id, text=_("The game has already started")) elif len(game.players) < MIN_PLAYERS: send_async( bot, chat.id, text=__( "At least {minplayers} players must /join the game " "before you can start it").format(minplayers=MIN_PLAYERS)) else: # Starting a game game.start() for player in game.players: player.draw_first_hand() choice = [[ InlineKeyboardButton(text=_("Jogar"), switch_inline_query_current_chat='') ]] first_message = (__( "First player: {name}\n" "Use /close to stop people from joining the game.\n" "Enable multi-translations with /enable_translations", multi=game.translate).format( name=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_message, reply_markup=InlineKeyboardMarkup(choice), timeout=TIMEOUT) send_first() start_player_countdown(bot, game, job_queue) elif len(args) and args[0] == 'select': players = gm.userid_players[update.message.from_user.id] groups = list() for player in players: 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.'), reply_markup=InlineKeyboardMarkup(groups)) else: help_handler(bot, update)