-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportal.lua
168 lines (151 loc) · 4.77 KB
/
portal.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
os.loadAPI("button")
m = peripheral.wrap("top")
m.clear()
rednet.open("bottom")
local page = 1
local pages = 0
local names = {}
local turtles = {}
local remove = false
function fillTurtles()
turtles[1] = 8
-- turtles[2] = 114
-- turtles[3] = 117
end
function fillTable()
m.clear()
button.clearTable()
local totalrows = 0
local numNames = 0
local col = 2
local row = 12
local countRow = 1
local currName = 0
local npp = 12 --names per page
for turt, data in pairs(names) do
for i,j in pairs(data) do
totalrows = totalrows+1
end
end
pages = math.ceil(totalrows/npp)
print(totalrows)
for turt, data in pairs(names) do
currName = 0
for slot, name in pairs(data) do
currName = currName + 1
if currName > npp*(page-1) and currName < npp*page+1 then
row = 4+(countRow)
names[turt][slot] = string.sub(name, 0, 17)
button.setTable(string.sub(name, 0, 17), runStuff, turt..":"..slot, col, col+17 , row, row)
if col == 21 then
col = 2
countRow = countRow + 2
else
col = col+19
end
end
end
end
button.setTable("Next Page", nextPage, "", 21, 38, 1, 1)
button.setTable("Prev Page", prevPage, "", 2, 19, 1, 1)
button.setTable("Refresh", checkNames, "", 21, 38, 19, 19)
button.setTable("Remove Book", removeIt, "", 2, 19, 19, 19)
button.label(15,3, "Page: "..tostring(page).." of "..tostring(pages))
button.screen()
end
function nextPage()
if page+1 <= pages then page = page+1 end
fillTable()
end
function prevPage()
if page-1 >= 1 then page = page-1 end
fillTable()
end
function getNames()
names = {}
for x, y in pairs(turtles) do
names[y] = {}
rednet.send(y, "getNames")
local id, msg, dist = rednet.receive(2)
-- print(msg)
names[y] = textutils.unserialize(msg)
end
end
function removeIt()
remove = not remove
-- print(remove)
button.toggleButton("Remove Book")
end
function runStuff(info)
if remove == true then
removeBook(info)
else
openPortal(info)
end
end
function removeBook(info)
local turt, slot = string.match(info, "(%d+):(%d+)")
button.toggleButton(names[tonumber(turt)][tonumber(slot)])
data = "remove"..tostring(slot)
rednet.send(tonumber(turt), data)
rednet.receive()
button.toggleButton(names[tonumber(turt)][tonumber(slot)])
remove=false
button.toggleButton("Remove Book")
-- sleep(1)
checkNames()
end
function openPortal(info)
local turt,slot = string.match(info, "(%d+):(%d+)")
-- print(names[tonumber(turt)][tonumber(slot)])
button.toggleButton(names[tonumber(turt)][tonumber(slot)])
print(names[tonumber(turt)][tonumber(slot)])
data = "books"..tostring(slot)
rednet.send(tonumber(turt), data)
rednet.receive()
button.toggleButton(names[tonumber(turt)][tonumber(slot)])
end
function checkNames()
button.flash("Refresh")
for num, turt in pairs(turtles) do
rednet.send(turt, "checkSlots")
msg = ""
while msg ~= "done" do
id, msg, dist = rednet.receive()
if msg == "getName" then
m.clear()
m.setCursorPos(5, 12)
m.write("New book detected.")
m.setCursorPos(5, 14)
m.write("Please enter the name")
m.setCursorPos(5, 16)
m.write("On the computer")
m.setCursorPos(5, 18)
m.write("<<----")
term.clear()
term.write("Please enter a name for the new book: ")
name = read()
rednet.send(id, name)
end
end
end
getNames()
fillTable()
end
function getClick()
event, side, x,y = os.pullEvent()
if event == "monitor_touch" then
button.checkxy(x,y)
elseif event == "redstone" then
print("redstone")
sleep(5)
checkNames()
end
end
fillTurtles()
fillTable()
checkNames()
while true do
getClick()
-- checkNames()
end