def start_playing_league(bot, update, league):
    try:
        logger = _get_logger()
        # Validaciones de argumentos
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        league["__$STATE"] = "PLAYING"
        league["partidos"] = []
        html = """<!DOCTYPE html><html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 400x;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}.header {background-color: #dddddd;}.nameColumn {width: 75px;}.pointColumn {width: 60px;}</style></head><body><h2>Partidos</h2><table><tr><td class='nameColumn header'>Jugador A</td><td class='nameColumn header'>Jugador B</td><td class='pointColumn header'>Golas A</td><td class='pointColumn header'>Goles B</td><td class='pointColumn header'>PJ</td></tr>"""
        for game in list(itertools.combinations(league["players"], 2)):
            tmp = {"games": 0}
            tmp[game[0]] = 0
            tmp[game[1]] = 0
            html += """<tr><td class='nameColumn'>{JUGA}</td><td class='nameColumn'>{JUGB}</td><td class='pointColumn'>{GA}</td><td class='pointColumn'>{GB}</td><td class='pointColumn'>{PJ}</td></tr>""".format(
                JUGA=game[0], JUGB=game[1], GA="0", GB="0", PJ="0")
            league["partidos"].append(tmp)
        html += """</table></body></html>"""
        file_name = str(uuid.uuid4()) + ".png"
        path_wkthmltopdf = WKHTMLTOIMAGE_PATH
        config = imgkit.config(wkhtmltoimage=path_wkthmltopdf)
        options = {'format': 'png', 'encoding': "UTF-8", 'crop-w': '435'}
        imgkit.from_string(html, file_name, options=options, config=config)
        file = open(file_name, 'rb')
        bot.send_photo(chat_id=update.message.chat_id, photo=file, timeout=60)
        file.close()
        os.remove(file_name)
        update_doc(LEAGUES_COLLECTION, {"__$STATE": "JOINING"}, league)
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def _help(bot, update):
    try:
        logger = _get_logger()
        _bot_history("_help", update, None)
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        bot.send_message(chat_id=update.message.chat_id,
                         text="""Lista de comandos:
/nuevojugador JUGADOR - Permite agregar un jugador nuevo
/listajugadores - Muestra una lista de jugadores
/ranking - Muestra el puntaje de los jugadores
/estadisticasjugador JUGADOR - Permite ver las estadisticas de un jugador
/partidosjugador JUGADOR - Permite ver los partidos de un jugador
/submit JUGADOR # JUGADOR # - Permite ingresar un nuevo partido
/submitliga JUGADOR # JUGADOR # - Permite ingresar un nuevo partido de liga
/link JUGADOR - Permite vincularse a un jugador actual
/joinliga - Permite unirse a la liga que se esta formando actualmente
/startliga - Permite crear una liga nueva si no hay ninguna en transito o empezar la liga sise esta configurando
/rankingliga - Muestra el ranking actual de la liga
/partidosliga - Muestra los partidos de la liga
/help - Si no entendes un carajo""",
                         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 submit_league(bot, update, args):
    try:
        logger = _get_logger()
        # Validaciones de argumentos
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        if not submit_result_goals(bot, update, args):
            return
        player_a = args[0]
        player_b = args[2]
        goals_a = int(args[1])
        goals_b = int(args[3])
        league = find_one(LEAGUES_COLLECTION, {"__$STATE": "PLAYING"})
        if not league:
            bot.send_message(chat_id=update.message.chat_id,
                             text='No hay ninguna liga en curso')
            return
        if not _submit_league_game(bot, update, league, player_a, player_b,
                                   goals_a, goals_b):
            return
        bot.send_message(chat_id=update.message.chat_id,
                         text='Partido de liga cargado con exito')
        _validate_end_league(bot, update, league)
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def join_league(bot, update):
    try:
        logger = _get_logger()
        # Validaciones de argumentos
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        liga = find_one(LEAGUES_COLLECTION, {"__$STATE": "JOINING"})
        if not liga:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Mi no entender")
            return
        player = find_one(PLAYERS_COLLECTION,
                          {"__$tel_id": update.message.from_user.id})
        if not player:
            bot.send_message(
                chat_id=update.message.chat_id,
                text=
                'No se encontro jugador, recuerde que debe linkear su usuario a un jugador'
            )
            return
        if str(player["__$name"]).lower() in liga["players"]:
            bot.send_message(chat_id=update.message.chat_id,
                             text='El jugador ya se encuentra en la liga')
            return
        liga["players"].append(player["__$name"].lower())
        update_doc(LEAGUES_COLLECTION, {"__$STATE": "JOINING"}, liga)
        bot.send_message(chat_id=liga["__$grupo"],
                         text="@" +
                         str(player["__$name"] + " Exito al unirse a la liga"))
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def player_info(bot, update, args):
    try:
        logger = _get_logger()
        _bot_history("player_info", update, args)
        # Validaciones de argumentos
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        if not args:
            args = [str(update.message.from_user.id)]
        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:]
                }, {
                    "__$tel_id": int(args[0])
                }]
            })
        if not player:
            bot.send_message(chat_id=update.message.chat_id,
                             text="El jugador no existe")
            return
        message = "Partidos de " + str(args[0]) + "\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]) + "\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 unknown(bot, update):
    try:
        logger = _get_logger()
        _bot_history("unknown", update, None)
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        # if str(update.message.from_user.id) != "528527409":
        #     bot.send_message(chat_id=update.message.chat_id, text="Mi no entender")
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def start_league(bot, update):
    try:
        logger = _get_logger()
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        league = find_one(LEAGUES_COLLECTION, {"__$STATE": {"$ne": "END"}})
        if league:
            league = find_one(LEAGUES_COLLECTION, {"__$STATE": "JOINING"})
            if not league:
                bot.send_message(chat_id=update.message.chat_id,
                                 text="Ya hay una liga en progreso")
                return
            start_playing_league(bot, update, league)
            bot.send_message(chat_id=update.message.chat_id,
                             text="Inicio la liga: " +
                             league["config"]["nombre_liga"])
            bot.send_message(
                chat_id=update.message.chat_id,
                text="Lista de jugadores:\n" +
                "\n".join([player for player in league["players"]]))
            return
        insert_one(
            LEAGUES_COLLECTION, {
                "__$STATE": "CONFIG",
                "__$DATE": datetime.today(),
                "__$organizador": {
                    "id": update.message.from_user.id,
                    "name": update.message.from_user.username
                },
                "config": {
                    "cant_partidos": 1
                },
                "__$grupo": update.message.chat_id,
                "players": []
            })
        bot.send_message(chat_id=update.message.chat_id,
                         text="La liga esta siendo configurada por: @" +
                         str(update.message.from_user.username))
        custom_keyboard = [['Solo ida', 'Ida y vuelta']]
        reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard)
        bot.send_message(chat_id=update.message.from_user.id,
                         text="Cantidad de cruces:",
                         reply_markup=reply_markup)
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def link(bot, update, args):
    try:
        logger = _get_logger()
        _bot_history("set_elo", update, args)
        # Validaciones de argumentos
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        if not args or len(args) != 1:
            bot.send_message(
                chat_id=update.message.chat_id,
                text=
                "Por favor, ingrese el nombre del jugador al que desea vincular su usuario"
            )
            return

        player = find_one(PLAYERS_COLLECTION,
                          {"__$tel_id": update.message.from_user.id})
        if player:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Ya estas vinculado a otro jugador")
            return
        player = find_one(
            PLAYERS_COLLECTION,
            {"__$name": re.compile("^" + args[0] + "$", re.IGNORECASE)})
        if not player:
            bot.send_message(chat_id=update.message.chat_id,
                             text="No se encontro al jugador")
            return
        if "__$link" in player:
            bot.send_message(chat_id=update.message.chat_id,
                             text="El jugador ya se encuentra vinculado")
            return
        user_id = update.message.from_user.id
        user_name = update.message.from_user.username
        update_doc(PLAYERS_COLLECTION,
                   {"__$name": re.compile("^" + args[0] + "$", re.IGNORECASE)},
                   {"$set": {
                       "__$tel_id": user_id,
                       "__$tel_name": user_name
                   }})
        bot.send_message(chat_id=update.message.chat_id,
                         text="Exito al vincular jugador")
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def add_player(bot, update, args):
    try:
        logger = _get_logger()
        _bot_history("add_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
        elif "__$" in args[0]:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Nombre invalido, no se acepta el simbolo $")
            return
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        player = find_one(
            PLAYERS_COLLECTION,
            {"__$name": re.compile("^" + args[0] + "$", re.IGNORECASE)})
        if not player:
            insert_one(PLAYERS_COLLECTION, {
                "__$name": args[0],
                "__$elo": 1800,
                "__$history": []
            })
            insert_one(WEEKLY, {
                "__$name": args[0],
                "__$elo": 1800,
                "__$history": []
            })
        else:
            bot.send_message(chat_id=update.message.chat_id,
                             text="El jugador ya existe")
            return
        bot.send_message(chat_id=update.message.chat_id,
                         text="Jugador agregado con exito")
    except Exception as ex:
        logger.exception(ex)
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        return
def players_list(bot, update):
    try:
        logger = _get_logger()
        _bot_history("players_list", update, None)
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        players = find(PLAYERS_COLLECTION, {})
        message = "Jugadores ( {} ):\n".format(players.count())
        for player in players:
            message = message + player["__$name"] + "\n"
        bot.send_message(chat_id=update.message.chat_id, text=message)
    except Exception as ex:
        logger.exception(ex)
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        return
def league_games(bot, update):
    try:
        logger = _get_logger()
        # Validaciones de argumentos
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        league = find_one(LEAGUES_COLLECTION, {"__$STATE": "PLAYING"})
        if not league:
            bot.send_message(chat_id=update.message.chat_id,
                             text='No hay ninguna liga en curso')
            return
        _render_league_games(bot, update, league)
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def get_elo_weekly(bot, update):
    try:
        logger = _get_logger()
        _bot_history("get_elo", update, None)
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        players = find(WEEKLY, {}, sort="-__$elo")
        html = "<!DOCTYPE html><html><head><style>table {font-family: arial, sans-serif;border-collapse: collapse;width: 500px;}td, th {border: 1px solid #dddddd;text-align: left;padding: 8px;}.header {background-color: #dddddd;}.nameColumn {width: 250px;}.pointColumn {width: 50px;}</style></head><body><h2>Ranking SEMANAL</h2><table><tr><td class='nameColumn header'>Nombre</td><td class='pointColumn header'>PJ</td><td class='pointColumn header'>PG</td><td class='pointColumn header'>%G</td><td class='pointColumn header'>DG</td><td class='pointColumn header'>Puntos</td></tr>"
        for player in players:
            average_goals, average_percent_games, games_played = _get_average_stats(
                player, percent=False)
            if int(games_played) == 0:
                percent = "-"
            else:
                percent = str(
                    round(
                        100 * float(
                            float(average_percent_games) /
                            float(games_played)), 1)) + "%"
            html = html + "<tr><td class='nameColumn'>{NOMBRE}</td><td class='pointColumn'>{PARTJU}</td><td class='pointColumn'>{PARTGA}</td><td class='pointColumn'>{PERCENT}</td><td class='pointColumn'>{GOLES}</td><td class='pointColumn'>{PUNTOS}</td></tr>".format(
                NOMBRE=player["__$name"],
                PARTJU=games_played,
                PARTGA=average_percent_games,
                PERCENT=str(percent),
                GOLES=average_goals,
                PUNTOS=str(int(player["__$elo"])))
        html = html + "</table></body></html>"
        file_name = str(uuid.uuid4()) + ".png"
        path_wkthmltopdf = WKHTMLTOIMAGE_PATH
        config = imgkit.config(wkhtmltoimage=path_wkthmltopdf)
        options = {'format': 'png', 'encoding': "UTF-8", 'crop-w': '515'}
        imgkit.from_string(html, file_name, options=options, config=config)
        file = open(file_name, 'rb')
        bot.send_photo(chat_id=update.message.chat_id, photo=file, timeout=60)
        file.close()
        os.remove(file_name)
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return
def submit_result_goals(bot, update, args):
    try:
        logger = _get_logger()
        _bot_history("submit_result_goals", update, args)
        # Validaciones de argumentos
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return False
        if not args:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Por favor, cargue algun dato")
            return False
        elif len(args) != 4:
            bot.send_message(
                chat_id=update.message.chat_id,
                text="La estructura del comando es: JUGADOR1 # JUGADOR2 #")
            return False
        try:
            int(args[1])
            int(args[3])
        except:
            bot.send_message(chat_id=update.message.chat_id,
                             text="Debe cargar goles numericos")
            return False

        if "&" in args[0] and "&" in args[2]:
            submit_doble(bot, update, args)
        elif "&" not in args[0] and "&" not in args[2]:
            submit_simple(bot, update, args)
        else:
            bot.send_message(
                chat_id=update.message.chat_id,
                text=
                "No se pueden cargar partidos de parejas contra un solo jugador"
            )
            return

        if (int(args[1]) == 8
                and int(args[3]) == 0) or (int(args[1]) == 0
                                           and int(args[3]) == 8):
            bot.sendDocument(chat_id=update.message.chat_id,
                             document=open(
                                 "/var/sources/metegolBotTelegram/8a0.gif",
                                 "rb"),
                             timeout=120,
                             reply_to_message_id=update.message.message_id)
        if (int(args[1]) == 7
                and int(args[3]) == 0) or (int(args[1]) == 0
                                           and int(args[3]) == 7):
            bot.sendDocument(chat_id=update.message.chat_id,
                             document=open(
                                 "/var/sources/metegolBotTelegram/7a0.gif",
                                 "rb"),
                             timeout=120,
                             reply_to_message_id=update.message.message_id)
        if (int(args[1]) == 7
                and int(args[3]) == 1) or (int(args[1]) == 1
                                           and int(args[3]) == 7):
            bot.sendDocument(chat_id=update.message.chat_id,
                             document=open(
                                 "/var/sources/metegolBotTelegram/7a1.gif",
                                 "rb"),
                             timeout=120,
                             reply_to_message_id=update.message.message_id)
        return True
    except Exception as ex:
        bot.send_message(chat_id=update.message.chat_id, text=str(ex))
        logger.exception(ex)
        return False
def player_statics(bot, update, args):
    try:
        logger = _get_logger()
        _bot_history("player_statics", update, args)
        # Validaciones de argumentos
        if not _authenticate(update):
            bot.send_message(chat_id=update.message.chat_id,
                             text="Grupo invalido")
            return
        if not args:
            args = [str(update.message.from_user.id)]
        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

        query = {
            "$or": [{
                "__$name": re.compile("^" + args[0] + "$", re.IGNORECASE)
            }, {
                "__$tel_name": args[0][1:]
            }]
        }
        try:
            query["$or"].append({"__$tel_id": int(args[0])})
        except:
            pass
        player = find_one(PLAYERS_COLLECTION, query)
        if not player:
            bot.send_message(chat_id=update.message.chat_id,
                             text="El jugador no existe")
            return
        message = "Estadisticas de " + str(player["__$name"]) + "\n"
        games_dict = {}
        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]
                    if enemy not in games_dict:
                        games_dict[enemy] = {"own": 0, "enemy": 0}
                    games_dict[enemy]["own"] = games_dict[enemy]["own"] + int(
                        game["__$own"] > game[enemy])
                    games_dict[enemy]["enemy"] = games_dict[enemy][
                        "enemy"] + int(game[enemy] > game["__$own"])
                else:
                    logger.error(game)
        for key in games_dict:
            percent = 100 * (
                float(games_dict[key]["own"]) /
                float(games_dict[key]["own"] + games_dict[key]["enemy"]))
            message = message + str(player["__$name"]) + ": " + str(
                games_dict[key]["own"]) + " | " + str(key) + ": " + str(
                    games_dict[key]["enemy"]) + " - (" + str(round(percent,
                                                                   1)) + "%)\n"

        average_goals, average_percent_games, games_played = _get_average_stats(
            player, percent=True)
        message = message + "Promedio diferencia de goles: " + str(
            average_goals) + "\n"
        message = message + "Promedio de partidos ganados: " + str(
            average_percent_games) + "%\n"
        message = message + "Partidos jugados: " + str(games_played) + "\n"
        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