discord highscore post

master
BuckarooBanzay 2020-03-10 11:49:13 +01:00
parent 864f7db982
commit dceaf1a33e
4 changed files with 124 additions and 1 deletions

105
discord_highscore.lua Normal file
View File

@ -0,0 +1,105 @@
local http = epic_score.http
local update_formspec = function(meta)
local topic = meta:get_string("topic")
meta:set_string("infotext", "Discord highscore post block: '" .. topic .. "'")
meta:set_string("formspec", "size[8,3;]" ..
-- col 2
"field[0.2,0.5;8,1;topic;Topic;" .. topic .. "]" ..
-- col 3
"field[0.2,1.5;8,1;url;URL (blanked, type to change);]" ..
-- col 4
"button_exit[0.1,2.5;8,1;save;Save]" ..
"")
end
local execute = function(webhook_url, topic)
local score_table = epic_score.get_score(topic)
if not score_table then
return
end
local content = "Highscore table for '" .. topic .. "'\n"
for rank, entry in ipairs(score_table) do
content = content .. "(" .. rank .. ") " ..
"**" .. entry.score .. "** " ..
"" .. entry.playername .. " \n";
end
local data = {
content = content
}
local json = minetest.write_json(data)
-- new rank
http.fetch({
url = webhook_url,
extra_headers = { "Content-Type: application/json" },
timeout = 5,
post_data = json
}, function()
-- ignore error
end)
end
minetest.register_node("epic_score:discord_highscore", {
description = "Epic discord highscore block",
tiles = {
"epic_node_bg.png",
"epic_node_bg.png",
"epic_node_bg.png",
"epic_node_bg.png",
"epic_node_bg.png",
"epic_node_bg.png^epic_discord.png",
},
paramtype2 = "facedir",
groups = {cracky=3,oddly_breakable_by_hand=3,epic=1},
on_rotate = screwdriver.rotate_simple,
after_place_node = function(pos, placer)
local meta = minetest.get_meta(pos)
meta:set_string("owner", placer:get_player_name())
meta:set_string("url", "")
meta:set_string("topic", "my maze")
meta:mark_as_private("url")
update_formspec(meta, pos)
end,
on_receive_fields = function(pos, _, fields, sender)
local meta = minetest.get_meta(pos);
if not sender or sender:get_player_name() ~= meta:get_string("owner") then
-- not allowed
return
end
if fields.save then
if fields.url and fields.url ~= "" then
-- only update if changed
meta:set_string("url", fields.url)
meta:mark_as_private("url")
end
meta:set_string("topic", fields.topic or "")
update_formspec(meta, pos)
end
end,
epic = {
on_enter = function(_, meta, _, ctx)
local url = meta:get_string("url")
local topic = meta:get_string("topic")
execute(url, topic)
ctx.next()
end
}
})

View File

@ -3,7 +3,8 @@ epic_score = {
hud = {
posx = tonumber(minetest.settings:get("epic_score.hud.offsetx") or 0.5),
posy = tonumber(minetest.settings:get("epic_score.hud.offsety") or 0.7)
}
},
http = minetest.request_http_api()
}
local MP = minetest.get_modpath("epic_score")
@ -23,9 +24,18 @@ dofile(MP.."/inventory_item_score.lua")
dofile(MP.."/highscore.lua")
dofile(MP.."/highscore_save.lua")
-- webhooks
if epic_score.http then
dofile(MP.."/discord_highscore.lua")
end
-- items
dofile(MP.."/items.lua")
-- forms
dofile(MP.."/forms/highscore_view.lua")
dofile(MP.."/forms/highscore_configure.lua")
-- clear http api after all is set up
epic_score.http = nil

View File

@ -18,6 +18,14 @@ Features
* score per item in inventory
* highscore board
# Setup
This mod can post the highscore to a discord webhook if the `http` api is available
minetest.conf
```
secure.http_mods = epic_score
```
# Licenses

BIN
textures/epic_discord.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 920 B