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 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 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 _recalculate_points(): games = [] jugadores = {} players = find(PLAYERS_COLLECTION,{}) for player in players: for game in player["__$history"]: tmp = dict() if "type" not in game: enemy = _get_enemy(game) tmp[enemy] = game[enemy] if enemy not in list(jugadores.keys()): jugadores[enemy] = 1800 if player["__$name"] not in list(jugadores.keys()): jugadores[player["__$name"]] = 1800 tmp[player["__$name"]] = game["__$own"] tmp["__$time"] = game["__$date"] tmp["__$gameid"] = game["__$game_id"] tmp["type"] = "normal" tmp["names"] = [enemy,player["__$name"]] if game["__$game_id"] not in [g["__$gameid"] for g in games if "__$gameid" in g]: games.append(tmp) elif game["type"] == "liga": tmp["type"] = "liga" tmp["__$name"] = player["__$name"] tmp["__$time"] = game["__$date"] tmp["players"] = game["players"] tmp["points"] = game["points"] games.append(tmp) games = sorted(games,key=lambda k: k["__$time"]) for game in games: if game["type"] == "normal": play = game["names"] jugadores[play[0]], jugadores[play[1]] = _calculate_elo(jugadores[play[0]], jugadores[play[1]], game[play[0]], game[play[1]]) elif game["type"] == "liga": jugadores[game["__$name"]] = jugadores[game["__$name"]] + (game["points"]*game["players"]) return jugadores