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

blockexchange / blockexchange / 13331372500

14 Feb 2025 02:36PM UTC coverage: 51.84% (+1.1%) from 50.789%
13331372500

push

github

BuckarooBanzay
move hud code to commands

40 of 68 new or added lines in 9 files covered. (58.82%)

1 existing line in 1 file now uncovered.

972 of 1875 relevant lines covered (51.84%)

1890.14 hits per line

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

59.13
/commands/save.lua
1
local has_monitoring = minetest.get_modpath("monitoring")
1✔
2

3
local uploaded_blocks
4

5
if has_monitoring then
1✔
6
        uploaded_blocks = monitoring.counter(
×
7
                "blockexchange_uploaded_blocks",
8
                "number of successfully uploaded mapblocks"
9
        )
10
end
11

12
function blockexchange.save(playername, pos1, pos2, schemaname)
2✔
13
        pos1, pos2 = blockexchange.sort_pos(pos1, pos2)
2✔
14

15
        local token = blockexchange.get_token(playername)
1✔
16
        local claims = blockexchange.get_claims(playername)
1✔
17
        local license = blockexchange.get_license(playername)
1✔
18

19
        if not token or not claims then
1✔
20
                return Promise.rejected("not logged in")
×
21
        end
22

23
        local ctx = {
1✔
24
                hud_icon = "blockexchange_upload.png",
25
                hud_text = "Saving '" .. claims.username .. "/" .. schemaname .. "'"
1✔
26
        }
27

28
        local schema = {
1✔
29
                size_x = pos2.x - pos1.x + 1,
1✔
30
                size_y = pos2.y - pos1.y + 1,
1✔
31
                size_z = pos2.z - pos1.z + 1,
1✔
32
                description = "",
33
                license = license,
1✔
34
                name = schemaname
1✔
35
        }
36

37
        local total_size = 0
1✔
38
        local retries = 0
1✔
39
        local mod_names = {}
1✔
40
        local total_parts = 0
1✔
41

42
        local promise = Promise.async(function(await)
2✔
43
                local _, err
44
                schema, err = await(blockexchange.api.create_schema(token, schema))
3✔
45
                if err then
1✔
46
                        error("error creating schema: " .. err, 0)
×
47
                end
48

49
                for current_pos, relative_pos, progress in blockexchange.iterator(pos1, pos1, pos2) do
19✔
50
                        local current_pos2 = vector.add(current_pos, 15)
8✔
51
                        current_pos2.x = math.min(current_pos2.x, pos2.x)
8✔
52
                        current_pos2.y = math.min(current_pos2.y, pos2.y)
8✔
53
                        current_pos2.z = math.min(current_pos2.z, pos2.z)
8✔
54

55
                        local progress_percent = math.floor(progress * 100 * 10) / 10
8✔
56
                        ctx.hud_text = "Saving '" .. claims.username .. "/" .. schemaname ..
8✔
57
                                "', progress: " .. progress_percent .. " %"
8✔
58

59
                        local data, node_count, air_only = blockexchange.serialize_part(current_pos, current_pos2)
8✔
60
                        blockexchange.collect_node_count(node_count, mod_names)
8✔
61

62
                        if not air_only then
8✔
63
                                local schemapart = {
8✔
64
                                        schema_uid = schema.uid,
8✔
65
                                        offset_x = relative_pos.x,
8✔
66
                                        offset_y = relative_pos.y,
8✔
67
                                        offset_z = relative_pos.z,
8✔
68
                                        data = minetest.encode_base64(blockexchange.compress_data(data)),
16✔
69
                                        metadata = minetest.encode_base64(blockexchange.compress_metadata(data))
16✔
70
                                }
71

72
                                total_size = total_size + #schemapart.data + #schemapart.metadata
8✔
73
                                total_parts = total_parts + 1
8✔
74

75
                                while true do
76
                                        -- retry loop
77
                                        _, err = await(blockexchange.api.create_schemapart(token, schemapart))
24✔
78
                                        if err then
8✔
NEW
79
                                                if retries > 12 then
×
NEW
80
                                                        error("create schemapart failed after " .. retries .. " retries: " .. err, 0)
×
81
                                                end
82
                                                -- retry again later
83
                                                await(Promise.after(5))
×
NEW
84
                                                retries = retries + 1
×
85
                                        else
86
                                                -- saved successfully
87
                                                if has_monitoring then
8✔
88
                                                        uploaded_blocks.inc(1)
×
89
                                                end
90
                                                break
91
                                        end
92
                                end
93

94
                                -- TODO
95
                        end
96

97
                        if ctx.cancel then
8✔
98
                                error("canceled", 0)
×
99
                        end
100

101
                        await(Promise.after(blockexchange.min_delay))
16✔
102
                end
103

104
                -- create an array with mod names
105
                local mod_names_list = {}
1✔
106
                for k in pairs(mod_names) do
2✔
107
                        table.insert(mod_names_list, k)
1✔
108
                end
109

110
                _, err = await(blockexchange.api.create_schemamods(token, schema.uid, mod_names_list))
3✔
111
                if err then
1✔
112
                        error("error creating mod-list: " .. err, 0)
×
113
                end
114

115
                _, err = await(blockexchange.api.update_schema_stats(token, schema.uid))
3✔
116
                if err then
1✔
117
                        error("error updating stats: " .. err, 0)
×
118
                end
119

120
                -- get updated schema
121
                schema, err = await(blockexchange.api.get_schema_by_name(claims.username, schemaname))
3✔
122
                if err then
1✔
123
                        error("error fetching updated schema: " .. err, 0)
×
124
                elseif not schema then
1✔
125
                        error("saved schema not found: " .. claims.username .. "/" .. schemaname, 0)
×
126
                end
127

128
                -- register area
129
                blockexchange.register_area(pos1, pos2, playername, claims.username, schema)
1✔
130

131
                return {
1✔
132
                        total_parts = total_parts,
1✔
133
                        total_size = total_size,
1✔
134
                        schema = schema
1✔
135
                }
1✔
136
        end)
137

138
        blockexchange.set_job_context(playername, ctx, promise)
1✔
139
        return promise
1✔
140
end
141

142

143
Promise.register_chatcommand("bx_save", {
2✔
144
        params = "<name>",
145
        description = "Uploads the selected region to the blockexchange server",
146
        func = function(name, schemaname)
147
                if blockexchange.get_job_context(name) then
×
148
                        return true, "There is a job already running"
×
149
                end
150

151
                local has_protected_upload_priv = minetest.check_player_privs(name, { blockexchange_protected_upload = true })
×
152
                local has_blockexchange_priv = minetest.check_player_privs(name, { blockexchange = true })
×
153
                local has_protection_bypass_priv = minetest.check_player_privs(name, { protection_bypass = true })
×
154

155
                if not has_blockexchange_priv and not has_protected_upload_priv then
×
156
                        return true, "Required privs: 'blockexchange' or 'blockexchange_protected_upload'"
×
157
                end
158

159
                if not schemaname then
×
160
                        return true, "Usage: /bx_save <schemaname>"
×
161
                end
162

163
                if not blockexchange.validate_name(schemaname) then
×
164
                        return true, "schema name can only contain letters, numbers and a handful of special chars: - _ ."
×
165
                end
166

167
                local token = blockexchange.get_token(name)
×
168
                if not token then
×
169
                        -- TODO check validity
170
                        return true, "Please login first to upload a schematic"
×
171
                end
172

173
                local pos1 = blockexchange.get_pos(1, name)
×
174
                local pos2 = blockexchange.get_pos(2, name)
×
175

176
                if not pos1 or not pos2 then
×
177
                        return true, "you need to set /bx_pos1 and /bx_pos2 first!"
×
178
                end
179

180
                if not blockexchange.check_size(pos1, pos2) then
×
181
                        return true, "axis size limit of " .. blockexchange.max_size .. " nodes exceeded"
×
182
                end
183

184
                -- force-enable player-hud
185
                blockexchange.set_player_hud(name, true)
×
186

187
                return Promise.async(function(await)
×
188
                        local err, result
189
                        if not has_blockexchange_priv and has_protected_upload_priv and not has_protection_bypass_priv then
×
190
                                -- check protection first
191
                                result, err = await(blockexchange.protectioncheck(name, pos1, pos2))
×
192
                                if err then
×
193
                                        error("protection check error: " .. err, 0)
×
194
                                elseif not result.success then
×
195
                                        local msg = "protection check failed between pos " .. minetest.pos_to_string(result.pos1) ..
×
196
                                                " and " .. minetest.pos_to_string(result.pos2)
×
197
                                        error(msg, 0)
×
198
                                end
199
                        end
200

201
                        result, err = await(blockexchange.save(name, pos1, pos2, schemaname))
×
202
                        if err then
×
203
                                error("save error: " .. err)
×
204
                        end
205

206
                        return "save complete with " .. result.total_parts .. " parts and " .. result.total_size .. " bytes"
×
207
                end)
208
        end
209
})
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