Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
class ItemDAO(DAO, IItemDAO):
    DATABASE_TABLE = 'Item'
    TYPE = ObjectType.ITEM


    def __init__(self):
        self.database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()


    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 update(self, item: Item):
        """
        Update item in database
        :param item: Item object with new data
        """
        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,
            })

        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
            })

        self.database.update(self.DATABASE_TABLE, item.id, intValues)
        self.database.update_translate(strValues, item.lang, item.id, self.TYPE)


    def delete(self, item_id: int):
        """
        Delete Item from database and from translate
        :param item_id: id of Item
        """
        self.database.delete(self.DATABASE_TABLE, item_id)
        self.database.delete_where('translates',
                                   {'target_id': item_id, 'type': ObjectType.ITEM})


    def get_all(self, lang=None) -> list:
        """
        Get list of items for selected lang
        :param lang: lang of items
        :return: list of items
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all('Item')

        items = []
        for line in lines:
            item = self.get(line['ID'], lang)
            items.append(item)
        return items


    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
Ejemplo n.º 4
0
 def __init__(self):
     self.treeDAO = PlayerTreeDAO()
Ejemplo n.º 5
0
class PlayerTreeManager(IPlayerTreeManager):
    """
    Tree manager for tree widget
    """
    def __init__(self):
        self.treeDAO = PlayerTreeDAO()

    def get_tree(self, object_type: ObjectType) -> list:
        """
        Return list of root in tree, root has children ( recursive)
        :return: list of root
        """
        roots = self.treeDAO.get_root_nodes(object_type)

        for i in range(len(roots)):
            root = roots[i]
            new = self.__create_tree(root, object_type)
            roots[i] = new

        return roots

    def search_tree_nodes(self, object_type: ObjectType, text: str) -> list:
        """
        Search tree nodes by object type and text, its for finding
        :param object_type: object type (context)
        :param text: text that should be included
        :return: list of root
        """
        roots = self.treeDAO.get_nodes_search(object_type, text)

        for root in roots:
            self.__create_tree(root, object_type)

        return roots

    def __create_tree(self, node: Node, type: ObjectType):
        """
        Recursive function for create tree with children
        :param node: Current node
        :param type: parent of node
        """

        children = self.treeDAO.get_children_nodes(type, node.id)

        # Sub children
        for i in range(len(children)):
            child = children[i]
            new = self.__create_tree(child, type)
            children[i] = new

        node.children = children
        return node

    def create_folder(self,
                      name: str,
                      contextType: ObjectType,
                      parentId: int = None) -> Node:
        """
        Create new node        
        :param name:  name of node
        :param contextType: Context type
        :param parentId: Id of parent node        
        :return: New node folder object
        """

        node = Folder(None, name, parentId)
        id = self.treeDAO.insert_node(node, contextType)
        node.id = id

        return node

    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 delete_node(self, node, targetObject: Object):
        """
        Delete node
        :param node: Node that you want to delete
        :param targetObject: Target object, that will be deleted too
        """
        self.treeDAO.delete_node(node.id)

        if targetObject:
            targetObject.DAO()().delete(targetObject.id)

    def get_node(self, id: int):
        """
        Get node by ID
        :param id: id of node
        :return: Node
        """
        return self.treeDAO.get_node(id)

    def update_node(self, node):
        """
        Update node in database
        :param node: Updated node
        """
        self.treeDAO.update_node(node)

    def update_node_parent(self, node: Node, parentId: int,
                           context: ObjectType) -> bool:
        """
        Update parent of node, check if parent is Folder
        :param node: Node object
        :param parentId: parent id
        :param context: Context Type
        :return:
        """
        updated = False
        if node.parent_id != parentId:
            updated = True
            parentNode = self.treeDAO.get_node(parentId)

            if self.available_parent(node, parentNode, context):
                node.parent_id = parentId
                if isinstance(node, Folder):
                    self.treeDAO.update_node(node)
                else:
                    self.treeDAO.update_node(node)
        return updated

    def available_parent(self, node, parent_node, context: ObjectType) -> bool:
        """
        Validate if can change parent_id for node
        Go recursive to root, if find object, just chceck if can be
        If there is no object, just add it
        :param node: Node
        :param parent_node: Parent node
        :return: True if can be there, False otherwise
        """
        while parent_node is not None:
            if isinstance(parent_node, Folder):
                if parent_node.parent_id is None:
                    parent_node = None
                else:
                    parent_node = self.treeDAO.get_node(parent_node.parent_id)
            else:
                if isinstance(node, Folder):
                    if NodeType.FOLDER in parent_node.object.treeChildren:
                        return True
                    else:
                        return False
                else:
                    if node.object.object_type in parent_node.object.treeChildren:
                        return True
                    else:
                        return False

        if isinstance(node, Folder):
            return True
        else:
            if node.object.object_type is context:
                return True
            else:
                return False

    def have_tree_children(self, node) -> bool:
        """
        Find out if node have children
        :param node: tree node
        :return: True if have children, False otherwise
        """
        while node is not None:
            if isinstance(node, Folder):
                if node.parent_id is None:
                    node = None
                else:
                    node = self.treeDAO.get_node(node.parent_id)
            else:
                return len(node.object.treeChildren) > 0

        return False

    def create_empty_object(self, object_type: ObjectType) -> NodeObject:
        """
        Create empty object
        :param object_type: obkect type
        :return: NodeObject if created, None otherwise
        """
        obj = object_type.instance()()
        obj.id = object_type.instance().DAO()().create(obj)

        return obj

    def get_object(self, node_id: int):
        """
        Get object from node
        :param node_id: id of node
        :return: Node if exist, None otherwise
        """
        node = self.treeDAO.get_node(node_id)
        return node.object

    def export_to_html(self, selected: list, path: str) -> None:
        """
        Export data to HTML
        :param selected:  list of selected items
        :param path: path to file, where it will be created
        :return: 
        """
        exporting = []
        for id in selected:
            node = self.treeDAO.get_node(id)
            obj = node.object.DAO()().get(node.object.id, None, node.id,
                                          node.object.object_type)
            exporting.append(obj)

        HtmlHandler().create_html(exporting, path)

    def export_to_xml(self, selected: list, path: str):
        """
        Export selected templates to xml
        :param selected: list of selected node in tree
        :param path: path to file, where will be final file
        """
        exporting = []
        for id in selected:
            node = self.treeDAO.get_node(id)
            exporting.append(node)

        ParserHandler().create_xml(exporting, path)

    def import_from_xml(self,
                        file_path,
                        parentType: ObjectType,
                        parent=None,
                        strict: bool = False):
        """
        Import templates from XML file
        :param file_path: path to XML file
        :param parentType:
        :param strict:
        :param parent: parent node id
        """
        objects = ParserHandler().import_xml(file_path)

        # ObjectDatabase('test.db').set_many(True)

        for languages in objects:
            defLang = SettingsDAO().get_value('language', str)
            if defLang in languages:
                leaderObject = languages.pop(defLang)
                leaderCode = defLang

            else:
                leader = languages.popitem()
                leaderCode = leader[0]
                leaderObject = leader[1]

            if strict and leaderObject.object_type != parentType:
                continue

            if not LangManager().lang_exists(leaderCode):
                LangManager().create_lang(leaderCode, leaderCode)

            leaderId = leaderObject.DAO()().create(leaderObject)

            for lang in languages.values():
                if not LangManager().lang_exists(lang.lang):
                    LangManager().create_lang(lang.lang, lang.lang)

                lang.id = leaderId
                leaderObject.DAO()().update(lang)

                # ObjectDatabase('test.db').insert_many_execute()
                # ObjectDatabase('test.db').set_many(False)

    def tree_folder(self, node: Node) -> bool:
        """
        Function found if node is folder in root of tree
        :param node: target node
        :return: True if is folder, False otherwise
        """
        while node.parent_id:
            parentNode = self.get_node(node.parent_id)
            if isinstance(parentNode, Folder):
                node = parentNode
            else:
                return False

        return True
Ejemplo n.º 6
0
class LocationDAO(DAO, ILocationDAO):
    DATABASE_TABLE = 'Location'
    TYPE = ObjectType.LOCATION

    def __init__(self):
        self.database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()

    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 update(self, location: Location):
        """
        Update location in database
        :param location: Location object with new data
        """
        strValues = {
            'name': location.name,
            'description': location.description
        }

        self.database.update_translate(strValues, location.lang, location.id,
                                       self.TYPE)

    def delete(self, location_id: int):
        """
        Delete Location from database and from translate
        :param location_id: id of Location
        """
        self.database.delete(self.DATABASE_TABLE, location_id)
        self.database.delete_where('translates', {
            'target_id': location_id,
            'type': self.TYPE
        })

    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_all(self, lang=None) -> list:
        """
        Get list of locations for selected lang
        :param lang: lang of locations
        :return: list of locations
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)
        items = []
        for line in lines:
            item = self.get(line['ID'], lang)
            items.append(item)
        return items
Ejemplo n.º 7
0
class ScenarioDAO(DAO, IScenarioDAO):
    DATABASE_TABLE = 'Scenario'
    TYPE = ObjectType.SCENARIO

    def __init__(self):
        self.database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()

    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 update(self, scenario: Scenario):
        """
        Update scenario in database
        :param scenario: Scenario object with new data
        """
        intValues = {
            'date': scenario.date.toordinal() if scenario.date else None
        }

        strValues = {
            'name': scenario.name,
            'description': scenario.description
        }

        self.database.update(self.DATABASE_TABLE, scenario.id, intValues)
        self.database.update_translate(strValues, scenario.lang, scenario.id,
                                       self.TYPE)

    def delete(self, scenario_id: int):
        """
        Delete Scenario from database and all his translates
        :param scenario_id: id of Scenario
        """
        self.database.delete(self.DATABASE_TABLE, scenario_id)
        self.database.delete_where('translates', {
            'target_id': scenario_id,
            'type': ObjectType.SCENARIO
        })

    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 get_all(self, lang=None) -> list:
        """
        Get list of Scenario for selected lang
        :param lang: lang of Scenario
        :return: list of Scenario
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)
        items = []
        for line in lines:
            item = self.get(line['ID'], lang)
            items.append(item)
        return items
Ejemplo n.º 8
0
class CharacterDAO(DAO, ISpellDAO):
    DATABASE_TABLE = 'Character'
    TYPE = ObjectType.CHARACTER


    def __init__(self):
        self.database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()


    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 update(self, character: Character):
        """
        Update spell in database
        :param character: Character object with new data
        """
        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
        }

        self.database.update(self.DATABASE_TABLE, character.id, intValues)
        self.database.update_translate(strValues, character.lang, character.id, self.TYPE)


    def delete(self, character_id: int):
        """
        Delete character from database and from translate
        :param character_id: id of character
        """
        self.database.delete(self.DATABASE_TABLE, character_id)
        self.database.delete_where('translates',
                                   {'target_id': character_id, 'type': ObjectType.CHARACTER})


    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 get_all(self, lang=None) -> list:
        """
        Get list of characters for selected lang
        :param lang: lang of characters
        :return: list of characters
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)
        characters = []
        for line in lines:
            character = self.get(line['ID'], lang)
            characters.append(character)
        return characters
Ejemplo n.º 9
0
class EffectDAO(DAO, IEffectDAO):
    DATABASE_TABLE = 'Effect'
    TYPE = ObjectType.EFFECT

    def __init__(self):
        self.database = ObjectDatabase(self.DATABASE_DRIVER)
        # self.obj_database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()

    def create(self,
               effect: Effect,
               nodeParentId: int = None,
               contextType: ObjectType = None) -> int:
        """
        Create new effect
        :param effect: Effect object
        :param nodeParentId: id of parent node in tree
        :param contextType: Object type of tree, where item is located
        :return: id of created effect
        """
        if not contextType:
            contextType = self.TYPE

        if type(effect.active) is str:
            active = True if effect.active == 'true' else False
        else:
            active = effect.active

        intValues = {
            'targetType':
            effect.targetType.value if effect.targetType else None,
            'active': int(active) if active else 0
        }

        strValues = {'name': effect.name, 'description': effect.description}

        id = self.database.insert(self.DATABASE_TABLE, intValues)
        effect.id = id

        self.database.insert_translate(strValues, effect.lang, id, self.TYPE)

        # Create node for tree structure
        node = NodeObject(None, effect.name, nodeParentId, effect)
        nodeId = self.treeDAO.insert_node(node, contextType)

        for modifier in effect.modifiers:
            ModifierDAO().create(modifier, nodeId, contextType)
        return id

    def update(self, effect: Effect) -> None:
        """
        Update effect in database
        :param effect: Effect object with new data
        """
        if type(effect.active) is str:
            active = True if effect.active == 'true' else False
        else:
            active = effect.active

        intValues = {
            'targetType':
            effect.targetType.value if effect.targetType else None,
            'active': int(active) if active else 0,
        }

        strValues = {'name': effect.name, 'description': effect.description}

        self.database.update(self.DATABASE_TABLE, effect.id, intValues)
        self.database.update_translate(strValues, effect.lang, effect.id,
                                       self.TYPE)

    def delete(self, effect_id: int) -> None:
        """
        Delete Effect from database and from translate
        :param effect_id: id of effect
        """
        self.database.delete(self.DATABASE_TABLE, effect_id)
        self.database.delete_where('translates', {
            'target_id': effect_id,
            'type': ObjectType.EFFECT
        })

    def get(self,
            effect_id: int,
            lang: str = None,
            nodeId: int = None,
            contextType: ObjectType = None) -> Effect:
        """
        Get Effect , 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 effect_id: id of Effect
        :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: Effect object
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)

        data = self.database.select(self.DATABASE_TABLE, {'ID': effect_id})
        if not data:
            return None
        else:
            data = dict(data[0])
        tr_data = self.database.select_translate(effect_id, self.TYPE.value,
                                                 lang)

        index = data.get('targetType', 1) if data.get('targetType',
                                                      1) is not None else 1
        targetType = ModifierTargetTypes(index)
        effect = Effect(effect_id, lang, tr_data.get('name', ''),
                        tr_data.get('description', ''), targetType,
                        bool(data.get('active', 0)))

        if nodeId and contextType:
            children = self.treeDAO.get_children_objects(nodeId, contextType)

            modifiers = []
            for child in children:
                if child.object.object_type is ObjectType.MODIFIER:
                    modifiers.append(ModifierDAO().get(child.object.id, None,
                                                       child.id, contextType))
            effect.modifiers = modifiers
        return effect

    def get_all(self, lang=None) -> list:
        """
        Get list of effects for selected lang
        :param lang: lang of effects
        :return: list of effects
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)
        effects = []
        for line in lines:
            character = self.get(line['ID'], lang)
            effects.append(character)
        return effects
Ejemplo n.º 10
0
 def __init__(self, databaseDriver: str = None):
     self.database = ObjectDatabase(
         databaseDriver if databaseDriver else self.DATABASE_DRIVER)
     self.treeDAO = PlayerTreeDAO()
Ejemplo n.º 11
0
class AbilityDAO(DAO, IAbilityDAO):
    DATABASE_TABLE = 'Ability'
    TYPE = ObjectType.ABILITY

    def __init__(self, databaseDriver: str = None):
        self.database = ObjectDatabase(
            databaseDriver if databaseDriver else self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()

    def create(self,
               ability: Ability,
               nodeParentId: int = None,
               contextType: ObjectType = None) -> int:
        """
        Create new ability
        :param ability: Ability object
        :param nodeParentId: id of parent node in tree
        :param contextType: Object type of tree, where item is located
        :return: id of created ability
        """

        if not contextType:
            contextType = self.TYPE

        intValues = {
            'drd_race': ability.drd_race.value if ability.drd_race else None,
            'drd_class':
            ability.drd_class.value if ability.drd_class else None,
            'level': ability.level
        }

        strValues = {
            'name': ability.name,
            'description': ability.description,
            'chance': ability.chance
        }

        id = self.database.insert(self.DATABASE_TABLE, intValues)
        ability.id = id

        self.database.insert_translate(strValues, ability.lang, id, self.TYPE)

        # Create node for tree structure
        node = NodeObject(None, ability.name, nodeParentId, ability)
        nodeId = self.treeDAO.insert_node(node, contextType)

        for context in ability.contexts:
            AbilityContextDAO().create(context, nodeId, contextType)

        return id

    def update(self, ability: Ability):
        """
        Update ability in database
        :param ability: Ability object with new data
        """
        intValues = {
            'drd_race': ability.drd_race.value if ability.drd_race else None,
            'drd_class':
            ability.drd_class.value if ability.drd_class else None,
            'level': ability.level
        }

        strValues = {
            'name': ability.name,
            'description': ability.description,
            'chance': ability.chance
        }

        self.database.update(self.DATABASE_TABLE, ability.id, intValues)
        self.database.update_translate(strValues, ability.lang, ability.id,
                                       self.TYPE)

    def delete(self, ability_id: int):
        """
        Delete ability from database and from translate
        :param ability_id: id of ability
        """

        self.database.delete(self.DATABASE_TABLE, ability_id)
        self.database.delete_where('translates', {
            'target_id': ability_id,
            'type': ObjectType.ABILITY
        })

    def get(self,
            ability_id: int,
            lang=None,
            nodeId: int = None,
            contextType: ObjectType = None) -> Ability:
        """
        Get ability , 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 ability_id: id of ability
        :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: Ability object
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)

        data = self.database.select(self.DATABASE_TABLE, {'ID': ability_id})
        if not data:
            return None
        else:
            data = dict(data[0])

        tr_data = self.database.select_translate(ability_id,
                                                 ObjectType.ABILITY.value,
                                                 lang)

        drd_class = Classes(data.get('drd_class')) if data.get(
            'drd_class') is not None else None
        drd_race = Races(
            data.get('drd_race')) if data.get('drd_race') is not None else None
        ability = Ability(ability_id, lang, tr_data.get('name', ''),
                          tr_data.get('description', ''),
                          tr_data.get('chance', ''), drd_race, drd_class,
                          data.get('level', 1))

        if nodeId and contextType:
            children = self.treeDAO.get_children_objects(nodeId, contextType)

            contexts = []
            for child in children:
                if child.object.object_type is ObjectType.ABILITY_CONTEXT:
                    contexts.append(AbilityContextDAO().get(
                        child.object.id, None, child.id, contextType))
            ability.contexts = contexts

        return ability

    def get_all(self, lang=None) -> list:
        """
        Get list of abilities for selected lang
        :param lang: lang of abilities
        :return: list of abilities
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)

        items = []
        for line in lines:
            item = self.get(line['ID'], lang)
            items.append(item)
        return items
Ejemplo n.º 12
0
class ModifierDAO(DAO, IModifierDAO):
    DATABASE_TABLE = 'Modifier'
    TYPE = ObjectType.MODIFIER

    def __init__(self):
        self.database = Database(self.DATABASE_DRIVER)
        self.obj_database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()

    def create(self,
               modifier: Modifier,
               nodeParentId: int = None,
               contextType: ObjectType = None) -> int:
        """
        Create new Modifier
        :param modifier: Modifier object
        :param nodeParentId: id of parent node in tree
        :param contextType: Object type of tree, where item is located
        :return: id of created Modifier
        """
        if not contextType:
            contextType = self.TYPE

        if modifier.valueType is ModifierValueTypes.TYPE_ARMOR_SIZE:
            value = ArmorSize.by_name(ArmorSize, modifier.value).value
        elif modifier.valueType is ModifierValueTypes.TYPE_WEAPON_HANDLING:
            value = Handling.by_name(Handling, modifier.value).value
        elif modifier.valueType is ModifierValueTypes.TYPE_WEAPON_WEIGHT:
            value = WeaponWeight.by_name(WeaponWeight, modifier.value).value
        else:
            value = modifier.value

        intValues = {
            'value':
            value,
            'valueType':
            modifier.valueType.value if modifier.valueType else None,
            'targetType':
            modifier.targetType.value if modifier.targetType else None,
            'characterTargetAttribute':
            modifier.characterTargetAttribute.value
            if modifier.characterTargetAttribute else None,
            'itemTargetAttribute':
            modifier.itemTargetAttribute.value
            if modifier.itemTargetAttribute else None
        }

        strValues = {
            'name': modifier.name,
            'desccription': modifier.description
        }

        id = self.database.insert(self.DATABASE_TABLE, intValues)
        modifier.id = id

        self.obj_database.insert_translate(strValues, modifier.lang, id,
                                           self.TYPE)

        # Create node for tree structure
        node = NodeObject(None, modifier.name, nodeParentId, modifier)
        self.treeDAO.insert_node(node, contextType)

        return id

    def update(self, modifier: Modifier) -> None:
        """
        Update modifier in database
        :param modifier: Modifier object with new data
        """

        intValues = {
            'value':
            modifier.value
            if type(modifier.value) is int else modifier.value.value,
            'valueType':
            modifier.valueType.value if modifier.valueType else None,
            'targetType':
            modifier.targetType.value if modifier.targetType.value else None,
            'characterTargetAttribute':
            modifier.characterTargetAttribute.value
            if modifier.characterTargetAttribute else None,
            'itemTargetAttribute':
            modifier.itemTargetAttribute.value
            if modifier.itemTargetAttribute else None
        }

        strValues = {
            'name': modifier.name,
            'desccription': modifier.description
        }

        self.database.update(self.DATABASE_TABLE, modifier.id, intValues)
        self.obj_database.update_translate(strValues, modifier.lang,
                                           modifier.id, self.TYPE)

    def delete(self, modifier_id: int) -> None:
        """
        Delete Modifier from database and all his translates
        :param modifier_id: id of Modifier
        """
        self.obj_database.delete(self.DATABASE_TABLE, modifier_id)
        self.database.delete_where('translates', {
            'target_id': modifier_id,
            'type': ObjectType.MODIFIER
        })

    def get(self,
            modifier_id: int,
            lang: str = None,
            nodeId: int = None,
            contextType: ObjectType = None) -> Modifier:
        """
        Get Modifier , 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 modifier_id: id of Modifier
        :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: Modifier object
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)

        data = self.obj_database.select(self.DATABASE_TABLE,
                                        {'ID': modifier_id})
        if not data:
            return None
        else:
            data = dict(data[0])

        tr_data = dict(
            self.database.select_translate(modifier_id,
                                           ObjectType.MODIFIER.value, lang))

        targetTypeIndex = data.get('targetType', None)
        targetType = ModifierTargetTypes(
            int(targetTypeIndex)) if targetTypeIndex is not None else None

        characterAttributeIndex = data.get('characterTargetAttribute', None)
        itemAttributeIndex = data.get('itemTargetAttribute', None)

        characterTargetAttribute = CharacterAttributes(
            characterAttributeIndex
        ) if characterAttributeIndex is not None else None
        itemTargetAttribute = ItemsAttributes(
            itemAttributeIndex) if itemAttributeIndex is not None else None

        valueTypeIndex = data.get('valueType', None)
        valueType = ModifierValueTypes(
            valueTypeIndex) if valueTypeIndex else None

        value = data.get('value', 0)
        if valueType is ItemsAttributes.WEAPON_MELEE_HANDLING:
            value = Handling(value)
        elif valueType is ItemsAttributes.WEAPON_WEIGHT:
            value = WeaponWeight(value)
        elif valueType is ItemsAttributes.ARMOR_SIZE:
            valueType = ArmorSize(value)
        else:
            value = value

        modifier = Modifier(modifier_id, lang, tr_data.get('name', ''),
                            tr_data.get('description', ''), valueType, value,
                            characterTargetAttribute, itemTargetAttribute,
                            targetType)

        return modifier

    def get_all(self, lang: str = None) -> list:
        """
        Get list of modifiers for selected lang
        :param lang: lang of modifiers
        :return: list of modifiers
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all('Item')

        modifiers = []
        for line in lines:
            item = self.get(line['ID'], lang)
            modifiers.append(item)
        return modifiers
Ejemplo n.º 13
0
class AbilityContextDAO(DAO, IAbilityContextDAO):
    DATABASE_TABLE = 'AbilityContext'
    TYPE = ObjectType.ABILITY_CONTEXT

    def __init__(self):
        self.database = Database(self.DATABASE_DRIVER)
        self.obj_database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()

    def create(self,
               context: AbilityContext,
               nodeParentId: int = None,
               contextType: ObjectType = None) -> int:
        """
        Create new ability context
        :param context: Ability context object
        :param nodeParentId: id of parent node in tree
        :param contextType: Object type of tree, where item is located
        :return: id of created ability context
        """
        if contextType is None:
            contextType = self.TYPE

        intValues = {
            'value':
            context.value,
            'valueType':
            context.valueType.value if context.valueType else None,
            'targetAttribute':
            context.targetAttribute.value if context.targetAttribute else None
        }

        strValues = {'name': context.name, 'description': context.description}

        id = self.database.insert(self.DATABASE_TABLE, intValues)
        context.id = id

        self.obj_database.insert_translate(strValues, context.lang, id,
                                           self.TYPE)

        # Create node for tree structure
        node = NodeObject(None, context.name, nodeParentId, context)
        self.treeDAO.insert_node(node, contextType)

        return id

    def update(self, context: AbilityContext) -> None:
        """
        Update Ability context with new values
        :param context: Ability context object with new values    
        """
        intValues = {
            'value':
            context.value,
            'valueType':
            context.valueType.value if context.valueType else None,
            'targetAttribute':
            context.targetAttribute.value if context.targetAttribute else None
        }

        strValues = {'name': context.name, 'description': context.description}

        self.database.update(self.DATABASE_TABLE, context.id, intValues)
        self.obj_database.update_translate(strValues, context.lang, context.id,
                                           self.TYPE)

    def delete(self, context_id: int) -> None:
        """
        Delete ability context from database
        :param context_id: id of context 
        :return: 
        """
        self.obj_database.delete(self.DATABASE_TABLE, context_id)
        self.database.delete_where('translates', {
            'target_id': context_id,
            'type': ObjectType.ABILITY_CONTEXT
        })

    def get(self,
            context_id: int,
            lang: str = None,
            nodeId: int = None,
            contextType: ObjectType = None) -> AbilityContext:
        """
        Get ability context, object transable attributes depends on lang
        If nodeId and contextType is specified, whole object is returned (with all subobjects)
        If not specified, only basic attributes are set.        
        :param context_id: id of ability context
        :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: Ability context object
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)

        data = self.obj_database.select(self.DATABASE_TABLE,
                                        {'ID': context_id})
        if not data:
            return None
        else:
            data = dict(data[0])

        tr_data = dict(
            self.database.select_translate(context_id, self.TYPE.value, lang))

        valueType = ModifierValueTypes(
            data['valueType']) if data['valueType'] else None
        targetAttribute = CharacterAttributes(
            data['targetAttribute']) if data['targetAttribute'] else None

        context = AbilityContext(data['ID'], lang, tr_data.get('name', ''),
                                 tr_data.get('description', ''), valueType,
                                 data.get('value', 0), targetAttribute)
        return context

    def get_all(self, lang: str = None) -> list:
        """
        Gel list of all Ability context
        :param lang: lang of objects
        :return: list of Ability context
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)

        items = []
        for line in lines:
            item = self.get(line['ID'], lang)
            items.append(item)
        return items
Ejemplo n.º 14
0
class MonsterDAO(DAO, IMonsterDAO):
    DATABASE_TABLE = 'Monster'
    TYPE = ObjectType.MONSTER


    def __init__(self):
        self.database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()


    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 update(self, monster: Monster):
        """
        Update monster in database
        :param monster: Monster object with new data
        """
        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,
        }

        self.database.update(self.DATABASE_TABLE, monster.id, intValues)
        self.database.update_translate(strValues, monster.lang, monster.id, self.TYPE)


    def delete(self, monster_id: int):
        """
        Delete Monster from database and all his translates
        :param monster_id: id of Monster
        """
        self.database.delete(self.DATABASE_TABLE, monster_id)
        self.database.delete_where('translates',
                                   {'target_id': monster_id, 'type': ObjectType.SPELL})


    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 get_all(self, lang=None) -> list:
        """
        Get list of Monsters for selected lang
        :param lang: lang of Monsters
        :return: list of Monsters
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)
        items = []
        for line in lines:
            item = self.get(line['ID'], lang)
            items.append(item)
        return items
Ejemplo n.º 15
0
class MapDAO(DAO, IMapDAO):
    DATABASE_TABLE = 'Map'
    TYPE = ObjectType.MAP

    def __init__(self):
        self.database = Database(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()

    def create(self,
               map: Map,
               nodeParentId: int = None,
               contextType: ObjectType = None) -> int:
        """
        Create new map and create empty map image, because of exporting        
        :param map: Map object
        :param nodeParentId: id of parent node in tree
        :param contextType: Object type of tree, where item is located
        :return: id of created map
        """
        if contextType is None:
            contextType = self.TYPE

        values = {
            'name': map.name if map.name else '',
            'description': map.description if map.description else '',
            'map_file': map.mapFile,
        }

        id = self.database.insert(self.DATABASE_TABLE, values)
        map.id = id

        node = NodeObject(None, map.name, nodeParentId, map)
        nodeId = self.treeDAO.insert_node(node, contextType)

        for mapItem in map.mapItems:
            mapItem.mapId = id
            MapItemDAO().create(mapItem)

        self.create_map_image(map)
        return id

    def update(self, map: Map):
        """
        Update map in database
        :param map: Location object with new data
        """
        values = {
            'name': map.name,
            'description': map.description,
            'map_file': map.mapFile,
        }

        self.database.set_many(True)
        for mapItem in map.mapItems:
            MapItemDAO().update(mapItem)

        self.database.insert_many_execute()
        self.database.set_many(False)

        self.database.update(self.DATABASE_TABLE, map.id, values)

    def delete(self, map_id: int):
        """
        Delete Map from database and from translate and delete all maps linked with map
        :param map_id: id of Map
        """
        map = self.get(map_id)
        self.database.delete(self.DATABASE_TABLE, map_id)
        os.remove(map.mapFile)
        os.remove('resources/maps/exportedMap-1.png')

    def get(self,
            map_id: int,
            lang: str = None,
            nodeId: int = None,
            contextType: ObjectType = None) -> Map:
        """
        Get Map , 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 map_id: id of Map
        :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: Map object
        """

        data = self.database.select(self.DATABASE_TABLE, {'ID': map_id})

        if not data:
            return None
        else:
            data = dict(data[0])

        map = Map(map_id, None, data.get('name', ' '),
                  data.get('description', ' '), data.get('map_file', None))

        sql = self.database.select('Map_item', {'map_id': map.id})

        map.XMLMap = 'map-{}.png'.format(map_id)
        mapItems = []
        for line in sql:
            mapItem = MapItemDAO().get(line['ID'])
            mapItems.append(mapItem)
        map.mapItems = mapItems

        return map

    def get_all(self, lang=None) -> list:
        """
        Get list of maps for selected lang
        :param lang: lang of maps
        :return: list of maps
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)
        maps = []
        for line in lines:
            item = self.get(line['ID'], lang)
            maps.append(item)
        return maps

    def create_map_image(self, map: Map) -> None:
        """
        Create empty image for map, important because of exporting
        :param map: map object         
        """
        from PyQt5.QtWidgets import QGraphicsView
        from PyQt5.QtWidgets import QGraphicsScene
        from PyQt5.QtGui import QPainter, QPixmap
        from PyQt5.QtGui import QImage
        from presentation.widgets.MapWidget import MapItemDraw

        grview = QGraphicsView()
        grview.setRenderHints(grview.renderHints() | QPainter.Antialiasing
                              | QPainter.SmoothPixmapTransform)

        scene = QGraphicsScene()
        grview.setScene(scene)
        if not map.mapFile:
            mapFile = 'resources/icons/no_map.png'
        else:
            mapFile = map.mapFile
        pixMap = QPixmap(mapFile)
        sceneMap = scene.addPixmap(pixMap)

        for num, mapItem in enumerate(map.mapItems):
            mapItem.number = num + 1
            item = MapItemDraw(mapItem, None)
            scene.addItem(item)
            # self.map.addMapItemDraws(item)

        scene.setSceneRect(scene.itemsBoundingRect())
        img = QImage(scene.sceneRect().size().toSize(), QImage.Format_ARGB32)

        painter = QPainter(img)
        scene.render(painter)

        name = 'resources/maps/exportedMap-{}.png'.format(map.id)
        img.save(name)

        del painter
Ejemplo n.º 16
0
 def __init__(self):
     self.database = ObjectDatabase(self.DATABASE_DRIVER)
     self.treeDAO = PlayerTreeDAO()
Ejemplo n.º 17
0
class SpellDAO(DAO, ISpellDAO):
    DATABASE_TABLE = 'Spell'
    TYPE = ObjectType.SPELL

    def __init__(self):
        self.database = ObjectDatabase(self.DATABASE_DRIVER)
        self.treeDAO = PlayerTreeDAO()

    def create(self,
               spell: Spell,
               nodeParentId: int = None,
               contextType: ObjectType = None) -> int:
        """
        Create new Spell
        :param spell: Spell object
        :param nodeParentId: id of parent node in tree
        :param contextType: Object type of tree, where item is located
        :return: id of created Spell
        """

        if not contextType:
            contextType = self.TYPE

        intValues = {
            'cast_time': spell.cast_time if spell.cast_time else 0,
            'drd_class': spell.drd_class.value if spell.drd_class else None
        }

        strValues = {
            'name': spell.name,
            'description': spell.description,
            'mana_cost_initial': spell.mana_cost_initial,
            'mana_cost_continual': spell.mana_cost_continual,
            'range': spell.range,
            'scope': spell.scope,
            'duration': spell.duration
        }

        # Insert NON transable values
        id = self.database.insert(self.DATABASE_TABLE, intValues)

        # Insert transable values
        self.database.insert_translate(strValues, spell.lang, id, self.TYPE)

        spell.id = id

        # Create node for tree structure
        node = NodeObject(None, spell.name, nodeParentId, spell)
        self.treeDAO.insert_node(node, contextType)

        return id

    def update(self, spell: Spell):
        """
        Update spell in database
        :param spell: Spell object with new data
        """
        if spell.id is None:
            raise ValueError('Cant update object without ID')
        data = self.database.select(self.DATABASE_TABLE, {'ID': spell.id})

        if not data:
            raise ValueError('Cant update none existing object')

        intValues = {
            'cast_time': spell.cast_time,
            'drd_class': spell.drd_class.value if spell.drd_class else None
        }

        self.database.update(self.DATABASE_TABLE, spell.id, intValues)

        strValues = {
            'name': spell.name,
            'description': spell.description,
            'mana_cost_initial': spell.mana_cost_initial,
            'mana_cost_continual': spell.mana_cost_continual,
            'range': spell.range,
            'scope': spell.scope,
            'duration': spell.duration
        }

        self.database.update_translate(strValues, spell.lang, spell.id,
                                       self.TYPE)

    def delete(self, spell_id: int):
        """
        Delete spell from database and all his translates
        :param spell_id: id of spell
        """
        self.database.delete(self.DATABASE_TABLE, spell_id)
        self.database.delete_where('translates', {
            'target_id': spell_id,
            'type': ObjectType.SPELL
        })

    def get(self,
            spell_id: int,
            lang: str = None,
            nodeId: int = None,
            contextType: ObjectType = None) -> Spell:
        """
        Get Spell , 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 spell_id: id of Spell
        :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: Spell object
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)

        data = self.database.select(self.DATABASE_TABLE, {'ID': spell_id})
        if not data:
            return None
        else:
            data = dict(data[0])
        tr_data = self.database.select_translate(spell_id,
                                                 ObjectType.SPELL.value, lang)
        drdClassIndex = data.get('drd_class', None)
        drdClass = Classes(
            drdClassIndex) if drdClassIndex is not None else None
        spell = Spell(spell_id, lang, tr_data.get('name', ''),
                      tr_data.get('description', ''),
                      tr_data.get('mana_cost_initial', ''),
                      tr_data.get('mana_cost_continual', ''),
                      tr_data.get('range', ''), tr_data.get('scope', ''),
                      data.get('cast_time', 0), tr_data.get('duration', ''),
                      drdClass)

        return spell

    def get_all(self, lang=None) -> list:
        """
        Get list of Spell for selected lang
        :param lang: lang of Spell
        :return: list of Spell
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)
        items = []
        for line in lines:
            item = self.get(line['ID'], lang)
            items.append(item)
        return items