def preview_model_params(self, params): """ params = {"node_type_code": "radar", "company": "mat","size": "large", "tech_balls": {"1": 10, "2": 5}}""" params['tech_balls'] = {int(key): val for key, val in params['tech_balls'].items()} model_params = calc_model_params(self, params) modif = {"small": "mult_small", "large": "mult_large", "medium": "1"}.get(params['size']) size_modifiers = self.db.fetchDict(f"""select parameter_code, {modif} modifier from model_has_parameters where node_code=:node_type_code""", params, 'parameter_code', 'modifier') # Применяем модификаторы размера for param, modifier in size_modifiers.items(): model_params[param] = roundTo(model_params[param] * modifier) techs = read_techs(self, params) model_params['level'] = get_model_level_by_technologies([ [techs.get(key, techs.get(int(key))).get('level'), value] for key, value in params['tech_balls'].items() ]) # Применяем модификаторы компании model = params model['params'] = model_params apply_companies_perks(model) model_params['weight'] = calc_weight(params['node_type_code'], params['size'], model_params['volume']) return model['params']
def flight_returned(self, params): """ params = { "flight_id": int, "flight_time": int, // время полета в секундах "az_damage": { "march_engine": 32.5, "warp_engine": 12.9, } / float }""" self.db.query("update flights set status='returned' where id = :flight_id", params, need_commit=True) # Находим все узлы того полета nodes = self.db.fetchDict( "select node_type_code, node_id from builds where flight_id=:flight_id", params, 'node_type_code', 'node_id') for node_type_code, node_id in nodes.items(): if node_type_code == 'hull': az_damage = roundTo(params['flight_time'] / (5 * 60)) else: if isinstance(params['az_damage'], dict): az_damage = roundTo( params['az_damage'].get(node_type_code, 0) + params['flight_time'] / (8 * 60)) else: az_damage = roundTo(params['az_damage'] + params['flight_time'] / (8 * 60)) self.db.query( f"update nodes set status='free', az_level = az_level - {az_damage} where id={node_id}", None, need_commit=True) logger.info("Полет {flight_id} вернулся".format(**params)) nodes_to_decomm = self.db.fetchColumn( "select id from nodes where az_level <= 15 and status='free'") for node_id in nodes_to_decomm: decomm_node(self, {"node_id": node_id, "is_auto": 1}) return api_ok(nodes_to_decomm=nodes_to_decomm)
def calc_weight(node_type: str, size: str, volume: float) -> float: densities = { "hull": 0.2, "march_engine": 0.5, "shunter": 0.5, "warp_engine": 0.7, "scaner": 0.4, "radar": 0.4, "shields": 0.6, "fuel_tank": 0.9, "lss": 0.6, } node_weight_multiplier = 1.2 hull_weight_add = {"small": 0, "medium": 900, "large": 3000} weight = volume * densities[node_type] return roundTo(weight + hull_weight_add[size] if node_type == 'hull' else weight * node_weight_multiplier)
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
def preview_model_params(self, params): """ params = {"node_type_code": "radar", "company": "mat","size": "large", "tech_balls": {"1": 10, "2": 5}}""" model_params = calc_model_params(None, params) modif = {"small": "mult_small", "large": "mult_large", "medium": "1"}.get(params['size']) size_modifiers = self.db.fetchDict(f"""select parameter_code, {modif} modifier from model_has_parameters where node_code=:node_type_code""", params, 'parameter_code', 'modifier') # Применяем модификаторы размера for param, modifier in size_modifiers.items(): model_params[param] = roundTo(model_params[param] * modifier) # Применяем модификаторы компании model = params model['params'] = model_params apply_companies_perks(model) model_params['weight'] = calc_weight(params['node_type_code'], params['size'], model_params['volume']) return model['params']
def read_models(self=None, params=None, read_nodes=True): """ params = {<name>: str, <node_type_code>: str/list[str], <level>: int/list[int], <size>: str/list[str], <company>: str/list[str], <node_id>: int} """ params = params or {} sql = "SELECT m.* from models m " if 'node_id' in params: sql += " join nodes n on n.model_id = m.id WHERE n.id = :node_id" else: add_where = self.db.construct_where(params) if add_where: sql += " where " + add_where params = self.db.construct_params(params) models = self.db.fetchAll(sql, params) if not models: return [] ids = {"model_id": [model['id'] for model in models]} params_where = self.db.construct_where(ids) ids = self.db.construct_params(ids) all_params = self.db.fetchAll( "select * from v_model_params where " + params_where, ids) if read_nodes: nodes_sql = """ select n.id, n.model_id, n.name, n.az_level, n.status, n.date_created, m.node_type_code, n.password, if ((n.password is not null and n.password!="") and ( n.premium_expires = 0 or n.premium_expires is null or n.premium_expires > Now() ), 1, 0) is_premium from nodes n join models m on m.id = n.model_id where """ + params_where if 'node_id' in params: nodes_sql += f" and n.id={int(params['node_id'])}" all_nodes = self.db.fetchAll(nodes_sql, ids, 'id') if all_nodes: hull_ids = [ id for id, node in all_nodes.items() if node['node_type_code'] == 'hull' ] if hull_ids: node_ids = str(tuple(hull_ids)).replace(",)", ")") hull_data_sql = f""" SELECT hp.hull_id, group_concat(concat(nt.name, ', ', lower(p.short_name), ': ', hp.value, '%') separator "<br>\n") perks, hs.slots_json FROM hull_perks hp JOIN node_types nt on hp.node_type_code = nt.code JOIN parameters_list p on hp.parameter_code = p.code JOIN hull_slots hs on hp.hull_id = hs.hull_id WHERE hp.hull_id in {node_ids} GROUP by hp.hull_id """ hull_data = self.db.fetchAll(hull_data_sql, {}, 'hull_id') for hull_id, data in hull_data.items(): all_nodes[hull_id]['perks'] = data['perks'] all_nodes[hull_id]['slots'] = json.loads( data['slots_json']) for model in models: model['params'] = { item['parameter_code']: item['value'] for item in all_params if item['model_id'] == model['id'] } model = apply_companies_perks(model) model['params']['weight'] = calc_weight(model['node_type_code'], model['size'], model['params']['volume']) model['params'] = { key: roundTo(val) for key, val in model['params'].items() } if read_nodes: model['nodes'] = [ node for node in all_nodes.values() if node['model_id'] == model['id'] ] return models