Beispiel #1
0
def create_node(self, params):
    """ params: {model_id: int, password: str} """
    model_id = params.get('model_id', 0)
    if not model_id:
        raise Exception("Не указан model_id!")
    model_id_dict = {"id": model_id}
    model = read_models(None, model_id_dict, read_nodes=False)

    if len(model) < 0:
        raise Exception(f"No model with id {model_id}")
    model = model[0]
    existing_nodes = self.db.fetchOne(
        'select count(*) from nodes where model_id=:id', model_id_dict)
    insert_data = {
        "model_id": model_id,
        "name": "",
        "az_level": model['params']['az_level'],
        "password": params.get('password', ''),
        'premium_expires':
        None if not existing_nodes else model['premium_expires']
    }
    node_id = self.db.insert('nodes', insert_data)
    add_node_upkeep_pump(None, node_id=node_id, model=model)
    if model['node_type_code'] != 'hull':
        result = self.db.fetchRow('select * from nodes where id=:id',
                                  {"id": node_id})
        return result
    else:
        return create_hull(self, model, node_id=node_id)
Beispiel #2
0
def reserve_node(self, params):
    """ params = {"user_id": int, "node_id": int, "password": str} """
    build_item = check_reserve_node(None, params)
    # build_item =  {"flight_id":int, "node_id": int}
    if 'errors' in build_item:
        return build_item
    already_reserved = self.db.fetchRow(
        """
    select m.node_type_code, b.node_id
from nodes n
join models m on n.model_id = m.id
left join builds b on b.node_type_code = m.node_type_code and b.flight_id=:flight_id
where n.id=:node_id""", build_item)
    build_item['node_type_code'] = already_reserved['node_type_code']
    if already_reserved.get('node_id'):
        # Убираем ранее зарезервированный нод
        self.db.query('update nodes set status_code="free" where id=:node_id',
                      already_reserved)
        self.db.query(
            'delete from builds where flight_id=:flight_id and node_type_code=:node_type_code',
            build_item)
    if build_item['node_type_code'] != 'hull':
        # рассчитываем вектора
        build_item['vector'] = build_item['total'] = get_node_vector(
            None, params)
        build_item['correction'] = "0" * 16
        params_json = get_node_params_with_desync(vector=build_item['vector'],
                                                  node_id=params['node_id'])

        build_item['params_json'] = json.dumps(params_json)

    else:
        model = read_models(None, {"node_id": build_item['node_id']})[0]
        hull_params = {
            "weight": model['params']['weight'],
            "volume": model['params']['volume'],
            "az_level": model['nodes'][0]['az_level']
        }
        build_item['params_json'] = json.dumps({
            key: {
                "percent": 100,
                "value": value
            }
            for key, value in hull_params.items()
        })
    self.db.insert('builds', build_item)
    self.db.query("""update nodes 
        set status_code="reserved", 
            connected_to_hull_id = null 
        where id=:node_id""",
                  build_item,
                  need_commit=True)
    return {"status": "ok"}
Beispiel #3
0
def calc_node_params_with_desync(vector,
                                 params=None,
                                 node_id=None,
                                 node_type_code=None):
    if not params or not node_type_code:
        model = read_models(None, {"node_id": node_id}, read_nodes=False)[0]
        params = model['params']
        node_type_code = model['node_type_code']
    desync_percents = get_desync_percent(vector, node_type_code)
    for param, val in params.items():
        percent = desync_percents.get(param, 100)
        params[param] = {
            "percent": percent,
            "value": roundTo(val * percent / 100)
        }
    return params
Beispiel #4
0
def create_node(self, params):
    """ params: {model_id: int, password: str} """
    model_id = params.get('model_id', 0)
    if not model_id:
        raise Exception("Не указан model_id!")
    model_id_dict = {"id": model_id}
    model = read_models(self, model_id_dict, read_nodes=False)

    if len(model) < 0:
        raise Exception(f"No model with id {model_id}")
    model = model[0]

    insufficient = get_insufficient_for_node(company=model['company'],
                                             model_id=model_id)
    if insufficient:
        return api_fail(
            "Для создания узла вам не хватает следующих ресурсов: " +
            dict2str(insufficient))

    existing_nodes = self.db.fetchOne(
        'select count(*) from nodes where model_id=:id', model_id_dict)
    insert_data = {
        "model_id": model_id,
        "name": "",
        "az_level": model['params']['az_level'],
        "password": params.get('password', ''),
        'premium_expires':
        None if not existing_nodes else model['premium_expires']
    }
    node_id = self.db.insert('nodes', insert_data)
    add_node_upkeep_pump(self, node_id=node_id, model=model)
    if model['node_type_code'] != 'hull':
        result = self.db.fetchRow('select * from nodes where id=:id',
                                  {"id": node_id})
        return result
    else:
        return create_hull(self, model, node_id=node_id)