• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

blockexchange / blockexchange / 13331571900

14 Feb 2025 02:48PM UTC coverage: 52.251% (+0.4%) from 51.84%
13331571900

push

github

BuckarooBanzay
deduplicate schemapart code

12 of 13 new or added lines in 5 files covered. (92.31%)

48 existing lines in 8 files now uncovered.

975 of 1866 relevant lines covered (52.25%)

1898.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.54
/commands/load.lua
1

2
--- load a schematic asynchronously
3
-- @param playername the playername to use in messages
4
-- @param pos1 lower position to load
5
-- @param username the username/owner of the schema
6
-- @param schemaname the name of the schema
7
-- @param[opt] from_mtime start with block mtime
8
-- @return a promise that resolves if the operation is complete
9
function blockexchange.load(playername, pos1, username, schemaname, from_mtime)
2✔
10
        local ctx = {
1✔
11
                hud_icon = "blockexchange_download.png",
12
                hud_text = "Download '" .. username .. "/" .. schemaname .. "' starting"
1✔
13
        }
14

15
        local retries = 0
1✔
16
        local current_part = 0
1✔
17

18
        local promise = Promise.async(function(await)
2✔
19
                local schema, err = await(blockexchange.api.get_schema_by_name(username, schemaname, true))
2✔
20
                if err then
1✔
21
                        error("error fetching schema: " .. err, 0)
×
22
                elseif not schema then
1✔
23
                        error("schema not found: '" .. username .. "/" .. schemaname .. "'", 0)
×
24
                end
25
                local pos2 = vector.add(pos1, blockexchange.get_schema_size(schema))
2✔
26

27
                -- current mtime, if set
28
                local mtime = from_mtime or 0
1✔
29

30
                local total_parts
31
                total_parts, err = await(blockexchange.api.count_next_schemapart_by_mtime(schema.uid, mtime))
3✔
32
                if err then
1✔
33
                        error("error fetching total parts: " .. err, 0)
×
34
                end
35

36
                -- current schemapart
37
                local schemapart
38

39
                while true do
40
                        if mtime > 0 then
9✔
41
                                -- incremental download by mtime
42
                                schemapart, err = await(blockexchange.api.get_next_schemapart_by_mtime(schema.uid,  mtime))
×
43
                                if err then
×
44
                                        -- retry later
45
                                        retries = retries + 1
×
46
                                        await(Promise.after(5))
×
47
                                elseif schemapart then
×
48
                                        -- success
49
                                        current_part = current_part + 1
×
50
                                        mtime = schemapart.mtime
×
51
                                        blockexchange.place_schemapart(schemapart, pos1)
×
52
                                else
53
                                        -- no more schemaparts
54
                                        break
55
                                end
56
                        else
57
                                -- full download
58
                                if not schemapart then
9✔
59
                                        -- first part
60
                                        schemapart, err = await(blockexchange.api.get_first_schemapart(schema.uid))
3✔
61
                                        if err then
1✔
62
                                                -- retry later
63
                                                retries = retries + 1
×
64
                                                await(Promise.after(5))
×
65
                                        elseif not schemapart then
1✔
66
                                                -- empty schema
67
                                                break
68
                                        else
69
                                                current_part = current_part + 1
1✔
70
                                                blockexchange.place_schemapart(schemapart, pos1)
1✔
71
                                        end
72
                                else
73
                                        -- other parts
74
                                        local pos = {
8✔
75
                                                x = schemapart.offset_x,
8✔
76
                                                y = schemapart.offset_y,
8✔
77
                                                z = schemapart.offset_z
8✔
78
                                        }
79
                                        schemapart, err = await(blockexchange.api.get_next_schemapart(schema.uid, pos))
24✔
80
                                        if err then
8✔
81
                                                retries = retries + 1
×
82
                                                await(Promise.after(5))
×
83
                                        elseif not schemapart then
8✔
84
                                                -- done
85
                                                break
1✔
86
                                        else
87
                                                current_part = current_part + 1
7✔
88
                                                blockexchange.place_schemapart(schemapart, pos1)
7✔
89
                                        end
90
                                end
91
                        end
92

93
                        -- compute stats
94
                        local progress_percent = math.floor(current_part / total_parts * 100 * 10) / 10
8✔
95
                        ctx.hud_text = "Downloading '" .. username .. "/" .. schemaname ..
8✔
96
                                "', progress: " .. progress_percent .. " %"
8✔
97

98
                        await(Promise.after(blockexchange.min_delay))
16✔
99

100
                        if ctx.cancel then
8✔
101
                                error("canceled", 0)
×
102
                        end
103
                end
104

105
                blockexchange.register_area(pos1, pos2, playername, username, schema)
1✔
106

107
                return {
1✔
108
                        total_parts = total_parts,
1✔
109
                        last_schemapart = schemapart,
1✔
110
                        schema = schema
1✔
111
                }
1✔
112
        end)
113

114
        blockexchange.set_job_context(playername, ctx, promise)
1✔
115
        return promise
1✔
116
end
117

118

119

120
Promise.register_chatcommand("bx_load", {
2✔
121
    params = "<username> <schemaname>",
122
    description = "Downloads a schema from the blockexchange to the selected pos1",
123
    privs = {blockexchange = true},
1✔
124
    func = function(name, param)
125

126
        if blockexchange.get_job_context(name) then
×
127
            return true, "There is a job already running"
×
128
        end
129

130
        local _, _, username, schemaname = string.find(param, "^([^%s]+)%s+(.*)$")
×
131

132
        if not username or not schemaname then
×
133
            return false, "Usage: /bx_load <username> <schemaname>"
×
134
        end
135

136
        local pos1 = blockexchange.get_pos(1, name)
×
137

138
        if not pos1 then return false, "you need to set /bx_pos1 first!" end
×
139

UNCOV
140
        return blockexchange.load(name, pos1, username, schemaname):next(function(result)
×
141
            return "Download complete with " .. result.schema.total_parts .. " parts"
×
142
        end)
143
    end
144
})
145

146
Promise.register_chatcommand("bx_load_update", {
2✔
147
    params = "[area_id?]",
148
    description = "downloads changes",
149
    privs = {blockexchange = true},
1✔
150
    func = function(name, area_id)
UNCOV
151
        local area, err_msg = blockexchange.select_player_area(name, area_id)
×
UNCOV
152
        if err_msg then
×
UNCOV
153
            return false, err_msg
×
154
        end
155

UNCOV
156
                return blockexchange.load(name, area.pos1, area.username, area.name, area.mtime):next(function(stat)
×
157
                        if stat.last_schemapart then
×
158
                                -- some parts have been updated
159
                                -- save last mtime
UNCOV
160
                                area.mtime = stat.last_schemapart.mtime
×
UNCOV
161
                                blockexchange.save_areas()
×
162
                                return "Load-update complete with " .. stat.total_parts .. " parts"
×
163
                        else
164
                                -- nothing has been updated
UNCOV
165
                                return "No updates available"
×
166
                        end
167
                end)
168
    end
169
})
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc