View difference between Paste ID: JxauVSec and g9gfxwsb
SHOW: | | - or go back to the newest paste.
1-
local monitor = peripheral.find "monitor"
1+
local sign = peripheral.find "minecraft:sign"
2
local sensor = peripheral.find "plethora:sensor"
3-
local function set_color(c)
3+
4-
	monitor.setBackgroundColor(c)
4+
local chest = peripheral.wrap "bottom"
5-
	monitor.clear()
5+
6
sign.setSignText "Rotating"
7
8-
set_color(colors.lime)
8+
local powered_rail_side = "right"
9
10
--[[
11-
local switch_config = dofile "config.lua"
11+
while true do
12
	local presence, meta = turtle.inspect()
13
	if presence and meta.name == "minecraft:activator_rail" then
14
		break
15
	end
16
	turtle.turnRight()
17
end
18
]]
19
20
turtle.turnLeft()
21
local presence, meta = turtle.inspect()
22
if presence and meta.name == "minecraft:golden_rail" then
23
	powered_rail_side = "left"
24
end
25
turtle.turnRight()
26
27
local function sign_display(player)
28
	local l2, l3 = "", ""
29
	if player then
30
		l2 = "Welcome, "
31
		l3 = player
32
	end
33-
		send_packet { type = "identify", implementation = "ARR switching unit", key = settings.get "spudnet_key" }
33+
	sign.setSignText(label, l2, l3, "\\arr goto [dest]")
34
end
35
sign_display()
36
37
local function spudnet()
38
	if not http or not http.websocket then return "Websockets do not actually exist on this platform" end
39
40
	local ws
41
42
	local function send_packet(msg)
43
		local ok, err = pcall(ws.send, textutils.serialiseJSON(msg))
44
		if not ok then printError(err) try_connect_loop() end
45
	end
46
47
	local function send(data)
48
		send_packet { type = "send", channel = "comm:arr", data = data }
49
	end
50
51
	local function connect()
52
		if ws then ws.close() end
53
		ws, err = http.websocket "wss://spudnet.osmarks.net/v4?enc=json"
54
		if not ws then print("websocket failure %s", err) return false end
55
		ws.url = "wss://spudnet.osmarks.net/v4?enc=json"
56
57
		send_packet { type = "identify", implementation = "ARR station unit", key = settings.get "spudnet_key" }
58
		send_packet { type = "set_channels", channels = { "comm:arr" } }
59
60
		print("websocket connected")
61
62
		return true
63
	end
64
65
	local function try_connect_loop()
66
		while not connect() do
67
			sleep(0.5)
68
		end
69
	end
70
71
	try_connect_loop()
72
73
	local function recv()
74
		while true do
75
			local e, u, x = os.pullEvent "websocket_message"
76
			if u == ws.url then return textutils.unserialiseJSON(x) end
77
		end
78
	end
79
80
	local ping_timeout_timer = nil
81
82
	local function ping_timer()
83
		while true do
84
			local _, t = os.pullEvent "timer"
85
			if t == ping_timeout_timer and ping_timeout_timer then
86
				-- 15 seconds since last ping, we probably got disconnected
87
				print "SPUDNET timed out, attempting reconnect"
88
				try_connect_loop()
89-
local directions = {
89+
90-
	["+ "] = "east",
90+
91-
	[" +"] = "south",
91+
92-
	["- "] = "west",
92+
93-
	[" -"] = "north"
93+
94-
}
94+
95
			local packet = recv()
96-
local function direction_name(vec)
96+
97-
	local function symbol(v)
97+
98-
		if math.abs(v) < 0.1 then return " "
98+
99-
		elseif v < 0 then return "-"
99+
100-
		else return "+" end
100+
101
				print("SPUDNET error", packet["for"], packet.error, packet.detail, textutils.serialise(packet))
102-
	return directions[symbol(vec.x) .. symbol(vec.z)]
102+
103
				os.queueEvent("spudnet_message", packet.data)
104
			end
105
		end
106
	end
107
108
	return send, function() parallel.waitForAll(ping_timer, main) end
109-
			entity.position = vector.new(entity.x, entity.y, entity.z) + switch_config.offset
109+
110-
			entity.velocity = vector.new(entity.motionX, entity.motionY, entity.motionZ)
110+
111
local spudnet_send, spudnet_handler = spudnet()
112-
		table.sort(entities, function(a, b) return a.position:length() < b.position:length() end)
112+
113-
		local carts = {}
113+
114
	while true do
115-
			if entity.displayName == "entity.MinecartRideable.name" then
115+
116-
				entity.riders = {}
116+
		local players = {}
117-
				table.insert(carts, entity)
117+
118-
				break
118+
			entity.position = vector.new(entity.x, entity.y, entity.z)
119
			if entity.position:length() < 5 and entity.displayName == entity.name then
120
				table.insert(players, entity.displayName)
121-
		local new_carts = {}
121+
122-
		local relevant_carts = 0
122+
123-
		for _, cart in pairs(carts) do
123+
		if #players > 0 then
124-
			local new = {
124+
			sign_display(players[1])
125-
				pos = direction_name(cart.position), dir = direction_name(cart.velocity),
125+
			spudnet_send { id = label, type = "st_ping", players = players }
126-
				distance = cart.position:length(),
126+
127-
				riders = {},
127+
		if turtle.suck() then
128-
				id = cart.id
128+
			turtle.select(1)
129-
			}
129+
			chest.pullItems("up", 1)
130-
			for _, entity in pairs(entities) do
130+
131-
				if entity.displayName ~= "entity.MinecartRideable.name" and entity ~= cart and (cart.position - entity.position):length() < 1 then
131+
		sleep(1)
132-
						table.insert(new.riders, entity.displayName)
132+
133-
					break
133+
134
135
local busy
136-
			if new.dir and #new.riders > 0 then
136+
137-
				relevant_carts = relevant_carts + 1
137+
138
	    --[[
139-
			table.insert(new_carts, new)
139+
140
		if type(data) == "table" and data.type == "st_cmd" and (data.id == label or data.id == nil) then
141-
		spudnet_send { id = label, type = "sw_ping", carts = new_carts }
141+
142-
		if relevant_carts == 0 then sleep(0.5) elseif relevant_carts == 1 then sleep(0.25) else sleep(0.1) end
142+
			if data.cmd == "place_cart" then
143
				if busy then
144
					spudnet_send { id = label, cid = data.cid, type = "st_ack", status = "busy" }
145
				else
146
					busy = true
147
					chest.pullItems("up", 1)
148
					local items = chest.list()
149-
		if type(data) == "table" and data.type == "sw_cmd" and (data.id == label or data.id == nil) then
149+
					local cart_slot
150
					for slot, content in pairs(items) do
151-
			if data.cmd == "set" then
151+
						cart_slot = slot
152-
				print("set", data.switch)
152+
					end
153-
				if data.lamp then set_color(colors[data.lamp]) end
153+
					if not cart_slot then
154-
				if data.switch ~= nil then rs.setOutput(switch_config.side, data.switch) end
154+
						spudnet_send { id = label, cid = data.cid, type = "st_ack", status = "no_cart" }
155-
				spudnet_send { type = "sw_ack", cid = data.cid }
155+
					else
156
						chest.pushItems("up", cart_slot, 1, 1)
157-
				local h, e = http.get "https://pastebin.com/raw/g9gfxwsb"
157+
						if powered_rail_side == "left" then turtle.turnLeft() else turtle.turnRight() end
158
						turtle.place()
159
						if powered_rail_side == "left" then turtle.turnRight() else turtle.turnLeft() end
160-
					set_color(colors.purple)
160+
						spudnet_send { id = label, cid = data.cid, type = "st_ack", status = "done" }
161
					end
162
					busy = false
163
				end
164
			elseif data.cmd == "update" then
165
				local h, e = http.get "https://pastebin.com/raw/JxauVSec"
166
				if not h then printError(e)
167
				else
168
					sign.setSignText("Update", "in progress")
169
					local t = h.readAll()
170
					h.close()
171
					local f, e = load(t)
172
					if f then
173
						local f = fs.open("startup", "w")
174
						f.write(t)
175
						f.close()
176
						print "reboot"
177
						sleep(1)
178
						os.reboot()
179
					else printError(e) end
180
181
				end
182
			end
183
		end
184
		]]
185
		write "> "
186
		local x = read()
187
		spudnet_send { id = label, type = "control", msg = x }
188
	end
189
end
190
191
parallel.waitForAll(spudnet_handler, main, spudnet_listen)
192