Advertisement
Chaos_Cash

server.lua

Mar 27th, 2025
386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.63 KB | None | 0 0
  1. -- for kind of a documentation go to https://pastebin.com/DBP8CBMd
  2.  
  3.  
  4.  
  5.  
  6. function requestPayment(player, amount, text) -- sends the specified player a request with the specified text to pay the computer the specified amount of money. If the player confirms the payment the server will send back a rednet message in this form: {["action"] = "receivedPayment", ["amount"] = amount, ["player"] = player}
  7. rednet.send(systemId,createMessageString({["action"]="requestPayment",["player"]=player,["amount"]=amount,["text"]=text}),"server")
  8. end
  9.  
  10.  
  11.  
  12. function pay(player, amount, text) -- pays the specified player the specified amount of money and sends them the specified message (if the message is specified). If the payment is succesfull then the server helper will send back a rednet message in this form: {["action"] = "paymentConfirmation", ["player"] = playerName, ["amount"] = amount}
  13. rednet.send(systemId,createMessageString({["action"]="pay",["player"]=player,["amount"]=amount,["text"]=text}),"server")
  14. end
  15.  
  16.  
  17.  
  18. function getBalance() -- requests the balance of the computer, if it is succesfull, the server helper will send back a rednet message in this form: {["action"] = "sendingBalance", ["balance"] = registeredComputers[id]["balance"]}
  19. rednet.send(systemId,createMessageString({["action"]="getBalance"}),"server")
  20. end
  21.  
  22.  
  23.  
  24. function getOwner() -- requests the name of the owner from the server helper, if it is succesfull, the server helper will send back a rednet message in this form: {["action"] = "sendingOwner", ["owner"] = registeredComputers[id]["owner"]}
  25. rednet.send(systemId,createMessageString({["action"]="getOwner"}),"server")
  26. end
  27.  
  28.  
  29.  
  30. function encrypt(text)
  31. local result = ""
  32. local keyLength = #key
  33.    
  34.     for i = 1, #text do
  35.     local textByte = text:byte(i)
  36.     local keyByte = key:byte((i - 1) % keyLength + 1)
  37.     result = result .. string.char(bit.bxor(textByte, keyByte))
  38.     end
  39.    
  40. return result
  41. end
  42.  
  43.  
  44.  
  45. function decrypt(encryptedText)
  46. return encrypt(encryptedText)
  47. end
  48.  
  49.  
  50.  
  51. function createMessageString(messageTable)
  52. local outTable = {}
  53. local returnString = "@@"
  54. local i = 1
  55.     for k,v in pairs(messageTable) do
  56.     outTable[i] = {["key"] = k, ["value"] = v}
  57.     i=i+1
  58.     end
  59.  
  60.     for i = 1, #outTable do
  61.     local i = math.random(1,#outTable)
  62.     returnString = returnString .. outTable[i]["key"] .. "=" .. outTable[i]["value"] .. ","
  63.     table.remove(outTable,i)
  64.     end
  65.  
  66.  
  67. returnString = encrypt(returnString)
  68.  
  69. return returnString
  70. end
  71.  
  72.  
  73.  
  74. function getMessageTable(messageString) -- returns the decoded and processed message as a table (example: diwjaifwifhwoifhaiwdj --> action=test,value=hello there, --> {["action"]="test",["value"]="hello there"}) be carefull tough, cause every value and key will be strings
  75. local messageString = decrypt(messageString)
  76.  
  77.     if string.sub(messageString,1,2) == "@@" then
  78.     messageString = string.sub(messageString,3,-1)
  79.     else
  80.     return {}
  81.     end
  82.  
  83.  
  84. local returnTable = {}
  85.  
  86. local curPos = 1
  87.     while true do
  88.     local equalPos = string.find(messageString,"=",curPos)
  89.         if not equalPos then
  90.         break
  91.         end
  92.     local curKey = string.sub(messageString,curPos,equalPos-1)
  93.     curPos = equalPos+1
  94.     local commaPos = string.find(messageString,",",curPos)
  95.         if not commaPos then
  96.         break
  97.         end
  98.     local curValue = string.sub(messageString,curPos,commaPos-1)
  99.     curPos = commaPos+1
  100.     returnTable[curKey] = curValue
  101.     end
  102.    
  103. return returnTable
  104. end
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.     for k,name in pairs(peripheral.getNames()) do
  113.         if peripheral.getType(name) == "modem" then
  114.             if peripheral.wrap(name).isWireless() then
  115.             rednet.open(name)
  116.             end
  117.         end
  118.     end
  119.  
  120.     if not fs.exists("key") then
  121.     term.setTextColor(colors.red)
  122.     print("Please enter the key:")
  123.     key = read()
  124.     local file = fs.open("key","w")
  125.     file.write(key)
  126.     file.flush()
  127.     file.close()
  128.     else
  129.     local file = fs.open("key","r")
  130.     key = file.readAll()
  131.     file.close()
  132.     end
  133.  
  134.     if not fs.exists("systemId") then
  135.     term.setTextColor(colors.white)
  136.     print("Connecting to the system...")
  137.         while true do
  138.         rednet.broadcast(createMessageString({["action"]="register",["key"]=key}),"server")
  139.         id, message, protocol = rednet.receive("serverResponse",5)
  140.             if message and protocol == "serverResponse" then
  141.                 local response = getMessageTable(message)
  142.                 if response["action"] == "registrationResponse" then
  143.                 term.setTextColor(colors.green)
  144.                 print("Succesfully connected to the system.")
  145.                 systemId = id
  146.                 local file = fs.open("systemId","w")
  147.                 file.write(id)
  148.                 file.flush()
  149.                 file.close()
  150.                 break
  151.                 end
  152.             end
  153.         print("Registration failed, trying again.")
  154.         end
  155.     else
  156.     local file = fs.open("systemId","r")
  157.     systemId = tonumber(file.readAll())
  158.     file.close()
  159.     end
  160.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement