class EffectManager(IEffectManager): def __init__(self): self.DAO = EffectDAO() def create(self, effect: Effect, nodeParentId: int = None, contextType: ObjectType = None) -> int: return self.DAO.create(effect, nodeParentId, contextType) def update(self, effect: Effect) -> None: return self.DAO.update(effect) def delete(self, effectId: int) -> None: return self.DAO.delete(effectId) def get(self, effectId: int, lang: str = None, nodeId: int = None, contextType: ObjectType = None) -> Effect: return self.DAO.get(effectId, lang, nodeId, contextType) def get_all(self, lang: str) -> list: return self.DAO.get_all(lang) def update_effect(self, effect: Effect): return self.DAO.update(effect) def create_empty(self, lang: str) -> Effect: character = Effect(None, lang) id = self.DAO.create(character) character.id = id return character
def __set_table_data(self, treeNode) -> None: """ Set data to table, in table should be list of Modifiers :param treeNode: Tree node where ability is located, need for find Modifiers """ self.table.setColumnCount(5) heades = [ TR().tr('Name'), TR().tr('Target_type'), TR().tr('Target_attribute_type'), TR().tr('Value_type'), TR().tr('Value') ] self.table.setHorizontalHeaderLabels(heades) header = self.table.horizontalHeader() header.setStretchLastSection(True) # print(treeNode) effect = EffectDAO().get(self.object.id, self.object.lang, treeNode.id, treeNode.context) self.table.setRowCount(len(effect.modifiers)) for i, modifier in enumerate(effect.modifiers): self.table.setItem(i, 0, QtWidgets.QTableWidgetItem(modifier.name)) self.table.setItem( i, 1, QtWidgets.QTableWidgetItem(TR().tr( (modifier.targetType)))) if modifier.itemTargetAttribute is not None: self.table.setItem( i, 2, QtWidgets.QTableWidgetItem(TR().tr( (modifier.itemTargetAttribute)))) else: self.table.setItem( i, 2, QtWidgets.QTableWidgetItem(TR().tr( (modifier.characterTargetAttribute)))) if modifier.itemTargetAttribute in ( ItemsAttributes.WEAPON_MELEE_HANDLING, ItemsAttributes.WEAPON_WEIGHT): self.table.setItem( i, 4, QtWidgets.QTableWidgetItem(TR().tr((modifier.valueType)))) else: self.table.setItem( i, 3, QtWidgets.QTableWidgetItem(TR().tr((modifier.valueType)))) self.table.setItem( i, 4, QtWidgets.QTableWidgetItem(str(modifier.value)))
def create(self, scenario: Scenario, nodeParentId: int = None, contextType: ObjectType = None) -> int: """ Create new scenario :param scenario: Scenario object :param nodeParentId: id of parent node in tree :param contextType: Object type of tree, where item is located :return: id of created Scenario """ if not contextType: contextType = self.TYPE if isinstance(scenario.date, dd.date): curDate = scenario.date else: curDate = datetime.strptime(scenario.date, '%d/%m/%Y') if scenario.date else None intValues = {'date': curDate.toordinal() if curDate else None} strValues = { 'name': scenario.name, 'description': scenario.description } id = self.database.insert(self.DATABASE_TABLE, intValues) scenario.id = id self.database.insert_translate(strValues, scenario.lang, id, self.TYPE) # Create node for tree structure node = NodeObject(None, scenario.name, nodeParentId, scenario) nodeId = self.treeDAO.insert_node(node, contextType) for location in scenario.locations: LocationDAO().create(location, nodeId, contextType) for spell in scenario.spells: SpellDAO().create(spell, nodeId, contextType) for ability in scenario.abilities: AbilityDAO().create(ability, nodeId, contextType) for effect in scenario.effects: EffectDAO().create(effect, nodeId, contextType) for character in scenario.party: PartyCharacterDAO().create(character, nodeId, contextType) return id
def create_node_link(self, nodeType: NodeType, name: str, parentId: int, parentType: ObjectType = None, targetObject: object = None): """ Create node in tree, that link to some object, that already exist :param nodeType: Type of node (Folder, NodeObject) :param name: name of node :param parentId: id of parent object :param parentType: :param targetObject: target object :return: node """ if nodeType is NodeType.FOLDER: node = Folder(None, name, parentId) self.treeDAO.insert_node(node, parentType) else: if targetObject.object_type is ObjectType.MODIFIER: parentObjectId = self.treeDAO.get_node(parentId).object.id parentObject = parentType.instance().DAO()().get( parentObjectId) EffectDAO().create_link(parentObject, targetObject) elif targetObject.object_type is ObjectType.EFFECT and parentType is ObjectType.ITEM: parentObjectId = self.treeDAO.get_node(parentId).object.id parentObject = parentType.instance().DAO()().get( parentObjectId) ItemDAO().create_effect_link(parentObject, targetObject) elif targetObject.object_type is ObjectType.ABILITY_CONTEXT: parentObjectId = self.treeDAO.get_node(parentId).object.id parentObject = parentType.instance().DAO()().get( parentObjectId) AbilityDAO().create_context_link(parentObject, targetObject) else: node = NodeObject(None, name, parentId, targetObject) self.treeDAO.insert_node(node, parentType)
def test_effect(self): """ Test effect DAO :return: """ DAO = EffectDAO() effect = Effect(None, 'cs', 'Magic defence', 'Magic defence for armor', ModifierTargetTypes.ARMOR, True) effect2 = Effect(None, 'cs', 'Magic attack', 'Magic attack for sword', ModifierTargetTypes.MELEE_WEAPON, False) id = DAO.create(effect) id2 = DAO.create(effect2) getObject = DAO.get(id) getObject2 = DAO.get(id2) self.assertEqual(effect, getObject, 'First effect is not equal') self.assertEqual(effect2, getObject2, 'Second effect is not equal') self.assertNotEqual(getObject, getObject2, 'First and second effect is equal') effect.name = 'Updated magic defence' DAO.update(effect) getObject = DAO.get(id) self.assertEqual(getObject, effect) DAO.delete(id) deleted = DAO.get(id) self.assertIsNone(deleted)
def create(self, item: Item, nodeParentId: int = None, contextType: ObjectType = None) -> int: """ Create new Item, depend on item type, insert correct data Items types: Item, Armor, Container, MeleeWeapon, ThrowableWeapon, RangedWeapon, Money :param item: Item object :param nodeParentId: id of parent node in tree :param contextType: Object type of tree, where item is located :return: id of created item """ if not contextType: contextType = self.TYPE strValues = { 'name' : item.name, 'description': item.description } intValues = { 'amount': item.amount, 'price' : item.price, 'type' : item.type.value if item.type else None, 'weight': item.weight } if isinstance(item, Armor): intValues.update({ 'quality': item.quality, 'weightA': item.weightA, 'weightB': item.weightB, 'weightC': item.weightC, 'size' : item.size.value if item.size else None, }) elif isinstance(item, Container): intValues.update({ 'capacity' : item.capacity, 'parent_id': item.parent_id }) elif isinstance(item, MeleeWeapon): intValues.update({ 'strength' : item.strength, 'rampancy' : item.rampancy, 'defence' : item.defence, 'length' : item.length, 'initiative' : item.initiative, 'handling' : item.handling.value if item.handling else None, 'weaponWeight': item.weaponWeight.value if item.weaponWeight else None, 'racial' : item.racial.value if item.racial else None }) elif isinstance(item, Money): intValues.update({ 'copper': item.copper, 'silver': item.silver, 'gold' : item.gold }) elif isinstance(item, RangeWeapon): intValues.update({ 'initiative' : item.initiative, 'strength' : item.strength, 'rampancy' : item.rampancy, 'rangeLow' : item.rangeLow, 'rangeMedium' : item.rangeMedium, 'rangeHigh' : item.rangeHigh, 'weaponWeight': item.weaponWeight.value if item.weaponWeight else None, 'racial' : item.racial.value if item.racial else None }) elif isinstance(item, ThrowableWeapon): intValues.update({ 'initiative' : item.initiative, 'strength' : item.strength, 'rampancy' : item.rampancy, 'rangeLow' : item.rangeLow, 'rangeMedium' : item.rangeMedium, 'rangeHigh' : item.rangeHigh, 'defence' : item.defence, 'weaponWeight': item.weaponWeight.value if item.weaponWeight else None, 'racial' : item.racial.value if item.racial else None }) id = self.database.insert(self.DATABASE_TABLE, intValues) item.id = id self.database.insert_translate(strValues, item.lang, id, self.TYPE) # Create node for tree structure node = NodeObject(None, item.name, nodeParentId, item) nodeId = self.treeDAO.insert_node(node, contextType) for effect in item.effects: EffectDAO().create(effect, nodeId, contextType) if isinstance(item, Container): for one in item.containers + item.armors + item.moneyList + item.meleeWeapons + item.rangedWeapons + item.throwableWeapons + item.items: self.create(one, nodeId, contextType) return id
def get(self, item_id: int, lang=None, nodeId: int = None, contextType: ObjectType = None) -> Item: """ Get Item , object transable attributes depends on lang If nodeId and contextType is specified, whole object is returned (with all sub objects) If not specified, only basic attributes are set. Returned correct type of item, depend on database, possible classes are: Item, Container, Armor, Money, MeleeWeapon, RangedWeapon, ThrowableWeapon :param item_id: id of Item :param lang: lang of object :param nodeId: id of node in tree, where object is located :param contextType: object type of tree, where is node :return: Item object """ if lang is None: lang = SettingsDAO().get_value('language', str) data = self.database.select(self.DATABASE_TABLE, {'ID': item_id}) if not data: return None else: data = dict(data[0]) tr_data = dict(self.database.select_translate(item_id, ObjectType.ITEM.value, lang)) if data['type'] == Items.CONTAINER.value: item = Container(item_id, lang, tr_data.get('name', ''), tr_data.get('description', ''), data.get('parent_id', 0), data.get('weight', 0), data.get('price', 0), data.get('capacity', 0), data.get('amount', 1)) elif data['type'] == Items.MELEE_WEAPON.value: weaponWeightIndex = data.get('weaponWeight', None) weaponWeight = WeaponWeight(weaponWeightIndex) if weaponWeightIndex else None handlingIndex = data.get('handling', None) handling = Handling(handlingIndex) if handlingIndex else None racialIndex = data.get('racial', None) racial = Races(racialIndex) if racialIndex else None item = MeleeWeapon(item_id, lang, tr_data.get('name', ''), tr_data.get('description', ''), data.get('parent_id', 0), data.get('weight', 0), data.get('price', 0), data.get('strength', 0), data.get('rampancy', 0), data.get('defence', 0), data.get('length', 0), weaponWeight, handling, data.get('amount', 1), data.get('initiative', 0), racial) elif data['type'] == Items.THROWABLE_WEAPON.value: weaponWeightIndex = data.get('weaponWeight', None) weaponWeight = WeaponWeight(weaponWeightIndex) if weaponWeightIndex else None racialIndex = data.get('racial', None) racial = Races(racialIndex) if racialIndex else None item = ThrowableWeapon(item_id, lang, tr_data.get('name', ''), tr_data.get('description', ''), data.get('parent_id', 0), data.get('weight', 0), data.get('price', 0), data.get('initiative', 0), data.get('strength', 0), data.get('rampancy', 0), data.get('rangeLow', 0), data.get('rangeMedium', 0), data.get('rangeHigh', 0), data.get('defence', 0), weaponWeight, data.get('amount', 1), racial) elif data['type'] == Items.RANGED_WEAPON.value: weaponWeightIndex = data.get('weaponWeight', None) weaponWeight = WeaponWeight(weaponWeightIndex) if weaponWeightIndex else None racialIndex = data.get('racial', None) racial = Races(racialIndex) if racialIndex else None item = RangeWeapon(item_id, lang, tr_data.get('name', ''), tr_data.get('description', ''), data.get('parent_id', 0), data.get('weight', 0), data.get('price', 0), data.get('initiative', 0), data.get('strength', 0), data.get('rampancy', 0), data.get('rangeLow', 0), data.get('rangeMedium', 0), data.get('rangeHigh', 0), data.get('amount', 1), weaponWeight, racial) elif data['type'] == Items.ARMOR.value: sizeIndex = data.get('size', None) size = ArmorSize(sizeIndex) if sizeIndex else None item = Armor(item_id, lang, tr_data.get('name', ''), tr_data.get('description', ''), data.get('parent_id', 0), data.get('price', 0), data.get('quality', 0), data.get('weightA', 0), data.get('weightB', 0), data.get('weightC', 0), size, data.get('amount', 1)) elif data['type'] == Items.MONEY.value: item = Money(item_id, lang, tr_data.get('name', ''), tr_data.get('description', ''), data.get('parent_id', 0), data.get('copper'), data.get('silver'), data.get('gold'), data.get('amount', 1)) else: item = Item(item_id, lang, tr_data.get('name', ''), tr_data.get('description', ''), data.get('parent_id', 0), data.get('weight', 0), data.get('price', 0), data.get('amount', 1)) if nodeId and contextType: objects = PlayerTreeDAO().get_children_objects(nodeId, contextType) effects = [] armors = [] containers = [] items = [] moneys = [] meleeWeapons = [] rangedWeapons = [] throwableWeapons = [] for one in objects: if one.object.object_type is ObjectType.ITEM and data['type'] == Items.CONTAINER.value: childItem = self.get(one.object.id, None, one.id, contextType) if isinstance(childItem, Armor): armors.append(childItem) elif isinstance(childItem, Container): containers.append(childItem) elif isinstance(childItem, Money): moneys.append(childItem) elif isinstance(childItem, MeleeWeapon): meleeWeapons.append(childItem) elif isinstance(childItem, RangeWeapon): rangedWeapons.append(childItem) elif isinstance(childItem, ThrowableWeapon): throwableWeapons.append(childItem) else: items.append(childItem) elif one.object.object_type is ObjectType.EFFECT: effect = EffectDAO().get(one.object.id, None, one.id, contextType) effects.append(effect) if data['type'] is Items.CONTAINER.value: item.items = items item.armors = armors item.moneyList = moneys item.meleeWeapons = meleeWeapons item.rangedWeapons = rangedWeapons item.throwableWeapons = throwableWeapons item.containers = containers item.effects = effects return item
def get(self, scenario_id: int, lang: str = None, nodeId: int = None, contextType: ObjectType = None) -> Scenario: """ Get Scenario , object transable attributes depends on lang If nodeId and contextType is specified, whole object is returned (with all sub objects) If not specified, only basic attributes are set. :param scenario_id: id of Scenario :param lang: lang of object :param nodeId: id of node in tree, where object is located :param contextType: object type of tree, where is node :return: Scenario object """ if lang is None: lang = SettingsDAO().get_value('language', str) data = self.database.select(self.DATABASE_TABLE, {'ID': scenario_id}) if not data: return None else: data = dict(data[0]) tr_data = dict( self.database.select_translate(scenario_id, self.TYPE.value, lang)) scenarioDate = date.fromordinal( data.get('date')) if data.get('date') else None scenario = Scenario(data.get('ID'), lang, tr_data.get('name', ''), tr_data.get('description', ''), scenarioDate) if nodeId and contextType: children = self.treeDAO.get_children_objects(nodeId, contextType) abilities = [] spells = [] effects = [] locations = [] partyCharacters = [] for child in children: if child.object.object_type is ObjectType.ABILITY: ability = AbilityDAO().get(child.object.id, None, child.id, contextType) abilities.append(ability) elif child.object.object_type is ObjectType.SPELL: spell = SpellDAO().get(child.object.id, None, child.id, contextType) spells.append(spell) elif child.object.object_type is ObjectType.EFFECT: effect = EffectDAO().get(child.object.id, None, child.id, contextType) effects.append(effect) elif child.object.object_type is ObjectType.LOCATION: location = LocationDAO().get(child.object.id, None, child.id, contextType) locations.append(location) elif child.object.object_type is ObjectType.CHARACTER: character = CharacterDAO().get(child.object.id, None, child.id, contextType) partyCharacter = PartyCharacterDAO().get(character.id) if not partyCharacter: partyCharacter = PartyCharacter() partyCharacter.character = character partyCharacters.append(partyCharacter) # Search non connected party character chars = self.database.select('PartyCharacter', {'scenario_id': scenario_id}) for char in chars: if char['character_id'] is None: partyCharacters.append(PartyCharacterDAO().get_by_id( char['ID'])) scenario.spells = spells scenario.abilities = abilities scenario.effects = effects scenario.locations = locations scenario.party = partyCharacters return scenario
def __init__(self): self.DAO = EffectDAO()
def create(self, character: Character, nodeParentId: int = None, contextType: ObjectType = None) -> int: """ Create new character :param character: Character object :param nodeParentId: id of parent node in tree :param contextType: Object type of tree, where item is located :return: id of created character """ if contextType is None: contextType = self.TYPE intValues = { 'agility' : character.agility, 'charisma' : character.charisma, 'intelligence' : character.intelligence, 'mobility' : character.mobility, 'strength' : character.strength, 'toughness' : character.toughness, 'age' : character.age, 'height' : character.height, 'weight' : character.weight, 'level' : character.level, 'xp' : character.xp, 'maxHealth' : character.maxHealth, 'maxMana' : character.maxMana, 'currentHealth': character.currentHealth, 'currentMana' : character.currentMana, 'drdClass' : character.drdClass.value if character.drdClass else None, 'drdRace' : character.drdRace.value if character.drdRace else None, 'alignment' : character.alignment.value if character.alignment else None, } strValues = { 'name' : character.name, 'description': character.description } id = self.database.insert(self.DATABASE_TABLE, intValues) character.id = id self.database.insert_translate(strValues, character.lang, id, self.TYPE) # Create node for tree structure node = NodeObject(None, character.name, nodeParentId, character) nodeId = self.treeDAO.insert_node(node, contextType) for spell in character.spells: SpellDAO().create(spell, nodeId, contextType) for ability in character.abilities: AbilityDAO().create(ability, nodeId, contextType) for effect in character.effects: EffectDAO().create(effect, nodeId, contextType) if character.inventory is None: c = Container(None, None, TR().tr('Inventory'), None, -1) inventoryId = ItemDAO().create(c, nodeId, contextType) else: character.inventory.parent_id = -1 inventoryId = ItemDAO().create(character.inventory, nodeId, contextType) if character.ground is None: c = Container(None, None, TR().tr('Ground'), None, -2) groundId = ItemDAO().create(c, nodeId, contextType) else: character.ground.parent_id = -2 groundId = ItemDAO().create(character.ground, nodeId, contextType) self.database.update(self.DATABASE_TABLE, id, {'inventoryId': inventoryId, 'groundId': groundId}) return id
def get(self, character_id: int, lang: str = None, nodeId: int = None, contextType: ObjectType = None) -> Character: """ Get Character , object transable attributes depends on lang If nodeId and contextType is specified, whole object is returned (with all sub objects) If not specified, only basic attributes are set. :param character_id: id of Character :param lang: lang of object :param nodeId: id of node in tree, where object is located :param contextType: object type of tree, where is node :return: Character object """ if lang is None: lang = SettingsDAO().get_value('language', str) data = self.database.select(self.DATABASE_TABLE, {'ID': character_id}) if not data: return None else: data = dict(data[0]) tr_data = self.database.select_translate(character_id, ObjectType.CHARACTER.value, lang) drdClass = Classes(data.get('drdClass')) if data.get('drdClass') is not None else None drdRace = Races(data.get('drdRace')) if data.get('drdRace') is not None else None alignment = Alignment(data.get('alignment')) if data.get('alignment') is not None else None character = Character(data.get('ID'), lang, tr_data.get('name', ''), tr_data.get('description', ''), data.get('agility', 0), data.get('charisma', 0), data.get('intelligence', 0), data.get('mobility', 0), data.get('strength', 0), data.get('toughness', 0), data.get('age', 0), data.get('height', 0), data.get('weight', 0), data.get('level', 0), data.get('xp', 0), data.get('maxHealth', 0), data.get('maxMana', 0), drdClass, drdRace, alignment, data.get('currentHealth', 0), data.get('currentMana', 0)) if nodeId and contextType: children = self.treeDAO.get_children_objects(nodeId, contextType) abilities = [] spells = [] effects = [] for child in children: if child.object.object_type is ObjectType.SPELL: spell = SpellDAO().get(child.object.id, None, child.id, contextType) spells.append(spell) elif child.object.object_type is ObjectType.ABILITY: ability = AbilityDAO().get(child.object.id, None, child.id, contextType) abilities.append(ability) elif child.object.object_type is ObjectType.EFFECT: effect = EffectDAO().get(child.object.id, None, child.id, contextType) effects.append(effect) elif child.object.object_type is ObjectType.ITEM and child.object.type is Items.CONTAINER and child.object.parent_id == -1: inventory = ItemDAO().get(child.object.id, None, child.id, contextType) character.inventory = inventory elif child.object.object_type is ObjectType.ITEM and child.object.type is Items.CONTAINER and child.object.parent_id == -2: ground = ItemDAO().get(child.object.id, None, child.id, contextType) character.ground = ground character.spells = spells character.abilities = abilities character.effects = effects return character