summaryrefslogtreecommitdiff
path: root/src/Api/entityDecoder.ts
blob: 96d2247a10636f2ffb06387a3986053511c2dc48 (plain) (blame)
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
import { Portal, Link, Field } from "./types";

export const decodeEntity = (entity: any[]): Portal | Link | Field | null => {
    switch (entity[2][0]) {
        case 'r':
            // Field
            return decodeField(entity[2])
        case 'p':
            //Portal
            return decodePortal(entity[2])
        case 'e':
            // Link
            return decodeLink(entity[2])
        default:
            return null
    }
}

export const decodePortal = (e: any[]): Portal => {
    return new Portal(
        e[8],
        e[7],
        e[1],
        { latitude: e[2] / 1E6, longitude: e[3] / 1E6, guid: e[8] },
        e[4],
        e[6],
        e[10],
        e[5],
        e[13],
        e[15],
        e[14],
        e[16],
    )
}

export const decodeField = (e: any[]): Field => {
    return new Field(
        e[1],
        [
            { latitude: e[2][0][1] / 1E6, longitude: e[2][0][2] / 1E6, guid: e[2][0][0] },
            { latitude: e[2][1][1] / 1E6, longitude: e[2][1][2] / 1E6, guid: e[2][1][0] },
            { latitude: e[2][2][1] / 1E6, longitude: e[2][2][2] / 1E6, guid: e[2][2][0] }
        ]
    )
}

export const decodeLink = (e: any[]): Link => {
    return new Link(
        e[1],
        [
            { latitude: e[3] / 1E6, longitude: e[4] / 1E6, guid: e[2] },
            { latitude: e[6] / 1E6, longitude: e[7] / 1E6, guid: e[5] }
        ]
    )
}