Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Bank server
- -- Channels
- local BANK_SERVER = 12
- local REGISTER_COMPUTER = 14
- local IMAGE_SERVER = 17
- local EVENT_MANAGER = 18
- local TURTLESCREEN_COMS = 19
- local CARDREADER = 21
- rednet.open("top")
- -- Libs
- -- Code
- local databasepath = "db"
- local db
- local defaultbalance = 0
- if not fs.exists(databasepath) then
- database = {}
- local file = fs.open(databasepath, "w")
- file.write("{}")
- file.close()
- else
- local file = fs.open(databasepath, "r")
- database = textutils.unserialise(file.readAll())
- file.close()
- end
- print("initialized database with success")
- while true do
- local id, data = rednet.receive("bank")
- print(textutils.serialise(data))
- if data.type == "getbalance" then
- print("getting balance of user ", data.user)
- rednet.send(id, database[data.user], "bank")
- elseif data.type == "setbalance" then
- print("setting balance of " .. data.user .. " to " .. data.balance)
- database[data.user].balance = data.balance
- local file = fs.open(databasepath, "w")
- file.write(textutils.serialise(database))
- file.close()
- rednet.send(id, nil, "bank")
- elseif data.type == "registeruser" then
- print("Registering user " .. data.user .. ":" .. data.name)
- database[data.user] = {
- name=data.name,
- balance=defaultbalance
- }
- local file = fs.open(databasepath, "w")
- file.write(textutils.serialise(database))
- file.close()
- rednet.send(id, nil, "bank")
- end
- end
Add Comment
Please, Sign In to add comment