def progress_neighbors(maximum_total, extra, progress):
    neighbor_count = len([
        ally for ally in allies.values()
        if ally.get("friend") and ally.get("neighbor")
    ])
    total = min(neighbor_count, int(maximum_total))
    extra["total"] = total
    return total != progress
def prepopulate_task(task):
    if task["_action"] == 'countPlaced':
        item = lookup_item_by_code(task["_item"])
        if 'stateMachineValues' in item:
            state_machine = lookup_state_machine(item['stateMachineValues']['-stateMachineName'],
                                                 item['stateMachineValues'].get('define', []))
        else:
            state_machine = None
        objects = lookup_objects_by_item_name(item['-name'])
        built_objects = [e for e in objects if
                 int(e.get('state', 0)) >= (int(state_machine['-builtState']) if state_machine else 0)]
        number_built = len(built_objects)
        return min(number_built, int(task["_total"])), number_built >= int(task["_total"])
    if task["_action"] == 'countPlacements':
        item = lookup_item_by_code(task.get("_item", task.get('_code')))
        objects = lookup_objects_by_item_name(item['-name'])
        number_placed = len(objects)
        return min(number_placed, int(task["_total"])), number_placed >= int(task["_total"])
    elif task["_action"] == 'inventoryCount':
        item_inventory = session['user_object']["userInfo"]["player"]["inventory"]["items"]
        return min(item_inventory.get(task["_item"], 0), int(task["_total"])), item_inventory.get(task["_item"], 0) >= int(task["_total"])
    elif task["_action"] == 'population':
        return min(session['population'], int(task["_total"])), session['population'] >= int(task["_total"])
    elif task["_action"] == 'neighborsAdded':
        neighbor_count = len([ally for ally in allies.values() if ally.get("friend") and ally.get("neighbor")])
        return min(neighbor_count, int(task["_total"])), neighbor_count >= int(task["_total"])
    elif task["_action"] == 'countUpgrades':
        research = session['user_object']["userInfo"]["world"]["research"]
        total = 0
        unit = task["unit"]
        for k, v in research.items():
            if "_item" in unit:
                if k == unit["_item"]:
                    total += len(v)
            elif "_unitClass" in unit:
                item = lookup_item_by_code(k)
                if item["-unitClass"] == unit["_unitClass"]:
                    total += len(v)
            elif "_subtype" in unit:
                item = lookup_item_by_code(k)
                if item["-subtype"] == unit["_subtype"]:
                    total += len(v)

        return min(total, int(task["_total"])), total >= int(task["_total"])
    elif task["_action"] == 'autoComplete':
        return 1, True
    else:
        return 0, False