• 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

6.67
/commands/save_update.lua
1
-- update a region
2
function blockexchange.save_update(playername, origin, pos1, pos2, username, schema_uid)
2✔
3
        pos1, pos2 = blockexchange.sort_pos(pos1, pos2)
×
4

5
        local token = blockexchange.get_token(playername)
×
6

UNCOV
7
        local ctx = {
×
8
                type = "upload_update",
9
                playername = playername,
10
                username = username,
11
                schema_uid = schema_uid,
NEW
12
                progress_percent = 0
×
13
        }
14

NEW
15
        local mod_names = {}
×
16

NEW
17
        local promise = Promise.async(function(await)
×
NEW
18
                for current_pos, relative_pos, progress in blockexchange.iterator(origin, pos1, pos2) do
×
NEW
19
                        local current_pos2 = vector.add(current_pos, 15)
×
NEW
20
                        current_pos2.x = math.min(current_pos2.x, pos2.x)
×
NEW
21
                        current_pos2.y = math.min(current_pos2.y, pos2.y)
×
NEW
22
                        current_pos2.z = math.min(current_pos2.z, pos2.z)
×
23

NEW
24
                        ctx.progress_percent = math.floor(progress * 100 * 10) / 10
×
25

NEW
26
                        local data, node_count = blockexchange.serialize_part(current_pos, current_pos2)
×
NEW
27
                        blockexchange.collect_node_count(node_count, mod_names)
×
28

29
                        -- package data properly over the wire
NEW
30
                        local schemapart = {
×
31
                                schema_uid = schema_uid,
32
                                offset_x = relative_pos.x,
33
                                offset_y = relative_pos.y,
34
                                offset_z = relative_pos.z,
35
                                data = minetest.encode_base64(blockexchange.compress_data(data)),
NEW
36
                                metadata = minetest.encode_base64(blockexchange.compress_metadata(data))
×
37
                        }
38

39
                        -- upload part online
NEW
40
                        local _, err = await(blockexchange.api.create_schemapart(token, schemapart))
×
NEW
41
                        if err then
×
NEW
42
                                error("create error at " .. minetest.pos_to_string(relative_pos) .. ": " .. err, 0)
×
43
                        end
44

45
                        -- create an array with mod names
NEW
46
                        local mod_names_list = {}
×
NEW
47
                        for k in pairs(mod_names) do
×
NEW
48
                                table.insert(mod_names_list, k)
×
49
                        end
50

NEW
51
                        _, err = await(blockexchange.api.create_schemamods(token, schema_uid, mod_names_list))
×
NEW
52
                        if err then
×
NEW
53
                                error("error creating mod-list: " .. err, 0)
×
54
                        end
55
                end
56

NEW
57
                if ctx.cancel then
×
NEW
58
                        error("canceled", 0)
×
59
                end
60

NEW
61
                await(Promise.after(blockexchange.min_delay))
×
62
        end)
63

NEW
64
        blockexchange.set_job_context(playername, ctx, promise)
×
NEW
65
        return promise
×
66
end
67

68
-- update a region of a schema
69
function blockexchange.save_update_area(playername, pos1, pos2, save_pos1, save_pos2, username, schema_uid)
2✔
70
        -- clip to schema area
71
        save_pos1, save_pos2 = blockexchange.clip_area(pos1, pos2, save_pos1, save_pos2)
×
72

73
        -- get offset within schema area
74
        local offset_pos1 = blockexchange.get_schemapart_offset(pos1, save_pos1)
×
75
        local _, offset_pos2 = blockexchange.get_schemapart_offset(pos1, save_pos2)
×
76

77
        -- get absolute coords
78
        local abs_pos1 = vector.add(pos1, offset_pos1)
×
79
        local abs_pos2 = vector.add(pos1, offset_pos2)
×
80

81
        abs_pos1, abs_pos2 = blockexchange.clip_area(pos1, pos2, abs_pos1, abs_pos2)
×
82
        abs_pos1, abs_pos2 = blockexchange.sort_pos(abs_pos1, abs_pos2)
×
83

84
        return blockexchange.save_update(playername, pos1, abs_pos1, abs_pos2, username, schema_uid)
×
85
end
86

87
-- update a region on a single position
88
function blockexchange.save_update_pos(playername, pos1, pos2, pos, username, schema_uid)
2✔
89
        local offset_pos1, offset_pos2 = blockexchange.get_schemapart_offset(pos1, pos)
×
90
        local abs_pos1 = vector.add(pos1, offset_pos1)
×
91
        local abs_pos2 = vector.add(pos1, offset_pos2)
×
92

93
        abs_pos1, abs_pos2 = blockexchange.clip_area(pos1, pos2, abs_pos1, abs_pos2)
×
94
        return blockexchange.save_update(playername, pos1, abs_pos1, abs_pos2, username, schema_uid)
×
95
end
96

97
Promise.register_chatcommand("bx_save_update", {
2✔
98
    params = "[area_id?]",
99
    description = "uploads selected changes or the whole area",
100
    func = function(name, area_id)
101
        -- force-enable the hud
NEW
102
        blockexchange.set_player_hud(name, true)
×
103

NEW
104
        local area, err_msg = blockexchange.select_player_area(name, area_id)
×
NEW
105
        if err_msg then
×
NEW
106
            return true, err_msg
×
107
        end
108

NEW
109
        local claims = blockexchange.get_claims(name)
×
NEW
110
        if not claims then
×
NEW
111
            return true, "Not logged in"
×
112
        end
113

NEW
114
        if claims.username ~= area.username then
×
NEW
115
            return true, "You are not authorized to update that schema"
×
116
        end
117

NEW
118
        local pos1 = blockexchange.get_pos(1, name)
×
NEW
119
        local pos2 = blockexchange.get_pos(2, name)
×
120

NEW
121
        if not pos1 or not pos2 then
×
122
            -- upload everything
NEW
123
            pos1 = area.pos1
×
NEW
124
            pos2 = area.pos2
×
125
        end
126

127
        -- partial update with the marked area
NEW
128
        pos1, pos2 = blockexchange.sort_pos(pos1, pos2)
×
129

NEW
130
                return Promise.async(function(await)
×
NEW
131
                        local _, err = await(
×
NEW
132
                                blockexchange.save_update_area(name, area.pos1, area.pos2, pos1, pos2, claims.username, area.schema_uid)
×
133
                        )
NEW
134
                        if err then
×
NEW
135
                                error("save error: " .. err, 0)
×
136
                        end
137

138
                        local schema
NEW
139
                        schema, err = await(blockexchange.api.get_schema_by_uid(area.schema_uid))
×
NEW
140
                        if err then
×
NEW
141
                                error("fetch new schema error: " .. err, 0)
×
NEW
142
                        elseif not schema then
×
NEW
143
                                error("schema not found: " .. area.schema_uid, 0)
×
144
                        end
145

146
                        -- update mtime
NEW
147
                        area.mtime = schema.mtime
×
NEW
148
            blockexchange.save_areas()
×
149
                end)
150
    end
151
})
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