-
Notifications
You must be signed in to change notification settings - Fork 3
/
init.lua
83 lines (68 loc) · 1.94 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
local worldpath = minetest.get_worldpath()
local gamefile = worldpath .. "/api/game.json"
local http = minetest.request_http_api()
local cache = {}
if table.indexof(minetest.get_dir_list(worldpath, true), "api") == -1 then
minetest.log("[server_stats] /api/ folder not found in world dir, aborting...")
return -- Comment out when testing
minetest.mkdir(worldpath .. "/api/")
end
local function get_player_list()
local player_names = {}
for _, player in ipairs(minetest.get_connected_players()) do
table.insert(player_names, player:get_player_name())
end
return player_names, #player_names
end
local function update(data)
local file = io.open(gamefile, "w")
file:write(minetest.write_json(data, true))
if http then
http.fetch_async({
url = "http://localhost:5173/api",
timeout = 10,
method = "PUT",
extra_headers = { "Content-Type: application/json" },
data = minetest.write_json(data),
})
end
file:close()
end
ctf_api.register_on_new_match(function()
cache.current_map = {
name = ctf_map.current_map.name,
technical_name = ctf_map.current_map.dirname,
start_time = os.time(), -- Can be converted to local date here https://www.unixtimestamp.com/
}
cache.current_mode = {
name = ctf_modebase.current_mode,
matches = ctf_modebase.current_mode_matches,
matches_played = ctf_modebase.current_mode_matches_played,
}
update(cache)
end)
minetest.register_on_joinplayer(function()
local player_names, player_count = get_player_list()
cache.player_info = {
players = player_names,
count = player_count
}
update(cache)
end)
minetest.register_on_leaveplayer(function()
minetest.after(0, function()
local player_names, player_count = get_player_list()
cache.player_info = {
players = player_names,
count = player_count
}
update(cache)
end)
end)
minetest.register_on_shutdown(function()
cache = {
error = 1,
error_message = "Server has shut down (or is restarting). Try again later",
}
update(cache)
end)