mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
Apply "and" to server list & content search terms (#15365)
This commit is contained in:
parent
e952a0807b
commit
0e06590ffd
2 changed files with 28 additions and 36 deletions
|
@ -563,30 +563,32 @@ function contentdb.filter_packages(query, by_type)
|
||||||
end
|
end
|
||||||
|
|
||||||
local keywords = {}
|
local keywords = {}
|
||||||
for word in query:lower():gmatch("%S+") do
|
for word in query:gmatch("%S+") do
|
||||||
table.insert(keywords, word)
|
table.insert(keywords, word:lower())
|
||||||
|
end
|
||||||
|
|
||||||
|
local function contains_all_keywords(str)
|
||||||
|
str = str:lower()
|
||||||
|
for _, keyword in ipairs(keywords) do
|
||||||
|
if not str:find(keyword, 1, true) then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
local function matches_keywords(package)
|
local function matches_keywords(package)
|
||||||
for k = 1, #keywords do
|
return contains_all_keywords(package.name) or
|
||||||
local keyword = keywords[k]
|
contains_all_keywords(package.title) or
|
||||||
|
contains_all_keywords(package.author) or
|
||||||
if string.find(package.name:lower(), keyword, 1, true) or
|
contains_all_keywords(package.short_description)
|
||||||
string.find(package.title:lower(), keyword, 1, true) or
|
|
||||||
string.find(package.author:lower(), keyword, 1, true) or
|
|
||||||
string.find(package.short_description:lower(), keyword, 1, true) then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
return false
|
|
||||||
end
|
end
|
||||||
|
|
||||||
contentdb.packages = {}
|
contentdb.packages = {}
|
||||||
for _, package in pairs(contentdb.packages_full) do
|
for _, package in pairs(contentdb.packages_full) do
|
||||||
if (query == "" or matches_keywords(package)) and
|
if (query == "" or matches_keywords(package)) and
|
||||||
(by_type == nil or package.type == by_type) then
|
(by_type == nil or package.type == by_type) then
|
||||||
contentdb.packages[#contentdb.packages + 1] = package
|
table.insert(contentdb.packages, package)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -195,8 +195,7 @@ local function search_server_list(input)
|
||||||
-- setup the keyword list
|
-- setup the keyword list
|
||||||
local keywords = {}
|
local keywords = {}
|
||||||
for word in input:gmatch("%S+") do
|
for word in input:gmatch("%S+") do
|
||||||
word = word:gsub("(%W)", "%%%1")
|
table.insert(keywords, word:lower())
|
||||||
table.insert(keywords, word)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if #keywords == 0 then
|
if #keywords == 0 then
|
||||||
|
@ -207,26 +206,17 @@ local function search_server_list(input)
|
||||||
|
|
||||||
-- Search the serverlist
|
-- Search the serverlist
|
||||||
local search_result = {}
|
local search_result = {}
|
||||||
for i = 1, #serverlistmgr.servers do
|
for i, server in ipairs(serverlistmgr.servers) do
|
||||||
local server = serverlistmgr.servers[i]
|
local name_matches, description_matches = true, true
|
||||||
local found = 0
|
for _, keyword in ipairs(keywords) do
|
||||||
for k = 1, #keywords do
|
name_matches = name_matches and not not
|
||||||
local keyword = keywords[k]
|
(server.name or ""):lower():find(keyword, 1, true)
|
||||||
if server.name then
|
description_matches = description_matches and not not
|
||||||
local sername = server.name:lower()
|
(server.description or ""):lower():find(keyword, 1, true)
|
||||||
local _, count = sername: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
|
end
|
||||||
if found > 0 then
|
if name_matches or description_matches then
|
||||||
local points = (#serverlistmgr.servers - i) / 5 + found
|
server.points = #serverlistmgr.servers - i
|
||||||
server.points = points
|
+ (name_matches and 50 or 0)
|
||||||
table.insert(search_result, server)
|
table.insert(search_result, server)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue