All files / app/codeCharta/util/gameObjectsParser gameObjectsImporter.ts

100% Statements 74/74
93.75% Branches 15/16
100% Functions 18/18
100% Lines 69/69

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 20213x 13x                                       13x   13x 1x   1x                   1x 1x   5x 1x 1x   1x 6x 6x 5x       1x 1x 1x 1x 1x       1x 5x 3x                         16x 5x       11x 11x           11x 5x   6x     11x 6x   5x     56x 11x   11x 11x       22x       11x       5x 5x 5x                     11x   11x 11x 11x 11x   11x 11x 11x 11x 11x     11x 5x     11x       44x 44x       5x 5x 5x 5x       2x 2x 2x   2x       1x                   1x               1x 1x                            
import md5 from "md5"
import { AttributeTypes, AttributeTypeValue, CodeMapNode, Edge, FixedPosition, NodeType } from "../../codeCharta.model"
import { ExportWrappedCCFile } from "../../codeCharta.api.model"
 
export interface GameObject {
    name: string
    position: Coordinates
    scale: Coordinates
}
 
export interface Coordinates {
    x: number
    y: number
    z: number
}
 
export interface Cycle {
    from: string
    to: string
}
 
const BASE_NAME = "base"
 
export function parseGameObjectsFile(data): ExportWrappedCCFile {
    const { gameObjectPositions: gameObjects, cycles = [] } = JSON.parse(data)
 
    const codeChartaJson: ExportWrappedCCFile = {
        checksum: "",
        data: {
            projectName: "GameObjects",
            fileChecksum: "",
            apiVersion: "1.3",
            nodes: []
        }
    }
 
    const nodes = [{ name: BASE_NAME, type: NodeType.FOLDER, attributes: {}, children: [] }]
    fixGameObjectsNames(gameObjects)
 
    const rootGameObject = gameObjects.find(gameObject => gameObject.name === "root")
    const baseGameObject = createBaseGameObjectPosition(rootGameObject.scale)
    gameObjects.push(baseGameObject)
 
    for (const gameObject of gameObjects) {
        const nodeNames = gameObject.name.split(".")
        if (nodeNames[0] !== BASE_NAME) {
            addNodeRecursively(nodeNames, nodes[0].children, BASE_NAME, gameObject, gameObjects, rootGameObject)
        }
    }
 
    codeChartaJson.data.nodes = nodes
    codeChartaJson.data.edges = cycles.map(cycle => createEdge(cycle))
    codeChartaJson.data.attributeTypes = createAttributeTypes()
    codeChartaJson.checksum = md5(JSON.stringify(codeChartaJson.data))
    return codeChartaJson
}
 
function fixGameObjectsNames(gameObjects: GameObject[]) {
    for (const gameObject of gameObjects) {
        if (!gameObject.name.startsWith("root")) {
            gameObject.name = gameObject.name.startsWith(".") ? `root${gameObject.name}` : `root.${gameObject.name}`
        }
    }
}
 
function addNodeRecursively(
    nodeNames: string[],
    nodes: CodeMapNode[],
    parentNodeName: string,
    gameObject: GameObject,
    gameObjects: GameObject[],
    rootGameObject: GameObject
) {
    if (nodeNames.length === 0) {
        return
    }
 
    // get current node name and create cc node structure
    const [nodeName] = nodeNames
    let node: CodeMapNode = {
        name: nodeName,
        type: isFile(nodeNames) ? NodeType.FILE : NodeType.FOLDER,
        attributes: {}
    }
 
    if (isFile(nodeNames)) {
        node = wrapFileInAFolder(nodeName, node, gameObject)
    } else {
        node.children = []
    }
 
    if (nodeAlreadyExists(nodes, nodeName)) {
        node = nodes.find(singleNode => singleNode.name === nodeName)
    } else {
        nodes.push(node)
    }
 
    const parentGameObject = gameObjects.find(gameObject => gameObject.name === parentNodeName)
    node.fixedPosition = calculateFixedFolderPosition(node, parentGameObject, gameObject, rootGameObject.name)
 
    const newParentName = parentNodeName === BASE_NAME ? node.name : `${parentNodeName}.${node.name}`
    addNodeRecursively(nodeNames.slice(1), node.children, newParentName, gameObject, gameObjects, rootGameObject)
}
 
function isFile(names: string[]) {
    return names.length === 1
}
 
function nodeAlreadyExists(nodes: CodeMapNode[], name: string) {
    return nodes.some(singleNode => singleNode.name === name)
}
 
function wrapFileInAFolder(nodeName: string, node: CodeMapNode, gameObject: GameObject): CodeMapNode {
    const childNode = { ...node }
    childNode.attributes = { height: gameObject.scale.y }
    return { name: nodeName, type: NodeType.FOLDER, attributes: {}, children: [childNode] }
}
 
function calculateFixedFolderPosition(
    node: CodeMapNode,
    parentGameObject: GameObject,
    childGameObject: GameObject,
    rootGameObjectName: string
): FixedPosition {
    let position: FixedPosition
 
    if (node.type === NodeType.FOLDER) {
        // translate the center point from the middle of the gameObject to the corner (needed for fixedPosition)
        const cornerXofParent = parentGameObject.position.x - parentGameObject.scale.x / 2
        const cornerZofParent = parentGameObject.position.z - parentGameObject.scale.z / 2
        const cornerXofChild = childGameObject.position.x - childGameObject.scale.x / 2
        const cornerZofChild = childGameObject.position.z - childGameObject.scale.z / 2
 
        const top = round(((cornerXofChild - cornerXofParent) / parentGameObject.scale.x) * 100, 2)
        const left = round(((cornerZofChild - cornerZofParent) / parentGameObject.scale.z) * 100, 2)
        const width = round((childGameObject.scale.z / parentGameObject.scale.z) * 100, 2)
        const height = round((childGameObject.scale.x / parentGameObject.scale.x) * 100, 2)
        position = { left, top, width, height }
    }
 
    if (node.name === rootGameObjectName) {
        position = getCenteredRootPosition(position)
    }
 
    return position
}
 
function round(value: number, decimalPoints: number): number {
    const roundingValue = Math.pow(10, decimalPoints)
    return Math.round(value * roundingValue) / roundingValue
}
 
function getCenteredRootPosition(rootFixedPosition: FixedPosition): FixedPosition {
    const centeredPosition: FixedPosition = { ...rootFixedPosition }
    centeredPosition.top = Math.floor(50 - centeredPosition.height / 2)
    centeredPosition.left = Math.floor(50 - centeredPosition.width / 2)
    return centeredPosition
}
 
function wrapFilePath(filePath: string): string {
    const filePathWithSlash = filePath.replaceAll(".", "/")
    const splitFilePath = filePath.split(".")
    const fileName = splitFilePath.slice(-1)
    // add file name again to the end because the file has been wrapped in a folder. (with the same name)
    return `/${BASE_NAME}/${filePathWithSlash}/${fileName}`
}
 
function createEdge(cycle: Cycle): Edge {
    return {
        fromNodeName: wrapFilePath(cycle.from),
        toNodeName: wrapFilePath(cycle.to),
        attributes: {
            coupling: 100
        }
    }
}
 
function createAttributeTypes(): AttributeTypes {
    return {
        edges: {
            coupling: AttributeTypeValue.relative
        }
    }
}
 
function createBaseGameObjectPosition(rootGameObjectScale: Coordinates): GameObject {
    const longEdge = Math.max(rootGameObjectScale.x, rootGameObjectScale.z)
    return {
        name: BASE_NAME,
        position: {
            x: 0,
            y: 0,
            z: 0
        },
        scale: {
            x: longEdge,
            y: 0,
            z: longEdge
        }
    }
}