1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Move values the mainmenu caches to dedicated files (#14433)

This commit is contained in:
sfan5 2024-03-30 11:06:28 +01:00 committed by GitHub
parent bb6782ca58
commit 6c6e48f006
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 142 additions and 132 deletions

View file

@ -17,7 +17,7 @@
serverlistmgr = {
-- continent code we detected for ourselves
my_continent = core.get_once("continent"),
my_continent = nil,
-- list of locally favorites servers
favorites = nil,
@ -26,6 +26,15 @@ serverlistmgr = {
servers = nil,
}
do
if check_cache_age("geoip_last_checked", 3600) then
local tmp = cache_settings:get("geoip") or ""
if tmp:match("^[A-Z][A-Z]$") then
serverlistmgr.my_continent = tmp
end
end
end
--------------------------------------------------------------------------------
-- Efficient data structure for normalizing arbitrary scores attached to objects
-- e.g. {{"a", 3.14}, {"b", 3.14}, {"c", 20}, {"d", 0}}
@ -112,6 +121,22 @@ local public_downloading = false
local geoip_downloading = false
--------------------------------------------------------------------------------
local function fetch_geoip()
local http = core.get_http_api()
local url = core.settings:get("serverlist_url") .. "/geoip"
local response = http.fetch_sync({ url = url })
if not response.succeeded then
return
end
local retval = core.parse_json(response.data)
if type(retval) ~= "table" then
return
end
return type(retval.continent) == "string" and retval.continent
end
function serverlistmgr.sync()
if not serverlistmgr.servers then
serverlistmgr.servers = {{
@ -129,37 +154,23 @@ function serverlistmgr.sync()
return
end
-- only fetched once per MT instance
if not serverlistmgr.my_continent and not geoip_downloading then
geoip_downloading = true
core.handle_async(
function(param)
local http = core.get_http_api()
local url = core.settings:get("serverlist_url") .. "/geoip"
local response = http.fetch_sync({ url = url })
if not response.succeeded then
return
end
local retval = core.parse_json(response.data)
return retval and type(retval.continent) == "string" and retval.continent
end,
nil,
function(result)
geoip_downloading = false
if not result then
return
end
serverlistmgr.my_continent = result
core.set_once("continent", result)
-- reorder list if we already have it
if serverlistmgr.servers then
serverlistmgr.servers = order_server_list(serverlistmgr.servers)
core.event_handler("Refresh")
end
core.handle_async(fetch_geoip, nil, function(result)
geoip_downloading = false
if not result then
return
end
)
serverlistmgr.my_continent = result
cache_settings:set("geoip", result)
cache_settings:set("geoip_last_checked", tostring(os.time()))
-- re-sort list if applicable
if serverlistmgr.servers then
serverlistmgr.servers = order_server_list(serverlistmgr.servers)
core.event_handler("Refresh")
end
end)
end
if public_downloading then
@ -167,6 +178,7 @@ function serverlistmgr.sync()
end
public_downloading = true
-- note: this isn't cached because it's way too dynamic
core.handle_async(
function(param)
local http = core.get_http_api()