Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- make a clients.txt, "whitelist.txt", log.txt, and "items.txt" file
- myusername = "username"
- mypassword = "password"
- function messageWhitelistItems(whitelist, itemlist)
- rednet.open("top")
- computers = getDatabase(whitelist)
- if #computers ~= 0 then
- for _, line in pairs(computers) do
- if tonumber(line) ~= nil then
- rednet.send(tonumber(line), "itemlist please")
- rednet.send(tonumber(line), getAllItems(itemlist))
- end
- end
- end
- end
- function displayLog(filename, scrollPosition, maxLines)
- local instruction = "Press Backspace to Leave"
- local database = getDatabase(filename) -- Retrieve the database using your existing function
- table.insert(database, 1, instruction)
- local startIndex = math.max(1, scrollPosition)
- local endIndex = math.min(#database, scrollPosition + maxLines - 1)
- while true do
- term.clear()
- -- Display the instruction
- print(instruction)
- -- Display the database content after the instruction
- for i = startIndex, endIndex do
- print(database[i])
- end
- local event, key = os.pullEvent("key")
- if key == keys.up then
- scrollPosition = math.max(1, scrollPosition - 1)
- elseif key == keys.down then
- scrollPosition = math.min(#database - maxLines + 1, scrollPosition + 1)
- elseif key == keys.backspace then
- break -- Exit loop on backspace key press
- end
- startIndex = math.max(1, scrollPosition)
- endIndex = math.min(#database, scrollPosition + maxLines - 1)
- end
- end
- function dateTime()
- time = os.epoch("local") / 1000
- time_table = os.date("*t", time)
- return os.date("%Y-%m-%d %H:%M:%S", currentTime)
- end
- function log(id, line, success)
- local file = fs.open("log.txt", "a")
- file.writeLine(dateTime() .. " | Computer " .. tostring(id) .. " - " .. line .. " | Success = " .. tostring(success))
- file.close()
- end
- function addItem(item, value, filename, whitelist)
- local file = fs.open(filename, "a") -- Open file in append mode
- if not file then
- print("Error: Unable to open file for appending.")
- return
- end
- file.writeLine(item .. " " .. value)
- file.close()
- messageWhitelistItems(whitelist, filename)
- end
- function getItem(item, filename)
- -- Open the file for reading
- local file = fs.open(filename, "r")
- -- Read and process each line of the file
- local line = file.readLine()
- while line do
- if twoInputStrip(line) == item then
- return line
- end
- line = file.readLine()
- end
- file.close()
- return nil
- end
- function getAllItems(filename)
- -- Open the file for reading
- local file = fs.open(filename, "r")
- -- Read and process each line of the file
- local line = file.readLine()
- local itemdatabase = {}
- while line do
- table.insert(itemdatabase, line)
- line = file.readLine()
- end
- file.close()
- return itemdatabase
- end
- function deleteItem(item, filename, whitelist)
- ditem = getItem(item, filename)
- if ditem ~= nil then
- database = getAllItems(filename)
- local file = fs.open(filename, "w")
- for _, line in pairs(database) do
- if line ~= ditem then
- file.write(line .. "\n")
- end
- end
- file.close()
- messageWhitelistItems(whitelist, filename)
- else
- print(number, " not found in database")
- end
- end
- function addComputer(number, filename)
- local file = fs.open(filename, "a") -- Open file in append mode
- if not file then
- print("Error: Unable to open file for appending.")
- return
- end
- -- Write the number to the file
- file.writeLine(tostring(number))
- file.close()
- print("Computer " .. number .. " added to the whitelist.")
- end
- function getComputer(number, filename)
- local file = fs.open(filename, "r")
- local line = file.readLine()
- while line do
- if tonumber(number) == tonumber(line) then
- -- Print the separated components
- file.close()
- return true
- end
- line = file.readLine()
- end
- file.close()
- return false
- end
- function deleteComputer(number, filename)
- if getComputer(number, filename) then
- database = getDatabase(filename)
- local file = fs.open(filename, "w")
- for _, line in pairs(database) do
- if tonumber(line) ~= tonumber(number) then
- file.write(line .. "\n")
- end
- end
- file.close()
- else
- print(number, " not found in database")
- end
- end
- function stripLine(line)
- username, password, value, address = line:match("([^|]+)|([^|]+)|([^|]+)|([^|]+)")
- return username, password, value, address
- end
- function twoInputStrip(str)
- first, second = str:match("(%S+)%s+(%S+)")
- return first, second
- end
- function threeInputStrip(str)
- first, second, third = str:match("(%S+)%s+(%S+)%s+(%S+)")
- return first, second, third
- end
- function fourInputStrip(str)
- first, second, third, fourth = str:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
- return first, second, third, fourth
- end
- function fiveInputStrip(str)
- first, second, third, fourth, five = str:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
- return first, second, third, fourth, five
- end
- function searchDatabase(u, filename)
- local file = fs.open(filename, "r")
- local line = file.readLine()
- while line do
- -- Split the line into string and double components
- local username, password, value = stripLine(line)
- if u == username and string.len(u) == string.len(username) then
- file.close()
- return username, password
- end
- -- Read the next line
- line = file.readLine()
- end
- file.close()
- end
- function getDatabase(filename)
- -- Open the file for reading
- local file = fs.open(filename, "r")
- -- Read and process each line of the file
- local line = file.readLine()
- local database = {}
- while line do
- table.insert(database, line)
- line = file.readLine()
- end
- file.close()
- return database
- end
- function getValue(u, filename)
- -- Open the file for reading
- local file = fs.open(filename, "r")
- -- Read and process each line of the file
- local line = file.readLine()
- while line do
- -- Split the line into string and double components
- local username, password, value = stripLine(line)
- if u == username then
- -- Print the separated components
- file.close()
- return value
- end
- -- Read the next line
- line = file.readLine()
- end
- file.close()
- end
- function getAddress(u, filename)
- -- Open the file for reading
- local file = fs.open(filename, "r")
- -- Read and process each line of the file
- local line = file.readLine()
- while line do
- -- Split the line into string and double components
- local username, password, value, address = stripLine(line)
- if u == username then
- -- Print the separated components
- file.close()
- return address
- end
- -- Read the next line
- line = file.readLine()
- end
- file.close()
- end
- function addUser(u, p, v, a, filename)
- -- Open the file for reading
- local file = fs.open(filename, "a")
- if searchDatabase(u, filename) == nil then
- file.write(u .. "|" .. p .. "|" .. v .. "|" .. a .. "\n")
- file.close()
- print("Client Added")
- return true
- else
- print("Client Already in Database")
- end
- file.close()
- end
- function deleteUser(u, filename)
- if searchDatabase(u, filename) ~= nil then
- database = getDatabase(filename)
- local file = fs.open(filename, "w")
- for _, line in pairs(database) do
- username, password, value = stripLine(line)
- if username ~= u then
- file.write(line .. "\n")
- end
- end
- file.close()
- else
- print(u, " not found in database")
- end
- end
- function changeUsername(u, nu, filename)
- if searchDatabase(u, filename) ~= nil then
- database = getDatabase(filename)
- local file = fs.open(filename, "w")
- for _, line in pairs(database) do
- username, password, value, address = stripLine(line)
- if username ~= u then
- file.write(line .. "\n")
- else
- file.write(nu .. "|" .. password .. "|" .. value .. "|" .. address .. "\n")
- end
- end
- file.close()
- else
- print(u, " not found in database")
- end
- end
- function changePassword(u, np, filename)
- if searchDatabase(u, filename) ~= nil then
- database = getDatabase(filename)
- local file = fs.open(filename, "w")
- for _, line in pairs(database) do
- username, password, value = stripLine(line)
- if username ~= u then
- file.write(line .. "\n")
- else
- file.write(username .. "|" .. np .. "|" .. value .. "|" .. address .. "\n")
- end
- end
- file.close()
- else
- print(u, " not found in database")
- end
- end
- function changeValue(u, nv, filename)
- if searchDatabase(u, filename) ~= nil then
- database = getDatabase(filename)
- local file = fs.open(filename, "w")
- for _, line in pairs(database) do
- username, password, value = stripLine(line)
- if username ~= u then
- file.write(line .. "\n")
- else
- file.write(username .. "|" .. password .. "|" .. nv .. "|" .. address .. "\n")
- end
- end
- file.close()
- end
- end
- function changeAddress(u, na, filename)
- if searchDatabase(u, filename) ~= nil then
- database = getDatabase(filename)
- local file = fs.open(filename, "w")
- for _, line in pairs(database) do
- username, password, value = stripLine(line)
- if username ~= u then
- file.write(line .. "\n")
- else
- file.write(username .. "|" .. password .. "|" .. value .. "|" .. na .. "\n")
- end
- end
- file.close()
- end
- end
- local function displayClientMenu()
- print("User Menu")
- print("1. Add user")
- print("2. Delete user")
- print("3. Change username")
- print("4. Change password")
- print("5. Change balance")
- print("6. Change address")
- print("7. Display database")
- print("8. Display user")
- print("0. Exit")
- end
- local function displayWhitelistMenu()
- print("1. Add a Computer")
- print("2. Display Whitelist")
- print("3. Delete Computer")
- print("4. Display Log")
- print("0. Exit")
- end
- local function itemMenu()
- print("1. Add an Item")
- print("2. Display Items")
- print("3. Remove an Item")
- print("0. Exit")
- end
- -- Function to display the menu
- local function displayMenu()
- print("User Menu")
- print("1. clientMenu")
- print("2. whitelistMenu")
- print("3. itemMenu")
- print("4. Turn Off Server")
- print("0. Exit")
- end
- -- Main function to handle user input
- local function main(filename, whitelist, itemlist)
- while true do
- shell.run("clear")
- displayMenu()
- io.write("Enter your choice: ")
- local gchoice = tonumber(io.read())
- if gchoice == 1 then
- shell.run("clear")
- while true do
- displayClientMenu()
- io.write("Enter your choice: ")
- local choice = tonumber(io.read())
- if choice == 1 then
- print("Adding a new user")
- print("Enter username:")
- local username = io.read()
- print("Enter password:")
- local password = io.read()
- print("Enter value:")
- local value = tonumber(io.read())
- print("Enter address (format: x,y,z):")
- local address = io.read()
- -- Add user to the database
- addUser(username, password, value, address, filename)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 2 then
- print("Deleting a user")
- print("Enter username to delete:")
- local username = io.read()
- -- Delete user from the database
- deleteUser(username, filename)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 3 then
- print("Changing username")
- print("Enter username to change:")
- local oldUsername = io.read()
- print("Enter new username:")
- local newUsername = io.read()
- -- Change username in the database
- changeUsername(oldUsername, newUsername, filename)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 4 then
- print("Changing password")
- print("Enter username:")
- local username = io.read()
- print("Enter new password:")
- local newPassword = io.read()
- -- Change password in the database
- changePassword(username, newPassword, filename)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 5 then
- print("Changing balance")
- print("Enter username:")
- local username = io.read()
- print("Enter new value:")
- local newValue = tonumber(io.read())
- -- Change value in the database
- changeValue(username, newValue, filename)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 6 then
- print("Changing address")
- print("Enter username:")
- local username = io.read()
- print("Enter new address:")
- local newAddress = io.read()
- changeAddress(username, newAddress, filename)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 7 then
- print("Displaying database")
- -- Display the database
- database = getDatabase(filename)
- print("\n")
- for _, line in pairs(database) do
- username, password, value, address = stripLine(line)
- print(username, " ", password, " ", value, " ", address, "\n")
- end
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 8 then
- print("Enter username:")
- local username = io.read()
- username, password = searchDatabase(username, filename)
- if username == nil then
- print("User not Found")
- else
- print(username, " ", password, " ", getValue(username, filename), "\n")
- end
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 0 then
- print("Exiting...")
- shell.run("clear")
- break
- else
- print("Invalid choice. Please try again.")
- end
- end
- elseif gchoice == 2 then
- shell.run("clear")
- while true do
- displayWhitelistMenu()
- io.write("Enter your choice: ")
- local choice = tonumber(io.read())
- if choice == 1 then
- print("Enter the Computers ID:")
- local id = io.read()
- addComputer(id, whitelist)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 2 then
- print("Displaying database")
- -- Display the database
- computers = getDatabase(whitelist)
- print("\n")
- for _, line in pairs(computers) do
- print(line, "\n")
- end
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 3 then
- print("Enter the Computers ID:")
- local id = io.read()
- deleteComputer(id, whitelist)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 4 then
- shell.run("clear")
- displayLog("log.txt", 1, 10)
- shell.run("clear")
- elseif choice == 0 then
- print("Exiting...")
- shell.run("clear")
- break
- else
- print("Invalid choice. Please try again.")
- end
- end
- elseif gchoice == 3 then
- while true do
- itemMenu()
- io.write("Enter your choice: ")
- local choice = tonumber(io.read())
- if choice == 1 then
- print("Adding a new item")
- print("Enter item minecraft name:")
- local iname = io.read()
- print("Enter item value:")
- local ivalue = io.read()
- -- Add user to the database
- addItem(iname, ivalue, itemlist, whitelist)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 2 then
- print("Displaying items")
- -- Display the database
- database = getAllItems(itemlist)
- print("\n")
- for _, line in pairs(database) do
- print(line, "\n")
- end
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 3 then
- print("Deleting an item")
- print("Enter item minecraft name:")
- local iname = io.read()
- deleteItem(iname, itemlist, whitelist)
- io.write("Press Anything to Continue: ")
- io.read()
- shell.run("clear")
- elseif choice == 0 then
- print("Exiting...")
- shell.run("clear")
- break
- else
- print("Invalid choice. Please try again.")
- end
- end
- elseif gchoice == 4 then
- return true
- elseif gchoice == 0 then
- print("Exiting...")
- shell.run("clear")
- break
- else
- print("Invalid choice. Please try again.")
- end
- end
- end
- local function serverCoroutine(filename, whitelist, itemlist)
- orders = {}
- while true do
- gsuccess = false
- -- Listen for incoming messages
- local senderId, message = rednet.receive()
- if getComputer(senderId, whitelist) then
- success, command, line = pcall(twoInputStrip, message)
- if success and line ~= nil then
- if command == "balance" then
- success, command, nusername, npassword, balancechange = pcall(fourInputStrip, message)
- if success and nusername ~= nil and npassword ~= nil and balancechange ~= nil then
- dusername, dpassword = searchDatabase(nusername, filename)
- if tostring(dusername) ~= nil and tostring(dpassword) ~= nil then
- if npassword == dpassword then
- nv = tonumber(getValue(nusername, filename)) + tonumber(balancechange)
- changeValue(nusername, nv, filename)
- rednet.send(senderId, getValue(nusername, filename))
- gsuccess = true
- end
- end
- end
- elseif command == "address" then
- success, command, nusername, npassword, balancechange = pcall(fourInputStrip, message)
- if success and nusername ~= nil then
- dusername, dpassword = searchDatabase(nusername, filename)
- daddress = getAddress(dusername, filename)
- if tostring(dusername) ~= nil and daddress ~= nil then
- rednet.send(senderId, daddress)
- gsuccess = true
- end
- end
- end
- end
- end
- success, command, line = pcall(twoInputStrip, message)
- if success and line ~= nil then
- if command == "buy" then
- success, command, nusername, npassword, balancechange = pcall(fourInputStrip, message)
- if success and nusername ~= nil and npassword ~= nil and balancechange ~= nil then
- dusername, dpassword = searchDatabase(nusername, filename)
- if tostring(dusername) ~= nil and tostring(dpassword) ~= nil then
- if npassword == dpassword then
- if tonumber(balancechange) >= 0 and tonumber(getValue(nusername, filename)) >= tonumber(balancechange) then
- nv = tonumber(getValue(nusername, filename)) - tonumber(balancechange)
- changeValue(nusername, nv, filename)
- rednet.send(senderId, getValue(nusername, filename))
- gsuccess = true
- if orders ~= nil then
- for i, order in ipairs(orders) do
- print(order)
- print(tonumber(order[2]) == tonumber(balancechange))
- print(tonumber(order[3]) == tonumber(id))
- print(tonumber(order[3]))
- print(tonumber(senderId))
- if tonumber(order[2]) == tonumber(balancechange) and tonumber(order[3]) == tonumber(senderId) then
- cashierId = tonumber(order[1])
- print(cashierId)
- rednet.send(cashierId, "true")
- table.remove(orders, i)
- break
- end
- end
- end
- end
- end
- end
- end
- elseif command == "search" then
- success, command, nusername, npassword = pcall(threeInputStrip, message)
- if success and nusername ~= nil and npassword ~= nil then
- dusername, dpassword = searchDatabase(nusername, filename)
- if tostring(dusername) ~= nil and tostring(dpassword) ~= nil then
- if npassword == dpassword then
- rednet.send(senderId, getValue(nusername, filename))
- gsuccess = true
- end
- end
- end
- elseif command == "transfer" then
- success, command, nusername, npassword, balancechange, nusername2 = pcall(fiveInputStrip, message)
- if success and nusername ~= nil and npassword ~= nil and balancechange ~= nil and nusername2 ~= nil and nusername ~= nusername2 then
- dusername, dpassword = searchDatabase(nusername, filename)
- nusername2 = searchDatabase(nusername2, filename)
- if tostring(dusername) ~= nil and tostring(dpassword) ~= nil then
- if npassword == dpassword then
- if nusername2 ~= nil then
- if tonumber(balancechange) > 0 and tonumber(balancechange) <= tonumber(getValue(nusername, filename)) then
- nv = tonumber(getValue(nusername, filename) - tonumber(balancechange))
- nv2 = tonumber(getValue(nusername2, filename) + tonumber(balancechange))
- changeValue(nusername, nv, filename)
- changeValue(nusername2, nv2, filename)
- rednet.send(senderId, tonumber(getValue(nusername, filename)))
- gsuccess = true
- end
- end
- end
- end
- end
- elseif command == "items" then
- rednet.send(senderId, getAllItems(itemlist))
- gsuccess = true
- elseif command == "serverbuyrequest" then
- command, balance, phoneID = threeInputStrip(message)
- if balance ~= nil and phoneID ~= nil then
- table.insert(orders, {senderId, balance, phoneID})
- gsuccess = true
- end
- elseif command == "removebuyrequest" then
- command, balance, phoneID = threeInputStrip(message)
- if orders ~= nil then
- for i, order in ipairs(orders) do
- if tonumber(id) == tonumber(order[1]) and tonumber(balance) == tonumber(order[2]) and tonumber(phoneID) == tonumber(order[3]) then
- table.remove(orders, i)
- gsuccess = true
- end
- end
- end
- end
- log(senderId, message, gsuccess)
- if gsuccess == false then
- rednet.send(senderId, "false")
- end
- end
- end
- end
- -- Main coroutine for the login screen
- local function loginCoroutine(filename, whitelist, itemlist)
- while true do
- -- Display login screen
- print("Login Screen")
- print("Enter username:")
- local username = read()
- print("Enter password:")
- local password = read("*")
- -- If login is successful, unlock the screen
- if username == myusername and password == mypassword then
- print("Login successful")
- if main(filename, whitelist, itemlist) then
- break
- end
- shell.run("clear")
- else
- print("Login failed")
- end
- end
- end
- os.pullEvent = os.pullEventRaw
- whitelist = "whitelist.txt"
- filename = "clients.txt"
- itemlist = "items.txt"
- if fs.exists(filename) then
- rednet.open("top")
- parallel.waitForAny(
- function() loginCoroutine(filename, whitelist, itemlist) end,
- function() serverCoroutine(filename, whitelist, itemlist) end
- )
- else
- print("No File to Protect")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement