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
class SpellManager(ISpellManager): def __init__(self): from data.DAO.SpellDAO import SpellDAO self.DAO = SpellDAO() def create(self, spell: Spell, nodeParentId: int = None, contextType: ObjectType = None) -> int: return self.DAO.create(spell, nodeParentId, contextType) def update_spell(self, spell: Spell): return self.DAO.update(spell) def delete(self, spellId: int): return self.DAO.delete(spellId) def get(self, spellId: int, lang: str = None, nodeId: int = None, contextType: ObjectType = None) -> Spell: return self.DAO.get(spellId, lang, nodeId, contextType) def get_all(self, lang=None) -> list: return self.DAO.get_all(lang) def create_empty(self, lang): spell = Spell(None, lang) id = self.DAO.create(spell) spell.id = id return spell
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 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_spell(self): """ Test Spell DAO :return: """ DAO = SpellDAO() spell = Spell(None, 'cs', 'Fireball', 'Mighty fireball', '8', '5', '2', '12', 2, '15', Classes.MAGICIAN) spell2 = Spell(None, 'cs', 'Blink', 'Fast blink', '2', '1', '6', '15', 4, '5', Classes.MAGICIAN) id = DAO.create(spell) id2 = DAO.create(spell2) getObject = DAO.get(id) getObject2 = DAO.get(id2) self.assertEqual(spell, getObject, 'First spell is not equal') self.assertEqual(spell2, getObject2, 'Second spell is not equal') self.assertNotEqual(getObject, getObject2, 'First and second spell is equal') spell.name = 'Updated fireball' DAO.update(spell) getObject = DAO.get(id) self.assertEqual(getObject, spell) DAO.delete(id) deleted = DAO.get(id) self.assertIsNone(deleted)
def __init__(self): from data.DAO.SpellDAO import SpellDAO self.DAO = SpellDAO()
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