def __set_table_data(self, treeNode: NodeObject) -> None: """ Set data to table, in table should be list of ability Contexts :param treeNode: Tree node where ability is located, need for find ability contexts """ self.table.setColumnCount(4) heades = [ TR().tr('Name'), 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) ability = AbilityDAO().get(self.object.id, self.object.lang, treeNode.id, treeNode.context) self.table.setRowCount(len(ability.contexts)) for i, context in enumerate(ability.contexts): self.table.setItem(i, 0, QtWidgets.QTableWidgetItem(context.name)) self.table.setItem( i, 1, QtWidgets.QTableWidgetItem(TR().tr((context.targetAttribute)))) self.table.setItem( i, 2, QtWidgets.QTableWidgetItem(TR().tr((context.valueType)))) self.table.setItem(i, 3, QtWidgets.QTableWidgetItem(str(context.value)))
class AbilityManager(IAbilityManager): def __init__(self): self.DAO = AbilityDAO() def create(self, ability: Ability, nodeParentId: int = None, contextType: ObjectType = None) -> int: return self.DAO.create(ability, nodeParentId, contextType) def update_ability(self, ability: Ability): self.DAO.update(ability) def delete(self, abilityId: int): return self.DAO.delete(abilityId) def get(self, abilityId: int, lang=None, nodeId: int = None, contextType: ObjectType = None) -> Ability: return self.DAO.get(abilityId, lang, nodeId, contextType) def get_all(self, lang: str) -> list: return self.DAO.get_all(lang) def create_empty(self, lang) -> Ability: ability = Ability(None, lang) id = self.DAO.create(ability) ability.id = id return ability
def create(self, monster: Monster, nodeParentId: int = None, contextType: ObjectType = None) -> int: """ Create new Monster :param monster: Modifier object :param nodeParentId: id of parent node in tree :param contextType: Object type of tree, where item is located :return: id of created monster """ if not contextType: contextType = self.TYPE intValues = { 'defense' : monster.defense, 'endurance' : monster.endurance, 'rampancy' : monster.rampancy, 'mobility' : monster.mobility, 'perseverance': monster.perseverance, 'intelligence': monster.intelligence, 'charisma' : monster.charisma, 'experience' : monster.experience, 'hp' : monster.hp, 'alignment' : monster.alignment.value if monster.alignment else None, 'monsterRace' : monster.monsterRace.value if monster.monsterRace else None, 'size' : monster.size.value if monster.size else None, } strValues = { 'name' : monster.name, 'description': monster.description, 'offense' : monster.offense, 'viability' : monster.viability, } id = self.database.insert(self.DATABASE_TABLE, intValues) monster.id = id self.database.insert_translate(strValues, monster.lang, id, self.TYPE) # Create node for tree structure node = NodeObject(None, monster.name, nodeParentId, monster) nodeId = self.treeDAO.insert_node(node, contextType) for one in monster.containers + monster.armors + monster.moneyList + monster.meleeWeapons + monster.rangedWeapons + monster.throwableWeapons + monster.items: ItemDAO().create(one, nodeId, contextType) for spell in monster.spells: SpellDAO().create(spell, nodeId, contextType) for ability in monster.abilities: AbilityDAO().create(ability, nodeId, contextType) return id
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 get(self, monster_id: int, lang: str = None, nodeId: int = None, contextType: ObjectType = None) -> Monster: """ Get Monster , 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 monster_id: id of Monster :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: Monster object """ if lang is None: lang = SettingsDAO().get_value('language', str) data = self.database.select(self.DATABASE_TABLE, {'ID': monster_id}) if not data: return None else: data = dict(data[0]) tr_data = self.database.select_translate(monster_id, ObjectType.MONSTER.value, lang) monsterRace = MonsterRace(data['monsterRace']) if data['monsterRace'] else None monsterSize = MonsterSize(data['size']) if data['size'] else None alignment = Alignment(data['alignment']) if data['alignment'] else None monster = Monster(data['ID'], lang, tr_data.get('name', ''), tr_data.get('description', ''), tr_data.get('viability', 0), tr_data.get('offense', ''), data.get('defense', 0), data.get('endurance', 0), data.get('rampancy', 0), data.get('mobility', 0), data.get('perseverance', 0), data.get('intelligence', 0), data.get('charisma', 0), alignment, data.get('experience', 0), data.get('hp', 0), monsterRace, monsterSize) if nodeId and contextType: children = self.treeDAO.get_children_objects(nodeId, contextType) abilities = [] spells = [] items = [] armors = [] moneys = [] containers = [] meleeWeapons = [] rangedWeapons = [] throwableWeapons = [] 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.ITEM: childItem = ItemDAO().get(child.object.id, None, child.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) monster.abilities = abilities monster.spells = spells monster.items = items monster.armors = armors monster.moneyList = moneys monster.containers = containers monster.meleeWeapons = meleeWeapons monster.rangedWeapons = rangedWeapons monster.throwableWeapons = throwableWeapons return monster
def test_ability(self): """ Test ability DAO :return: """ DAO = AbilityDAO('unitTests.db') ability = Ability(None, 'cs', 'Find steps', 'You can find steps everywhere', '10% in wood', Races.ELF, Classes.RANGER, 2) ability2 = Ability(None, 'cs', 'Fly', 'I believe i can fly', '0.000001%', Races.ELF, Classes.RANGER, 2) id = DAO.create(ability) id2 = DAO.create(ability2) ability.id = id ability2.id = id2 getAbility = DAO.get(id) getAbility2 = DAO.get(id2) self.assertEqual(ability, getAbility, 'First ability is not equal') self.assertEqual(ability2, getAbility2, 'Second ability is not equal') self.assertNotEqual(getAbility, getAbility2, 'First and second ability is equal') ability.name = 'Updated find steps' DAO.update(ability) getObject = DAO.get(id) self.assertEqual(getObject, ability) DAO.delete(id) deleted = DAO.get(id) self.assertIsNone(deleted)
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 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
def __init__(self): self.DAO = AbilityDAO()