def remove_player(bot, update, args):
    try:
        logger = _get_logger()
        _bot_history("remove_player", update, args)
        # Validaciones de argumentos
        if not args:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Por favor, agregar nombre del jugador")
            return
        elif len(args) != 1:
            bot.send_message(
                chat_id=update.message.chat_id,
                text="Por favor, el nombre del jugador no puede tener espacios"
            )
            return
        if not _authenticate_admin(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Requiere autorizacion de un administrador")
            return

        query = {}
        query[args[0]] = {"$exists": True}
        remove_by_query(PLAYERS_COLLECTION, {"__$name": args[0]})
        remove_by_query(WEEKLY, {"__$name": args[0]})
        bot.send_message(chat_id=update.message.chat_id,
                         text="Jugador eliminado con exito")
    except Exception as ex:
        logger.exception(ex)
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        return
def recalculate_elo(bot, update):
    try:
        logger = _get_logger()
        if not _authenticate_admin(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Requiere autorizacion de un administrador")
            return
        players = _recalculate_points()
        for key in players:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Actualizando " + str(key),
                             timeout=60)
            update_doc(PLAYERS_COLLECTION, {"__$name": key},
                       {"$set": {
                           "__$elo": int(players[key])
                       }})
            bot.send_message(chat_id=update.message.chat_id,
                             text=str(key) + " actualizado con exito",
                             timeout=60)
        bot.send_message(chat_id=update.message.chat_id,
                         text="Puntos actualizados",
                         timeout=60)
    except Exception as ex:
        logger.exception(ex)
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        return
def admin_player_info(bot, update, args):
    try:
        logger = _get_logger()
        _bot_history("admin_player_info", update, args)
        # Validaciones de argumentos
        if not _authenticate_admin(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Requiere autorizacion de un administrador")
            return
        if not args:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Por favor, agregar nombre del jugador")
            return
        elif len(args) != 1:
            bot.send_message(
                chat_id=update.message.chat_id,
                text="Por favor, el nombre del jugador no puede tener espacios"
            )
            return

        player = find_one(
            PLAYERS_COLLECTION, {
                "$or": [{
                    "__$name":
                    re.compile("^" + args[0] + "$", re.IGNORECASE)
                }, {
                    "__$tel_name": args[0][1:]
                }]
            })
        if not player:
            bot.send_message(chat_id=update.message.chat_id,
                             text="El jugador no existe")
            return
        message = "Partidos de " + str(player["__$name"]) + "\n"
        for game in player["__$history"]:
            if "type" not in game:
                enemy = [
                    player for player in list(game.keys())
                    if "__$" not in player
                ]
                if len(enemy) == 1:
                    enemy = enemy[0]
                    message = message + "- " + str(
                        player["__$name"]) + ": " + str(
                            game["__$own"]) + " | " + str(enemy) + ": " + str(
                                game[enemy]
                            ) + " / " + game["__$game_id"] + "\n"
                else:
                    logger.error(game)
        bot.send_message(chat_id=update.message.chat_id, text=message)
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def set_elo(bot, update, args):
    try:
        logger = _get_logger()
        _bot_history("admin_remove_game", update, args)
        # Validaciones de argumentos
        if not _authenticate_admin(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Requiere autorizacion de un administrador")
            return
        if not args:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Por favor, agregar nombre del jugador")
            return
        if len(args) != 1:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Por favor, solo un ID")
            return

        query = {"__$history.__$game_id": args[0]}
        players = find(PLAYERS_COLLECTION, query)
        if players.count() == 0:
            bot.send_message(chat_id=update.message.chat_id,
                             text="No se encontro el ID")
            return
        for player in players:
            new_data = player
            partida = [
                game for game in player["__$history"]
                if game["__$game_id"] == args[0]
            ][0]
            new_data["__$history"] = [
                game for game in player["__$history"]
                if game["__$game_id"] != args[0]
            ]
            partida[player["__$name"]] = partida.pop("__$own")
            partida.pop("__$date")
            partida.pop("__$game_id")
            partida = str(list(partida.keys())[0]) + ": " + str(
                partida[list(partida.keys())[0]]) + " | " + str(
                    list(partida.keys())[1]) + ": " + str(partida[list(
                        partida.keys())[1]])
            update_doc(PLAYERS_COLLECTION, {"__$name": player["__$name"]},
                       new_data)
        bot.send_message(chat_id=update.message.chat_id,
                         text="Exito al borrar la partida:\n" + str(partida))
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def _help_admin(bot, update):
    try:
        logger = _get_logger()
        if not _authenticate_admin(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Requiere autorizacion de un administrador")
            return
        bot.send_message(chat_id=update.message.chat_id,
                         text="""Lista de comandos:
/removegame GAME - `Elimina una partida`
/eliminarjugador JUGADOR - `Elimina un jugador`
/partidosjugadoradmin JUGADOR - `Muestra los partidos con ID`""",
                         parse_mode=ParseMode.MARKDOWN)
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def end_league(bot, update):
    try:
        logger = _get_logger()
        if not _authenticate_admin(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Requiere autorizacion de un administrador")
            return
        league = find_one(LEAGUES_COLLECTION, {"__$STATE": "PLAYING"})
        if not league:
            bot.send_message(chat_id=update.message.chat_id,
                             text="No se està jugando ninguna liga")
            return
        _validate_end_league(bot, update, league, ignore_games=True)
        bot.send_message(chat_id=update.message.chat_id, text="Liga terminada")
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def agregar_a_liga(bot, update, args):
    try:
        logger = _get_logger()
        if not _authenticate_admin(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Requiere autorizacion de un administrador")
            return
        if not args or len(args) != 1:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Carga solo un jugador")
            return
        league = find_one(LEAGUES_COLLECTION, {"__$STATE": "PLAYING"})
        if not league:
            bot.send_message(chat_id=update.message.chat_id,
                             text="No se està jugando ninguna liga")
            return
        player = find_one(PLAYERS_COLLECTION, {"__$name": args[0]})
        if not player:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Jugador no existe")
            return

        for p in league["players"]:
            tmp = {}
            tmp["games"] = 0
            tmp[player["__$name"]] = 0
            tmp[p] = 0
            league["partidos"].append(tmp)

        league["players"].append(player["__$name"])

        update_doc(LEAGUES_COLLECTION, {"__$STATE": "PLAYING"}, league)
        bot.send_message(chat_id=update.message.chat_id,
                         text="Jugador agregado con exito a la liga!")
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return