Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- rednet.open("top")
- local requestTypes = {"userExists", "checkPassword", "getBalance", "getLoan"}
- local postTypes = {"newUser", "newPassword", "removeUser", "changeBalance", "changeLoan"}
- local dbDir = "disk/database.txt"
- function readTable(dir)
- local file = fs.open(dir, "r")
- local data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- function saveTable(table, dir)
- local file = fs.open(dir, "w")
- file.write(textutils.serialize(table))
- file.close()
- end
- if not fs.exists(dbDir) then
- saveTable({}, dbDir)
- end
- function handleRequests(data)
- local requestType = "false_request"
- for i, type in pairs(requestTypes) do
- if type == data[1] then
- requestType = type
- end
- end
- if requestType == "false_request" then
- print("false_request TRUE")
- return "false_request"
- end
- if requestType == "userExists" then
- local db = readTable(dbDir)
- if db[data[2]] then
- print("userExists TRUE")
- return "true"
- else
- print("userExists FALSE")
- return "false"
- end
- elseif requestType == "checkPassword" then
- local db = readTable(dbDir)
- if db[data[2]] then
- if db[data[2]].password == data[3] then
- print("checkPassword TRUE")
- return "true"
- else
- print("checkPassword FALSE")
- return "false"
- end
- else
- print("checkPassword FALSE")
- return "false"
- end
- elseif requestType == "getBalance" then
- local db = readTable(dbDir)
- if db[data[2]] then
- print("getBalance TRUE")
- return db[data[2]].bal
- else
- print("getBalance FALSE")
- return "false"
- end
- elseif requestType == "getLoan" then
- local db = readTable(dbDir)
- if db[data[2]] then
- print("getLoan TRUE")
- return db[data[2]].loan
- else
- print("getLoan FALSE")
- return "false"
- end
- end
- end
- function handlePosts(data)
- local postType = "false_post"
- for i, type in pairs(postTypes) do
- if type == data[1] then
- postType = type
- end
- end
- if requestType == "false_post" then
- print("false_post TRUE")
- return "false_post"
- end
- if postType == "newUser" then
- local db = readTable(dbDir)
- local exists = handleRequests({"userExists", data[2]})
- if exists == "true" then
- print("newUser FALSE")
- return "false"
- else
- if data[3] ~= "" then
- db[data[2]] = {}
- db[data[2]].password = data[3]
- db[data[2]].bal = 0
- db[data[2]].loan = 0
- saveTable(db, dbDir)
- print("newUser TRUE")
- return "true"
- end
- end
- elseif postType == "removeUser" then
- local db = readTable(dbDir)
- local exists = handleRequests({"userExists", data[2]})
- if exists == "true" then
- db[data[2]] = nil
- saveTable(db, dbDir)
- print("removeUser TRUE")
- return "true"
- else
- print("removeUser FALSE")
- return "false"
- end
- elseif postType == "newPassword" then
- local db = readTable(dbDir)
- local exists = handleRequests({"userExists", data[2]})
- if exists == "true" then
- db[data[2]].password = data[3]
- saveTable(db, dbDir)
- print("newPassword TRUE")
- return "true"
- else
- print("newPassword FALSE")
- return "false"
- end
- elseif postType == "changeBalance" then
- local db = readTable(dbDir)
- local exists = handleRequests({"userExists", data[2]})
- if exists == "true" then
- db[data[2]].bal = db[data[2]].bal + data[3]
- saveTable(db, dbDir)
- print("changeBalance TRUE")
- return "true"
- else
- print("changeBalance FALSE")
- return "false"
- end
- elseif postType == "changeLoan" then
- local db = readTable(dbDir)
- local exists = handleRequests({"userExists", data[2]})
- if exists == "true" then
- db[data[2]].loan = db[data[2]].loan + data[3]
- saveTable(db, dbDir)
- print("changeLoan TRUE")
- return "true"
- else
- print("changeLoan FALSE")
- return "false"
- end
- end
- end
- while true do
- local senderID, receivedMessage = rednet.receive("graviBank")
- if type(receivedMessage) ~= "table" then
- print("isTable FALSE")
- rednet.send(senderID, {"unrecognized_datatype"}, "graviBank")
- else
- if receivedMessage[1] == "request" then
- rednet.send(senderID, {handleRequests(receivedMessage[2])}, "graviBank")
- elseif receivedMessage[1] == "post" then
- rednet.send(senderID, {handlePosts(receivedMessage[2])}, "graviBank")
- else
- print("isCorrectMessagetype FALSE")
- rednet.send(senderID, {"unrecognized_messagetype"}, "graviBank")
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement