Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- for kind of a documentation go to https://pastebin.com/DBP8CBMd
- 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}
- rednet.send(systemId,createMessageString({["action"]="requestPayment",["player"]=player,["amount"]=amount,["text"]=text}),"server")
- end
- 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}
- rednet.send(systemId,createMessageString({["action"]="pay",["player"]=player,["amount"]=amount,["text"]=text}),"server")
- end
- 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"]}
- rednet.send(systemId,createMessageString({["action"]="getBalance"}),"server")
- end
- 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"]}
- rednet.send(systemId,createMessageString({["action"]="getOwner"}),"server")
- end
- function encrypt(text)
- local result = ""
- local keyLength = #key
- for i = 1, #text do
- local textByte = text:byte(i)
- local keyByte = key:byte((i - 1) % keyLength + 1)
- result = result .. string.char(bit.bxor(textByte, keyByte))
- end
- return result
- end
- function decrypt(encryptedText)
- return encrypt(encryptedText)
- end
- function createMessageString(messageTable)
- local outTable = {}
- local returnString = "@@"
- local i = 1
- for k,v in pairs(messageTable) do
- outTable[i] = {["key"] = k, ["value"] = v}
- i=i+1
- end
- for i = 1, #outTable do
- local i = math.random(1,#outTable)
- returnString = returnString .. outTable[i]["key"] .. "=" .. outTable[i]["value"] .. ","
- table.remove(outTable,i)
- end
- returnString = encrypt(returnString)
- return returnString
- end
- 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
- local messageString = decrypt(messageString)
- if string.sub(messageString,1,2) == "@@" then
- messageString = string.sub(messageString,3,-1)
- else
- return {}
- end
- local returnTable = {}
- local curPos = 1
- while true do
- local equalPos = string.find(messageString,"=",curPos)
- if not equalPos then
- break
- end
- local curKey = string.sub(messageString,curPos,equalPos-1)
- curPos = equalPos+1
- local commaPos = string.find(messageString,",",curPos)
- if not commaPos then
- break
- end
- local curValue = string.sub(messageString,curPos,commaPos-1)
- curPos = commaPos+1
- returnTable[curKey] = curValue
- end
- return returnTable
- end
- for k,name in pairs(peripheral.getNames()) do
- if peripheral.getType(name) == "modem" then
- if peripheral.wrap(name).isWireless() then
- rednet.open(name)
- end
- end
- end
- if not fs.exists("key") then
- term.setTextColor(colors.red)
- print("Please enter the key:")
- key = read()
- local file = fs.open("key","w")
- file.write(key)
- file.flush()
- file.close()
- else
- local file = fs.open("key","r")
- key = file.readAll()
- file.close()
- end
- if not fs.exists("systemId") then
- term.setTextColor(colors.white)
- print("Connecting to the system...")
- while true do
- rednet.broadcast(createMessageString({["action"]="register",["key"]=key}),"server")
- id, message, protocol = rednet.receive("serverResponse",5)
- if message and protocol == "serverResponse" then
- local response = getMessageTable(message)
- if response["action"] == "registrationResponse" then
- term.setTextColor(colors.green)
- print("Succesfully connected to the system.")
- systemId = id
- local file = fs.open("systemId","w")
- file.write(id)
- file.flush()
- file.close()
- break
- end
- end
- print("Registration failed, trying again.")
- end
- else
- local file = fs.open("systemId","r")
- systemId = tonumber(file.readAll())
- file.close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement