CelticCoder

bankserver

May 6th, 2024 (edited)
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.31 KB | None | 0 0
  1. -- make a clients.txt, "whitelist.txt", log.txt, and "items.txt" file
  2. myusername = "username"
  3. mypassword = "password"
  4.  
  5. function messageWhitelistItems(whitelist, itemlist)
  6.     rednet.open("top")
  7.     computers = getDatabase(whitelist)
  8.     if #computers ~= 0 then
  9.         for _, line in pairs(computers) do
  10.             if tonumber(line) ~= nil then
  11.                 rednet.send(tonumber(line), "itemlist please")
  12.                 rednet.send(tonumber(line), getAllItems(itemlist))
  13.             end
  14.         end
  15.     end
  16. end
  17.  
  18. function displayLog(filename, scrollPosition, maxLines)
  19.     local instruction = "Press Backspace to Leave"
  20.     local database = getDatabase(filename)  -- Retrieve the database using your existing function
  21.     table.insert(database, 1, instruction)
  22.     local startIndex = math.max(1, scrollPosition)
  23.     local endIndex = math.min(#database, scrollPosition + maxLines - 1)
  24.    
  25.     while true do
  26.         term.clear()
  27.         -- Display the instruction
  28.         print(instruction)
  29.  
  30.         -- Display the database content after the instruction
  31.         for i = startIndex, endIndex do
  32.             print(database[i])
  33.         end
  34.        
  35.         local event, key = os.pullEvent("key")
  36.         if key == keys.up then
  37.             scrollPosition = math.max(1, scrollPosition - 1)
  38.         elseif key == keys.down then
  39.             scrollPosition = math.min(#database - maxLines + 1, scrollPosition + 1)
  40.         elseif key == keys.backspace then
  41.             break  -- Exit loop on backspace key press
  42.         end
  43.  
  44.         startIndex = math.max(1, scrollPosition)
  45.         endIndex = math.min(#database, scrollPosition + maxLines - 1)
  46.     end
  47. end
  48.  
  49.  
  50. function dateTime()
  51.     time = os.epoch("local") / 1000
  52.     time_table = os.date("*t", time)
  53.     return os.date("%Y-%m-%d %H:%M:%S", currentTime)
  54. end
  55.  
  56. function log(id, line, success)
  57.     local file = fs.open("log.txt", "a")
  58.     file.writeLine(dateTime() .. " | Computer " .. tostring(id) .. " - " .. line .. " | Success = " .. tostring(success))
  59.     file.close()
  60. end
  61.  
  62. function addItem(item, value, filename, whitelist)
  63.     local file = fs.open(filename, "a") -- Open file in append mode
  64.    
  65.     if not file then
  66.         print("Error: Unable to open file for appending.")
  67.         return
  68.     end
  69.  
  70.     file.writeLine(item .. " " .. value)
  71.     file.close()
  72.     messageWhitelistItems(whitelist, filename)
  73. end
  74.  
  75. function getItem(item, filename)
  76.     -- Open the file for reading
  77.     local file = fs.open(filename, "r")
  78.    
  79.     -- Read and process each line of the file
  80.     local line = file.readLine()
  81.     while line do
  82.         if twoInputStrip(line) == item then
  83.             return line
  84.         end
  85.         line = file.readLine()
  86.     end
  87.     file.close()
  88.     return nil
  89. end
  90.  
  91. function getAllItems(filename)
  92.     -- Open the file for reading
  93.     local file = fs.open(filename, "r")
  94.    
  95.     -- Read and process each line of the file
  96.     local line = file.readLine()
  97.     local itemdatabase = {}
  98.     while line do
  99.         table.insert(itemdatabase, line)
  100.         line = file.readLine()
  101.     end
  102.     file.close()
  103.     return itemdatabase
  104. end
  105.  
  106. function deleteItem(item, filename, whitelist)
  107.     ditem = getItem(item, filename)
  108.     if ditem ~= nil then
  109.         database = getAllItems(filename)
  110.         local file = fs.open(filename, "w")
  111.         for _, line in pairs(database) do
  112.             if line ~= ditem then
  113.                 file.write(line .. "\n")
  114.             end
  115.         end
  116.         file.close()
  117.         messageWhitelistItems(whitelist, filename)
  118.     else
  119.         print(number, " not found in database")
  120.     end
  121. end
  122.  
  123. function addComputer(number, filename)
  124.     local file = fs.open(filename, "a") -- Open file in append mode
  125.    
  126.     if not file then
  127.         print("Error: Unable to open file for appending.")
  128.         return
  129.     end
  130.    
  131.     -- Write the number to the file
  132.     file.writeLine(tostring(number))
  133.     file.close()
  134.    
  135.     print("Computer " .. number .. " added to the whitelist.")
  136. end
  137.  
  138.  
  139. function getComputer(number, filename)
  140.     local file = fs.open(filename, "r")
  141.     local line = file.readLine()
  142.     while line do
  143.         if tonumber(number) == tonumber(line) then
  144.             -- Print the separated components
  145.             file.close()
  146.             return true
  147.         end
  148.         line = file.readLine()
  149.     end
  150.     file.close()
  151.     return false
  152. end
  153.  
  154. function deleteComputer(number, filename)
  155.     if getComputer(number, filename) then
  156.         database = getDatabase(filename)
  157.         local file = fs.open(filename, "w")
  158.         for _, line in pairs(database) do
  159.             if tonumber(line) ~= tonumber(number) then
  160.                 file.write(line .. "\n")
  161.             end
  162.         end
  163.         file.close()
  164.     else
  165.         print(number, " not found in database")
  166.     end
  167. end
  168.  
  169. function stripLine(line)
  170.     username, password, value, address = line:match("([^|]+)|([^|]+)|([^|]+)|([^|]+)")
  171.     return username, password, value, address
  172. end
  173.  
  174. function twoInputStrip(str)
  175.     first, second = str:match("(%S+)%s+(%S+)")
  176.     return first, second
  177. end
  178.  
  179. function threeInputStrip(str)
  180.     first, second, third = str:match("(%S+)%s+(%S+)%s+(%S+)")
  181.     return first, second, third
  182. end
  183.  
  184. function fourInputStrip(str)
  185.     first, second, third, fourth = str:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
  186.     return first, second, third, fourth
  187. end
  188.  
  189. function fiveInputStrip(str)
  190.     first, second, third, fourth, five = str:match("(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)")
  191.     return first, second, third, fourth, five
  192. end
  193.  
  194. function searchDatabase(u, filename)
  195.     local file = fs.open(filename, "r")
  196.     local line = file.readLine()
  197.     while line do
  198.         -- Split the line into string and double components
  199.         local username, password, value = stripLine(line)
  200.         if u == username and string.len(u) == string.len(username) then
  201.             file.close()
  202.             return username, password
  203.         end
  204.        
  205.         -- Read the next line
  206.         line = file.readLine()
  207.     end
  208.     file.close()
  209. end
  210.  
  211. function getDatabase(filename)
  212.      -- Open the file for reading
  213.     local file = fs.open(filename, "r")
  214.    
  215.     -- Read and process each line of the file
  216.     local line = file.readLine()
  217.     local database = {}
  218.     while line do
  219.         table.insert(database, line)
  220.         line = file.readLine()
  221.     end
  222.     file.close()
  223.     return database
  224. end
  225.  
  226.  
  227. function getValue(u, filename)
  228.     -- Open the file for reading
  229.     local file = fs.open(filename, "r")
  230.    
  231.     -- Read and process each line of the file
  232.     local line = file.readLine()
  233.     while line do
  234.         -- Split the line into string and double components
  235.         local username, password, value = stripLine(line)
  236.        
  237.         if u == username then
  238.             -- Print the separated components
  239.             file.close()
  240.             return value
  241.         end
  242.        
  243.         -- Read the next line
  244.         line = file.readLine()
  245.     end
  246.     file.close()
  247. end
  248.  
  249. function getAddress(u, filename)
  250.     -- Open the file for reading
  251.     local file = fs.open(filename, "r")
  252.    
  253.     -- Read and process each line of the file
  254.     local line = file.readLine()
  255.     while line do
  256.         -- Split the line into string and double components
  257.         local username, password, value, address = stripLine(line)
  258.        
  259.         if u == username then
  260.             -- Print the separated components
  261.             file.close()
  262.             return address
  263.         end
  264.        
  265.         -- Read the next line
  266.         line = file.readLine()
  267.     end
  268.     file.close()
  269. end
  270.  
  271. function addUser(u, p, v, a, filename)
  272.     -- Open the file for reading
  273.     local file = fs.open(filename, "a")
  274.     if searchDatabase(u, filename) == nil then
  275.         file.write(u .. "|" .. p .. "|" .. v .. "|" .. a .. "\n")
  276.         file.close()
  277.         print("Client Added")
  278.         return true
  279.     else
  280.         print("Client Already in Database")
  281.     end
  282.     file.close()
  283. end
  284.  
  285. function deleteUser(u, filename)
  286.     if searchDatabase(u, filename) ~= nil then
  287.         database = getDatabase(filename)
  288.         local file = fs.open(filename, "w")
  289.         for _, line in pairs(database) do
  290.             username, password, value = stripLine(line)
  291.             if username ~= u then
  292.                 file.write(line .. "\n")
  293.             end
  294.         end
  295.         file.close()
  296.     else
  297.         print(u, " not found in database")
  298.     end
  299. end
  300.  
  301. function changeUsername(u, nu, filename)
  302.     if searchDatabase(u, filename) ~= nil then
  303.         database = getDatabase(filename)
  304.         local file = fs.open(filename, "w")
  305.         for _, line in pairs(database) do
  306.             username, password, value, address = stripLine(line)
  307.             if username ~= u then
  308.                 file.write(line .. "\n")
  309.             else
  310.                 file.write(nu .. "|" .. password .. "|" .. value .. "|" .. address .. "\n")
  311.             end
  312.         end
  313.         file.close()
  314.     else
  315.         print(u, " not found in database")
  316.     end
  317. end
  318.  
  319. function changePassword(u, np, filename)
  320.     if searchDatabase(u, filename) ~= nil then
  321.         database = getDatabase(filename)
  322.         local file = fs.open(filename, "w")
  323.         for _, line in pairs(database) do
  324.             username, password, value = stripLine(line)
  325.             if username ~= u then
  326.                 file.write(line .. "\n")
  327.             else
  328.                 file.write(username .. "|" .. np .. "|" .. value .. "|" .. address .. "\n")
  329.             end
  330.         end
  331.         file.close()
  332.     else
  333.         print(u, " not found in database")
  334.     end
  335. end
  336.  
  337. function changeValue(u, nv, filename)
  338.     if searchDatabase(u, filename) ~= nil then
  339.         database = getDatabase(filename)
  340.         local file = fs.open(filename, "w")
  341.         for _, line in pairs(database) do
  342.             username, password, value = stripLine(line)
  343.             if username ~= u then
  344.                 file.write(line .. "\n")
  345.             else
  346.                 file.write(username .. "|" .. password .. "|" .. nv .. "|" .. address .. "\n")
  347.             end
  348.         end
  349.         file.close()
  350.     end
  351. end
  352.  
  353. function changeAddress(u, na, filename)
  354.     if searchDatabase(u, filename) ~= nil then
  355.         database = getDatabase(filename)
  356.         local file = fs.open(filename, "w")
  357.         for _, line in pairs(database) do
  358.             username, password, value = stripLine(line)
  359.             if username ~= u then
  360.                 file.write(line .. "\n")
  361.             else
  362.                 file.write(username .. "|" .. password .. "|" .. value .. "|" .. na .. "\n")
  363.             end
  364.         end
  365.         file.close()
  366.     end
  367. end
  368.  
  369. local function displayClientMenu()
  370.     print("User Menu")
  371.     print("1. Add user")
  372.     print("2. Delete user")
  373.     print("3. Change username")
  374.     print("4. Change password")
  375.     print("5. Change balance")
  376.     print("6. Change address")
  377.     print("7. Display database")
  378.     print("8. Display user")
  379.     print("0. Exit")
  380. end
  381.  
  382. local function displayWhitelistMenu()
  383.     print("ATM Item Menu")
  384.     print("1. Add a Computer")
  385.     print("2. Display Whitelist")
  386.     print("3. Delete Computer")
  387.     print("4. Display Log")
  388.     print("0. Exit")
  389. end
  390.  
  391. local function displayVMWhitelistMenu()
  392.     print("VM Item Menu")
  393.     print("1. Add a Computer")
  394.     print("2. Display Whitelist")
  395.     print("3. Delete Computer")
  396.     print("4. Display Log")
  397.     print("0. Exit")
  398. end
  399.  
  400. local function ATMItemMenu()
  401.     print("ATM Item Menu")
  402.     print("1. Add an Item")
  403.     print("2. Display Items")
  404.     print("3. Remove an Item")
  405.     print("0. Exit")
  406. end
  407.  
  408. local function VMItemMenu()
  409.     print("Vending Machine Item Menu")
  410.     print("1. Add an Item")
  411.     print("2. Display Items")
  412.     print("3. Remove an Item")
  413.     print("0. Exit")
  414. end
  415.  
  416. -- Function to display the menu
  417. local function displayMenu()
  418.     print("User Menu")
  419.     print("1. clientMenu")
  420.     print("2. whitelistMenu")
  421.     print("3. ATMItemMenu")
  422.     print("4. VMwhitelistMenu")
  423.     print("5. VMItemMenu")
  424.     print("6. Turn Off Server")
  425.     print("0. Exit")
  426. end
  427.  
  428. -- Main function to handle user input
  429. local function main(filename, whitelist, vmwhitelist, itemlist, vmitemlist)
  430.     while true do
  431.         shell.run("clear")
  432.         displayMenu()
  433.         io.write("Enter your choice: ")
  434.         local gchoice = tonumber(io.read())
  435.         if gchoice == 1 then
  436.             shell.run("clear")
  437.             while true do
  438.                 displayClientMenu()
  439.                 io.write("Enter your choice: ")
  440.                 local choice = tonumber(io.read())
  441.                 if choice == 1 then
  442.                     print("Adding a new user")
  443.                     print("Enter username:")
  444.                     local username = io.read()
  445.                     print("Enter password:")
  446.                     local password = io.read()
  447.                     print("Enter value:")
  448.                     local value = tonumber(io.read())
  449.                     print("Enter address (format: x,y,z):")
  450.                     local address = io.read()
  451.                     -- Add user to the database
  452.                     addUser(username, password, value, address, filename)
  453.                     io.write("Press Anything to Continue: ")
  454.                     io.read()
  455.                     shell.run("clear")
  456.                 elseif choice == 2 then
  457.                     print("Deleting a user")
  458.                     print("Enter username to delete:")
  459.                     local username = io.read()
  460.                     -- Delete user from the database
  461.                     deleteUser(username, filename)
  462.                     io.write("Press Anything to Continue: ")
  463.                     io.read()
  464.                     shell.run("clear")
  465.                 elseif choice == 3 then
  466.                     print("Changing username")
  467.                     print("Enter username to change:")
  468.                     local oldUsername = io.read()
  469.                     print("Enter new username:")
  470.                     local newUsername = io.read()
  471.                     -- Change username in the database
  472.                     changeUsername(oldUsername, newUsername, filename)
  473.                     io.write("Press Anything to Continue: ")
  474.                     io.read()
  475.                     shell.run("clear")
  476.                 elseif choice == 4 then
  477.                     print("Changing password")
  478.                     print("Enter username:")
  479.                     local username = io.read()
  480.                     print("Enter new password:")
  481.                     local newPassword = io.read()
  482.                     -- Change password in the database
  483.                     changePassword(username, newPassword, filename)
  484.                     io.write("Press Anything to Continue: ")
  485.                     io.read()
  486.                     shell.run("clear")
  487.                 elseif choice == 5 then
  488.                     print("Changing balance")
  489.                     print("Enter username:")
  490.                     local username = io.read()
  491.                     print("Enter new value:")
  492.                     local newValue = tonumber(io.read())
  493.                     -- Change value in the database
  494.                     changeValue(username, newValue, filename)
  495.                     io.write("Press Anything to Continue: ")
  496.                     io.read()
  497.                     shell.run("clear")
  498.                 elseif choice == 6 then
  499.                     print("Changing address")
  500.                     print("Enter username:")
  501.                     local username = io.read()
  502.                     print("Enter new address:")
  503.                     local newAddress = io.read()
  504.                     changeAddress(username, newAddress, filename)
  505.                     io.write("Press Anything to Continue: ")
  506.                     io.read()
  507.                     shell.run("clear")
  508.                 elseif choice == 7 then
  509.                     print("Displaying database")
  510.                     -- Display the database
  511.                     database = getDatabase(filename)
  512.                     print("\n")
  513.                     for _, line in pairs(database) do
  514.                         username, password, value, address = stripLine(line)
  515.                         print(username, " ", password, " ", value, " ",  address, "\n")
  516.                     end
  517.                     io.write("Press Anything to Continue: ")
  518.                     io.read()
  519.                     shell.run("clear")
  520.                 elseif choice == 8 then
  521.                     print("Enter username:")
  522.                     local username = io.read()
  523.                     username, password = searchDatabase(username, filename)
  524.                     if username == nil then
  525.                         print("User not Found")
  526.                     else
  527.                         print(username, " ", password, " ", getValue(username, filename), "\n")
  528.                     end
  529.                     io.write("Press Anything to Continue: ")
  530.                     io.read()
  531.                     shell.run("clear")
  532.                 elseif choice == 0 then
  533.                     print("Exiting...")
  534.                     shell.run("clear")
  535.                     break
  536.                 else
  537.                     print("Invalid choice. Please try again.")
  538.                 end
  539.             end
  540.         elseif gchoice == 2 then
  541.             shell.run("clear")
  542.             while true do
  543.                 displayWhitelistMenu()
  544.                 io.write("Enter your choice: ")
  545.                 local choice = tonumber(io.read())
  546.                 if choice == 1 then
  547.                     print("Enter the Computers ID:")
  548.                     local id = io.read()
  549.                     addComputer(id, whitelist)
  550.                     io.write("Press Anything to Continue: ")
  551.                     io.read()
  552.                     shell.run("clear")
  553.                 elseif choice == 2 then
  554.                     print("Displaying database")
  555.                     -- Display the database
  556.                     computers = getDatabase(whitelist)
  557.                     print("\n")
  558.                     for _, line in pairs(computers) do
  559.                         print(line, "\n")
  560.                     end
  561.                     io.write("Press Anything to Continue: ")
  562.                     io.read()
  563.                     shell.run("clear")
  564.                 elseif choice == 3 then
  565.                     print("Enter the Computers ID:")
  566.                     local id = io.read()
  567.                     deleteComputer(id, whitelist)
  568.                     io.write("Press Anything to Continue: ")
  569.                     io.read()
  570.                     shell.run("clear")
  571.                 elseif choice == 4 then
  572.                     shell.run("clear")
  573.                     displayLog("log.txt", 1, 10)
  574.                     shell.run("clear")
  575.                 elseif choice == 0 then
  576.                     print("Exiting...")
  577.                     shell.run("clear")
  578.                     break
  579.                 else
  580.                     print("Invalid choice. Please try again.")
  581.                 end
  582.             end
  583.         elseif gchoice == 3 then
  584.             while true do
  585.                 ATMItemMenu()
  586.                 io.write("Enter your choice: ")
  587.                 local choice = tonumber(io.read())
  588.                 if choice == 1 then
  589.                     print("Adding a new item")
  590.                     print("Enter item minecraft name:")
  591.                     local iname = io.read()
  592.                     print("Enter item value:")
  593.                     local ivalue = io.read()
  594.                     -- Add user to the database
  595.                     addItem(iname, ivalue, itemlist, whitelist)
  596.                     io.write("Press Anything to Continue: ")
  597.                     io.read()
  598.                     shell.run("clear")
  599.                 elseif choice == 2 then
  600.                     print("Displaying items")
  601.                     -- Display the database
  602.                     database = getAllItems(itemlist)
  603.                     print("\n")
  604.                     for _, line in pairs(database) do
  605.                         print(line, "\n")
  606.                     end
  607.                     io.write("Press Anything to Continue: ")
  608.                     io.read()
  609.                     shell.run("clear")
  610.                 elseif choice == 3 then
  611.                     print("Deleting an item")
  612.                     print("Enter item minecraft name:")
  613.                     local iname = io.read()
  614.                     deleteItem(iname, itemlist, whitelist)
  615.                     io.write("Press Anything to Continue: ")
  616.                     io.read()
  617.                     shell.run("clear")
  618.                 elseif choice == 0 then
  619.                     print("Exiting...")
  620.                     shell.run("clear")
  621.                         break
  622.                 else
  623.                     print("Invalid choice. Please try again.")
  624.                 end
  625.             end
  626.         elseif gchoice == 4 then
  627.             shell.run("clear")
  628.             while true do
  629.                 displayVMWhitelistMenu()
  630.                 io.write("Enter your choice: ")
  631.                 local choice = tonumber(io.read())
  632.                 if choice == 1 then
  633.                     print("Enter the Computers ID:")
  634.                     local id = io.read()
  635.                     addComputer(id, vmwhitelist)
  636.                     io.write("Press Anything to Continue: ")
  637.                     io.read()
  638.                     shell.run("clear")
  639.                 elseif choice == 2 then
  640.                     print("Displaying database")
  641.                     -- Display the database
  642.                     computers = getDatabase(vmwhitelist)
  643.                     print("\n")
  644.                     for _, line in pairs(computers) do
  645.                         print(line, "\n")
  646.                     end
  647.                     io.write("Press Anything to Continue: ")
  648.                     io.read()
  649.                     shell.run("clear")
  650.                 elseif choice == 3 then
  651.                     print("Enter the Computers ID:")
  652.                     local id = io.read()
  653.                     deleteComputer(id, vmwhitelist)
  654.                     io.write("Press Anything to Continue: ")
  655.                     io.read()
  656.                     shell.run("clear")
  657.                 elseif choice == 4 then
  658.                     shell.run("clear")
  659.                     displayLog("log.txt", 1, 10)
  660.                     shell.run("clear")
  661.                 elseif choice == 0 then
  662.                     print("Exiting...")
  663.                     shell.run("clear")
  664.                     break
  665.                 else
  666.                     print("Invalid choice. Please try again.")
  667.                 end
  668.             end
  669.         elseif gchoice == 5 then
  670.             while true do
  671.                 VMItemMenu()
  672.                 io.write("Enter your choice: ")
  673.                 local choice = tonumber(io.read())
  674.                 if choice == 1 then
  675.                     print("Adding a new item")
  676.                     print("Enter item minecraft name:")
  677.                     local iname = io.read()
  678.                     print("Enter item value:")
  679.                     local ivalue = io.read()
  680.                     -- Add user to the database
  681.                     addItem(iname, ivalue, vmitemlist, vmwhitelist)
  682.                     io.write("Press Anything to Continue: ")
  683.                     io.read()
  684.                     shell.run("clear")
  685.                 elseif choice == 2 then
  686.                     print("Displaying items")
  687.                     -- Display the database
  688.                     database = getAllItems(vmitemlist)
  689.                     print("\n")
  690.                     for _, line in pairs(database) do
  691.                         print(line, "\n")
  692.                     end
  693.                     io.write("Press Anything to Continue: ")
  694.                     io.read()
  695.                     shell.run("clear")
  696.                 elseif choice == 3 then
  697.                     print("Deleting an item")
  698.                     print("Enter item minecraft name:")
  699.                     local iname = io.read()
  700.                     deleteItem(iname, vmitemlist, vmwhitelist)
  701.                     io.write("Press Anything to Continue: ")
  702.                     io.read()
  703.                     shell.run("clear")
  704.                 elseif choice == 0 then
  705.                     print("Exiting...")
  706.                     shell.run("clear")
  707.                         break
  708.                 else
  709.                     print("Invalid choice. Please try again.")
  710.                 end
  711.             end
  712.         elseif gchoice == 6 then
  713.             return true
  714.         elseif gchoice == 0 then
  715.             print("Exiting...")
  716.             shell.run("clear")
  717.             break
  718.         else
  719.             print("Invalid choice. Please try again.")
  720.         end
  721.     end
  722. end
  723.  
  724. local function serverCoroutine(filename, whitelist, vmwhitelist, itemlist, vmitemlist)
  725.     orders = {}
  726.     while true do
  727.         gsuccess = false
  728.         -- Listen for incoming messages
  729.         local senderId, message = rednet.receive()
  730.         if getComputer(senderId, whitelist) then
  731.             success, command, line = pcall(twoInputStrip, message)
  732.             if success and line ~= nil then
  733.                 if command == "balance" then
  734.                     success, command, nusername, npassword, balancechange = pcall(fourInputStrip, message)
  735.                     if success and nusername ~= nil and npassword ~= nil and balancechange ~= nil then
  736.                         dusername, dpassword = searchDatabase(nusername, filename)
  737.                         if tostring(dusername) ~= nil and tostring(dpassword) ~= nil then
  738.                             if npassword == dpassword then
  739.                                 nv = tonumber(getValue(nusername, filename)) + tonumber(balancechange)
  740.                                 changeValue(nusername, nv, filename)
  741.                                 rednet.send(senderId, getValue(nusername, filename))
  742.                                 gsuccess = true
  743.                             end
  744.                         end
  745.                     end
  746.                 elseif command == "address" then
  747.                     success, command, nusername, npassword, balancechange = pcall(fourInputStrip, message)
  748.                     if success and nusername ~= nil then
  749.                         dusername, dpassword = searchDatabase(nusername, filename)
  750.                         daddress = getAddress(dusername, filename)
  751.                         if tostring(dusername) ~= nil and daddress ~= nil then
  752.                             rednet.send(senderId, daddress)
  753.                             gsuccess = true
  754.                         end
  755.                     end
  756.                 end
  757.             end
  758.         end
  759.         success, command, line = pcall(twoInputStrip, message)
  760.         if success and line ~= nil then
  761.             if command == "buy" then
  762.                 success, command, nusername, npassword, balancechange = pcall(fourInputStrip, message)
  763.                 if success and nusername ~= nil and npassword ~= nil and balancechange ~= nil then
  764.                     dusername, dpassword = searchDatabase(nusername, filename)
  765.                     if tostring(dusername) ~= nil and tostring(dpassword) ~= nil then
  766.                         if npassword == dpassword then
  767.                             if tonumber(balancechange) >= 0 and tonumber(getValue(nusername, filename)) >= tonumber(balancechange) then
  768.                                 nv = tonumber(getValue(nusername, filename)) - tonumber(balancechange)
  769.                                 changeValue(nusername, nv, filename)
  770.                                 rednet.send(senderId, getValue(nusername, filename))
  771.                                 gsuccess = true
  772.                                 if orders ~= nil then
  773.                                     for i, order in ipairs(orders) do
  774.                                         print(order)
  775.                                         print(tonumber(order[2]) == tonumber(balancechange))
  776.                                         print(tonumber(order[3]) == tonumber(id))
  777.                                         print(tonumber(order[3]))
  778.                                         print(tonumber(senderId))
  779.                                         if tonumber(order[2]) == tonumber(balancechange) and tonumber(order[3]) == tonumber(senderId) then
  780.                                             cashierId = tonumber(order[1])
  781.                                             print(cashierId)
  782.                                             rednet.send(cashierId, "true")
  783.                                             table.remove(orders, i)
  784.                                             break
  785.                                         end
  786.                                     end
  787.                                 end
  788.                             end
  789.                         end
  790.                     end
  791.                 end
  792.             elseif command == "search" then
  793.                 success, command, nusername, npassword = pcall(threeInputStrip, message)
  794.                 if success and nusername ~= nil and npassword ~= nil then
  795.                     dusername, dpassword = searchDatabase(nusername, filename)
  796.                     if tostring(dusername) ~= nil and tostring(dpassword) ~= nil then
  797.                         if npassword == dpassword then
  798.                             rednet.send(senderId, getValue(nusername, filename))
  799.                             gsuccess = true
  800.                         end
  801.                     end
  802.                 end
  803.             elseif command == "transfer" then
  804.                 success, command, nusername, npassword, balancechange, nusername2 = pcall(fiveInputStrip, message)
  805.                 if success and nusername ~= nil and npassword ~= nil and balancechange ~= nil and nusername2 ~= nil and nusername ~= nusername2 then
  806.                     dusername, dpassword = searchDatabase(nusername, filename)
  807.                     nusername2 = searchDatabase(nusername2, filename)
  808.                     if tostring(dusername) ~= nil and tostring(dpassword) ~= nil then
  809.                         if npassword == dpassword then
  810.                             if nusername2 ~= nil then
  811.                                 if tonumber(balancechange) > 0 and tonumber(balancechange) <= tonumber(getValue(nusername, filename)) then
  812.                                     nv = tonumber(getValue(nusername, filename) - tonumber(balancechange))
  813.                                     nv2 = tonumber(getValue(nusername2, filename) + tonumber(balancechange))
  814.                                     changeValue(nusername, nv, filename)
  815.                                     changeValue(nusername2, nv2, filename)
  816.                                     rednet.send(senderId, tonumber(getValue(nusername, filename)))
  817.                                     gsuccess = true
  818.                                 end
  819.                             end
  820.                         end
  821.                     end
  822.                 end
  823.             elseif command == "items" then
  824.                 rednet.send(senderId, getAllItems(itemlist))
  825.                 gsuccess = true
  826.             elseif command == "vmitems" then
  827.                 rednet.send(senderId, getAllItems(vmitemlist))
  828.                 gsuccess = true
  829.             elseif command == "serverbuyrequest" then
  830.                 command, balance, phoneID = threeInputStrip(message)
  831.                 if balance ~= nil and phoneID ~= nil then
  832.                     table.insert(orders, {senderId, balance, phoneID})
  833.                     gsuccess = true
  834.                 end
  835.             elseif command == "removebuyrequest" then
  836.                 command, balance, phoneID = threeInputStrip(message)
  837.                 if orders ~= nil then
  838.                     for i, order in ipairs(orders) do
  839.                         if tonumber(id) == tonumber(order[1]) and tonumber(balance) == tonumber(order[2]) and tonumber(phoneID) == tonumber(order[3]) then
  840.                             table.remove(orders, i)
  841.                             gsuccess = true
  842.                         end
  843.                     end
  844.                 end
  845.             end
  846.         log(senderId, message, gsuccess)
  847.         if gsuccess == false then
  848.             rednet.send(senderId, "false")
  849.         end
  850.         end
  851.     end
  852. end
  853.  
  854. -- Main coroutine for the login screen
  855. local function loginCoroutine(filename, whitelist, vmwhitelist, itemlist, vmitemlist)
  856.     while true do
  857.         -- Display login screen
  858.         print("Login Screen")
  859.         print("Enter username:")
  860.         local username = read()
  861.         print("Enter password:")
  862.         local password = read("*")
  863.  
  864.         -- If login is successful, unlock the screen
  865.         if username == myusername and password == mypassword then
  866.             print("Login successful")
  867.             if main(filename, whitelist, vmwhitelist, itemlist, vmitemlist) then
  868.                 break
  869.             end
  870.             shell.run("clear")
  871.         else
  872.             print("Login failed")
  873.         end
  874.     end
  875. end
  876.  
  877. os.pullEvent = os.pullEventRaw
  878. whitelist = "whitelist.txt"
  879. vmwhitelist = "vmwhitelist.txt"
  880. filename = "clients.txt"
  881. itemlist = "items.txt"
  882. vmitemlist = "vmitems.txt"
  883. if fs.exists(filename) then
  884.     rednet.open("top")
  885.     parallel.waitForAny(
  886.         function() loginCoroutine(filename, whitelist, vmwhitelist, itemlist, vmitemlist) end,
  887.         function() serverCoroutine(filename, whitelist, vmwhitelist, itemlist, vmitemlist) end
  888.     )
  889. else
  890.     print("No File to Protect")
  891. end
Advertisement
Add Comment
Please, Sign In to add comment