Beispiel #1
0
def format_map_vote(rcon, format_type="line", short_names=True):
    selection = get_current_selection()
    if not selection:
        return ""
    human_map = SHORT_HUMAN_MAP_NAMES if short_names else LONG_HUMAN_MAP_NAMES
    human_map_mod = NO_MOD_SHORT_HUMAN_MAP_NAMES if short_names else NO_MOD_LONG_HUMAN_MAP_NAMES
    vote_dict = numbered_maps(selection)
    maps_to_numbers = dict(zip(vote_dict.values(), vote_dict.keys()))
    items = [f"[{k}] {human_map.get(v, v)}"  for k, v in vote_dict.items()]
    if format_type == "line":
        return ' '.join(items)
    if format_type == "max_length":
        return format_by_line_length(items)
    if format_type == 'vertical':
        return '\n'.join(items)
    if format_type.startswith('by_mod'):
        categorized = categorize_maps(selection)
        off = join_vote_options('  ', categorized['offensive'], human_map_mod, maps_to_numbers)
        warfare = join_vote_options('  ', categorized['warfare'], human_map_mod, maps_to_numbers)
        if format_type == 'by_mod_line':
            return "OFFENSIVE: {} WARFARE: {}".format(off, warfare)
        if format_type == 'by_mod_vertical':
            return "OFFENSIVE:\n{}\nWARFARE:\n{}".format(off, warfare)
        if format_type == 'by_mod_split':
            return "OFFENSIVE: {}\nWARFARE: {}".format(off, warfare)
        if format_type == 'by_mod_vertical_all':
            return "OFFENSIVE:\n{}\nWARFARE:\n{}".format(
                join_vote_options('\n', categorized['offensive'], human_map_mod, maps_to_numbers),
                join_vote_options('\n', categorized['warfare'], human_map_mod, maps_to_numbers)
            )
Beispiel #2
0
def suggest_next_maps(
    maps_history,
    all_maps,
    selection_size=6,
    exclude_last_n=4,
    offsensive_ratio=0.5,
    consider_offensive_as_same_map=True,
    allow_consecutive_offensive=True,
    allow_consecutive_offensives_of_opposite_side=False,
    current_map=None,
):
    last_n_map = set(m["name"] for m in maps_history[:exclude_last_n])
    logger.info("Excluding last %s player maps: %s", exclude_last_n,
                last_n_map)
    remaining_maps = set(all_maps) - last_n_map
    logger.info("Remaining maps to suggest from: %s", remaining_maps)

    try:
        current_map = current_map or maps_history[0]["name"]
    except (IndexError, KeyError):
        logger.exception("Unable to get current map for generating selection")
        raise

    current_side = get_map_side(current_map)

    if consider_offensive_as_same_map:
        last_names = set(map_name(m) for m in last_n_map)
        logger.info("Considering offensive mode as same map, excluding %s",
                    last_names)
        remaining_maps = set(m for m in remaining_maps
                             if not map_name(m) in last_names)
        logger.info("Remaining maps to suggest from: %s", remaining_maps)

    if not allow_consecutive_offensives_of_opposite_side and current_side:
        opposite_side = "us" if current_side == "ger" else "ger"
        logger.info(
            "Not allowing consecutive offensive with opposite side: %s",
            opposite_side)
        remaining_maps = [
            m for m in remaining_maps if get_map_side(m) != opposite_side
        ]
        logger.info("Remaining maps to suggest from: %s", remaining_maps)

    # Handle case if all maps got excluded
    categorized_maps = categorize_maps(remaining_maps)
    if offsensive_ratio == 0:
        nb_offensive = 0
    else:
        nb_offensive = round(offsensive_ratio * selection_size)
    warfares = []

    offensives = _get_random_map_selection(categorized_maps["offensive"],
                                           nb_offensive)

    if not allow_consecutive_offensive and "offensive" in current_map:
        logger.info(
            "Current map %s is offensive. Excluding all offsensives from suggestions",
            current_map,
        )
        offensives = []

    warfares = _get_random_map_selection(categorized_maps["warfare"],
                                         selection_size - len(offensives))
    selection = offensives + warfares

    if not selection:
        logger.error("No maps can be suggested with the given parameters.")
        raise ValueError("Unable to suggest map")
    logger.info("Suggestion %s", selection)
    return selection