effectconverter.py (7996B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\dogma\effects\effectconverter.py 2 import contextlib 3 import yaml 4 import dogma.const as dgmconst 5 6 def get_domain_idx_from_value(expressionValue): 7 if expressionValue == 'Ship': 8 return 'shipID' 9 if expressionValue == 'Target': 10 return 'targetID' 11 if expressionValue == 'Char': 12 return 'charID' 13 if expressionValue == 'Self': 14 return 'itemID' 15 if expressionValue == 'Other': 16 return 'otherID' 17 raise RuntimeError('Could not decipher %s' % expressionValue) 18 19 20 EXPRESSIONS = set() 21 22 class ModifierExpression(object): 23 24 def __init__(self, row, modifier_type, attributes, generator): 25 self.__modifier_type__ = modifier_type 26 self._generator = generator 27 self._row = row 28 self._yaml_expression = {} 29 self._get_modified_info() 30 self._attributes = attributes 31 self.update_funcs_by_attribute = {'domain': self._update_domain, 32 'skillTypeID': self._update_skill_type_id, 33 'groupID': self._update_group_id} 34 35 def _get_modified_info(self): 36 self._modified_info = self._generator.get_db_expression_as_yaml_expression(self._row.arg1) 37 38 def get_expression(self): 39 self._update_expression() 40 return self._yaml_expression 41 42 def _update_expression(self): 43 self._update_func() 44 self._update_operator() 45 self._update_modified_attribute() 46 self._update_modifying_attribute() 47 for attribute in self._attributes: 48 self.update_funcs_by_attribute[attribute]() 49 50 def _update_func(self): 51 self._yaml_expression['func'] = self.__modifier_type__ 52 53 def _update_modifying_attribute(self): 54 self._yaml_expression['modifyingAttributeID'] = self._generator.get_db_expression_as_yaml_expression(self._row.arg2)['attribute'] 55 56 def _update_modified_attribute(self): 57 self._yaml_expression['modifiedAttributeID'] = self._modified_info['attribute'] 58 59 def _update_domain(self): 60 self._yaml_expression['domain'] = self._modified_info['domain'] 61 62 def _update_operator(self): 63 self._yaml_expression['operator'] = self._get_operator() 64 65 def _get_operator(self): 66 try: 67 op = getattr(dgmconst, 'dgmAss%s' % self._modified_info['operator']) 68 except: 69 op = int(self._modified_info['operator']) 70 71 return op 72 73 def _update_skill_type_id(self): 74 self._yaml_expression['skillTypeID'] = self._modified_info['skillTypeID'] 75 76 def _update_group_id(self): 77 self._yaml_expression['groupID'] = self._modified_info['groupID'] 78 79 80 class YamlExpressionGenerator: 81 82 def __init__(self, get_expression_row): 83 self._get_expression_row = get_expression_row 84 85 def get_db_expression_as_yaml_expression(self, expression_id): 86 global EXPRESSIONS 87 EXPRESSIONS.add(expression_id) 88 expression_row = self._get_expression_row(expression_id) 89 operandID = expression_row.operandID 90 try: 91 expression = self.get_expression_by_operand_id(expression_row) 92 except KeyError: 93 if operandID == dgmconst.operandCOMBINE: 94 return self.get_db_expression_as_yaml_expression(expression_row.arg1) + self.get_db_expression_as_yaml_expression(expression_row.arg2) 95 if operandID == dgmconst.operandDEFENVIDX: 96 return {'domain': get_domain_idx_from_value(expression_row.expressionValue)} 97 if operandID == dgmconst.operandDEFATTRIBUTE: 98 return {'attribute': expression_row.expressionAttributeID} 99 if operandID == dgmconst.operandDEFASSOCIATION: 100 return {'operator': expression_row.expressionValue} 101 if operandID == dgmconst.operandDEFTYPEID: 102 return {'skillTypeID': expression_row.expressionTypeID} 103 if operandID == dgmconst.operandDEFGROUP: 104 group_id = expression_row.expressionGroupID 105 if group_id is None: 106 raise InvalidExpression() 107 return {'groupID': group_id} 108 if operandID == dgmconst.operandGETTYPE: 109 raise InvalidExpression() 110 else: 111 yaml_expression = {} 112 for arg in (expression_row.arg1, expression_row.arg2): 113 if arg is not None: 114 yaml_expression.update(self.get_db_expression_as_yaml_expression(arg)) 115 116 return yaml_expression 117 else: 118 return [expression.get_expression()] 119 120 def get_expression_by_operand_id(self, expression_row): 121 operand_id = expression_row.operandID 122 modifier_name, attributes = MODIFIER_INFO_BY_OPERAND[operand_id] 123 return ModifierExpression(expression_row, modifier_name, attributes, self) 124 125 126 def get_db_expression_as_yaml_expression(expression_id): 127 return YamlExpressionGenerator(get_expression_row_from_db).get_db_expression_as_yaml_expression(expression_id) 128 129 130 class InvalidExpression(Exception): 131 pass 132 133 134 MODIFIER_INFO_BY_OPERAND = {dgmconst.operandALRSM: ('LocationRequiredSkillModifier', ('domain', 'skillTypeID')), 135 dgmconst.operandAORSM: ('OwnerRequiredSkillModifier', ('domain', 'skillTypeID')), 136 dgmconst.operandAIM: ('ItemModifier', ('domain',)), 137 dgmconst.operandALGM: ('LocationGroupModifier', ('domain', 'groupID')), 138 dgmconst.operandALM: ('LocationModifier', ('domain',))} 139 EXPRESSIONS_BY_ID = None 140 141 def get_expression_row_from_db(expression_id): 142 global EXPRESSIONS_BY_ID 143 if EXPRESSIONS_BY_ID is None: 144 with db_connection() as connection: 145 expressions = connection.execute('select * from dogma.expressions').fetchall() 146 EXPRESSIONS_BY_ID = {r.expressionID:r for r in expressions} 147 return EXPRESSIONS_BY_ID[expression_id] 148 149 150 @contextlib.contextmanager 151 def db_connection(): 152 import pyodbc 153 sqlConnection = pyodbc.connect('DRIVER={SQL Server};SERVER=sqldev1is;DATABASE=local_eve_audunn;Trusted_Connection=yes') 154 sqlConnection.autocommit = True 155 cursor = sqlConnection.cursor() 156 try: 157 yield cursor 158 finally: 159 sqlConnection.close() 160 161 162 def get_data_for_conversion(path = None): 163 modifier_info_by_effect = {} 164 effect_ids = [4, 165 901, 166 263, 167 9, 168 10, 169 4491, 170 4492, 171 3855, 172 17, 173 18, 174 6165, 175 4689, 176 26, 177 2971, 178 4489, 179 34, 180 27, 181 6692, 182 4773, 183 38, 184 5163, 185 1199, 186 4656, 187 3380, 188 649, 189 569, 190 4490, 191 650, 192 1086, 193 4928, 194 67, 195 2757, 196 6753, 197 4936, 198 3852, 199 1738, 200 6690, 201 4686, 202 2255, 203 592, 204 3793, 205 5275, 206 4942, 207 4921, 208 4687, 209 6693, 210 4688, 211 6715, 212 2726, 213 102, 214 103, 215 104, 216 1129, 217 2413, 218 4728, 219 127] 220 with db_connection() as connection: 221 for effect in connection.execute('select effectID, preExpression, modifierInfo from dogma.effects').fetchall(): 222 if effect.modifierInfo or effect.effectID not in effect_ids: 223 continue 224 pre_expression = effect.preExpression 225 expression = get_expression_row_from_db(pre_expression) 226 if expression.operandID in MODIFIER_INFO_BY_OPERAND or expression.operandID == dgmconst.operandCOMBINE: 227 try: 228 modifier_info_by_effect[effect.effectID] = get_db_expression_as_yaml_expression(pre_expression) 229 except InvalidExpression: 230 continue 231 except Exception: 232 raise RuntimeError('Failed on effect %s' % effect.effectID) 233 234 if path is None: 235 with open('modifier_data33.yaml', 'w') as f: 236 yaml.dump(modifier_info_by_effect, f, default_flow_style=False) 237 else: 238 with open(path, 'w') as f: 239 yaml.dump(modifier_info_by_effect, f, default_flow_style=False) 240 241 242 if __name__ == '__main__': 243 get_data_for_conversion()