• 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

9.46
/commands/allocate.lua
1

2
-- list of builtin mods
3
local builtin_mods = {
1✔
4
  air = true
×
5
}
6

7
local function get_missing_mods(mods)
8
  -- collect missing mods in a list
9
  local missing_mods = ""
×
10
  for _, modname in ipairs(mods) do
×
11
    if not builtin_mods[modname] and not minetest.get_modpath(modname) then
×
12
      if #missing_mods > 0 then
×
13
        -- add comma separator
14
        missing_mods = missing_mods .. ","
×
15
      end
16
      missing_mods = missing_mods .. modname
×
17
    end
18
  end
19

20
  return missing_mods
×
21
end
22

23
function blockexchange.allocate(playername, pos1, username, schemaname, local_load)
2✔
24

NEW
25
  return Promise.async(function(await)
×
NEW
26
    if local_load then
×
27
      -- local operation
NEW
28
      local filename = blockexchange.get_local_filename(schemaname)
×
NEW
29
      local f = io.open(filename, "rb")
×
NEW
30
      if not f then
×
NEW
31
        error("file not found: " .. filename, 0)
×
32
      end
NEW
33
      local z, err_msg = mtzip.unzip(f)
×
NEW
34
      if err_msg then
×
NEW
35
        error("unzip error: " .. err_msg, 0)
×
36
      end
37

38
      local schema_str
NEW
39
      schema_str, err_msg = z:get("schema.json", true)
×
NEW
40
      if err_msg then
×
NEW
41
        error("schema.json error: " .. err_msg, 0)
×
42
      end
NEW
43
      local schema = minetest.parse_json(schema_str)
×
44
      if not schema then
×
NEW
45
        error("Schema not found: '" .. schemaname .. "'", 0)
×
46
      end
NEW
47
      local pos2 = vector.add(pos1, blockexchange.get_schema_size(schema))
×
NEW
48
      pos2 = vector.subtract(pos2, 1)
×
49

NEW
50
      blockexchange.set_pos(2, playername, pos2)
×
51

52
      local mods_str
NEW
53
      mods_str, err_msg = z:get("mods.json")
×
NEW
54
      if err_msg then
×
NEW
55
        error("mods.json error: " .. err_msg, 0)
×
56
      end
NEW
57
      f:close()
×
58

NEW
59
      local mods = minetest.parse_json(mods_str)
×
NEW
60
      local missing_mods = get_missing_mods(mods)
×
61

NEW
62
      return {
×
63
        schema = schema,
NEW
64
        missing_mods = missing_mods
×
65
      }
66
    else
67
      -- online
NEW
68
      local schema, err = await(blockexchange.api.get_schema_by_name(username, schemaname, false))
×
NEW
69
      if err then
×
NEW
70
        error("fetch schema error: " .. err, 0)
×
NEW
71
      elseif not schema then
×
NEW
72
        error("Schema not found: '" .. username .. "/" .. schemaname .. "'", 0)
×
73
      end
74

75
      local pos2 = vector.add(pos1, blockexchange.get_schema_size(schema))
×
76
      pos2 = vector.subtract(pos2, 1)
×
77
      blockexchange.set_pos(2, playername, pos2)
×
78

79
      local mods
NEW
80
      mods, err = await(blockexchange.api.get_schemamods(schema.uid))
×
NEW
81
      if err then
×
NEW
82
        error("fetch mods error: " .. err, 0)
×
83
      end
84

NEW
85
      local missing_mods = get_missing_mods(mods)
×
NEW
86
      return {
×
87
        schema = schema,
NEW
88
        missing_mods = missing_mods
×
89
      }
90
    end
91
  end)
92
end
93

94
local function allocate_reporter(result)
NEW
95
  local msg = "Total parts: " .. result.schema.total_parts ..
×
NEW
96
    " total size: " .. result.schema.total_size .. " bytes"
×
97

NEW
98
  if #result.missing_mods > 0 then
×
NEW
99
    msg = msg .. ", "
×
NEW
100
    msg = msg .. minetest.colorize("#ff0000", "missing mods: " .. result.missing_mods)
×
101
  end
102

NEW
103
  return msg
×
104
end
105

106
Promise.register_chatcommand("bx_allocate_local", {
2✔
107
  params = "<schemaname>",
108
  description = "Show where the selected schema would end up",
109
  privs = { blockexchange = true },
1✔
110
  func = function(name, schemaname)
NEW
111
    local pos1 = blockexchange.get_pos(1, name)
×
112

NEW
113
    if not pos1 then
×
NEW
114
      return false, "you need to set /bx_pos1 first!"
×
115
    end
116

NEW
117
    if not schemaname or schemaname == "" then
×
NEW
118
      return false, "Usage: /bx_allocate_local <schemaname>"
×
119
    end
120

NEW
121
    return blockexchange.allocate(name, pos1, name, schemaname, true):next(allocate_reporter)
×
122
  end
123
})
124

125
if blockexchange.is_online then
1✔
126
  Promise.register_chatcommand("bx_allocate", {
2✔
127
    params = "<username> <schemaname>",
128
    description = "Show where the selected schema would end up",
129
    privs = { blockexchange = true },
1✔
130
    func = function(name, param)
NEW
131
      local pos1 = blockexchange.get_pos(1, name)
×
132

NEW
133
      if not pos1 then
×
NEW
134
        return false, "you need to set /bx_pos1 first!"
×
135
      end
136

NEW
137
      local _, _, username, schemaname = string.find(param, "^([^%s]+)%s+(.*)$")
×
NEW
138
      if not username or not schemaname then
×
NEW
139
        return false, "Usage: /bx_allocate <username> <schemaname>"
×
140
      end
141

NEW
142
      return blockexchange.allocate(name, pos1, username, schemaname):next(allocate_reporter)
×
143
    end
144
  })
145
end
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