Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local cmp = require("component")
- if cmp.isAvailable("redstone") == false then print("redstone card or redstone controller block is required") os.exit() end
- rs = cmp.redstone
- local wifi = cmp.modem
- local position = -1
- event = require("event")
- local sides = require("sides")
- local history = {-1,-1,-1}
- local outCycles = 4
- playerName = ""
- local inCycles = 0
- local bankServerAddr
- local cyc = 0
- local cycDistance = 0
- local steps = 0
- local gpu = cmp.gpu
- local pr = ""
- running = true
- function closer()
- running = false
- end
- local function connectToBank()
- print("Connecting to the SliwiBank...")
- wifi.broadcast(66,"BANK_CONNECT #GigiZaShagi 2v6rNacPg5uUCpIX")
- local mes = {event.pull(5,"modem_message")}
- if mes[1] == nil then
- print("Handshake timeout exceeded")
- return false
- end
- bankServerAddr = mes[3]
- local status = string.match(mes[6], "[A-Z_]+")
- if status == "BANK_HELLO" then
- print("Server sent HELLO. Connection established")
- return true
- end
- print("Server sent error: "..mes[6])
- return false
- end
- local function disconnectFromBank()
- print("Disconnecting...")
- wifi.send(bankServerAddr, 66, "BANK_DISCONNECT")
- end
- local function payToPlayer()
- --if playerName == nil then return false end
- local money = cycDistance/5
- pr = pr.."Player "..playerName.." will receive "..money
- wifi.send(bankServerAddr, 66, "BANK_TRANSFER "..playerName.." "..money)
- local e = {event.pull("modem_message")}
- pr = e[6]
- end
- function onBioTouch(_, _, _, _, _, name)
- playerName = name
- pr = pr..playerName
- require("component").redstone.setOutput(require("sides").east, 15)
- event.timer(4, function()
- require("component").redstone.setOutput(sides.east, 0)
- end)
- end
- event.listen("interrupted", closer)
- event.listen("touch", onBioTouch)
- function findDelta()
- history[1] = history[2]
- history[2] = history[3]
- history[3] = position
- print(history[1].." "..history[2].." "..history[3])
- if position >= 0 then
- local delta = history[1]-history[2]
- print("delta "..(history[1]-history[2]))
- if inCycles <= 10 then inCycles=inCycles+1 end
- steps = steps+math.abs(delta)
- cycDistance = cycDistance+math.abs(delta)
- elseif position == -1 then
- outCycles = outCycles+1
- if outCycles > 3 then
- if inCycles > 10 then
- print("player out")
- playerName = nil
- else
- outCycles = 0
- end
- else
- print("player out "..outCycles) end
- end
- print("in cycles "..inCycles)
- print(steps.." steps")
- end
- wifi.open(66)
- if connectToBank() == false then
- os.exit()
- end
- while running == true do
- cyc=cyc+1
- print(pr)
- if cyc == 1000 then
- payToPlayer()
- cyc = 0
- cycDistance = 0
- end
- print("distance :"..cycDistance)
- print("cyc = "..cyc)
- local r = rs.getBundledInput(sides.north)
- local poses = {}
- for i=0,15,1 do
- local sig = r[i]
- if sig > 0 then
- poses[#poses+1] = i
- end
- end
- --print("poses "..poses[1]..","..poses[2])
- if #poses == 0 then
- position = -1
- print("no player")
- elseif #poses == 1 then
- position = poses[1]
- print("player at "..position)
- elseif #poses == 2 then
- if math.abs(poses[1]-poses[2]) == 1 then
- position = (poses[1]+poses[2])/2
- print("player at "..position)
- else
- position = -2
- print("multiple players error")
- end
- else
- print("multiple players error")
- position = -2
- end
- findDelta()
- os.sleep(0.01)
- require("term").setCursor(1,1)
- gpu.fill(1,1,160,50," ")
- end
- disconnectFromBank()
- event.ignore("interrupted", closer)
- event.ignore("touch", onBioTouch)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement