From f39a524f716c5481067098eb12586e76baa396d2 Mon Sep 17 00:00:00 2001 From: Daleth Darko Date: Sat, 30 Oct 2021 17:52:49 +0200 Subject: [PATCH] update --- .gitignore | 1 + storage/database.lua | 43 ++++++++++++++++++++++++++++++++++++++++++- storage/remote.lua | 16 ++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 3f56ebd..17f6473 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ # Visual Studio Workspace File *.code-workspace +.history diff --git a/storage/database.lua b/storage/database.lua index a22654a..b23f4fc 100644 --- a/storage/database.lua +++ b/storage/database.lua @@ -22,19 +22,60 @@ function load(name) return textutils.unserialize(data) end local items = load("data") +local regex = load("data_regex") if not items then items = {} end +if not regex then + regex = {} +end + while true do local senderId, s, protocol = rednet.receive() if s.call == "set" then items[s.itemname] = s.item save(items, "data") + elseif s.call == "setr" then + local exists = false + for entry in regex do + if s.str == entry.str then + rednet.broadcast({["return"] = "exists", ["entry"] = entry}) + exists = true + break + end + + end + if not exists then + local i = #regex+1 + regex[i] = s.location + regex[i]["str"] = s.str + save(regex, "data_regex") + end elseif s.call == "get" then if not items[s.item] then - rednet.broadcast({["return"] = "none"}) + local found = false + for entry in regex do + if string.match(s.item, entry.str) then + rednet.broadcast({["return"] = "success", ["location"] = entry, ["item"] = s.item}) + found = true + break + end + end + if not found then rednet.broadcast({["return"] = "none"}) end else rednet.broadcast({["return"] = "success", ["location"] = items[s.item], ["item"] = s.item}) end + elseif s.call == "del" then + if items[s.item] then + items[s.item] = nil + else + for k, v in pairs(regex) do + if v.str == s.item then + regex[k] = nil + break + end + end + end + end end \ No newline at end of file diff --git a/storage/remote.lua b/storage/remote.lua index 4948f05..3e12d6c 100644 --- a/storage/remote.lua +++ b/storage/remote.lua @@ -72,6 +72,22 @@ if method == "set" then call["item"] = item rednet.broadcast(call) print(serializeTable(get(tArgs[2]))) +elseif method == "setr" then + if #tArgs < 6 then return end + call["call"] = "setr" + call["str"] = tArgs[2] + local item = {} + item["x"] = tArgs[3] + item["y"] = tArgs[4] + item["z"] = tArgs[5] + item["dir"] = dirs[tArgs[6]] + call["location"] = item + rednet.broadcast(call) + print(serializeTable(get(tArgs[2]))) +elseif method == "del" then + rednet.broadcast(call) + rednet.broadcast({["call"] = "get", ["del"] = tArgs[2]}) + print(serializeTable(get(tArgs[2]))) elseif method == "get" then print(serializeTable(get(tArgs[2]))) end