Repository navigation
Expand file tree
/
Copy pathstorageClient.lua
More file actions
3176 lines (2958 loc) · 111 KB
/
Copy pathstorageClient.lua
File metadata and controls
3176 lines (2958 loc) · 111 KB
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
math.randomseed(os.time() + (5 * os.getComputerID()))
local timeoutConnect = nil
local width, height = term.getSize()
local scroll = 0
local freeSlots = 0
local totalSlots = 0
local search = ""
local items = {}
local recipes = {}
local detailDB = {}
local displayedRecipes = {}
local menu = false
local menuSel = "storage"
local storageServerSocket, craftingServerSocket
local isWirelessModem = false
local cryptoNetURL = "https://raw.githubusercontent.com/SiliconSloth/CryptoNet/master/cryptoNet.lua"
local username = ""
local password = ""
local bootTime = os.epoch("utc") / 1000
if not fs.exists("cryptoNet") then
print("")
print("cryptoNet API not found on disk, downloading...")
local response = http.get(cryptoNetURL)
if response then
local file = fs.open("cryptoNet", "w")
file.write(response.readAll())
file.close()
response.close()
print("File downloaded as '" .. "cryptoNet" .. "'.")
else
print("Failed to download file from " .. cryptoNetURL)
end
end
os.loadAPI("cryptoNet")
-- Suppress IDE warnings
os.getComputerID = os.getComputerID
os.epoch = os.epoch
os.loadAPI = os.loadAPI
os.queueEvent = os.queueEvent
os.startThread = os.startThread
os.pullEvent = os.pullEvent
os.startTimer = os.startTimer
os.reboot = os.reboot
os.setComputerLabel = os.setComputerLabel
utf8 = utf8
cryptoNet = cryptoNet
-- Settings
-- Settings
settings.define("StorageServer", {
description = "storage server hostname",
default = "StorageServer",
type = "string"
})
settings.define("CraftingServer", {
description = "crafting server hostname",
default = "CraftingServer",
type = "string"
})
settings.define("debug", {
description = "Enables debug options",
default = "false",
type = "boolean"
})
settings.define("crafting", {
description = "Enables crafting support",
default = "false",
type = "boolean"
})
settings.define("exportChestName", {
description = "Name of the export chest for this client",
default = "minecraft:chest_0",
type = "string"
})
local logging = true
local debug = false
-- FIFO queues to match getAmount requests with responses in order
local pendingGetAmountResponses = {}
-- Settings fails to load
if settings.load() == false then
print("No settings have been found! Default values will be used!")
-- settings.set("StorageServer", "StorageServer")
-- settings.set("craftingServer", "CraftingServer")
settings.set("debug", false)
settings.set("crafting", false)
settings.set("exportChestName", "minecraft:chest_0")
print("Stop the client and edit .settings file with correct settings")
settings.save()
sleep(5)
end
term.setBackgroundColor(colors.blue)
Item = {
name = "",
count = 1,
nbt = "",
tags = ""
}
---@diagnostic disable-next-line: duplicate-set-field
function Item:new(name, count, nbt, tags)
obj = obj or {}
setmetatable(obj, self)
self.__index = self
self.name = name or ""
self.count = count or 1
self.nbt = nbt or ""
self.tags = tags or nil
return obj
end
---@diagnostic disable-next-line: duplicate-set-field
function Item:getTable()
local table = {}
if self.name ~= "" then
table["name"] = self.name
end
if self.count ~= 0 then
table["count"] = self.count
end
if self.nbt ~= "" then
table["nbt"] = self.nbt
end
return table
end
local function log(text)
if settings.get("debug") then
local logFile = fs.open("logs/clientDebug.log", "a")
if type(text) == "string" then
logFile.writeLine(text)
else
logFile.writeLine(textutils.serialise(text))
end
logFile.close()
end
end
local function dump(o)
if type(o) == "table" then
local s = ""
for k, v in pairs(o) do
if type(k) ~= "number" then
k = '"' .. k .. '"'
end
s = s .. "[" .. k .. "] = " .. dump(v) .. ","
end
return s
else
return tostring(o)
end
end
local function getRecipes(maxRetries)
maxRetries = maxRetries or 3
local attempts = 0
local timeoutSecs = 5
while attempts < maxRetries do
if not craftingServerSocket then
log("Warning: craftingServerSocket is nil, cannot request recipes")
print("Warning: craftingServerSocket is nil, cannot request recipes")
sleep(1)
return
end
cryptoNet.send(craftingServerSocket, {"getRecipes"})
local timer = os.startTimer(timeoutSecs)
local event, eventData
repeat
event, eventData = os.pullEvent()
until event == "gotRecipes" or (event == "timer" and eventData == timer)
if event == "gotRecipes" then
if next(recipes) then
return -- Successfully got recipes
end
end
-- Either timed out or got empty recipes, retry
attempts = attempts + 1
if attempts < maxRetries then
log("getRecipes attempt " .. attempts .. " failed, retrying...")
print("getRecipes attempt " .. attempts .. " failed, retrying...")
sleep(0.5)
end
end
log("Warning: Failed to get recipes after " .. maxRetries .. " attempts")
print("Warning: Failed to get recipes after " .. maxRetries .. " attempts")
sleep(1)
end
local function pingStorageServer()
cryptoNet.send(storageServerSocket, {"ping"})
local event
repeat
event = os.pullEvent("storageServerAck")
until event == "storageServerAck"
end
local function getItemDetails(item)
if type(item) ~= "nil" and type(item.chestName) ~= "nil" and type(item.slot) ~= "nil" then
cryptoNet.send(storageServerSocket, {"getItemDetails", item})
local event, data
repeat
event, data = os.pullEvent("gotItemDetails")
until event == "gotItemDetails"
if type(data) == "table" then
return data
else
sleep(0.5 + (math.random() % 0.2))
return getItemDetails()
end
end
end
local function centerText(text)
local x, y = term.getSize()
local x1, y1 = term.getCursorPos()
term.setCursorPos((math.floor(x / 2) - (math.floor(#text / 2))), y1)
term.write(text)
end
local function loadingScreen(text)
if type(text) == nil then
text = ""
end
term.setBackgroundColor(colors.red)
term.clear()
term.setCursorPos(1, 2)
centerText(text)
term.setCursorPos(1, 4)
centerText("Loading...")
term.setCursorPos(1, 6)
end
local function findInTable(arr, element)
for i, value in pairs(arr) do
if value.name == element.name and value.nbt == element.nbt then
return i
end
end
return nil
end
local function inTable(arr, element) -- function to check if something is in an table
for _, value in pairs(arr) do
if value.name == element.name and value.nbt == element.nbt then
return true
end
end
return false -- if no element was found, return false
end
local function removeDuplicates(arr)
local newArray = {} -- new array that will be arr, but without duplicates
for _, element in pairs(arr) do
if not inTable(newArray, element) then -- making sure we had not added it yet to prevent duplicates
-- if element.details == nil then
-- element.details = peripheral.wrap(element.chestName).getItemDetail(element.slot)
-- end
table.insert(newArray, element)
else
local index = findInTable(newArray, element)
if index ~= nil then
newArray[index]["count"] = newArray[index].count + element.count
end
end
end
return newArray -- returning the new, duplicate removed array
end
-- Filters items by search term
local function filterItems()
local filteredTable = {}
for k, v in pairs(items) do
if v["details"] == nil then
if string.find(string.lower(v["name"]), string.lower(search)) or
string.find(string.lower(v["name"]), string.lower(search:gsub(" ", "_"))) then
local tab = v
-- tab.details = peripheral.wrap(v.chestName).getItemDetail(v.slot)
table.insert(filteredTable, tab)
end
elseif string.find(string.lower(v["details"]["displayName"]), string.lower(search)) or
string.find(string.lower(v["details"]["displayName"]), string.lower(search:gsub(" ", "_"))) or
string.find(string.lower(v["name"]), string.lower(search)) or
string.find(string.lower(v["name"]), string.lower(search:gsub(" ", "_"))) then
table.insert(filteredTable, v)
end
end
local outputTable = removeDuplicates(filteredTable)
return outputTable
end
local function getDetailDBFromServer()
print("Loading item metadata")
cryptoNet.send(storageServerSocket, {"getDetailDB"})
local event
repeat
event = os.pullEvent("detailDBUpdated")
until event == "detailDBUpdated"
-- local count = 0
-- for _ in pairs(detailDB) do count = count + 1 end
-- print("Got " .. tostring(count) .. " items metadeta from storageserver")
-- sleep(5)
end
local function reloadDatabase()
--Ask server to force reload database
cryptoNet.send(storageServerSocket, {"reloadStorageDatabase"})
local event
repeat
event = os.pullEvent("databaseReloaded")
until event == "databaseReloaded"
if craftingServerSocket ~= nil then
cryptoNet.send(craftingServerSocket, {"databaseReload"})
end
end
local function getItems()
-- pingStorageServer()
cryptoNet.send(storageServerSocket, {"getItems"})
local event
repeat
event = os.pullEvent("itemsUpdated")
until event == "itemsUpdated"
cryptoNet.send(storageServerSocket, {"getFreeSlots"})
repeat
event = os.pullEvent("freeSlotsUpdated")
until event == "freeSlotsUpdated"
end
local function importAll()
loadingScreen("Importing from Export chests")
-- print("Waiting for server to be ready")
-- pingStorageServer()
-- print("Importing")
cryptoNet.send(storageServerSocket, {"importAll"})
local event
repeat
event = os.pullEvent("importAllComplete")
until event == "importAllComplete"
-- pingStorageServer()
print("Import Complete")
print("Reloading Database")
getItems()
end
local function export(item)
cryptoNet.send(storageServerSocket, {"export", {
item = item:getTable(),
chest = settings.get("exportChestName")
}})
end
local function discoverServers(serverType)
local serverList = {}
-- while next(serverList) == nil do
print("Looking for servers")
serverList = cryptoNet.discover()
-- end
local done = false
local scrollDiscovery = 0
while done == false do
term.setBackgroundColor(colors.gray)
term.clear()
term.setCursorPos(1, 1)
-- log(dump(serverList))
for k, v in pairs(serverList) do
if k > scrollDiscovery then
if k < (height + scrollDiscovery) then
local text = v.name
for i = 1, width, 1 do
term.setCursorPos(i, k - scrollDiscovery)
term.write(" ")
end
term.setCursorPos(1, k - scrollDiscovery)
term.write(text)
term.setCursorPos(1, height)
end
end
end
for k = 1, height - 1, 1 do
if type(serverList[k + scrollDiscovery]) == "nil" then
for i = 1, width, 1 do
term.setCursorPos(i, k)
term.write(" ")
end
end
end
-- refresh button
term.setCursorPos(width - 12, height - 1)
term.setBackgroundColor(colors.red)
term.write(" Refresh (F5) ")
term.setBackgroundColor(colors.black)
for i = 1, width, 1 do
term.setCursorPos(i, height)
term.write(" ")
end
term.setCursorPos(1, height)
term.write("Discovered " .. serverType .. " Servers")
term.setBackgroundColor(colors.gray)
local event, button, x, y
repeat
event, button, x, y = os.pullEvent()
until event == "mouse_click" or event == "key" or event == "mouse_scroll"
if event == "key" then
local key = button
if key == keys.backspace then
done = true
elseif key == keys.f5 then
done = true
discoverServers(serverType)
elseif key == keys.one then
if serverList[1] ~= nil then
settings.set(serverType, serverList[1].name)
settings.save()
done = true
end
elseif key == keys.two then
if serverList[2] ~= nil then
settings.set(serverType, serverList[2].name)
settings.save()
done = true
end
elseif key == keys.three then
if serverList[3] ~= nil then
settings.set(serverType, serverList[3].name)
settings.save()
end
elseif key == keys.four then
if serverList[4] ~= nil then
settings.set(serverType, serverList[4].name)
settings.save()
done = true
end
elseif key == keys.five then
if serverList[5] ~= nil then
settings.set(serverType, serverList[5].name)
settings.save()
done = true
end
elseif key == keys.six then
if serverList[6] ~= nil then
settings.set(serverType, serverList[6].name)
settings.save()
done = true
end
elseif key == keys.seven then
if serverList[7] ~= nil then
settings.set(serverType, serverList[7].name)
settings.save()
done = true
end
elseif key == keys.eight then
if serverList[8] ~= nil then
settings.set(serverType, serverList[8].name)
settings.save()
done = true
end
elseif key == keys.nine then
if serverList[9] ~= nil then
settings.set(serverType, serverList[9].name)
settings.save()
done = true
end
end
elseif event == "mouse_scroll" then
if button == -1 then
if scrollDiscovery > 0 then
scrollDiscovery = scrollDiscovery - 1
end
elseif button == 1 then
scrollDiscovery = scrollDiscovery + 1
end
elseif event == "mouse_click" then
-- log("mouse_click x" .. tostring(x) .. " y" .. tostring(y) .. " scroll: " .. tostring(scroll))
if y == height - 1 and x > width - 12 then
-- refresh button pressed
done = true
discoverServers(serverType)
elseif (serverList[y + scrollDiscovery] ~= nil) and y ~= height then
settings.set(serverType, serverList[y + scrollDiscovery].name)
settings.save()
done = true
end
end
end
end
-- Logs in using password hash
-- This allows multiple servers to use a central server as an auth server by passing the hash
local function login(socket, user, pass, servername)
-- Check if wireless server
local startIndex, endIndex = string.find(servername, "_Wireless")
if startIndex then
-- get the server name by cutting out "_Wireless"
servername = string.sub(servername, 1, startIndex - 1)
log("wireless server rename: " .. servername)
else
log("servername " .. servername)
end
local tmp = {}
tmp.username = user
tmp.password = pass
tmp.servername = servername
-- mark for garbage collection
pass = nil
-- log("hashLogin")
cryptoNet.send(socket, {"hashLogin", tmp})
-- mark for garbage collection
tmp = nil
local event
local loginStatus = false
local permissionLevel = 0
repeat
event, loginStatus, permissionLevel = os.pullEvent("hashLogin")
until event == "hashLogin"
log("loginStatus:" .. tostring(loginStatus))
if loginStatus == true then
socket.username = user
socket.permissionLevel = permissionLevel
os.queueEvent("login", user, socket)
else
term.setCursorPos(1, 1)
error("Failed to login to Server")
end
return socket
end
local function loginScreen()
local done = false
local user = ""
local pass = ""
local text = ""
local selectedField = "user"
while done == false do
term.setBackgroundColor(colors.gray)
term.clear()
term.setCursorPos(1, 1)
term.setTextColor(colors.white)
-- calc the width needed to fit the server name in login box
local border
border = math.ceil((width - string.len(settings.get("StorageServer")) - 2) / 2)
local widthBlanks = ""
for i = 1, width, 1 do
widthBlanks = widthBlanks .. " "
end
-- print computer information
if (settings.get("debug")) then
term.setCursorPos(1, 1)
term.write("DEBUG MODE")
end
term.setCursorPos(1, height)
term.write("ID:" .. tostring(os.getComputerID()))
-- print(tostring(border))
local forth = math.floor(height / 4)
for k = forth, height - forth, 1 do
if k == forth then
term.setBackgroundColor(colors.black)
else
term.setBackgroundColor(colors.lightGray)
end
term.setCursorPos(1, k)
term.write(widthBlanks)
end
term.setBackgroundColor(colors.black)
term.setCursorPos(1, forth)
centerText("Storage Login")
term.setTextColor(colors.black)
term.setBackgroundColor(colors.lightGray)
term.setCursorPos(1, forth + 2)
for i = border, width - border, 1 do
term.setCursorPos(i, forth + 2)
term.write("~")
end
term.setCursorPos(1, forth + 3)
centerText(settings.get("StorageServer"))
term.setCursorPos(1, forth + 4)
for i = border, width - border, 1 do
term.setCursorPos(i, forth + 4)
term.write("~")
end
-- Adjust for pocket screen
if pocket then
border = 1
else
border = math.ceil((width) / 4)
end
term.setBackgroundColor(colors.white)
term.setTextColor(colors.black)
for i = border + 6, width - border - 1, 1 do
term.setCursorPos(i, forth + 6)
term.write(" ")
end
term.setCursorPos(border + 6, forth + 6)
term.write(user)
term.setCursorPos(border + 1, forth + 6)
term.setBackgroundColor(colors.lightGray)
print("User:")
term.setBackgroundColor(colors.white)
for i = border + 6, width - border - 1, 1 do
term.setCursorPos(i, forth + 8)
term.write(" ")
end
term.setCursorPos(border + 6, forth + 8)
-- write password sub text
for i = 1, string.len(pass), 1 do
term.write("*")
end
term.setCursorPos(border + 1, forth + 8)
term.setBackgroundColor(colors.lightGray)
print("Pass:")
term.setCursorPos(border + 1, forth + 10)
term.setBackgroundColor(colors.red)
term.write(" Change Server ")
term.setCursorPos(width - border - 7, forth + 10)
term.setBackgroundColor(colors.green)
term.write(" Login ")
local event, button, x, y
repeat
event, button, x, y = os.pullEvent()
until event == "mouse_click" or event == "key" or event == "char"
if event == "char" then
local key = button
-- search = search .. key
if selectedField == "user" then
user = user .. key
else
pass = pass .. key
end
elseif event == "key" then
local key = button
if key == keys.backspace then
-- remove from text entry
if selectedField == "user" then
user = user:sub(1, -2)
else
pass = pass:sub(1, -2)
end
elseif key == keys.enter or key == keys.numPadEnter then
-- set creds
username = user
password = pass
user = ""
pass = ""
done = true
elseif key == keys.tab then
-- toggle user/pass text entry
if selectedField == "user" then
selectedField = "pass"
else
selectedField = "user"
end
end
elseif event == "mouse_click" then
-- log("mouse_click x" .. tostring(x) .. " y" .. tostring(y) .. " scroll: " .. tostring(scroll))
if y == math.floor(height / 4) + 10 then
if (x > width - border - 7 and x < width - border - 7 + 15) then
-- login
username = user
password = pass
user = ""
pass = ""
done = true
elseif (x > border + 1 and x < border + 1 + 7) then
-- change server
discoverServers("StorageServer")
if settings.get("crafting") then
discoverServers("CraftingServer")
end
end
end
end
end
term.setTextColor(colors.white)
term.setBackgroundColor(colors.red)
term.clear()
term.setCursorPos(1, 1)
end
local function drawDetailsmenu(sel)
local filteredItems = filterItems()
local amount = 1
local done = false
while done == false do
term.setBackgroundColor(colors.green)
for k = 3, height - 1, 1 do
for i = 1, width, 1 do
term.setCursorPos(i, k)
term.write(" ")
end
end
term.setCursorPos(1, 1)
term.setBackgroundColor(colors.black)
for i = 1, width, 1 do
term.setCursorPos(i, 1)
term.write(" ")
end
centerText("Details Menu")
term.setCursorPos(1, 2)
term.setBackgroundColor(colors.blue)
for i = 1, width, 1 do
term.setCursorPos(i, 2)
term.write(" ")
end
centerText(filteredItems[sel].name .. " #" .. tostring(filteredItems[sel].count))
term.setBackgroundColor(colors.green)
term.setCursorPos(1, 3)
if filteredItems[sel].details ~= nil then
write(dump(filteredItems[sel].details))
end
term.setCursorPos(width, 1)
term.setBackgroundColor(colors.red)
term.write("x")
term.setBackgroundColor(colors.green)
local event, button, x, y
repeat
event, button, x, y = os.pullEvent()
until event == "mouse_click" or event == "key"
if event == "key" then
local key = button
if key == keys.backspace then
done = true
end
end
if event == "mouse_click" then
if y < 2 and x > width - 1 then
done = true
end
end
-- sleep(5)
end
end
local function drawCraftingStatus(nowCrafting)
term.setBackgroundColor(colors.black)
cryptoNet.send(craftingServerSocket, {"watchCrafting"})
local table = {}
local logs = {}
local status
-- Request the current crafting status from the crafting server
local event
local currentlyCrafting = {}
cryptoNet.send(craftingServerSocket, {"getCurrentlyCrafting"})
repeat
event, currentlyCrafting = os.pullEvent("gotCurrentlyCrafting")
until event == "gotCurrentlyCrafting"
log("currentlyCrafting: " .. dump(currentlyCrafting))
if currentlyCrafting ~= nil and currentlyCrafting.table ~= nil then
log("if currentlyCrafting ~= nil and currentlyCrafting.table ~= nil then")
table = currentlyCrafting.table
logs = currentlyCrafting.log
nowCrafting = currentlyCrafting.nowCrafting
end
if not next(table) then
for row = 1, 3, 1 do
table[row] = {}
for slot = 1, 3, 1 do
table[row][slot] = 0
end
end
end
if nowCrafting == nil then
nowCrafting = ""
end
while true do
term.clear()
term.setCursorPos(1, 1)
if nowCrafting == "" then
centerText("Waiting for Crafting Server")
else
centerText("Crafting:" .. nowCrafting:match(".+:(.+)"))
end
-- Draw crafting table
term.setCursorPos(1, 2)
term.setBackgroundColor(colors.gray)
print(" ")
-- term.setCursorPos(1, 2 + 1)
term.setCursorPos(1, 1)
local pos = 1
for row = 1, 3, 1 do
term.setCursorPos(1, 2 + row)
term.setBackgroundColor(colors.gray)
term.write(" ")
for slot = 1, 3, 1 do
-- log(textutils.serialise(recipe.recipe[row][slot][1]))
if table[row][slot] == 0 then
term.setBackgroundColor(colors.black)
term.write(" ")
else
term.setBackgroundColor(colors.green)
term.write(string.format("%02d", table[row][slot]))
end
if slot ~= 3 then
-- term.setBackgroundColor(colors.gray)
-- term.write(" ")
end
end
term.setCursorPos(8, 2 + row)
term.setBackgroundColor(colors.gray)
term.write(" ")
end
term.setBackgroundColor(colors.gray)
term.setCursorPos(1, 6)
print(" ")
-- Draw logs
term.setBackgroundColor(colors.black)
local count = 0
if pocket then
for i = 8, (height - 2), 1 do
if #logs - count > 0 and type(logs[#logs - count]) ~= "nil" then
term.setCursorPos(1, i)
term.write(logs[#logs - count])
count = count + 1
end
end
else
for i = 2, (height - 2), 1 do
if #logs - count > 0 and type(logs[#logs - count]) ~= "nil" then
term.setCursorPos(9, i)
term.write(logs[#logs - count])
count = count + 1
end
end
end
-- draw close button
term.setCursorPos(width, 1)
term.setBackgroundColor(colors.red)
term.write("x")
-- draw crafting status
if status == true then
term.setCursorPos(1, height)
term.setBackgroundColor(colors.green)
centerText(" Crafting Complete! ")
elseif status == false then
term.setCursorPos(1, height)
term.setBackgroundColor(colors.red)
centerText(" Crafting Failed! ")
end
term.setBackgroundColor(colors.black)
local button, x, y
repeat
event, button, x, y = os.pullEvent()
until event == "craftingUpdate" or event == "mouse_click" or event == "key"
-- handle key and mouse inputs
if (event == "key" or event == "mouse_click") then
if button == keys.backspace or (y == 1 and x == width) then
cryptoNet.send(craftingServerSocket, {"stopWatchCrafting"})
return
end
end
local message = button
log(textutils.serialise(message))
-- update slots and crafting log
if type(message) == "table" then
if message.type == "craftingUpdate" then
log(textutils.serialise(message.message))
if type(message.message) == "boolean" then
if message.message then
status = true
log(" Crafting Complete! ")
else
status = false
log(" Crafting Failed! ")
end
term.setBackgroundColor(colors.black)
elseif message.message == "slotUpdate" then
table[message[1]][message[2]] = message[3]
elseif message.message == "itemUpdate" then
nowCrafting = message[1]
for row = 1, 3, 1 do
table[row] = {}
for slot = 1, 3, 1 do
table[row][slot] = 0
end
end
elseif message.message == "logUpdate" then
if logs[#logs] ~= message[1] then
logs[#logs + 1] = message[1]
end
end
end
end
end
end
local function craftRecipe(recipe, amount, canCraft)
recipe.amount = amount
if canCraft == true then
cryptoNet.send(craftingServerSocket, {"craftItem", recipe})
else
cryptoNet.send(craftingServerSocket, {"autoCraftItem", recipe})
end
drawCraftingStatus()
end
local function mergeCraftingQueue()
local event
local craftingQueue = {}
local currentlyCrafting
loadingScreen("Getting crafting queue")
-- Get what's crafting right now
cryptoNet.send(craftingServerSocket, {"getCurrentlyCrafting"})
repeat
event, currentlyCrafting = os.pullEvent("gotCurrentlyCrafting")
until event == "gotCurrentlyCrafting"
log("currentlyCrafting: " .. dump(currentlyCrafting))
-- Get what is in the crafting queue
cryptoNet.send(craftingServerSocket, {"getCraftingQueue"})
repeat
event, craftingQueue = os.pullEvent("gotCraftingQueue")
until event == "gotCraftingQueue"
-- log("craftingQueue: " .. dump(craftingQueue))
-- merge whats crafting and crafting queue
local queue = {}
if next(currentlyCrafting) then
queue = {currentlyCrafting}
end
for i = 1, #craftingQueue, 1 do
table.insert(queue, craftingQueue[i])
end
-- log("queue: " .. textutils.serialise(queue))
return queue
end
local function drawCraftingQueue()
menu = true
local scrollCraftingQueue = 0
local queue = mergeCraftingQueue()
local done = false
while done == false do
term.setBackgroundColor(colors.gray)
term.clear()
term.setCursorPos(1, 1)
-- log(dump(serverList))
for k, v in pairs(queue) do
if k > scrollCraftingQueue then
if k < (height + scrollCraftingQueue) then
local text
if v.displayName ~= nil then
text = v.displayName .. ": #" .. tostring(v.amount) .. " - " .. v.name
else
text = v.name .. ": #" .. tostring(v.amount) .. " - " .. v.name
end