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

blockexchange / blockexchange / 13327716991

14 Feb 2025 10:53AM UTC coverage: 50.79% (+2.4%) from 48.384%
13327716991

push

github

web-flow
refactor to async/await (#74)

237 of 556 new or added lines in 15 files covered. (42.63%)

5 existing lines in 4 files now uncovered.

964 of 1898 relevant lines covered (50.79%)

1870.06 hits per line

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

55.42
/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
                type = "download",
12
                retries = 0,
13
                playername = playername,
1✔
14
                username = username,
1✔
15
                schemaname = schemaname,
1✔
16
                current_part = 0,
NEW
17
                progress_percent = 0
×
18
        }
19

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

29
                -- current mtime, if set
30
                local mtime = from_mtime or 0
1✔
31

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

38
                -- current schemapart
39
                local schemapart
40

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

95
                        -- compute stats
96
                        ctx.progress_percent = math.floor(ctx.current_part / total_parts * 100 * 10) / 10
8✔
97

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

100
                        if ctx.cancel then
8✔
NEW
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

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

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

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

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

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

140
        -- force-enable player-hud
NEW
141
        blockexchange.set_player_hud(name, true)
×
142

NEW
143
        return blockexchange.load(name, pos1, username, schemaname):next(function(result)
×
NEW
144
            return "Download complete with " .. result.schema.total_parts .. " parts"
×
145
        end)
146
    end
147
})
148

149
Promise.register_chatcommand("bx_load_update", {
2✔
150
    params = "[area_id?]",
151
    description = "downloads changes",
152
    privs = {blockexchange = true},
1✔
153
    func = function(name, area_id)
154
        -- force-enable the hud
NEW
155
        blockexchange.set_player_hud(name, true)
×
156

NEW
157
        local area, err_msg = blockexchange.select_player_area(name, area_id)
×
NEW
158
        if err_msg then
×
NEW
159
            return false, err_msg
×
160
        end
161

NEW
162
                return blockexchange.load(name, area.pos1, area.username, area.name, area.mtime):next(function(stat)
×
NEW
163
                        if stat.last_schemapart then
×
164
                                -- some parts have been updated
165
                                -- save last mtime
NEW
166
                                area.mtime = stat.last_schemapart.mtime
×
NEW
167
                                blockexchange.save_areas()
×
NEW
168
                                return "Load-update complete with " .. stat.total_parts .. " parts"
×
169
                        else
170
                                -- nothing has been updated
NEW
171
                                return "No updates available"
×
172
                        end
173
                end)
174
    end
175
})
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