Jayex_Designs

server

Jun 15th, 2021 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.02 KB | None | 0 0
  1. TotalPotatoes = 0
  2.  
  3. rednet.open("left")
  4.  
  5.  
  6.  
  7. DBName = ""
  8.  
  9. function CreateDatabase(dbName)
  10.     DBName = dbName
  11.     if not fs.exists(dbName) then
  12.         local db = fs.open(dbName, "w")
  13.         db.close()
  14.     end
  15. end
  16.  
  17. function GetUserTable()
  18.     local db = fs.open(DBName, "r")
  19.     local dbData = db.readAll()
  20.     local users = {}
  21.     for userData in string.gmatch(dbData, "([^\n]+)") do
  22.         for u, p, b in string.gmatch(userData, "(%w+)%s(%w+)%s(%w+)") do
  23.             users[u] = {
  24.                 ["password"] = p,
  25.                 ["balance"] = b
  26.             }
  27.         end
  28.     end
  29.     db.close()
  30.     return users
  31. end
  32.  
  33. function Register(username, password)
  34.     if GetBalance(username) then
  35.         return false
  36.     else
  37.         local db = fs.open(DBName, "a")
  38.         db.writeLine(username .. " " .. password .. " " .. "0")
  39.         db.close()
  40.         return true
  41.     end
  42. end
  43.  
  44. function Login(username, password)
  45.     local users = GetUserTable()
  46.     for k, v in pairs(users) do
  47.         if k == username and v["password"] == password then
  48.             return true
  49.         end
  50.     end
  51.     return false
  52. end
  53.  
  54. function GetBalance(username)
  55.     local users = GetUserTable()
  56.     for k, v in pairs(users) do
  57.         if k == username then
  58.             return v["balance"]
  59.         end
  60.     end
  61.     return nil
  62. end
  63.  
  64. function ChangeBalance(username, balance)
  65.     local users = GetUserTable()
  66.     users[username]["balance"] = tostring(balance)
  67.     local dbData = ""
  68.     for k, v in pairs(users) do
  69.         dbData = dbData .. k .. " " .. v["password"] .. " " .. v["balance"] .. "\n"
  70.     end
  71.     local db = fs.open(DBName, "w")
  72.     db.write(dbData)
  73.     db.close()
  74. end
  75.  
  76.  
  77.  
  78. function RegisterRequest(id, message)
  79.     local username
  80.     local password
  81.     for u, p in string.gmatch(message, "(%w+)%s(%w+)") do
  82.         username = u
  83.         password = p
  84.     end
  85.     print("Got a register request from " .. username)
  86.     sleep(0.2)
  87.     rednet.send(id, Register(username, password), "registerRequest")
  88. end
  89.  
  90. function LoginRequest(id, message)
  91.     local username
  92.     local password
  93.     for u, p in string.gmatch(message, "(%w+)%s(%w+)") do
  94.         username = u
  95.         password = p
  96.     end
  97.     sleep(0.2)
  98.     rednet.send(id, Login(username, password), "loginRequest")
  99. end
  100.  
  101. function GetBalanceRequest(id, message)
  102.     sleep(0.2)
  103.     rednet.send(id, GetBalance(message), "getBalanceRequest")
  104. end
  105.  
  106. function AddBalance(id, message)
  107.     local username
  108.     local quantity
  109.     for u, q in string.gmatch(message, "(%w+)%s(%w+)") do
  110.         username = u
  111.         quantity = tonumber(q)
  112.     end
  113.     local balance = GetBalance(username)
  114.     sleep(0.2)
  115.     print("Adding " .. quantity .. " potato coins to " .. username)
  116.     ChangeBalance(username, balance + quantity)
  117.     rednet.send(id, true, "addBalance")
  118. end
  119.  
  120. function SubstractBalance(id, message)
  121.     local username
  122.     local quantity
  123.     for u, q in string.gmatch(message, "(%w+)%s(%w+)") do
  124.         username = u
  125.         quantity = tonumber(q)
  126.     end
  127.     local balance = GetBalance(username)
  128.     sleep(0.2)
  129.     if tonumber(balance) < quantity then
  130.         rednet.send(id, false, "substractBalance")
  131.     else
  132.         print("Substracting " .. quantity .. " potato coins to " .. username)
  133.         ChangeBalance(username, balance - quantity)
  134.         rednet.send(id, true, "substractBalance")
  135.     end
  136. end
  137.  
  138.  
  139. function PotatoQuantity(id, potatoes)
  140.     TotalPotatoes = potatoes
  141.     print("Total potatoes updated: " .. TotalPotatoes)
  142. end
  143.  
  144. function GivePotatoes(id, potatoes)
  145.     print("Vending machine giving away " .. potatoes .. " potatoes")
  146. end
  147.  
  148.  
  149. Clients = {}
  150.  
  151. function ClientLogin(id, data)
  152.     local username, password
  153.     for u, p in string.gmatch(data, "(%w+)%s(%w+)") do
  154.         username = u
  155.         password = p
  156.     end
  157.     if Login(username, password) then
  158.         Clients[tostring(id)] = {
  159.             ["username"] = username,
  160.             ["password"] = password
  161.         }
  162.     end
  163. end
  164.  
  165. function ClientLocation(id, location)
  166.     local function stringSplit(input, sep)
  167.         if sep == nil then
  168.             sep = "%s"
  169.         end
  170.         local t={}
  171.         for str in string.gmatch(input, "([^"..sep.."]+)") do
  172.             table.insert(t, str)
  173.         end
  174.         return t
  175.     end
  176.  
  177.     location = stringSplit(location)
  178.     if Clients[tostring(id)] then
  179.         Clients[tostring(id)]["x"] = location[1]
  180.         Clients[tostring(id)]["y"] = location[2]
  181.         Clients[tostring(id)]["z"] = location[3]
  182.     end
  183. end
  184.  
  185. function RequestPotatoes(id, quantity)
  186.     quantity = tonumber(quantity)
  187.     local client = Clients[tostring(id)]
  188.     if client and client["username"] and client["password"] and client["x"] and client["y"] and client["z"] and quantity then
  189.         local balance = GetBalance(client["username"])
  190.         if tonumber(balance) >= quantity then
  191.             ChangeBalance(client["username"], balance - quantity)
  192.         else
  193.             return false
  194.         end
  195.         if DeployerId then
  196.             rednet.send(DeployerId, tostring(client["x"]) .. " " .. tostring(client["y"]) .. " " .. tostring(client["z"]) .. " " .. tostring(quantity), "deploy")
  197.             print("Sending " .. quantity .. " potatoes to " .. client["username"] .. " in " .. client["x"] .. " " .. client["y"] .. " " .. client["z"])
  198.         end
  199.     end
  200. end
  201.  
  202.  
  203.  
  204. DeployerId = nil
  205.  
  206. function DeployerPing(id, message)
  207.     DeployerId = tonumber(id)
  208. end
  209.  
  210.  
  211.  
  212. Protocols = {
  213.     ["registerRequest"] = RegisterRequest,
  214.     ["loginRequest"] = LoginRequest,
  215.     ["getBalanceRequest"] = GetBalanceRequest,
  216.     ["addBalance"] = AddBalance,
  217.     ["substractBalance"] = SubstractBalance,
  218.     ["potatoQuantity"] = PotatoQuantity,
  219.     ["givePotatoes"] = GivePotatoes,
  220.     ["clientLogin"] = ClientLogin,
  221.     ["clientLocation"] = ClientLocation,
  222.     ["requestPotatoes"] = RequestPotatoes,
  223.     ["deployerPing"] = DeployerPing
  224. }
  225.  
  226.  
  227.  
  228. CreateDatabase("database")
  229.  
  230. while true do
  231.     local id, message, protocol = rednet.receive()
  232.     Protocols[protocol](id, message)
  233. end
Add Comment
Please, Sign In to add comment