Apply suggestions
This commit is contained in:
parent
25be8cb087
commit
7450ebd5e4
@ -266,16 +266,18 @@ end
|
|||||||
--------------------------------------------------------------------------------
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
local function parse_search_input(input)
|
local function parse_search_input(input)
|
||||||
-- Return nil if nothing to search for
|
|
||||||
if not input:find("%S") then
|
if not input:find("%S") then
|
||||||
return nil
|
return -- Return nil if nothing to search for
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Search is not case sensitive
|
||||||
|
input = input:lower()
|
||||||
|
|
||||||
local query = {keywords = {}, mods = {}, players = {}}
|
local query = {keywords = {}, mods = {}, players = {}}
|
||||||
|
|
||||||
-- Process quotation enclosed parts
|
-- Process quotation enclosed parts
|
||||||
input = input:gsub("([^ ]?)\"([^\"]*)\"([^ ]?)", function(before, match, after)
|
input = input:gsub('(%S?)"([^"]*)"(%S?)', function(before, match, after)
|
||||||
if #before == 0 and #after == 0 then -- Also have be separated by spaces
|
if before == "" and after == "" then -- Also have be separated by spaces
|
||||||
table.insert(query.keywords, match)
|
table.insert(query.keywords, match)
|
||||||
return " "
|
return " "
|
||||||
end
|
end
|
||||||
@ -290,15 +292,58 @@ local function parse_search_input(input)
|
|||||||
local player = word:match("^player:(.*)")
|
local player = word:match("^player:(.*)")
|
||||||
table.insert(query.players, player)
|
table.insert(query.players, player)
|
||||||
local game = word:match("^game:(.*)")
|
local game = word:match("^game:(.*)")
|
||||||
query.game = game
|
query.game = query.game or game
|
||||||
if not (mod or player or game) then
|
if not (mod or player or game) then
|
||||||
table.insert(query.keywords, word:lower())
|
table.insert(query.keywords, word)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return query
|
return query
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Returns false if filter doesn't match (special prefixes like "mod:", "game:", "player:")
|
||||||
|
-- otherwise returns true, name_matches, description_matches
|
||||||
|
local function matches_query(server, query)
|
||||||
|
-- Check if mods found
|
||||||
|
local mods_lower = {}
|
||||||
|
for j, mod in ipairs(server.mods or {}) do
|
||||||
|
mods_lower[j] = mod:lower()
|
||||||
|
end
|
||||||
|
for _, mod in ipairs(query.mods) do
|
||||||
|
if table.indexof(mods_lower, mod) < 0 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check if players found
|
||||||
|
local clients_list_lower = {}
|
||||||
|
for j, player in ipairs(server.clients_list or {}) do
|
||||||
|
clients_list_lower[j] = player:lower()
|
||||||
|
end
|
||||||
|
for _, player in ipairs(query.players) do
|
||||||
|
if table.indexof(clients_list_lower, player) < 0 then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Check if game matches
|
||||||
|
if query.game and query.game ~= server.gameid then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
local name_matches, description_matches = true, true
|
||||||
|
|
||||||
|
-- Check if keyword found
|
||||||
|
for _, keyword in ipairs(query.keywords) do
|
||||||
|
name_matches = name_matches and not not
|
||||||
|
(server.name or ""):lower():find(keyword, 1, true)
|
||||||
|
description_matches = description_matches and not not
|
||||||
|
(server.description or ""):lower():find(keyword, 1, true)
|
||||||
|
end
|
||||||
|
|
||||||
|
return true, name_matches, description_matches
|
||||||
|
end
|
||||||
|
|
||||||
local function search_server_list(input)
|
local function search_server_list(input)
|
||||||
menudata.search_result = nil
|
menudata.search_result = nil
|
||||||
if #serverlistmgr.servers < 2 then
|
if #serverlistmgr.servers < 2 then
|
||||||
@ -306,7 +351,7 @@ local function search_server_list(input)
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- setup the search query
|
-- setup the search query
|
||||||
local query = parse_search_input(input:lower())
|
local query = parse_search_input(input)
|
||||||
if not query then
|
if not query then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@ -316,44 +361,7 @@ local function search_server_list(input)
|
|||||||
-- Search the serverlist
|
-- Search the serverlist
|
||||||
local search_result = {}
|
local search_result = {}
|
||||||
for i, server in ipairs(serverlistmgr.servers) do
|
for i, server in ipairs(serverlistmgr.servers) do
|
||||||
local name_matches, description_matches, filter_matches = true, true, true
|
local filter_matches, name_matches, description_matches = matches_query(server, query)
|
||||||
|
|
||||||
-- Check if keyword found
|
|
||||||
for _, keyword in ipairs(query.keywords) do
|
|
||||||
name_matches = name_matches and not not
|
|
||||||
(server.name or ""):lower():find(keyword, 1, true)
|
|
||||||
description_matches = description_matches and not not
|
|
||||||
(server.description or ""):lower():find(keyword, 1, true)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Check if mods found
|
|
||||||
local mods_lower = {}
|
|
||||||
for j, mod in ipairs(server.mods or {}) do
|
|
||||||
mods_lower[j] = mod:lower()
|
|
||||||
end
|
|
||||||
for _, mod in ipairs(query.mods) do
|
|
||||||
if table.indexof(mods_lower, mod) < 0 then
|
|
||||||
filter_matches = false
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Check if players found
|
|
||||||
local clients_list_lower = {}
|
|
||||||
for j, player in ipairs(server.clients_list or {}) do
|
|
||||||
clients_list_lower[j] = player:lower()
|
|
||||||
end
|
|
||||||
for _, player in ipairs(query.players) do
|
|
||||||
if table.indexof(clients_list_lower, player) < 0 then
|
|
||||||
filter_matches = false
|
|
||||||
break
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Check if game matches
|
|
||||||
if query.game and query.game ~= server.gameid then
|
|
||||||
filter_matches = false
|
|
||||||
end
|
|
||||||
|
|
||||||
if filter_matches and (name_matches or description_matches) then
|
if filter_matches and (name_matches or description_matches) then
|
||||||
server.points = #serverlistmgr.servers - i
|
server.points = #serverlistmgr.servers - i
|
||||||
|
Loading…
Reference in New Issue
Block a user