dynmap.lua

by hugeblank
572 days agolua
COPY
1
local base, world, timestamp = "https://dynmap.sc3.io/", "SwitchCraft", os.epoch() -- "DIM1"
2
local out = {}
3

4
--[[Player object:
5
    {
6
        afk = false,
7
        name = "hugeblank",
8
        ...
9
    }
10
]]
11
out.getPositions = function()
12
    local response, data = out.getWorld()
13
    if response then
14
        local players = data.players
15
        for _, player in ipairs(players) do
16
            player.afk = player.name:find("?") ~= 1 -- Players who are afk have ??? prefixing their name in this field
17
            player.name = player.account
18
            player.account, player.health, player.armor, player.sort, player.type = nil, nil, nil, nil, nil -- defunct/useless values
19
        end
20
        return true, players
21
    else
22
        return false
23
    end
24
end
25

26
out.getWorld = function()
27
    local response, err = http.get(base.."up/world/"..world.."/"..timestamp)
28
    if response then
29
        local data = textutils.unserialiseJSON(response.readAll())
30
        response.close()
31
        return true, data
32
    else
33
        return false
34
    end
35
end
36

37

38
local function min(t)
39
    local m = t[1]
40
    for i = 2, #t do
41
        m = math.min(m, t[i])
42
    end
43
    return m
44
end
45

46
local function max(t)
47
    local m = t[1]
48
    for i = 2, #t do
49
        m = math.max(m, t[i])
50
    end
51
    return m
52
end
53

54
--[[ Claim Object
55
    {
56
        uuid = string,
57
        label = string,
58
        color = string,
59
        fillcolor = string, -- Same as `color` generally
60
        desc = string, -- HTML tag. Generally use `owner` instead.
61
        owner = string,
62
        xmin = number,
63
        xmax = number,
64
        zmin = number,
65
        zmax = number,
66
        ytop = number, -- Default: 64
67
        ybottom = number, -- Default: 64
68
        weight = number, -- Default: 2
69
        markup = boolean, -- Default: false
70
        opacity = number, -- Default: 0.8
71
        fillopacity = number, -- Default: 0.2
72
        x = table, -- Raw x values passed in by dynmap. Use xmin/xmax instead.
73
        z = table, -- Raw z values passed in by dynmap. Use zmin/zmax instead.
74
    }
75
]]
76
out.getClaims = function(includeAdmin, filterOwner)
77
    local response, err = http.get("https://dynmap.sc3.io/tiles/_markers_/marker_SwitchCraft.json")
78
    if response then
79
        local data = textutils.unserialiseJSON(response.readAll())
80
        response.close()
81
        local unparsed = data.sets["claimkit.claims"].areas
82
        if includeAdmin then
83
            for k, v in pairs(data.sets["claimkit.adminclaims"].areas) do
84
                v.owner = v.desc:gsub("<div><div style=\"text%-align:center;font%-size:20px;font%-weight:bold\">.*</div><div><i>Owned by <b>", ""):gsub("</b></i></div></div>", "")
85
                unparsed[k] = v
86
            end
87
        end
88
        local claims = {}
89
        for k, v in pairs(unparsed) do
90
            v.xmin, v.zmin = min(v.x), min(v.z)
91
            v.xmax, v.zmax = max(v.x), max(v.z)
92
            if not v.owner then
93
                v.owner = v.desc:gsub("<div><div style=\"text%-align:center;font%-size:20px;font%-weight:bold\">.*</div><div><b>Owner:</b> ", ""):gsub("</div></div>", "")
94
            end
95
            v.uuid = k
96
            if (filterOwner and v.owner == filterOwner) or not filterOwner then
97
                claims[#claims+1] = v
98
            end
99
        end
100
        return true, claims
101
    else
102
        return false
103
    end
104
end
105

106
out.getConfiguration = function()
107
    local response, err = http.get(base.."up/configuration")
108
    if response then
109
        local data = textutils.unserialiseJSON(response.readAll())
110
        response.close()
111
        return true, data
112
    else
113
        return false
114
    end
115
end
116

117
return out
118