Example #1
0
def leave_game(bot, update):
    """Handler for the /leave command"""
    chat = update.message.chat
    user = update.message.from_user

    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

    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!", multi=game.translate))

    else:
        send_async(bot, chat.id,
                   text=__("Okay. Next Player: {name}",
                           multi=game.translate).format(
                       name=display_name(game.current_player.user)),
                   reply_to_message_id=update.message.message_id)
Example #2
0
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)
Example #3
0
def ai_turn(bot, game):
    player = game.current_player
    while player.ai:
        reply = ''

        from ISMCTS import UNOState, ISMCTS
		from utils import display_color, display_name
        chat_id = game.chat.id
        state = UNOState(game)
        move = ISMCTS(state, itermax=ai_iterations, verbose=False)
        if move == 'draw':
            reply += 'Drawing\n'
        else:
            sticker_async(bot, chat_id,
                          sticker=c.STICKERS[str(move)])
            if move.special:
                reply += "Choosing color: %s\n" % display_color(move.color)

        state.DoMove(move)
        if len(player.cards) == 1:
            reply += "UNO!\n"
        if len(player.cards) == 0:
            reply += "%s won!\n" % player.user.first_name
            if len(game.players) < 3:
                reply += "Game ended!"
                gm.end_game(chat_id, player.next.user)
            else:
                player.leave()

        player = game.current_player
        if game in gm.chatid_games.get(chat_id, list()):
            reply += "Next player: " + display_name(player.user)

        send_async(bot, chat_id, text=reply)
Example #4
0
def leave_game(bot, update):
    """Handler for the /leave command"""
    chat = update.message.chat
    user = update.message.from_user

    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

    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!", multi=game.translate))

    else:
        send_async(bot, chat.id,
                   text=__("Okay. Next Player: {name}",
                           multi=game.translate).format(
                       name=display_name(game.current_player.user)),
                   reply_to_message_id=update.message.message_id)
Example #5
0
def status_update(bot, update):
    """Remove player from game if user leaves the group"""
    chat = update.message.chat

    if update.message.left_chat_member:
        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=__("chalo hogaya aaj ka, back to bihar.",
                               multi=game.translate))
        else:
            send_async(
                bot,
                chat.id,
                text=__("{name} bihari ko nikalo bc",
                        multi=game.translate).format(name=display_name(user)))
Example #6
0
def kill_game(bot, update):
    """Handler for the /kill command"""
    chat = update.message.chat
    user = update.message.from_user
    games = gm.chatid_games.get(chat.id)

    if update.message.chat.type == 'private':
        help(bot, update)
        return

    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 or user.id in get_admin_ids(bot, chat.id):

        try:
            gm.end_game(chat, user)
            send_async(bot, chat.id, text=__("Game ended!", multi=game.translate))

        except NoGameInChatError:
            send_async(bot, chat.id,
                       text=_("The game is not started yet. "
                              "Join the game with /join and start the game with /start"),
                       reply_to_message_id=update.message.message_id)

    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)
Example #7
0
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)
Example #8
0
def do_skip(bot, player, job_queue=None):
    game = player.game
    chat = game.chat
    skipped_player = game.current_player
    next_player = game.current_player.next

    if skipped_player.waiting_time > 0:
        skipped_player.anti_cheat += 1
        skipped_player.waiting_time -= TIME_REMOVAL_AFTER_SKIP
        if (skipped_player.waiting_time < 0):
            skipped_player.waiting_time = 0

        # if player get skipped after playing wildcard or +4
        # before getting to choose color, then choose color randomly
        if game.choosing_color:
            game.last_card.color = random.choice(c.COLORS)

        try:
            skipped_player.draw()
        except DeckEmptyError:
            pass

        n = skipped_player.waiting_time
        send_async(bot,
                   chat.id,
                   text="Waiting time to skip this player has "
                   "been reduced to {time} seconds.\n"
                   "Next player: {name}".format(time=n,
                                                name=display_name(
                                                    next_player.user)))
        logger.info(
            "{player} was skipped!. ".format(player=display_name(player.user)))
        game.turn()
        if job_queue:
            start_player_countdown(bot, game, job_queue)

    else:
        try:
            gm.leave_game(skipped_player.user, chat)
            send_async(bot,
                       chat.id,
                       text="{name1} ran out of time "
                       "and has been removed from the game!\n"
                       "Next player: {name2}".format(
                           name1=display_name(skipped_player.user),
                           name2=display_name(next_player.user)))
            logger.info("{player} was skipped!. ".format(
                player=display_name(player.user)))
            if job_queue:
                start_player_countdown(bot, game, job_queue)

        except NotEnoughPlayersError:
            send_async(bot,
                       chat.id,
                       text="{name} ran out of time "
                       "and has been removed from the game!\n"
                       "The game ended.".format(
                           name=display_name(skipped_player.user)))

            gm.end_game(chat, skipped_player.user)
Example #9
0
def skip_player(bot, update):
    """Handler for the /skip command"""
    chat = update.message.chat
    user = update.message.from_user

    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

    game = player.game
    skipped_player = game.current_player
    next_player = game.current_player.next

    started = skipped_player.turn_started
    now = datetime.now()
    delta = (now - started).seconds

    if delta < skipped_player.waiting_time:
        send_async(bot, chat.id,
                   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:
        skipped_player.anti_cheat += 1
        skipped_player.waiting_time -= 30
        try:
            skipped_player.draw()
        except DeckEmptyError:
            pass

        send_async(bot, chat.id,
                   text=__("Waiting time to skip this player has "
                           "been reduced to {time} seconds.\n"
                           "Next player: {name}", game.translate)
                   .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=__("{name1} was skipped four times in a row "
                               "and has been removed from the game.\n"
                               "Next player: {name2}", game.translate)
                       .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.", game.translate)
                       .format(name=display_name(skipped_player.user)))

            gm.end_game(chat.id, skipped_player.user)
Example #10
0
def leave_game(bot, update):
    """Handler for the /leave command"""
    chat = update.message.chat
    user = update.message.from_user

    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

    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!", multi=game.translate))

    else:
        if game.started:
            # send message to indicate the game has randomly choosen the color
            if game._randomed_color:
                send_async(bot,
                           chat.id,
                           text=__(
                               "Color randomly choosen to: {col}",
                               multi=game.translate).format(
                                   col=c.COLOR_ICONS[game.last_card.color]),
                           reply_to_message_id=update.message.message_id)
            send_async(
                bot,
                chat.id,
                text=__("Okay. Next Player: {name}",
                        multi=game.translate).format(
                            name=display_name(game.current_player.user)),
                reply_to_message_id=update.message.message_id)
        else:
            send_async(
                bot,
                chat.id,
                text=__("{name} left the game before it started.",
                        multi=game.translate).format(name=display_name(user)),
                reply_to_message_id=update.message.message_id)
Example #11
0
def do_skip(bot, player, job_queue=None):
    game = player.game
    chat = game.chat
    skipped_player = game.current_player
    next_player = game.current_player.next

    if skipped_player.waiting_time > 0:
        skipped_player.anti_cheat += 1
        skipped_player.waiting_time -= TIME_REMOVAL_AFTER_SKIP
        if (skipped_player.waiting_time < 0):
            skipped_player.waiting_time = 0

        try:
            skipped_player.draw()
        except DeckEmptyError:
            pass

        n = skipped_player.waiting_time
        send_async(bot,
                   chat.id,
                   text="Ye khiladi {time} seconds ke"
                   "liye hilane gaya hain\n"
                   "Agla chu: {name}".format(time=n,
                                             name=display_name(
                                                 next_player.user)))
        logger.info(
            "{player} was skipped!. ".format(player=display_name(player.user)))
        game.turn()
        if job_queue:
            start_player_countdown(bot, game, job_queue)

    else:
        try:
            gm.leave_game(skipped_player.user, chat)
            send_async(bot,
                       chat.id,
                       text="{name1} ran out of time "
                       "and has been removed from the game!\n"
                       "Next player: {name2}".format(
                           name1=display_name(skipped_player.user),
                           name2=display_name(next_player.user)))
            logger.info("{player} was skipped!. ".format(
                player=display_name(player.user)))
            if job_queue:
                start_player_countdown(bot, game, job_queue)

        except NotEnoughPlayersError:
            send_async(bot,
                       chat.id,
                       text="{name} ran out of time "
                       "and has been removed from the game!\n"
                       "The game ended.".format(
                           name=display_name(skipped_player.user)))

            gm.end_game(chat, skipped_player.user)
Example #12
0
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)
Example #13
0
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)
Example #14
0
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')
Example #15
0
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')
Example #16
0
def status_update(bot, update):
    """Remove player from game if user leaves the group"""
    chat = update.message.chat

    if update.message.left_chat_member:
        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!", game.translate))
        else:
            send_async(bot, chat.id, text=__("Removing {name} from the game",
                                             game.translate)
                       .format(name=display_name(user)))
Example #17
0
def kill_game(bot, update):
    """Handler for the /kill command"""
    chat = update.message.chat
    user = update.message.from_user
    games = gm.chatid_games.get(chat.id)

    if update.message.chat.type == 'private':
        help_handler(bot, update)
        return

    if not games:
        send_async(bot, chat.id, text="Abe Bihari Game hi chalu nahi :|")
        return

    game = games[-1]

    if user_is_creator_or_admin(user, game, bot, chat):

        try:
            gm.end_game(chat, user)
            send_async(bot,
                       chat.id,
                       text=__("chalo hogaya aaj ka, back to bihar.",
                               multi=game.translate))

        except NoGameInChatError:
            send_async(
                bot,
                chat.id,
                text=_(
                    "Kardi na bihari wali baat "
                    "Join the game with /join and start the game with /start"),
                reply_to_message_id=update.message.message_id)

    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)
Example #18
0
def status_update(bot, update):
    """Remove player from game if user leaves the group"""
    chat = update.message.chat

    if update.message.left_chat_member:
        user = update.message.left_chat_member

        try:
            game = gm.player_for_user_in_chat(user, chat).game
            gm.leave_game(user, chat)

        except NoGameInChatError:
            pass
        except NotEnoughPlayersError:
            gm.end_game(chat, user)
            send_async(bot, chat.id, text=__("Game ended!",
                                             multi=game.translate))
        else:
            send_async(bot, chat.id, text=__("Removing {name} from the game",
                                             multi=game.translate)
                       .format(name=display_name(user)))
Example #19
0
def kick_player(bot, update):
    """Handler for the /kick command"""

    if update.message.chat.type == 'private':
        help_handler(bot, update)
        return

    chat = update.message.chat
    user = update.message.from_user

    try:
        game = gm.chatid_games[chat.id][-1]

    except (KeyError, IndexError):
        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)
        return

    if not game.started:
        send_async(
            bot,
            chat.id,
            text=_("The game is not started yet. "
                   "Join the game with /join and start the game with /start"),
            reply_to_message_id=update.message.message_id)
        return

    if user_is_creator_or_admin(user, game, bot, chat):

        if update.message.reply_to_message:
            kicked = update.message.reply_to_message.from_user

            try:
                gm.leave_game(kicked, chat)

            except NoGameInChatError:
                send_async(
                    bot,
                    chat.id,
                    text=_("Player {name} is not found in the current game.".
                           format(name=display_name(kicked))),
                    reply_to_message_id=update.message.message_id)
                return

            except NotEnoughPlayersError:
                gm.end_game(chat, user)
                send_async(bot,
                           chat.id,
                           text=_("{0} was kicked by {1}".format(
                               display_name(kicked), display_name(user))))
                send_async(bot,
                           chat.id,
                           text=__("Game ended!", multi=game.translate))
                return

            send_async(bot,
                       chat.id,
                       text=_("{0} was kicked by {1}".format(
                           display_name(kicked), display_name(user))))

        else:
            send_async(
                bot,
                chat.id,
                text=
                _("Please reply to the person you want to kick and type /kick again."
                  ),
                reply_to_message_id=update.message.message_id)
            return

        send_async(bot,
                   chat.id,
                   text=__("Okay. Next Player: {name}",
                           multi=game.translate).format(
                               name=display_name(game.current_player.user)),
                   reply_to_message_id=update.message.message_id)

    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)
Example #20
0
def do_skip(bot, player, job_queue=None):
    game = player.game
    chat = game.chat
    skipped_player = game.current_player
    next_player = game.current_player.next

    if skipped_player.waiting_time > 0:
        skipped_player.anti_cheat += 1
        skipped_player.waiting_time -= TIME_REMOVAL_AFTER_SKIP
        if (skipped_player.waiting_time < 0):
            skipped_player.waiting_time = 0

        # skip drawing if current player should choose color
        # which means current player has played a card
        try:
            if not game.choosing_color:
                skipped_player.draw()
        except DeckEmptyError:
            pass

        n = skipped_player.waiting_time
        send_async(bot,
                   chat.id,
                   text=__(
                       "Waiting time to skip this player has "
                       "been reduced to {time} seconds.\n"
                       "Next player: {name}",
                       multi=game.translate).format(time=n,
                                                    name=display_name(
                                                        next_player.user)))
        logger.info(
            "{player} was skipped!. ".format(player=display_name(player.user)))
        game.turn()

        # send message to indicate the game has randomly choosen the color
        if game._randomed_color:
            send_async(bot,
                       chat.id,
                       text=__("Color randomly choosen to: {col}",
                               multi=game.translate).format(
                                   col=c.COLOR_ICONS[game.last_card.color]))

        if job_queue:
            start_player_countdown(bot, game, job_queue)

    else:
        try:
            gm.leave_game(skipped_player.user, chat)
            send_async(bot,
                       chat.id,
                       text=__(
                           "{name1} ran out of time "
                           "and has been removed from the game!\n"
                           "Next player: {name2}",
                           multi=game.translate).format(
                               name1=display_name(skipped_player.user),
                               name2=display_name(next_player.user)))
            logger.info("{player} was skipped!. ".format(
                player=display_name(player.user)))
            if job_queue:
                start_player_countdown(bot, game, job_queue)

        except NotEnoughPlayersError:
            send_async(bot,
                       chat.id,
                       text=__(
                           "{name} ran out of time "
                           "and has been removed from the game!\n"
                           "The game ended.",
                           multi=game.translate).format(
                               name=display_name(skipped_player.user)))

            gm.end_game(chat, skipped_player.user)
Example #21
0
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)
Example #22
0
def skip_player(bot, update):
    """Handler for the /skip command"""
    chat = update.message.chat
    user = update.message.from_user

    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

    game = player.game
    skipped_player = game.current_player
    next_player = game.current_player.next

    started = skipped_player.turn_started
    now = datetime.now()
    delta = (now - started).seconds

    if delta < skipped_player.waiting_time:
        n = skipped_player.waiting_time - delta
        send_async(bot,
                   chat.id,
                   text=_("Please wait {time} second",
                          "Please wait {time} seconds", n).format(time=n),
                   reply_to_message_id=update.message.message_id)

    elif skipped_player.waiting_time > 0:
        skipped_player.anti_cheat += 1
        skipped_player.waiting_time -= 30
        try:
            skipped_player.draw()
        except DeckEmptyError:
            pass

        n = skipped_player.waiting_time
        send_async(bot,
                   chat.id,
                   text=__(
                       "Waiting time to skip this player has "
                       "been reduced to {time} second.\n"
                       "Next player: {name}",
                       "Waiting time to skip this player has "
                       "been reduced to {time} seconds.\n"
                       "Next player: {name}",
                       n,
                       multi=game.translate).format(time=n,
                                                    name=display_name(
                                                        next_player.user)))
        game.turn()

    else:
        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}",
                           multi=game.translate).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.",
                           multi=game.translate).format(
                               name=display_name(skipped_player.user)))

            gm.end_game(chat.id, skipped_player.user)
Example #23
0
def do_skip(bot, player, job_queue=None):
    game = player.game
    chat = game.chat
    skipped_player = game.current_player
    next_player = game.current_player.next

    if game.mode == "waffle":
        try:
            skipped_player.draw()
        except DeckEmptyError:
            pass

        send_async(bot,
                   chat.id,
                   text=__("{name1} ran out of time "
                           "and has been skipped.\n"
                           "Next player: {name2}").format(
                               name1=display_name(skipped_player.user),
                               name2=display_name(next_player.user)))
        logger.info(
            "{player} was skipped! ".format(player=display_name(player.user)))
        game.turn()
        if job_queue:
            start_player_countdown(bot, game, job_queue)

    elif skipped_player.waiting_time > 0:
        skipped_player.anti_cheat += 1
        skipped_player.waiting_time -= TIME_REMOVAL_AFTER_SKIP
        if (skipped_player.waiting_time < 0):
            skipped_player.waiting_time = 0

        try:
            skipped_player.draw()
        except DeckEmptyError:
            pass

        n = skipped_player.waiting_time
        send_async(bot,
                   chat.id,
                   text=__(
                       "Waiting time to skip this player has "
                       "been reduced to {time} seconds.\n"
                       "Next player: {name}",
                       multi=game.translate).format(time=n,
                                                    name=display_name(
                                                        next_player.user)))
        logger.info(
            "{player} was skipped! ".format(player=display_name(player.user)))
        game.turn()
        if job_queue:
            start_player_countdown(bot, game, job_queue)

    else:
        try:
            gm.leave_game(skipped_player.user, chat)
            send_async(bot,
                       chat.id,
                       text=__(
                           "{name1} ran out of time "
                           "and has been removed from the game!\n"
                           "Next player: {name2}",
                           multi=game.translate).format(
                               name1=display_name(skipped_player.user),
                               name2=display_name(next_player.user)))
            logger.info("{player} was skipped! ".format(
                player=display_name(player.user)))
            if job_queue:
                start_player_countdown(bot, game, job_queue)

        except NotEnoughPlayersError:
            send_async(bot,
                       chat.id,
                       text=__(
                           "{name} ran out of time "
                           "and has been removed from the game!\n"
                           "The game ended.",
                           multi=game.translate).format(
                               name=display_name(skipped_player.user)))

            gm.end_game(chat, skipped_player.user)
Example #24
0
def kick_player(bot, update):
    """Handler for the /kick command"""

    if update.message.chat.type == 'private':
        help_handler(bot, update)
        return

    chat = update.message.chat
    user = update.message.from_user

    try:
        game = gm.chatid_games[chat.id][-1]

    except (KeyError, IndexError):
        send_async(bot,
                   chat.id,
                   text=_("kardi jatin bihari pruthi wali baat "
                          "Naya game bana /new "),
                   reply_to_message_id=update.message.message_id)
        return

    if not game.started:
        send_async(
            bot,
            chat.id,
            text=_(
                "kardi bihari wali baat, /join karke start kar aise /start "),
            reply_to_message_id=update.message.message_id)
        return

    if user_is_creator_or_admin(user, game, bot, chat):

        if update.message.reply_to_message:
            kicked = update.message.reply_to_message.from_user

            try:
                gm.leave_game(kicked, chat)

            except NoGameInChatError:
                send_async(
                    bot,
                    chat.id,
                    text=_("Player {name} is not found in the current game.".
                           format(name=display_name(kicked))),
                    reply_to_message_id=update.message.message_id)
                return

            except NotEnoughPlayersError:
                gm.end_game(chat, user)
                send_async(bot,
                           chat.id,
                           text=_("{0} was kicked by {1}".format(
                               display_name(kicked), display_name(user))))
                send_async(bot,
                           chat.id,
                           text=__("chalo hogaya aaj ka, back to bihar.",
                                   multi=game.translate))
                return

            send_async(bot,
                       chat.id,
                       text="{1} ne {0} ko bihar bhej diya".format(
                           display_name(kicked), display_name(user)))

        else:
            send_async(
                bot,
                chat.id,
                text=
                _("Please reply to the person you want to kick and type /kick again."
                  ),
                reply_to_message_id=update.message.message_id)
            return

        send_async(bot,
                   chat.id,
                   text="Okay. Agla Bihari: {name}".format(
                       name=display_name(me.current_player.user)),
                   reply_to_message_id=update.message.message_id)

    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)
Example #25
0
def do_skip(bot, player, job_queue=None):
    game = player.game
    chat = game.chat
    skipped_player = game.current_player
    next_player = game.current_player.next

    if skipped_player.waiting_time > 0:
        skipped_player.anti_cheat += 1
        skipped_player.waiting_time -= TIME_REMOVAL_AFTER_SKIP
        if (skipped_player.waiting_time < 0):
            skipped_player.waiting_time = 0

        try:
            skipped_player.draw()
        except DeckEmptyError:
            pass

        n = skipped_player.waiting_time
        send_async(bot,
                   chat.id,
                   text=__(
                       "Ye khiladi {time} seconds ke"
                       "liye bihar gaya hain\n"
                       "Agla bihari: {name}",
                       multi=game.translate).format(time=n,
                                                    name=display_name(
                                                        next_player.user)))
        logger.info(
            "{player} was skipped! ".format(player=display_name(player.user)))
        game.turn()
        if job_queue:
            start_player_countdown(bot, game, job_queue)

    else:
        try:
            gm.leave_game(skipped_player.user, chat)
            send_async(bot,
                       chat.id,
                       text=__(
                           "{name1} ka bihar jaane ka waqt ho gaya "
                           "and usko pakad ke le gaye\n"
                           "Agla Bihari: {name2}",
                           multi=game.translate).format(
                               name1=display_name(skipped_player.user),
                               name2=display_name(next_player.user)))
            logger.info("{player} was skipped! ".format(
                player=display_name(player.user)))
            if job_queue:
                start_player_countdown(bot, game, job_queue)

        except NotEnoughPlayersError:
            send_async(bot,
                       chat.id,
                       text=__(
                           "{name} ka bihar jaane ka waqt ho gaya "
                           "and usko pakad ke le gaye\n"
                           "Game khatam.",
                           multi=game.translate).format(
                               name=display_name(skipped_player.user)))

            gm.end_game(chat, skipped_player.user)