def create(self, character: PartyCharacter, nodeParentId: int = None, contextType: ObjectType = None) -> int: """ Create new PartyCharacter, if character given, linked with character and create character if character is not given, only party character created and liked with scenario :param character: PartyCharacter 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 not contextType: contextType = self.TYPE if character.character: if type(character.character) is dict: character_id = CharacterDAO().create( character.character.popitem()[1], nodeParentId, contextType) # TODO: default lang else: character_id = CharacterDAO().create( character.character, nodeParentId, contextType) # TODO: default lang else: character_id = None if nodeParentId: scenario = PlayerTreeDAO().get_node(nodeParentId).object else: scenario = None intValues = { 'deviceName': character.deviceName, 'MACAddress': character.MACAddress, 'character_id': character_id, 'scenario_id': scenario.id if scenario else None, 'name': character.name, } id = self.database.insert(self.DATABASE_TABLE, intValues) character.id = id for message in character.messages: message.partyCharacterId = id MessageDAO().create(message) return id
class CharacterManager: def __init__(self): from data.DAO.CharacterDAO import CharacterDAO self.DAO = CharacterDAO() def create(self, character: Character, nodeParentId: int = None, contextType: ObjectType = None) -> int: return self.DAO.create(character, nodeParentId, contextType) def update_character(self, character: Character): return self.DAO.update(character) def delete(self, characterId: int): return self.DAO.delete(characterId) def get(self, characterId: int, lang: str = None, nodeId: int = None, contextType: ObjectType = None) -> Character: return self.DAO.get(characterId, lang, nodeId, contextType) def get_all(self, lang: str = None) -> list: return self.DAO.get_all(lang) def create_empty(self, lang) -> Character: character = Character(None, lang) id = self.DAO.create(character) character.id = id return character
def create(self, location: Location, nodeParentId: int = None, contextType: ObjectType = None) -> int: """ Create new location :param location: Location object :param nodeParentId: id of parent node in tree :param contextType: Object type of tree, where item is located :return: id of created location """ if not contextType: contextType = self.TYPE strValues = { 'name': location.name, 'description': location.description } id = self.database.insert_null(self.DATABASE_TABLE) location.id = id self.database.insert_translate(strValues, location.lang, id, self.TYPE) # Create node for tree structure node = NodeObject(None, location.name, nodeParentId, location) nodeId = self.treeDAO.insert_node(node, contextType) for one in location.containers + location.armors + location.moneyList + location.meleeWeapons + location.rangedWeapons + location.throwableWeapons + location.items: ItemDAO().create(one, nodeId, contextType) for monster in location.monsters: MonsterDAO().create(monster, nodeId, contextType) for one in location.locations: self.create(one, nodeId, contextType) for character in location.characters: CharacterDAO().create(character, nodeId, contextType) return id
def test_character(self): """ Test character DAO :return: """ DAO = CharacterDAO() character = Character(None, 'cs', 'Wilson', 'Character Wilson', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, Classes.RANGER, Races.ELF, Alignment.LAWFUL_GOOD, 1, 1) character2 = Character(None, 'cs', 'Don', 'Character Don', 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Classes.MAGICIAN, Races.KROLL, Alignment.LAWFUL_EVIL, 1, 1) id = DAO.create(character) id2 = DAO.create(character2) getCharacter = DAO.get(id) getCharacter2 = DAO.get(id2) self.assertEqual(character, getCharacter, 'First character is not equal') self.assertEqual(character2, getCharacter2, 'Second character is not equal') self.assertNotEqual(getCharacter, getCharacter2, 'First and second character is equal') character.intelligence = 5 DAO.update(character) getObject = DAO.get(id) self.assertEqual(getObject, character) DAO.delete(id) deleted = DAO.get(id) self.assertIsNone(deleted)
def get(self, location_id: int, lang: str = None, nodeId: int = None, contextType: ObjectType = None) -> Location: """ Get Location , 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 location_id: id of Location :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: Location object """ if lang is None: lang = SettingsDAO().get_value('language', str) data = self.database.select(self.DATABASE_TABLE, {'ID': location_id}) if not data: return None else: data = dict(data[0]) tr_data = dict( self.database.select_translate(location_id, self.TYPE.value, lang)) location = Location(data.get('ID'), lang, tr_data.get('name', ''), tr_data.get('description', '')) if nodeId and contextType: children = self.treeDAO.get_children_objects(nodeId, contextType) locations = [] monsters = [] characters = [] items = [] armors = [] moneys = [] containers = [] meleeWeapons = [] rangedWeapons = [] throwableWeapons = [] maps = [] for child in children: if child.object.object_type is ObjectType.LOCATION: childLocation = self.get(child.object.id, None, child.id, contextType) locations.append(childLocation) elif child.object.object_type is ObjectType.MONSTER: monster = MonsterDAO().get(child.object.id, None, child.id, contextType) monsters.append(monster) elif child.object.object_type is ObjectType.CHARACTER: character = CharacterDAO().get(child.object.id, None, child.id, contextType) characters.append(character) elif child.object.object_type is ObjectType.MAP: map = MapDAO().get(child.object.id) maps.append(map) 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) location.items = items location.armors = armors location.moneyList = moneys location.containers = containers location.meleeWeapons = meleeWeapons location.rangedWeapons = rangedWeapons location.throwableWeapons = throwableWeapons location.locations = locations location.monsters = monsters location.characters = characters location.maps = maps return location
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): from data.DAO.CharacterDAO import CharacterDAO self.DAO = CharacterDAO()