From 1b7a21e3226ecc99d26d5b82c468fec13581d095 Mon Sep 17 00:00:00 2001 From: red-001 Date: Fri, 6 Jan 2017 20:29:29 +0000 Subject: [PATCH] Add keyword based search to serverlist --- builtin/mainmenu/common.lua | 2 +- builtin/mainmenu/init.lua | 2 +- builtin/mainmenu/tab_credits.lua | 2 +- builtin/mainmenu/tab_multiplayer.lua | 116 +++++++++++++++++++++++--- builtin/mainmenu/tab_server.lua | 2 +- builtin/mainmenu/tab_settings.lua | 4 +- builtin/mainmenu/tab_simple_main.lua | 4 +- builtin/mainmenu/tab_singleplayer.lua | 4 +- 8 files changed, 113 insertions(+), 23 deletions(-) diff --git a/builtin/mainmenu/common.lua b/builtin/mainmenu/common.lua index da36678..5e3df08 100644 --- a/builtin/mainmenu/common.lua +++ b/builtin/mainmenu/common.lua @@ -77,7 +77,7 @@ function order_favorite_list(list) end -------------------------------------------------------------------------------- -function render_favorite(spec, is_favorite) +function render_serverlist_row(spec, is_favorite) local text = "" if spec.name then text = text .. core.formspec_escape(spec.name:trim()) diff --git a/builtin/mainmenu/init.lua b/builtin/mainmenu/init.lua index 7f0c1e3..1b527a0 100644 --- a/builtin/mainmenu/init.lua +++ b/builtin/mainmenu/init.lua @@ -128,7 +128,7 @@ local function init_globals() end -- Create main tabview - local tv_main = tabview_create("maintab", {x = 12, y = 5.2}, {x = 0, y = 0}) + local tv_main = tabview_create("maintab", {x = 12, y = 5.4}, {x = 0, y = 0}) if PLATFORM == "Android" then tv_main:add(tabs.simple_main) diff --git a/builtin/mainmenu/tab_credits.lua b/builtin/mainmenu/tab_credits.lua index c2ad191..80e03f8 100644 --- a/builtin/mainmenu/tab_credits.lua +++ b/builtin/mainmenu/tab_credits.lua @@ -85,7 +85,7 @@ return { "label[0.5,3.5;http://minetest.net]" .. "tablecolumns[color;text]" .. "tableoptions[background=#00000000;highlight=#00000000;border=false]" .. - "table[3.5,-0.25;8.5,5.8;list_credits;" .. + "table[3.5,-0.25;8.5,6.05;list_credits;" .. "#FFFF00," .. fgettext("Core Developers") .. ",," .. table.concat(core_developers, ",,") .. ",,," .. "#FFFF00," .. fgettext("Active Contributors") .. ",," .. diff --git a/builtin/mainmenu/tab_multiplayer.lua b/builtin/mainmenu/tab_multiplayer.lua index 00150f2..3c2b2cd 100644 --- a/builtin/mainmenu/tab_multiplayer.lua +++ b/builtin/mainmenu/tab_multiplayer.lua @@ -20,7 +20,16 @@ local function get_formspec(tabview, name, tabdata) -- Update the cached supported proto info, -- it may have changed after a change by the settings menu. common_update_cached_supp_proto() - local fav_selected = menudata.favorites[tabdata.fav_selected] + local fav_selected = nil + if menudata.search_result then + fav_selected = menudata.search_result[tabdata.fav_selected] + else + fav_selected = menudata.favorites[tabdata.fav_selected] + end + + if not tabdata.search_for then + tabdata.search_for = "" + end local retval = "label[7.75,-0.15;" .. fgettext("Address / Port") .. "]" .. @@ -33,7 +42,9 @@ local function get_formspec(tabview, name, tabdata) "field[8,1.95;2.95,0.5;te_name;;" .. core.formspec_escape(core.setting_get("name")) .. "]" .. "pwdfield[10.78,1.95;1.77,0.5;te_pwd;]" .. - "box[7.73,2.35;4.3,2.28;#999999]" + "box[7.73,2.35;4.3,2.28;#999999]".. + "field[0.15,0.25;4.5,0.27;te_search;;"..core.formspec_escape(tabdata.search_for).."]".. + "button[4.8,0;2.7,0.1;btn_mp_search;" .. fgettext("Search") .. "]" if tabdata.fav_selected and fav_selected then if gamedata.fav then @@ -58,9 +69,27 @@ local function get_formspec(tabview, name, tabdata) image_column(fgettext("PvP enabled"), "pvp") .. ",padding=0.25;" .. "color,span=1;" .. "text,padding=1]" .. - "table[-0.15,-0.1;7.75,5.5;favourites;" + "table[-0.15,0.4;7.75,5.35;favourites;" - if #menudata.favorites > 0 then + if menudata.search_result then + for i = 1, #menudata.search_result do + local favs = core.get_favorites("local") + local server = menudata.search_result[i] + + for fav_id = 1, #favs do + if server.address == favs[fav_id].address and + server.port == favs[fav_id].port then + server.is_favorite = true + end + end + + if i ~= 1 then + retval = retval .. "," + end + + retval = retval .. render_serverlist_row(server, server.is_favorite) + end + elseif #menudata.favorites > 0 then local favs = core.get_favorites("local") if #favs > 0 then for i = 1, #favs do @@ -75,9 +104,9 @@ local function get_formspec(tabview, name, tabdata) end end end - retval = retval .. render_favorite(menudata.favorites[1], (#favs > 0)) + retval = retval .. render_serverlist_row(menudata.favorites[1], (#favs > 0)) for i = 2, #menudata.favorites do - retval = retval .. "," .. render_favorite(menudata.favorites[i], (i <= #favs)) + retval = retval .. "," .. render_serverlist_row(menudata.favorites[i], (i <= #favs)) end end @@ -92,6 +121,8 @@ end -------------------------------------------------------------------------------- local function main_button_handler(tabview, fields, name, tabdata) + local serverlist = menudata.search_result or menudata.favorites + if fields.te_name then gamedata.playername = fields.te_name core.setting_set("name", fields.te_name) @@ -99,10 +130,10 @@ local function main_button_handler(tabview, fields, name, tabdata) if fields.favourites then local event = core.explode_table_event(fields.favourites) - local fav = menudata.favorites[event.row] + local fav = serverlist[event.row] if event.type == "DCL" then - if event.row <= #menudata.favorites then + if event.row <= #serverlist then if menudata.favorites_is_public and not is_server_protocol_compat_or_error( fav.proto_min, fav.proto_max) then @@ -131,7 +162,7 @@ local function main_button_handler(tabview, fields, name, tabdata) end if event.type == "CHG" then - if event.row <= #menudata.favorites then + if event.row <= #serverlist then gamedata.fav = false local favs = core.get_favorites("local") local address = fav.address @@ -157,7 +188,7 @@ local function main_button_handler(tabview, fields, name, tabdata) if fields.key_up or fields.key_down then local fav_idx = core.get_table_index("favourites") - local fav = menudata.favorites[fav_idx] + local fav = serverlist[fav_idx] if fav_idx then if fields.key_up and fav_idx > 1 then @@ -176,7 +207,7 @@ local function main_button_handler(tabview, fields, name, tabdata) local address = fav.address local port = fav.port - + gamedata.serverdescription = fav.description if address and port then core.setting_set("address", address) core.setting_set("remote_port", port) @@ -199,6 +230,65 @@ local function main_button_handler(tabview, fields, name, tabdata) return true end + if fields.btn_mp_search or fields.key_enter_field == "te_search" then + tabdata.fav_selected = 1 + local input = fields.te_search:lower() + tabdata.search_for = fields.te_search + + if #menudata.favorites < 2 then + return true + end + + menudata.search_result = {} + + -- setup the keyword list + local keywords = {} + for word in input:gmatch("%S+") do + table.insert(keywords, word) + end + + if #keywords == 0 then + menudata.search_result = nil + return true + end + + -- Search the serverlist + local search_result = {} + for i = 1, #menudata.favorites do + local server = menudata.favorites[i] + local found = 0 + for k = 1, #keywords do + local keyword = keywords[k] + if server.name then + local name = server.name:lower() + local _, count = name:gsub(keyword, keyword) + found = found + count * 4 + end + + if server.description then + local desc = server.description:lower() + local _, count = desc:gsub(keyword, keyword) + found = found + count * 2 + end + end + if found > 0 then + local points = (#menudata.favorites - i) / 5 + found + server.points = points + table.insert(search_result, server) + end + end + if #search_result > 0 then + table.sort(search_result, function(a, b) + return a.points > b.points + end) + menudata.search_result = search_result + local first_server = search_result[1] + core.setting_set("address", first_server.address) + core.setting_set("remote_port", first_server.port) + end + return true + end + if (fields.btn_mp_connect or fields.key_enter) and fields.te_address and fields.te_port then gamedata.playername = fields.te_name gamedata.password = fields.te_pwd @@ -206,9 +296,9 @@ local function main_button_handler(tabview, fields, name, tabdata) gamedata.port = fields.te_port gamedata.selected_world = 0 local fav_idx = core.get_table_index("favourites") - local fav = menudata.favorites[fav_idx] + local fav = serverlist[fav_idx] - if fav_idx and fav_idx <= #menudata.favorites and + if fav_idx and fav_idx <= #serverlist and fav.address == fields.te_address and fav.port == fields.te_port then diff --git a/builtin/mainmenu/tab_server.lua b/builtin/mainmenu/tab_server.lua index 6b96825..be57ad7 100644 --- a/builtin/mainmenu/tab_server.lua +++ b/builtin/mainmenu/tab_server.lua @@ -26,7 +26,7 @@ local function get_formspec(tabview, name, tabdata) "button[4,4.15;2.6,0.5;world_delete;" .. fgettext("Delete") .. "]" .. "button[6.5,4.15;2.8,0.5;world_create;" .. fgettext("New") .. "]" .. "button[9.2,4.15;2.55,0.5;world_configure;" .. fgettext("Configure") .. "]" .. - "button[8.5,4.95;3.25,0.5;start_server;" .. fgettext("Start Game") .. "]" .. + "button[8.5,5;3.25,0.5;start_server;" .. fgettext("Start Game") .. "]" .. "label[4,-0.25;" .. fgettext("Select World:") .. "]" .. "checkbox[0.25,0.25;cb_creative_mode;" .. fgettext("Creative Mode") .. ";" .. dump(core.setting_getbool("creative_mode")) .. "]" .. diff --git a/builtin/mainmenu/tab_settings.lua b/builtin/mainmenu/tab_settings.lua index af8df0c..e59572a 100644 --- a/builtin/mainmenu/tab_settings.lua +++ b/builtin/mainmenu/tab_settings.lua @@ -209,12 +209,12 @@ local function formspec(tabview, name, tabdata) .. fgettext("Reset singleplayer world") .. "]" else tab_string = tab_string .. - "button[8,4.75;3.75,0.5;btn_change_keys;" + "button[8,4.85;3.75,0.5;btn_change_keys;" .. fgettext("Change keys") .. "]" end tab_string = tab_string .. - "button[0,4.75;3.75,0.5;btn_advanced_settings;" + "button[0,4.85;3.75,0.5;btn_advanced_settings;" .. fgettext("Advanced Settings") .. "]" diff --git a/builtin/mainmenu/tab_simple_main.lua b/builtin/mainmenu/tab_simple_main.lua index 3818f32..a773a49 100644 --- a/builtin/mainmenu/tab_simple_main.lua +++ b/builtin/mainmenu/tab_simple_main.lua @@ -70,9 +70,9 @@ local function get_formspec(tabview, name, tabdata) end end end - retval = retval .. render_favorite(menudata.favorites[1], (#favs > 0)) + retval = retval .. render_serverlist_row(menudata.favorites[1], (#favs > 0)) for i = 2, #menudata.favorites do - retval = retval .. "," .. render_favorite(menudata.favorites[i], (i <= #favs)) + retval = retval .. "," .. render_serverlist_row(menudata.favorites[i], (i <= #favs)) end end diff --git a/builtin/mainmenu/tab_singleplayer.lua b/builtin/mainmenu/tab_singleplayer.lua index 05060cb..236de76 100644 --- a/builtin/mainmenu/tab_singleplayer.lua +++ b/builtin/mainmenu/tab_singleplayer.lua @@ -57,7 +57,7 @@ local function singleplayer_refresh_gamebar() local btnbar = buttonbar_create("game_button_bar", game_buttonbar_button_handler, - {x=-0.3,y=5.65}, "horizontal", {x=12.4,y=1.15}) + {x=-0.3,y=5.9}, "horizontal", {x=12.4,y=1.15}) for i=1,#gamemgr.games,1 do local btn_name = "game_btnbar_" .. gamemgr.games[i].id @@ -96,7 +96,7 @@ local function get_formspec(tabview, name, tabdata) "button[4,4.15;2.6,0.5;world_delete;".. fgettext("Delete") .. "]" .. "button[6.5,4.15;2.8,0.5;world_create;".. fgettext("New") .. "]" .. "button[9.2,4.15;2.55,0.5;world_configure;".. fgettext("Configure") .. "]" .. - "button[8.5,4.95;3.25,0.5;play;".. fgettext("Play") .. "]" .. + "button[8.5,5;3.25,0.5;play;".. fgettext("Play") .. "]" .. "label[4,-0.25;".. fgettext("Select World:") .. "]".. "checkbox[0.25,0.25;cb_creative_mode;".. fgettext("Creative Mode") .. ";" .. dump(core.setting_getbool("creative_mode")) .. "]"..