Template:ScriptingExamples/UtilityCode/Lua/getClientFromText
(Redirected from Template:ScriptingExamples/UtilityCode/getClientFromText)
function getClientFromText(text)
local clients = getClients() local textLower = text:lower() local textLength = text:len()
-- player id local textNumber = tonumber(text, 10) if textNumber then for i,client in ipairs(clients) do if client.player.id == textNumber then return client end end end
-- player full name (case-insensitive) for i,client in ipairs(clients) do if client.player.name:lower() == textLower then return client end end
-- player partial name (case-insensitive) for i,client in ipairs(clients) do if client.player.name:lower():find(textLower, 1, true) then return client end end
-- nearest player name match (case-insensitive) local charMatchCounts = {} for i,client in ipairs(clients) do local playerNameChars = {} local playerNameLower = client.player.name:lower() local playerNameLength = client.player.name:len() for i2=1, playerNameLength do table.insert(playerNameChars, playerNameLower:sub(i2, i2)) end local charMatchCount = 0 local playerNameCharIndex for i2=1, textLength do playerNameCharIndex = false local textChar = textLower:sub(i2, i2) for i3,playerNameChar in ipairs(playerNameChars) do if textChar == playerNameChar then charMatchCount = charMatchCount + 1 playerNameCharIndex = i3 break end end if playerNameCharIndex then table.remove(playerNameChars, playerNameCharIndex) end end charMatchCounts[i] = { client, charMatchCount / playerNameLength } end local highestCharMatchCount = 0 local highestCharMatchCountClient for i,data in ipairs(charMatchCounts) do if data[2] > highestCharMatchCount then highestCharMatchCount = data[2] highestCharMatchCountClient = data[1] end end if highestCharMatchCountClient then return highestCharMatchCountClient end
-- no match return false
end