skynet/tracking.lua

61 lines
1.1 KiB
Lua

local levenshtein = require("levenshtein")
local pd = peripheral.find("player_detector")
if not pd then
print("no player detector :(")
return
end
local tk = {}
tk.plrs = {}
function tk:refreshOne(unm)
local p = pd.getPlayerPos(unm)
self.plrs[unm] = p or {}
end
function tk:refresh()
self.plrs = {}
for _, x in next, pd.getOnlinePlayers() do
self:refreshOne(x)
end
end
function tk:scanRange(range)
return pd.getPlayersInRange(range)
end
function tk:resTg(tgr)
if not tgr then return nil end
tgr = string.lower(tgr)
local lst, lstv = math.huge, nil
for x, _ in next, self.plrs do
local xl = x:lower():sub(1, #tgr)
if xl == tgr then
return x
end
-- compare if first letter matches
if xl:sub(1, 1) == tgr:sub(1, 1) then
local dis = levenshtein(tgr, xl)
if dis < lst then
lst = dis
lstv = x
end
end
end
if lst >= 2 then
return nil --too far
end
return lstv
end
return tk