-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker.lua
279 lines (244 loc) · 6.02 KB
/
tracker.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
trackerDB = {}
current_page = 0
modemSide = "back"
lang="fr"
local isDebug = false
--[[
Saving
]]
function saveData()
debug("Saving database...")
debug(textutils.serialize(trackerDB))
local f = fs.open("trackerDB/trackerDB", "w")
f.write(textutils.serialize(trackerDB))
f.close()
debug("Saved!")
end
function loadData()
debug("Checking for previous database...")
if fs.exists("trackerDB/trackerDB") then
f = fs.open("trackerDB/trackerDB", "r")
trackerDB = textutils.unserialize(f.readAll())
f.close()
debug("Database found. Loading data...")
end
end
function updateData(computer_id, table)
debug("Saving state of computer "..tostring(computer_id).." to trackerDB...")
trackerDB[computer_id] = {id = computer_id, name = table[8], location = vector.new(table[1], table[2], table[3])}
debug("Triggering database save")
saveData()
end
function debug(message)
if isDebug then
print(message)
sleep(.5)
end
end
--[[
Program functions
]]
function delete_turtle(computer_key)
while true do
computer = trackerDB[computer_key]
term.clear()
term.setCursorPos(1,1)
print("")
if lang == "fr" then
print("Tortue : ")
else
print("Turtle : ")
end
print(computer["name"])
print("")
if lang == "fr" then
print("Etes vous sur de vouloir supprimer cette tortue du tracker ?")
else
print("Are you sure you want to remove this turtle from the tracker?")
end
print("")
if lang == "fr" then
print("La tortue sera rajoutee de nouveau des qu'elle bougera...")
else
print("The turtle will be added again when it moves...")
end
print("")
print("")
if lang == "fr" then
print("Y pour Oui (Yes) / N pour Non ")
else
print("Y for yes / N for No")
end
local event, param1, param2, param3, param4 = os.pullEvent()
if event == "rednet_message" then
updateData(param1, param2)
-- print("Received rednet_message")
else
if event == "key" then
if param1 == 21 then
debug("Deleting turtle.")
trackerDB[computer_key] = nil
return true
else
if param1 == 49 then
return false
end
end
end
end
end
end
function track_or_edit(computer_key)
computer = trackerDB[computer_key]
term.clear()
term.setCursorPos(1,1)
print("")
if lang == "fr" then
print("Suivi de la tortue : ")
else
print("Tracking turtle:")
end
print(" "..computer["name"])
if lang == "fr" then
print("Derniere position connue :")
else
print("Last known location:")
end
loc = computer["location"]
if loc.x ~= nil and loc.y ~= nil and loc.z ~= nil then
print(" "..math.ceil(loc.x)..", "..math.ceil(loc.z).." ("..math.ceil(loc.y)..")")
else
if lang == "fr" then
print("Inconnue")
else
print("Unknown")
end
end
print("")
print("")
if lang == "fr" then
print("Appuyez sur une touche : ")
print(" X : Supprimer du tracker")
print(" B : Retour au menu")
else
print("Press a key : ")
print(" X : Delete from tracker")
print(" B : Go back to menu")
end
local event, param1, param2, param3, param4 = os.pullEvent()
if event == "rednet_message" then
updateData(param1, param2)
-- print("Received rednet_message")
else
if event == "key" then
if param1 == 48 then
return false
else
if param1 == 45 then
return (not delete_turtle(computer_key))
end
end
end
end
return true
end
function main()
local keyset={}
local n=0
term.clear()
term.setCursorPos(1,1)
for k,v in pairs(trackerDB) do
n=n+1
keyset[n]=k
end
table.sort(keyset)
if #keyset > 0 then
min = (current_page*9)+1
max = min+8
if min > #keyset then
current_page = 0
min = 1
max = 9
end
if max > #keyset then
max = #keyset
end
max_page = math.ceil(#keyset/9)
for i=min,max do
v = trackerDB[keyset[i]]
if v then
local loc = v["location"]
print("["..(i-min+1).."] "..v["name"])
end
end
else
if lang == "fr" then
print("Aucune tortue n'envoie sa position, et pas de tortues dans la base de donnees.")
else
print("No turtles sending its location, and no turtles in DB.")
end
end
if max_page and tonumber(max_page) > 0 then
print("")
print("Page "..tostring(tonumber(current_page)+1).."/"..max_page)
end
print("")
if lang == "fr" then
print("Appuyez sur une touche : ")
print(" 1-9 : Editer/Suivre")
print(" P : Changer pages")
print(" Q : Quitter")
else
print("Press a key : ")
print(" 1-9 : Edit/track turtle")
print(" P : Switch pages")
print(" Q : Quit this program")
end
print("")
local event, param1, param2, param3, param4 = os.pullEvent()
if event == "rednet_message" then
updateData(param1, param2)
-- print("Received rednet_message")
else
if event == "key" then
if param1 == 16 then
if lang == "fr" then
print("Stoppage du tracker")
else
print("Stopping tracker")
end
return false
else
if param1 == 25 then
current_page = current_page + 1
else
if (param1 <= 10 and param1 >= 2) or (param1 <= 81 and param1 >= 71) then
num = nil
if (param1 <= 10 and param1 >= 2) then num = (param1-1) end
if (param1 <= 73 and param1 >= 71) then num = (param1-64) end
if (param1 <= 77 and param1 >= 75) then num = (param1-71) end
if (param1 <= 81 and param1 >= 79) then num = (param1-78) end
num = (num+min-1)
if num >= min and num <= max then
while track_or_edit(keyset[num]) do
-- nothing
end
end
end
end
end
else
-- print("Received "..event)
end
end
return true
end
--[[
Start of program
]]
loadData()
rednet.open(modemSide)
while main() do
-- nothing
end
rednet.close()