2
0

serverlistmgr.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. --Minetest
  2. --Copyright (C) 2020 rubenwardy
  3. --
  4. --This program is free software; you can redistribute it and/or modify
  5. --it under the terms of the GNU Lesser General Public License as published by
  6. --the Free Software Foundation; either version 2.1 of the License, or
  7. --(at your option) any later version.
  8. --
  9. --This program is distributed in the hope that it will be useful,
  10. --but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. --MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. --GNU Lesser General Public License for more details.
  13. --
  14. --You should have received a copy of the GNU Lesser General Public License along
  15. --with this program; if not, write to the Free Software Foundation, Inc.,
  16. --51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. serverlistmgr = {
  18. -- continent code we detected for ourselves
  19. my_continent = core.get_once("continent"),
  20. -- list of locally favorites servers
  21. favorites = nil,
  22. -- list of servers fetched from public list
  23. servers = nil,
  24. }
  25. --------------------------------------------------------------------------------
  26. -- Efficient data structure for normalizing arbitrary scores attached to objects
  27. -- e.g. {{"a", 3.14}, {"b", 3.14}, {"c", 20}, {"d", 0}}
  28. -- -> {["d"] = 0, ["a"] = 0.5, ["b"] = 0.5, ["c"] = 1}
  29. local Normalizer = {}
  30. function Normalizer:new()
  31. local t = {
  32. map = {}
  33. }
  34. setmetatable(t, self)
  35. self.__index = self
  36. return t
  37. end
  38. function Normalizer:push(obj, score)
  39. if not self.map[score] then
  40. self.map[score] = {}
  41. end
  42. local t = self.map[score]
  43. t[#t + 1] = obj
  44. end
  45. function Normalizer:calc()
  46. local list = {}
  47. for k, _ in pairs(self.map) do
  48. list[#list + 1] = k
  49. end
  50. table.sort(list)
  51. local ret = {}
  52. for i, k in ipairs(list) do
  53. local score = #list == 1 and 1 or ( (i - 1) / (#list - 1) )
  54. for _, obj in ipairs(self.map[k]) do
  55. ret[obj] = score
  56. end
  57. end
  58. return ret
  59. end
  60. --------------------------------------------------------------------------------
  61. -- how much the pre-sorted server list contributes to the final ranking
  62. local WEIGHT_SORT = 2
  63. -- how much the estimated latency contributes to the final ranking
  64. local WEIGHT_LATENCY = 1
  65. local function order_server_list(list)
  66. -- calculate the scores
  67. local s1 = Normalizer:new()
  68. local s2 = Normalizer:new()
  69. for i, fav in ipairs(list) do
  70. -- first: the original position
  71. s1:push(fav, #list - i)
  72. -- second: estimated latency
  73. local ping = (fav.ping or 0) * 1000
  74. if ping < 400 then
  75. -- If ping is under 400ms replace it with our own estimate,
  76. -- we assume the server has latency issues anyway otherwise
  77. ping = estimate_continent_latency(serverlistmgr.my_continent, fav) or 0
  78. end
  79. s2:push(fav, -ping)
  80. end
  81. s1 = s1:calc()
  82. s2 = s2:calc()
  83. -- make a shallow copy and pre-calculate ordering
  84. local res, order = {}, {}
  85. for i = 1, #list do
  86. local fav = list[i]
  87. res[i] = fav
  88. local n = s1[fav] * WEIGHT_SORT + s2[fav] * WEIGHT_LATENCY
  89. order[fav] = n
  90. end
  91. -- now sort the list
  92. table.sort(res, function(fav1, fav2)
  93. return order[fav1] > order[fav2]
  94. end)
  95. return res
  96. end
  97. local public_downloading = false
  98. local geoip_downloading = false
  99. --------------------------------------------------------------------------------
  100. function serverlistmgr.sync()
  101. if not serverlistmgr.servers then
  102. serverlistmgr.servers = {{
  103. name = fgettext("Loading..."),
  104. description = fgettext_ne("Try reenabling public serverlist and check your internet connection.")
  105. }}
  106. end
  107. local serverlist_url = core.settings:get("serverlist_url") or ""
  108. if not core.get_http_api or serverlist_url == "" then
  109. serverlistmgr.servers = {{
  110. name = fgettext("Public server list is disabled"),
  111. description = ""
  112. }}
  113. return
  114. end
  115. -- only fetched once per MT instance
  116. if not serverlistmgr.my_continent and not geoip_downloading then
  117. geoip_downloading = true
  118. core.handle_async(
  119. function(param)
  120. local http = core.get_http_api()
  121. local url = core.settings:get("serverlist_url") .. "/geoip"
  122. local response = http.fetch_sync({ url = url })
  123. if not response.succeeded then
  124. return
  125. end
  126. local retval = core.parse_json(response.data)
  127. return retval and type(retval.continent) == "string" and retval.continent
  128. end,
  129. nil,
  130. function(result)
  131. geoip_downloading = false
  132. if not result then
  133. return
  134. end
  135. serverlistmgr.my_continent = result
  136. core.set_once("continent", result)
  137. -- reorder list if we already have it
  138. if serverlistmgr.servers then
  139. serverlistmgr.servers = order_server_list(serverlistmgr.servers)
  140. core.event_handler("Refresh")
  141. end
  142. end
  143. )
  144. end
  145. if public_downloading then
  146. return
  147. end
  148. public_downloading = true
  149. core.handle_async(
  150. function(param)
  151. local http = core.get_http_api()
  152. local url = ("%s/list?proto_version_min=%d&proto_version_max=%d"):format(
  153. core.settings:get("serverlist_url"),
  154. core.get_min_supp_proto(),
  155. core.get_max_supp_proto())
  156. local response = http.fetch_sync({ url = url })
  157. if not response.succeeded then
  158. return {}
  159. end
  160. local retval = core.parse_json(response.data)
  161. return retval and retval.list or {}
  162. end,
  163. nil,
  164. function(result)
  165. public_downloading = false
  166. local favs = order_server_list(result)
  167. if favs[1] then
  168. serverlistmgr.servers = favs
  169. end
  170. core.event_handler("Refresh")
  171. end
  172. )
  173. end
  174. --------------------------------------------------------------------------------
  175. local function get_favorites_path(folder)
  176. local base = core.get_user_path() .. DIR_DELIM .. "client" .. DIR_DELIM .. "serverlist" .. DIR_DELIM
  177. if folder then
  178. return base
  179. end
  180. return base .. core.settings:get("serverlist_file")
  181. end
  182. --------------------------------------------------------------------------------
  183. local function save_favorites(favorites)
  184. local filename = core.settings:get("serverlist_file")
  185. -- If setting specifies legacy format change the filename to the new one
  186. if filename:sub(#filename - 3):lower() == ".txt" then
  187. core.settings:set("serverlist_file", filename:sub(1, #filename - 4) .. ".json")
  188. end
  189. assert(core.create_dir(get_favorites_path(true)))
  190. core.safe_file_write(get_favorites_path(), core.write_json(favorites))
  191. end
  192. --------------------------------------------------------------------------------
  193. function serverlistmgr.read_legacy_favorites(path)
  194. local file = io.open(path, "r")
  195. if not file then
  196. return nil
  197. end
  198. local lines = {}
  199. for line in file:lines() do
  200. lines[#lines + 1] = line
  201. end
  202. file:close()
  203. local favorites = {}
  204. local i = 1
  205. while i < #lines do
  206. local function pop()
  207. local line = lines[i]
  208. i = i + 1
  209. return line and line:trim()
  210. end
  211. if pop():lower() == "[server]" then
  212. local name = pop()
  213. local address = pop()
  214. local port = tonumber(pop())
  215. local description = pop()
  216. if name == "" then
  217. name = nil
  218. end
  219. if description == "" then
  220. description = nil
  221. end
  222. if not address or #address < 3 then
  223. core.log("warning", "Malformed favorites file, missing address at line " .. i)
  224. elseif not port or port < 1 or port > 65535 then
  225. core.log("warning", "Malformed favorites file, missing port at line " .. i)
  226. elseif (name and name:upper() == "[SERVER]") or
  227. (address and address:upper() == "[SERVER]") or
  228. (description and description:upper() == "[SERVER]") then
  229. core.log("warning", "Potentially malformed favorites file, overran at line " .. i)
  230. else
  231. favorites[#favorites + 1] = {
  232. name = name,
  233. address = address,
  234. port = port,
  235. description = description
  236. }
  237. end
  238. end
  239. end
  240. return favorites
  241. end
  242. --------------------------------------------------------------------------------
  243. local function read_favorites()
  244. local path = get_favorites_path()
  245. -- If new format configured fall back to reading the legacy file
  246. if path:sub(#path - 4):lower() == ".json" then
  247. local file = io.open(path, "r")
  248. if file then
  249. local json = file:read("*all")
  250. file:close()
  251. return core.parse_json(json)
  252. end
  253. path = path:sub(1, #path - 5) .. ".txt"
  254. end
  255. local favs = serverlistmgr.read_legacy_favorites(path)
  256. if favs then
  257. save_favorites(favs)
  258. os.remove(path)
  259. end
  260. return favs
  261. end
  262. --------------------------------------------------------------------------------
  263. local function delete_favorite(favorites, del_favorite)
  264. for i=1, #favorites do
  265. local fav = favorites[i]
  266. if fav.address == del_favorite.address and fav.port == del_favorite.port then
  267. table.remove(favorites, i)
  268. return
  269. end
  270. end
  271. end
  272. --------------------------------------------------------------------------------
  273. function serverlistmgr.get_favorites()
  274. if serverlistmgr.favorites then
  275. return serverlistmgr.favorites
  276. end
  277. serverlistmgr.favorites = {}
  278. -- Add favorites, removing duplicates
  279. local seen = {}
  280. for _, fav in ipairs(read_favorites() or {}) do
  281. local key = ("%s:%d"):format(fav.address:lower(), fav.port)
  282. if not seen[key] then
  283. seen[key] = true
  284. serverlistmgr.favorites[#serverlistmgr.favorites + 1] = fav
  285. end
  286. end
  287. return serverlistmgr.favorites
  288. end
  289. --------------------------------------------------------------------------------
  290. function serverlistmgr.add_favorite(new_favorite)
  291. assert(type(new_favorite.port) == "number")
  292. -- Whitelist favorite keys
  293. new_favorite = {
  294. name = new_favorite.name,
  295. address = new_favorite.address,
  296. port = new_favorite.port,
  297. description = new_favorite.description,
  298. }
  299. local favorites = serverlistmgr.get_favorites()
  300. delete_favorite(favorites, new_favorite)
  301. table.insert(favorites, 1, new_favorite)
  302. save_favorites(favorites)
  303. end
  304. --------------------------------------------------------------------------------
  305. function serverlistmgr.delete_favorite(del_favorite)
  306. local favorites = serverlistmgr.get_favorites()
  307. delete_favorite(favorites, del_favorite)
  308. save_favorites(favorites)
  309. end