Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function save(table, filename)
- file = fs.open(filename,"w")
- file.write(textutils.serialize(table))
- file.close()
- end
- function load(filename)
- file = fs.open(filename,"r")
- data = file.readAll()
- file.close()
- return textutils.unserialize(data)
- end
- function newAcc(FromPort)
- I = #LogInTable
- unable = false
- event, side, x,y, ID = os.pullEvent("modem_message")
- print(ID)
- event, side, x,y, Pass = os.pullEvent("modem_message")
- print(Pass)
- for J = 1, #LogInTable, 2 do
- if LogInTable[J] == ID then
- unable = true
- modem.transmit(FromPort, 12345, "Used")
- print("Account "..ID.." already in use.")
- end
- end
- if not unable then
- modem.transmit(FromPort, 12345, "Unused")
- print("Created account for ID: "..ID)
- LogInTable[I + 1] = ID
- LogInTable[I + 2] = Pass
- J = #CurrencyTable
- CurrencyTable[J + 1] = ID
- CurrencyTable[J + 2] = 0
- end
- end
- function LogIn(FromPort)
- login = false
- print(FromPort.." is attempting to login.")
- event, side, x, y, ID = os.pullEvent("modem_message")
- print(ID)
- sleep(0.1)
- event, side, x, y, Pass = os.pullEvent("modem_message")
- print(Pass)
- for I = 1, #LogInTable, 2 do
- if ID == LogInTable[I] then
- if Pass == LogInTable[I + 1] then
- login = true
- loginCreds = LogInTable[I]
- end
- end
- end
- modem.transmit(FromPort, 12345, tostring(login))
- print("Sent "..tostring(login).." to "..FromPort)
- end
- function Transfer(FromPort)
- Owner = false
- request = false
- event, side, x,y, ID = os.pullEvent("modem_message")
- event, side, x,y, ToID = os.pullEvent("modem_message")
- event, side, x,y, Amount = os.pullEvent("modem_message")
- for I = 1, #LogInTable, 2 do
- if LogInTable[I] == tonumber(ID) then
- Currency = CurrencyTable[I + 1]
- Owner = true
- elseif LogInTable[I] == tonumber(ToID) then
- OtherCurrency = CurrencyTable[I + 1]
- request = true
- end
- end
- if not Owner or not request then
- modem.transmit(FromPort, 12345, "InvalidUser")
- elseif tonumber(Amount) > Currency or tonumber(Amount) < 0 then
- modem.transmit(FromPort, 12345, "NotEnough")
- else
- OtherCurrency = OtherCurrency + Amount
- Currency = Currency - Amount
- print(ID.." sent "..Amount.." to "..ToID)
- modem.transmit(FromPort, 12345, "Transfer Successful.")
- end
- for I = 1, #CurrencyTable, 2 do
- if CurrencyTable[I] == tonumber(ID) then
- CurrencyTable[I + 1] = Currency
- elseif CurrencyTable[I] == tonumber(ToID) then
- CurrencyTable[I + 1] = OtherCurrency
- end
- end
- end
- function init()
- if fs.exists("Currency") then
- CurrencyTable = load("Currency")
- else
- CurrencyTable = {}
- end
- if fs.exists("LogIn") then
- LogInTable = load("LogIn")
- else
- LogInTable = {}
- end
- modem = peripheral.wrap("back")
- modem.open(12345)
- end
- function getCurrency(FromPort)
- event, side, x, y, ID = os.pullEvent("modem_message")
- for I = 1, #CurrencyTable, 2 do
- if CurrencyTable[I] == tonumber(ID) then
- Currency = CurrencyTable[I + 1]
- end
- end
- modem.transmit(FromPort, 12345, Currency)
- print("Currency for "..ID..": "..Currency)
- end
- function main()
- print("Init complete.")
- while true do
- CurrencyTable = load("Currency")
- LogInTable = load("LogIn")
- event, side, x, FromUser, message = os.pullEvent("modem_message")
- if message == "LogIn" then
- LogIn(FromUser)
- elseif message == "CreateNew" then
- newAcc(FromUser)
- elseif message == "getCurrency" then
- getCurrency(FromUser)
- elseif message == "Transfer" then
- Transfer(FromUser)
- end
- save(CurrencyTable, "Currency")
- save(LogInTable, "LogIn")
- end
- end
- init()
- main()
Advertisement
Add Comment
Please, Sign In to add comment