示例#1
0
    def get_all_data(self, id: int) -> dict:
        """
        Get all data dictionary of one spell, all langs
        :param id: id of object
        :return: dictionary of all data
        """
        data = {}
        int_data = dict(
            Database(self.DATABASE_DRIVER).select(self.DATABASE_TABLE,
                                                  {'ID': id})[0])
        tr_data = Database(self.DATABASE_DRIVER).select(
            'translates', {
                'target_id': id,
                'type': self.TYPE.value
            })
        for key, value in int_data.items():
            data[key] = value

        for line in tr_data:
            name = line['name']
            lang = line['lang']
            value = line['value']

            if name in data:
                data[name][lang] = value
            else:
                data[name] = {}
                data[name][lang] = value

        return data
示例#2
0
class LangDAO(ILangDAO):
    """
    DAO for lang
    """
    TABLE_NAME = 'languages'
    DATABASE_DRIVER = "file::memory:?cache=shared"

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

    def get_all_langs(self) -> list:
        """
        Return list of all langs
        :return: List of lang objects
        """
        data = self.database.select_all(self.TABLE_NAME)
        return self.__map_objects(data)

    def get_lang_by_code(self, code: str) -> Lang:
        """
        Get lang from database by code
        :param code: Code of lang (2 char)
        :return: Lang object if exist, None otherwise
        """
        data = self.database.select(self.TABLE_NAME, {'code': code})
        return self.__map_objects(data)[0] if len(data) > 0 else None

    def get_lang(self, id: int) -> Lang:
        """
        Get lang by id
        :param id: id of lang
        :return: Lang object if exist, None otherwise
        """
        data = self.database.select(self.TABLE_NAME, {'ID': id})
        return self.__map_objects(data)[0] if len(data) > 0 else None

    def create_lang(self, lang: Lang) -> int:
        """
        Create new lang
        :param lang: lang object
        :return: id of created lang
        """
        values = {'name': lang.name, 'code': lang.code}
        return self.database.insert(self.TABLE_NAME, values)

    def __map_objects(self, data: dict) -> list:
        """
        Map data from database to Lang objects
        :param data:
        :return:
        """
        langs = []
        for line in data:
            lang = Lang(line['ID'], line['name'], line['code'])
            langs.append(lang)
        return langs
示例#3
0
    def get_languages(self, id: int) -> list:
        """
        Get list of all languages codes for this object
        :param id: id of spell
        :return: list of langs codes
        """
        if self.TYPE is None:
            raise ValueError(
                'Constant TYPE is not defined in class {}'.format(self))
        data = Database(self.DATABASE_DRIVER).select('translates', {
            'target_id': id,
            'type': self.TYPE.value
        })

        languages = []
        for line in data:
            if line['lang'] not in languages:
                languages.append(line['lang'])
        if len(languages) == 0:
            languages.append(SettingsDAO().get_value('language', str))
        return languages
示例#4
0
 def tearDownClass(cls):
     database = Database('unitTests.db')
     ObjectDatabase(':memory:').drop_table('Character')
示例#5
0
 def setUpClass(cls):
     cls.database = Database('unitTests.db')
     DatabaseTables().create_tables('unitTests.db')
示例#6
0
 def __init__(self):
     self.database = Database(self.DATABASE_DRIVER)
示例#7
0
class PlayerTreeDAO(IPlayerTreeDAO):
    """
    DAO for tree widget
    """
    TABLE_NAME = 'player_tree_structure'
    DATABASE_DRIVER = "file::memory:?cache=shared"

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

    def get_root_nodes(self, target_type: ObjectType) -> list:
        """
        Get all nodes with parent null
        :param target_type: Type of object
        :return: list of root nodes
        """
        data = self.database.select(self.TABLE_NAME, {
            'parent_id': None,
            'parent_type': target_type.value
        })

        return map_objects(data)

    def get_nodes_search(self, targetType: ObjectType, text: str):
        """
        Get nodes witch name contain searched text
        :param targetType: Type of object in node
        :param text: searching text
        :return: list of nodes with text included
        """
        data = self.database.select(self.TABLE_NAME, {
            'parent_type': targetType.value,
            'name': ('like', '%' + text + '%')
        })
        return map_objects(data)

    def get_node(self, id: int) -> Node:
        """
        Get node by id
        :param id: id of node
        :return: node object if exist, None otherwise
        """
        data = self.database.select(self.TABLE_NAME, {'ID': id})
        return map_objects(data)[0] if len(data) > 0 else None

    def get_children_objects(self, parentNodeId: int,
                             contextType: ObjectType) -> list:
        """
        Recursively find all child object of give type, only first level object (folders will be skipped and searching deep
        :param parentNodeId parent node, where you finding
        :param contextType object type of tree
        :return: list of objects in tree
        """

        nodes = self.get_children_nodes(contextType, parentNodeId)

        objects = []
        for node in nodes:
            if isinstance(node, Folder):
                objects += self.get_children_nodes(contextType, node.id)
            else:
                objects.append(node)

        return objects

    def __get_children_objects(self,
                               targetType,
                               nodeId: int,
                               parentType: ObjectType,
                               objects: list = None,
                               direct: bool = False):
        """
        Private function, return all children object of target node
        :param targetType: Object type
        :param nodeId: id of target node
        :param parentType: Context type
        :param objects: list of object for recursion
        :param direct: if true, only first level child will be add to object list 
        :return: list of children
        """
        if objects is None:
            objects = []

        children = self.get_children_nodes(parentType, nodeId)

        for child in children:
            if direct and isinstance(
                    child,
                    NodeObject) and child.object.object_type is not targetType:
                continue

            objects = self.__get_children_objects(targetType,
                                                  child.id,
                                                  parentType,
                                                  objects,
                                                  direct=direct)
            if isinstance(
                    child,
                    NodeObject) and child.object.object_type is targetType:
                objects.append(child.object)

        return objects

    def get_children_nodes(self, contextType: ObjectType,
                           parent_id: int) -> list:
        """
        Get all child of parent id
        :param target_type: target type
        :param parent_id: parent id
        :return: list of nodes with parent id, or empty list
        """
        data = self.database.select(self.TABLE_NAME, {
            'parent_id': parent_id,
            'parent_type': contextType.value
        })

        return map_objects(data)

    def update_node(self, node: Node):
        """
        Update node data
        :param node: node
        """
        if isinstance(node, Folder):
            values = {'parent_id': node.parent_id, 'name': node.name}
        else:
            values = {
                'target_id': node.object.id,
                'parent_id': node.parent_id,
                'name': node.name
            }
        self.database.update(self.TABLE_NAME, node.id, values)

    def insert_node(self, node: Node, object_type: ObjectType) -> int:
        """
        Create new node in database
        :param node: node object
        :return: id of created node
        """

        if isinstance(node, Folder):
            values = {
                'parent_type': object_type.value,
                'parent_id': node.parent_id,
                'type': NodeType.FOLDER.value,
                'name': node.name
            }
        else:
            values = {
                'target_type': node.object.object_type.value,
                'parent_type': object_type.value,
                'target_id': node.object.id,
                'parent_id': node.parent_id,
                'type': NodeType.OBJECT.value,
                'name': node.name
            }
        return self.database.insert(self.TABLE_NAME, values)

    def delete_node(self, id: int):
        """
        Delete node from database
        :param id: id of node
        """
        self.database.delete(self.TABLE_NAME, id)

    def get_node_by_object(self, object: object):
        """
        Return node based on object
        :param object: object that you finding
        :return: ObjectNode with object 
        """
        data = self.database.select(self.TABLE_NAME, {
            'target_id': object.id,
            'parent_type': object.object_type.value
        })

        return self.get_node(data[0]['id'])
示例#8
0
class MapItemDAO(IMapItemDAO):
    DATABASE_TABLE = 'Map_item'
    DATABASE_DRIVER = "file::memory:?cache=shared"
    TYPE = ObjectType.MAP_ITEM


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


    def create(self, mapItem: MapItem) -> int:
        """
        Create new map item        
        :param mapItem: Map object        
        :return: id of created mapItem
        """
        X = mapItem.coord.x()
        Y = mapItem.coord.y()

        values = {
            'name'       : mapItem.name,
            'description': mapItem.description,
            'number'     : mapItem.number,
            'scale'      : mapItem.scale,
            'positionX'  : X,
            'positionY'  : Y,
            'map_id'     : mapItem.mapId,
            'itemType'   : mapItem.itemType.value
        }
        id = self.database.insert(self.DATABASE_TABLE, values)

        return id


    def update(self, mapItem: MapItem):
        """
        Update map item in database
        :param mapItem: mapItem object with new data
        """
        X = mapItem.coord.x()
        Y = mapItem.coord.y()

        values = {
            'name'       : mapItem.name,
            'description': mapItem.description,
            'number'     : mapItem.number,
            'scale'      : mapItem.scale,
            'positionX'  : X,
            'positionY'  : Y,
        }

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


    def delete(self, mapitem_id: int):
        """
        Delete Map item from database and all his translates
        :param mapitem_id: id of Map item
        """
        self.database.delete(self.DATABASE_TABLE, mapitem_id)


    def get(self, mapitem_id: int) -> MapItem:
        """
        Get map item from database
        :param mapitem_id: id of map item        
        :return: Map item object
        """

        data = dict(self.database.select(self.DATABASE_TABLE, {'ID': mapitem_id})[0])

        coord = QPointF(data.get('positionX', 0), data.get('positionY', 0))

        itemType = MapItemType(data.get('itemType'))

        mapitem = MapItem(mapitem_id, data.get('name', ''), data.get('description', ''), coord,
                          data.get('scale', 0), data.get('number', 0), None, data.get('map_id'), itemType)

        return mapitem


    def get_all(self, lang=None) -> list:
        """
        Get list of map items for selected lang
        :param lang: lang of map items
        :return: list of map items
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)
        lines = self.database.select_all(self.DATABASE_TABLE)
        mapItems = []
        for line in lines:
            item = self.get(line['ID'], lang)
            mapItems.append(item)
        return mapItems
示例#9
0
 def __init__(self):
     self.database = Database(self.DATABASE_DRIVER)
     self.treeDAO = PlayerTreeDAO()
示例#10
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
示例#11
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
示例#12
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
示例#13
0
class PartyCharacterDAO(DAO, IPartyCharacterDAO):
    DATABASE_TABLE = 'PartyCharacter'
    TYPE = ObjectType.CHARACTER

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

    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

    def update(self, character: PartyCharacter):
        """
        Update party PartyCharacter in database, update only party character, not Character
        :param character: PartyCharacter object with new data
        """
        intValues = {
            'deviceName': character.deviceName,
            'MACAddress': character.MACAddress,
            'name': character.name
        }

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

    def delete(self, character_id: int):
        """
        Delete party character from database and all his translates
        :param character_id: id of party character
        """
        self.database.delete(self.DATABASE_TABLE, character_id)

    def get(self,
            character_id: int,
            lang: str = None,
            nodeId: int = None,
            contextType: ObjectType = None) -> PartyCharacter:
        """
        Get Party Character by character id, 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 Party 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: Party Character object
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)

        select = self.database.select(self.DATABASE_TABLE,
                                      {'character_id': character_id})

        if not select:
            return None

        data = dict(select[0])

        character = PartyCharacter(data.get('ID'), lang, data['name'], None,
                                   data.get('deviceName', ''),
                                   data.get('MACAddress'))

        messages = MessageDAO().get_by_party_character(character.id)

        character.messages = messages

        # character.character = CharacterDAO().get(data.get('character_id'))

        return character

    def get_by_id(self,
                  partyCharacterId: int,
                  lang: str = None) -> PartyCharacter:
        """
        Get party character by id, without character
        :param partyCharacterId: party character ID
        :param lang: lang of object
        :return: Party character object
        """
        data = dict(
            self.database.select(self.DATABASE_TABLE,
                                 {'ID': partyCharacterId})[0])

        character = PartyCharacter(data.get('ID'), lang, data['name'], None,
                                   data.get('deviceName', ''),
                                   data.get('MACAddress'))

        return character

    def get_all(self, lang=None) -> list:
        """
        Get list of all Party Characters from database, only one lang
        :param lang: lang code
        :return: list of Party 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
示例#14
0
class MessageDAO(DAO, IMessageDAO):
    DATABASE_TABLE = 'Message'
    TYPE = ObjectType.MESSAGE


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


    def create(self, message: Message) -> int:
        """
        Create new message in database
        :param message: Message object
        :return: id of autoincrement
        """
        curDate = datetime.strptime(message.date, '%d/%m/%Y %H:%M:%S') if message.date else None

        if type(message.isMine) is str:
            isMine = True if message.isMine == 'true' else False
        else:
            isMine = message.isMine

        intValues = {
            'text'            : message.text,
            'date'            : curDate.toordinal() if curDate else None,
            'isMine'          : int(isMine),
            'characterId'     : message.characterId,
            'partyCharacterId': message.partyCharacterId
        }

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

        return id


    def update(self, message: Message):
        """
        Update message in database
        :param message: Message object with new data
        """
        if type(message.isMine) is str:
            isMine = True if message.isMine == 'true' else False
        else:
            isMine = message.isMine

        intValues = {
            'text'  : message.text,
            'date'  : message.date.toordinal(),
            'isMine': int(isMine)
        }

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


    def delete(self, message_id: int):
        """
        Delete Message from database and all his translates
        :param message_id: id of Message
        """
        self.database.delete(self.DATABASE_TABLE, message_id)


    def get(self, message_id: int, lang: str = None) -> Message:
        """
        Get Message from database
        :param message_id: id of Message
        :param lang: lang of spell
        :return: Message object
        """
        if lang is None:
            lang = SettingsDAO().get_value('language', str)

        data = dict(self.database.select(self.DATABASE_TABLE, {'ID': message_id})[0])

        curDate = datetime.fromordinal(data.get('date')) if data.get('date') else None

        message = Message(message_id, data['text'], curDate, bool(data['isMine']), data.get('partyCharacterId'),
                          data.get('characterId'))

        return message


    def get_by_party_character(self, partyCharacterId: int) -> list:
        """
        Get all messages for one party character
        :param partyCharacterId: id of party character
        :return: list of Messages
        """
        select = self.database.select(self.DATABASE_TABLE, {'partyCharacterId': partyCharacterId})

        messages = []
        for one in select:
            data = dict(one)
            curDate = date.fromordinal(data.get('date')) if data.get('date') else None
            message = Message(data.get('id'), data.get('text', ''), curDate, bool(data['isMine']), data.get('partyCharacterId'),
                              None)

            messages.append(message)

        return messages


    def get_all(self, lang=None) -> list:
        """
        Get list of all messages from database, only one lang
        :param lang: lang code
        :return: list of messages
        """
        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