Update the timer in the formspec in real-time (#55)

This commit is contained in:
David Leal 2023-10-24 12:13:44 -06:00 committed by GitHub
parent c692fa2ed1
commit cf6a550378
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 7 deletions

@ -69,7 +69,12 @@ minetest.register_chatcommand("tpn", {
minetest.register_chatcommand("tpf", { minetest.register_chatcommand("tpf", {
description = S("Show all teleport requests, made by you or to you, that are still active"), description = S("Show all teleport requests, made by you or to you, that are still active"),
privs = {interact = true, tp = true}, privs = {interact = true, tp = true},
func = tp.list_requests func = function(player)
local playername = minetest.get_player_by_name(player)
tp.tpf_update_time[playername] = true
tp.list_requests(playername)
end
}) })
minetest.register_chatcommand("tpr_mute", { minetest.register_chatcommand("tpr_mute", {

@ -200,7 +200,9 @@ function tp.deny_request(id, own)
end end
end end
function tp.list_requests(playername) function tp.list_requests(player)
local playername = player:get_player_name()
local sent_requests = tp.get_requests(playername, "sender") local sent_requests = tp.get_requests(playername, "sender")
local received_requests = tp.get_requests(playername, "receiver") local received_requests = tp.get_requests(playername, "receiver")
local area_requests = tp.get_requests(playername, "area") local area_requests = tp.get_requests(playername, "area")
@ -277,7 +279,23 @@ function tp.list_requests(playername)
formspec = ("size[8,%f]label[1,0.3;%s:]"):format(math.min(y,10),S("Teleport Requests")) formspec = ("size[8,%f]label[1,0.3;%s:]"):format(math.min(y,10),S("Teleport Requests"))
..request_list_formspec ..request_list_formspec
end end
minetest.show_formspec(playername, "teleport_request_list", formspec) minetest.show_formspec(playername, "teleport_request_list", formspec)
local function update_time()
if formspec == "" or string.find(formspec, S("You have no requests.")) then
tp.tpf_update_time[player] = false
return
end
if tp.tpf_update_time[player] then
-- TODO: find a way to edit the text only and update
-- the formspec without re-calling the function.
tp.list_requests(player)
end
end
minetest.after(1, update_time)
end end
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
@ -307,8 +325,12 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
changes = true changes = true
end end
end end
if changes and not fields.quit then if changes and not fields.quit then
tp.list_requests(playername) tp.tpf_update_time[player] = true
tp.list_requests(player)
elseif fields.quit then
tp.tpf_update_time[player] = false
end end
end) end)

@ -31,10 +31,11 @@ local S = minetest.get_translator(minetest.get_current_modname())
tp = { tp = {
S = S, S = S,
tpr_list = {}, tpr_list = { },
tphr_list = {}, tphr_list = { },
tpc_list = {}, tpc_list = { },
tpn_list = {} tpn_list = { },
tpf_update_time = { }
} }
-- Clear requests when the player leaves -- Clear requests when the player leaves