def get_empty_object(self, target_type: ObjectType, id: int, lang: str) -> object:
     """
     Create empty object for id and lang
     :param target_type: Type ob object
     :param id: id of object
     :param lang: lang code of object
     :return: New empty object
     """
     return ItemDAO().get(id, lang)
Exemple #2
0
    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
Exemple #3
0
class ItemManager(IItemManager):
    def __init__(self):
        self.DAO = ItemDAO()

    def create(self,
               item: Item,
               nodeParentId: int = None,
               contextType: ObjectType = None) -> int:
        return self.DAO.create(item, nodeParentId, contextType)

    def update(self, item: Item):
        return self.DAO.update(item)

    def delete(self, itemId: int):
        return self.DAO.delete(itemId)

    def get(self,
            itemId: int,
            lang=None,
            nodeId: int = None,
            contextType: ObjectType = None) -> Item:
        return self.DAO.get(itemId, lang, nodeId, contextType)

    def get_all(self, lang=None) -> list:
        return self.DAO.get_all(lang)

    def create_empty(self, lang) -> Item:
        item = Item(None, lang)
        id = self.DAO.create(item)
        item.id = id

        return item
    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)
Exemple #5
0
    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
Exemple #6
0
    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
Exemple #7
0
    def test_item(self):
        """
        Test item DAO
        :return: 
        """
        DAO = ItemDAO()

        item = Armor(None, 'cs', 'Chain armor', 'Big armor', None, 15, 35, 10,
                     15, 20, ArmorSize.B, 1)
        item2 = Container(None, 'cs', 'Lather bag', 'Big lather bag', None, 15,
                          20, 150, 1)

        id = DAO.create(item)
        id2 = DAO.create(item2)

        getObject = DAO.get(id)
        getObject2 = DAO.get(id2)

        self.assertEqual(item, getObject, 'First item is not equal')
        self.assertEqual(item2, getObject2, 'Second item is not equal')
        self.assertNotEqual(getObject, getObject2,
                            'First and second item is equal')

        item.name = 'Updated chain armor'
        DAO.update(item)
        getObject = DAO.get(id)

        self.assertEqual(getObject, item)

        DAO.delete(id)

        deleted = DAO.get(id)
        self.assertIsNone(deleted)
Exemple #8
0
    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
Exemple #9
0
 def __init__(self):
     self.DAO = ItemDAO()
Exemple #10
0
    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
Exemple #11
0
    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