· last year · Mar 21, 2024, 05:55 PM
1local Item = peripheral.wrap("top")
2
3
4
5local turtleID = tostring(os.getComputerID())
6
7
8
9local chatBox = peripheral.find("chatBox")
10
11
12
13
14
15
16
17--local monitorPeripheral = "monitor_35"
18
19
20
21-- Read monitor peripheral name from mon.txt file
22
23
24
25local monitorFile = fs.open("mon.txt", "r")
26
27
28
29if monitorFile then
30
31
32
33 monitorPeripheral = monitorFile.readLine() or monitorPeripheral
34
35
36
37 monitorFile.close()
38
39
40
41else
42
43
44
45 print("Error: Unable to open mon.txt. Using default monitor peripheral name.")
46
47
48
49end
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-- Redstone Integrator initialization
78
79
80
81local Integrator = peripheral.find("redstoneIntegrator")
82
83
84
85
86
87
88
89-- Function to toggle the lights
90
91
92
93function toggleLights(color)
94
95
96
97 if color == "green" then
98
99
100
101 Integrator.setAnalogueOutput("top", 15) -- Turn on green light
102
103
104
105 sleep(0.3) -- Wait for 0.5 seconds
106
107
108
109 Integrator.setAnalogueOutput("top", 0) -- Turn off green light
110
111
112
113 elseif color == "red" then
114
115
116
117 Integrator.setAnalogueOutput("left", 15) -- Turn on red light
118
119
120
121 sleep(0.3) -- Wait for 0.5 seconds
122
123
124
125 Integrator.setAnalogueOutput("left", 0) -- Turn off red light
126
127
128
129 end
130
131
132
133end
134
135
136
137
138
139
140
141
142
143
144
145-- Function to get item details
146
147
148
149local function getItemDetails()
150
151
152
153 local itemDetails = Item.getItemDetail(1)
154
155
156
157 if itemDetails then
158
159
160
161 return itemDetails.displayName, itemDetails.name
162
163
164
165 end
166
167
168
169 return nil, nil
170
171
172
173end
174
175
176
177-- Function to check if a file exists
178
179
180
181local function fileExists(path)
182
183
184
185 return fs.exists(path) and not fs.isDir(path)
186
187
188
189end
190
191
192
193-- Function to check if "cost.txt" is formatted correctly
194
195
196
197local function checkCostFile()
198
199
200
201 local costPath = "cost.txt"
202
203
204
205 if not fileExists(costPath) then
206
207
208
209 print("Error: Missing 'cost.txt'. Please create and format the file.")
210
211
212
213 return false
214
215
216
217 end
218
219
220
221 local costFile = fs.open(costPath, "r")
222
223
224
225 local countLine = costFile.readLine()
226
227
228
229 local priceLine = costFile.readLine()
230
231
232
233 costFile.close()
234
235
236
237 if not countLine or not priceLine or not countLine:match("Count = (%d+)") or not priceLine:match("Price = (%d+)") then
238
239
240
241 print("Error: Incorrect format in 'cost.txt'. Please ensure it contains 'Count = <number>' and 'Price = <number>'.")
242
243
244
245 return false
246
247
248
249 end
250
251
252
253 return true
254
255
256
257end
258
259
260
261-- Function to check if an item is in front
262
263
264
265local function checkItemInFront()
266
267
268
269 local DisplayName, ItemName = getItemDetails()
270
271
272
273 if not DisplayName or not ItemName then
274
275
276
277 print("Error: No item detected in front. Please place an item in front of the turtle.")
278
279
280
281 return false
282
283
284
285 end
286
287
288
289 return true
290
291
292
293end
294
295
296
297-- Main initialization
298
299
300
301if not checkCostFile() or not checkItemInFront() then
302
303 print("rebooting in 3...")
304
305 os.sleep(3)
306
307 os.reboot()
308
309
310
311 -- Handle initialization errors and exit the program if necessary
312
313
314
315 return
316
317
318
319end
320
321
322
323-- Function to load the balance data from ShopBal.txt
324
325
326
327local function loadBalances()
328
329
330
331 local path = "disk/ShopBal.txt"
332
333
334
335 local file = fs.open(path, "r")
336
337
338
339 if file then
340
341
342
343 local content = file.readAll()
344
345
346
347 file.close()
348
349
350
351 local success, balances = pcall(textutils.unserialize, content)
352
353
354
355 if success and type(balances) == "table" then
356
357
358
359 return balances
360
361
362
363 else
364
365
366
367 print("Error: Unable to unserialize balance data from " .. path)
368
369
370
371 print("Error Message: " .. tostring(balances))
372
373
374
375 end
376
377
378
379 else
380
381
382
383 print("Error: Unable to open file " .. path)
384
385
386
387 end
388
389
390
391 return {}
392
393
394
395end
396
397
398
399-- Function to save the updated balances to ShopBal.txt
400
401
402
403local function saveBalances(balances)
404
405
406
407 local path = "disk/ShopBal.txt"
408
409
410
411 local file = fs.open(path, "w")
412
413
414
415 if file then
416
417
418
419 file.write(textutils.serialize(balances))
420
421
422
423 file.close()
424
425
426
427 else
428
429
430
431 print("Error: Unable to open file " .. path)
432
433
434
435 end
436
437
438
439end
440
441
442
443-- Function to save player items to a file with counts
444
445
446
447local function savePlayerItems(username, displayName, itemName, count)
448
449
450
451 local folderPath = "disk/Players/"
452
453
454
455 local filePath = folderPath .. username
456
457
458
459 local file
460
461
462
463 -- Create the folder if it doesn't exist
464
465
466
467 if not fs.isDir(folderPath) then
468
469
470
471 fs.makeDir(folderPath)
472
473
474
475 end
476
477
478
479 -- Open the file in append mode
480
481
482
483 if fs.exists(filePath) then
484
485
486
487 file = fs.open(filePath, "r")
488
489
490
491 local content = file.readAll()
492
493
494
495 file.close()
496
497
498
499 -- Parse existing content to update item counts
500
501
502
503 local playerItems = {}
504
505
506
507 for line in content:gmatch("[^\r\n]+") do
508
509
510
511 local itemDisplayName, itemNameExisting, existingCount = line:match("([^,]+),([^,]+),(%d+)")
512
513
514
515 if itemDisplayName and itemNameExisting and existingCount then
516
517
518
519 existingCount = tonumber(existingCount)
520
521
522
523 playerItems[itemDisplayName .. "," .. itemNameExisting] = existingCount
524
525
526
527 end
528
529
530
531 end
532
533
534
535 -- If the item doesn't exist in the file, add a new line
536
537
538
539 if not playerItems[displayName .. "," .. itemName] then
540
541
542
543 playerItems[displayName .. "," .. itemName] = 0
544
545
546
547 end
548
549
550
551 -- Update the count based on the specified count
552
553
554
555 playerItems[displayName .. "," .. itemName] = playerItems[displayName .. "," .. itemName] + count
556
557
558
559 file = fs.open(filePath, "w")
560
561
562
563 for key, value in pairs(playerItems) do
564
565
566
567 file.writeLine(key .. "," .. value)
568
569
570
571 end
572
573
574
575 file.close()
576
577
578
579 else
580
581
582
583 -- Open the file in write mode with count for the first occurrence
584
585
586
587 file = fs.open(filePath, "w")
588
589
590
591 if file then
592
593
594
595 file.writeLine(displayName .. "," .. itemName .. "," .. count)
596
597
598
599 file.close()
600
601
602
603 else
604
605
606
607 print("Error: Unable to open file " .. filePath)
608
609
610
611 end
612
613
614
615 end
616
617
618
619end
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639-- Function to handle successful money transaction
640
641
642
643local function Money_Success(username, cost, balances)
644
645
646
647 print("Transaction successful for " .. username .. ". Cost: " .. cost)
648
649
650
651 balances[username] = balances[username] - cost
652
653
654
655 saveBalances(balances)
656
657
658
659 local DisplayName, ItemName = getItemDetails()
660
661
662
663 -- Assuming 'cost.txt' contains individual count and price lines
664
665
666
667 local costPath = "cost.txt"
668
669
670
671 local costFile = fs.open(costPath, "r")
672
673
674
675 if costFile then
676
677
678
679 local countLine = costFile.readLine()
680
681
682
683 local priceLine = costFile.readLine()
684
685
686
687 local count = tonumber(countLine:match("Count = (%d+)"))
688
689
690
691 local price = tonumber(priceLine:match("Price = (%d+)"))
692
693
694
695 if count and price then
696
697
698
699 local cost = price -- Cost per item, not total cost
700
701
702
703 savePlayerItems(username, DisplayName, ItemName, count) -- Pass the count as the fourth parameter
704
705
706
707
708
709
710
711 -- Send toast notification to the player who clicked
712
713
714
715 local title = {
716
717
718
719 { text = "SHOP", color = "dark_purple"}
720
721
722
723 }
724
725
726
727
728
729
730
731 local message = {
732
733
734
735 { text = DisplayName .. " x " .. count .. " bought successfully!", color = "white"}
736
737
738
739 }
740
741
742
743
744
745
746
747 local titleJson = textutils.serializeJSON(title)
748
749
750
751 local messageJson = textutils.serializeJSON(message)
752
753
754
755
756
757
758
759 chatBox.sendFormattedToastToPlayer(messageJson, titleJson, username,"&2&l+", "()", "&2&l")
760
761
762
763 else
764
765
766
767 print("Error: Unable to read count and price from " .. costPath)
768
769
770
771 end
772
773
774
775 costFile.close()
776
777
778
779 else
780
781
782
783 print("Error: Unable to open file " .. costPath)
784
785
786
787 end
788
789
790
791 -- Toggle green light upon successful transaction
792
793
794
795 toggleLights("green")
796
797
798
799end
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823-- Function to handle failed money transaction
824
825
826
827local function Money_Fail(username, cost, currentBalance)
828
829
830
831 print("Transaction failed for " .. username .. ". Insufficient funds. Cost: " .. cost)
832
833
834
835 print("Current Balance for " .. username .. ": " .. currentBalance)
836
837
838
839 toggleLights("red")
840
841
842
843
844
845
846
847 -- Sending toast notification to the player
848
849
850
851 local title = {
852
853
854
855 { text = "SHOP", color = "dark_purple"}
856
857
858
859 }
860
861
862
863
864
865
866
867 local message = {
868
869
870
871 { text = "Insufficient Balance! Cannot complete transaction.", color = "white"}
872
873
874
875 }
876
877
878
879
880
881
882
883 local titleJson = textutils.serializeJSON(title)
884
885
886
887 local messageJson = textutils.serializeJSON(message)
888
889
890
891
892
893
894
895 chatBox.sendFormattedToastToPlayer(messageJson, titleJson, username,"&c&l!", "()", "&c&l")
896
897
898
899end
900
901
902
903
904
905
906
907
908
909
910
911-- Function to compare the balance and perform actions accordingly
912
913
914
915local function checkBalance(username, cost)
916
917
918
919 local balances = loadBalances()
920
921
922
923 if balances[username] then
924
925
926
927 local playerBalance = balances[username]
928
929
930
931 if playerBalance >= cost then
932
933
934
935 Money_Success(username, cost, balances)
936
937
938
939 else
940
941
942
943 Money_Fail(username, cost, playerBalance)
944
945
946
947 end
948
949
950
951 else
952
953
954
955 Money_Fail(username, cost, 0) -- Assuming initial balance is 0
956
957
958
959 end
960
961
962
963end
964
965
966
967
968
969
970
971-- Function to update the global database
972
973
974
975local function updateGlobalDatabase(DisplayName, Price, Count, TurtleID)
976
977
978
979 local path = "disk/GlobalDatabase.txt"
980
981
982
983 local file
984
985
986
987 -- Extract the item name without the "minecraft:" prefix
988
989
990
991 local itemName = DisplayName --:gsub("minecraft:", "")
992
993
994
995 if fs.exists(path) then
996
997
998
999 file = fs.open(path, "r")
1000
1001
1002
1003 local content = file.readAll()
1004
1005
1006
1007 file.close()
1008
1009
1010
1011 -- Parse existing content to find and update the line for the current TurtleID
1012
1013
1014
1015 local lines = {}
1016
1017
1018
1019 local foundTurtleID = false
1020
1021
1022
1023 for line in content:gmatch("[^\r\n]+") do
1024
1025
1026
1027 local itemNameExisting, priceExisting, countExisting, turtleIDExisting = line:match("([^|]+)|([^|]+)|([^|]+)|([^|]+)")
1028
1029
1030
1031 if itemNameExisting and priceExisting and countExisting and turtleIDExisting then
1032
1033
1034
1035 if tonumber(turtleIDExisting) == TurtleID then
1036
1037
1038
1039 foundTurtleID = true
1040
1041
1042
1043 line = itemName .. "|" .. Price .. "|" .. Count .. "|" .. TurtleID
1044
1045
1046
1047 end
1048
1049
1050
1051 table.insert(lines, line)
1052
1053
1054
1055 end
1056
1057
1058
1059 end
1060
1061
1062
1063 -- If the TurtleID was not found, append a new line
1064
1065
1066
1067 if not foundTurtleID then
1068
1069
1070
1071 table.insert(lines, itemName .. "|" .. Price .. "|" .. Count .. "|" .. TurtleID)
1072
1073
1074
1075 end
1076
1077
1078
1079 file = fs.open(path, "w")
1080
1081
1082
1083 for _, line in ipairs(lines) do
1084
1085
1086
1087 file.writeLine(line)
1088
1089
1090
1091 end
1092
1093
1094
1095 file.close()
1096
1097
1098
1099 else
1100
1101
1102
1103 -- Create the file and add a new line for the current TurtleID
1104
1105
1106
1107 file = fs.open(path, "w")
1108
1109
1110
1111 if file then
1112
1113
1114
1115 file.writeLine(itemName .. "|" .. Price .. "|" .. Count .. "|" .. TurtleID)
1116
1117
1118
1119 file.close()
1120
1121
1122
1123 else
1124
1125
1126
1127 print("Error: Unable to open file " .. path)
1128
1129
1130
1131 end
1132
1133
1134
1135 end
1136
1137
1138
1139end
1140
1141
1142
1143-- Function to update cost.txt with values associated with TurtleID
1144
1145
1146
1147local function updateCostFile(TurtleID)
1148
1149
1150
1151 local turtleCostsPath = "cost.txt"
1152
1153
1154
1155 local globalDatabasePath = "disk/GlobalDatabase.txt"
1156
1157
1158
1159 local file
1160
1161
1162
1163 local foundTurtleID = false
1164
1165
1166
1167 local count, price
1168
1169
1170
1171 -- Check if TurtleID is in globaldatabase.txt
1172
1173
1174
1175 if fs.exists(globalDatabasePath) then
1176
1177
1178
1179 file = fs.open(globalDatabasePath, "r")
1180
1181
1182
1183 local content = file.readAll()
1184
1185
1186
1187 file.close()
1188
1189
1190
1191 -- Check if TurtleID entry exists in globaldatabase.txt
1192
1193
1194
1195 if content:match(TurtleID) then
1196
1197
1198
1199 -- Extract values for the TurtleID from globaldatabase.txt
1200
1201
1202
1203 local itemName, priceExisting, countExisting = content:match("([^|]+)|([^|]+)|([^|]+)|" .. TurtleID)
1204
1205
1206
1207 if itemName and priceExisting and countExisting then
1208
1209
1210
1211 foundTurtleID = true
1212
1213
1214
1215 price = tonumber(priceExisting)
1216
1217
1218
1219 count = tonumber(countExisting)
1220
1221
1222
1223 end
1224
1225
1226
1227 end
1228
1229
1230
1231 else
1232
1233
1234
1235 print("Error: Unable to open file " .. globalDatabasePath)
1236
1237
1238
1239 return
1240
1241
1242
1243 end
1244
1245
1246
1247 -- Set values in cost.txt based on globaldatabase.txt
1248
1249
1250
1251 if foundTurtleID then
1252
1253
1254
1255 file = fs.open(turtleCostsPath, "w")
1256
1257
1258
1259 file.writeLine("Count = " .. count)
1260
1261
1262
1263 file.writeLine("Price = " .. price)
1264
1265
1266
1267 file.close()
1268
1269
1270
1271 --#IGNORE print("Updated cost.txt with values from GlobalDatabase.txt for TurtleID " .. TurtleID)
1272
1273
1274
1275 else
1276
1277
1278
1279 -- Add a new entry for the TurtleID with values from cost.txt to globaldatabase.txt
1280
1281
1282
1283 if fs.exists(turtleCostsPath) then
1284
1285
1286
1287 file = fs.open(turtleCostsPath, "r")
1288
1289
1290
1291 local countLine = file.readLine()
1292
1293
1294
1295 local priceLine = file.readLine()
1296
1297
1298
1299 file.close()
1300
1301
1302
1303 local itemName = DisplayName -- Use the actual item display name
1304
1305
1306
1307 if itemName then
1308
1309
1310
1311 print("Item Name Found")
1312
1313
1314
1315 else
1316
1317
1318
1319 print("Error: itemName is nil. Unable to update globalDatabase.txt.")
1320
1321
1322
1323 -- Add any additional handling or error reporting as needed
1324
1325
1326
1327 end
1328
1329
1330
1331 file = fs.open(globalDatabasePath, "a")
1332
1333
1334
1335 local formattedItemName = getItemDetails() or ItemName--:gsub("minecraft:", "")
1336
1337
1338
1339 file.writeLine(formattedItemName .. "|" .. priceLine:gsub("Price = ", "") .. "|" .. countLine:gsub("Count = ", "") .. "|" .. TurtleID)
1340
1341
1342
1343 --file.writeLine(itemName .. "|" .. priceLine:gsub("Price = ", "") .. "|" .. countLine:gsub("Count = ", "") .. "|" .. TurtleID)
1344
1345
1346
1347 file.close()
1348
1349
1350
1351 print("Added new entry for TurtleID " .. TurtleID .. " in GlobalDatabase.txt with values from cost.txt")
1352
1353
1354
1355 else
1356
1357
1358
1359 print("Error: Unable to open file " .. turtleCostsPath)
1360
1361
1362
1363 end
1364
1365
1366
1367 end
1368
1369
1370
1371end
1372
1373
1374
1375
1376
1377
1378
1379---IGNORE
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399local TurtleDataModule = {}
1400
1401
1402
1403
1404
1405
1406
1407-- Function to center text vertically and horizontally
1408
1409
1410
1411function TurtleDataModule.printCentered(monitor, text, yPos, color)
1412
1413
1414
1415 local width, _ = monitor.getSize()
1416
1417
1418
1419 local xPos = math.floor((width - #text) / 2) + 1 -- Center horizontally
1420
1421
1422
1423
1424
1425
1426
1427 monitor.setCursorPos(xPos, yPos)
1428
1429
1430
1431 monitor.setTextColor(color)
1432
1433
1434
1435 monitor.write(text)
1436
1437
1438
1439end
1440
1441
1442
1443
1444
1445
1446
1447-- Function to read the database file and find the corresponding turtle data
1448
1449
1450
1451function TurtleDataModule.findTurtleData(turtleID)
1452
1453
1454
1455 local file = fs.open("disk/GlobalDatabase.txt", "r")
1456
1457
1458
1459 if not file then
1460
1461
1462
1463 return nil -- File not found
1464
1465
1466
1467 end
1468
1469
1470
1471
1472
1473
1474
1475 local turtleData = nil
1476
1477
1478
1479 local line = file.readLine()
1480
1481
1482
1483 while line do
1484
1485
1486
1487 local displayName, price, count, id = line:match("([^|]+)|([^|]+)|([^|]+)|([^|]+)")
1488
1489
1490
1491 if id == turtleID then
1492
1493
1494
1495 turtleData = {displayName = displayName, price = tonumber(price), count = tonumber(count)}
1496
1497
1498
1499 break
1500
1501
1502
1503 end
1504
1505
1506
1507
1508
1509
1510
1511 line = file.readLine()
1512
1513
1514
1515 end
1516
1517
1518
1519
1520
1521
1522
1523 file.close()
1524
1525
1526
1527 return turtleData
1528
1529
1530
1531end
1532
1533
1534
1535
1536
1537
1538
1539-- Function to display turtle data
1540
1541
1542
1543function TurtleDataModule.displayTurtleData(monitorPeripheral, turtleID)
1544
1545
1546
1547 local monitor = peripheral.wrap(monitorPeripheral)
1548
1549
1550
1551 monitor.clear()
1552
1553
1554
1555
1556
1557
1558
1559 local turtleData = TurtleDataModule.findTurtleData(turtleID)
1560
1561
1562
1563
1564
1565
1566
1567 if turtleData then
1568
1569
1570
1571 local displayName = turtleData.displayName
1572
1573
1574
1575 print("Display Name:", displayName) -- Print the display name for debugging
1576
1577
1578
1579
1580
1581
1582
1583 local firstWord, secondWord
1584
1585
1586
1587 -- Extracting first and second words from the display name
1588
1589
1590
1591 local itemName = displayName:match(":[^:]+$") -- Extracts the part after the last colon
1592
1593
1594
1595 if itemName then
1596
1597
1598
1599 itemName = itemName:gsub("^:", "") -- Remove leading colon
1600
1601
1602
1603 local words = {}
1604
1605
1606
1607 for word in itemName:gmatch("[^_]+") do -- Split the itemName by underscores
1608
1609
1610
1611 table.insert(words, word)
1612
1613
1614
1615 end
1616
1617
1618
1619 firstWord = words[1] or "N/A" -- First word
1620
1621
1622
1623 secondWord = words[2] or "" -- Second word
1624
1625
1626
1627 -- Capitalize the first letter of each word
1628
1629
1630
1631 firstWord = firstWord:gsub("(%a)([%w_']*)", function(first, rest) return first:upper() .. rest:lower() end)
1632
1633
1634
1635 secondWord = secondWord:gsub("(%a)([%w_']*)", function(first, rest) return first:upper() .. rest:lower() end)
1636
1637
1638
1639 else
1640
1641
1642
1643 firstWord, secondWord = "N/A", "N/A"
1644
1645
1646
1647 end
1648
1649
1650
1651
1652
1653
1654
1655 local lines = {
1656
1657
1658
1659 {"========:", colors.yellow},
1660
1661
1662
1663 {firstWord or "N/A", colors.orange}, -- Display the first word
1664
1665
1666
1667 {secondWord or "N/A", colors.orange}, -- Display the second word
1668
1669
1670
1671 {"========:", colors.yellow},
1672
1673
1674
1675 {"", colors.black},
1676
1677
1678
1679 {" Price:", colors.white},
1680
1681
1682
1683 {tostring(turtleData.price or "N/A"), colors.orange},
1684
1685
1686
1687 {" Count:", colors.white},
1688
1689
1690
1691 {tostring(turtleData.count or "N/A"), colors.orange},
1692
1693
1694
1695 {"", colors.black},
1696
1697
1698
1699 {"<<====", colors.blue},
1700
1701
1702
1703 {"", colors.black},
1704
1705
1706
1707 }
1708
1709
1710
1711
1712
1713
1714
1715 local yPos = 1 -- Initial vertical position
1716
1717
1718
1719 for _, line in ipairs(lines) do
1720
1721
1722
1723 TurtleDataModule.printCentered(monitor, line[1], yPos, line[2]) -- Print each line centered vertically
1724
1725
1726
1727 yPos = yPos + 1 -- Move to the next line
1728
1729
1730
1731 end
1732
1733
1734
1735 else
1736
1737
1738
1739 TurtleDataModule.printCentered(monitor, "Turtle data not found!", 1, colors.red)
1740
1741
1742
1743 end
1744
1745
1746
1747end
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783---IGNORE
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807-- Main loop to constantly check for player clicks
1808
1809
1810
1811local prevItemName = nil -- Variable to store the previous item name
1812
1813
1814
1815while true do
1816
1817
1818
1819 -- Check for inactivity and restart if needed
1820
1821
1822
1823 event, username, device = os.pullEvent("playerClick")
1824
1825
1826
1827
1828
1829
1830
1831 -- Get item details when a player clicks
1832
1833
1834
1835 local DisplayName, ItemName = getItemDetails()
1836
1837
1838
1839 TurtleDataModule.displayTurtleData(monitorPeripheral, turtleID)
1840
1841
1842
1843 -- Check if item details are available
1844
1845
1846
1847 if DisplayName and ItemName then
1848
1849
1850
1851 print("DisplayName: " .. DisplayName)
1852
1853
1854
1855 print("ItemName: " .. ItemName)
1856
1857
1858
1859 local TurtleID = os.getComputerID()
1860
1861
1862
1863 -- Update cost.txt with values associated with the TurtleID
1864
1865
1866
1867 updateCostFile(TurtleID)
1868
1869
1870
1871 -- Check if the item in front has changed
1872
1873
1874
1875 if prevItemName ~= ItemName then
1876
1877
1878
1879 prevItemName = ItemName
1880
1881
1882
1883 -- Assuming 'cost.txt' contains individual count and price lines
1884
1885
1886
1887 local costPath = "cost.txt"
1888
1889
1890
1891 local costFile = fs.open(costPath, "r")
1892
1893
1894
1895 if costFile then
1896
1897
1898
1899 local countLine = costFile.readLine()
1900
1901
1902
1903 local priceLine = costFile.readLine()
1904
1905
1906
1907 if countLine and priceLine then
1908
1909
1910
1911 local count = tonumber(countLine:match("Count = (%d+)"))
1912
1913
1914
1915 local price = tonumber(priceLine:match("Price = (%d+)"))
1916
1917
1918
1919 if count and price then
1920
1921
1922
1923 local cost = price -- Cost per item, not total cost
1924
1925
1926
1927 checkBalance(username, cost)
1928
1929
1930
1931 -- Remove "minecraft:" from ItemName
1932
1933
1934
1935 local formattedItemName = ItemName--:gsub("minecraft:", "")
1936
1937
1938
1939 -- Update the item name in globalDatabase.txt
1940
1941
1942
1943 local globalDatabasePath = "disk/GlobalDatabase.txt"
1944
1945
1946
1947 local globalFile = fs.open(globalDatabasePath, "r")
1948
1949
1950
1951 local globalContent = globalFile.readAll()
1952
1953
1954
1955 globalFile.close()
1956
1957
1958
1959 local lines = {}
1960
1961
1962
1963 for line in globalContent:gmatch("[^\r\n]+") do
1964
1965
1966
1967 local itemNameExisting, priceExisting, countExisting, turtleIDExisting = line:match("([^|]+)|([^|]+)|([^|]+)|([^|]+)")
1968
1969
1970
1971 if itemNameExisting and priceExisting and countExisting and turtleIDExisting then
1972
1973
1974
1975 if tonumber(turtleIDExisting) == TurtleID then
1976
1977
1978
1979 -- Update the item name
1980
1981
1982
1983 line = formattedItemName .. "|" .. priceExisting .. "|" .. countExisting .. "|" .. turtleIDExisting
1984
1985
1986
1987 end
1988
1989
1990
1991 end
1992
1993
1994
1995 table.insert(lines, line)
1996
1997
1998
1999 end
2000
2001
2002
2003 globalFile = fs.open(globalDatabasePath, "w")
2004
2005
2006
2007 for _, line in ipairs(lines) do
2008
2009
2010
2011 globalFile.writeLine(line)
2012
2013
2014
2015 end
2016
2017
2018
2019 globalFile.close()
2020
2021
2022
2023 -- Add any additional code specific to your application
2024
2025
2026
2027 else
2028
2029
2030
2031 print("Error: Unable to read count and price from " .. costPath)
2032
2033
2034
2035 end
2036
2037
2038
2039 else
2040
2041
2042
2043 print("Error: Missing lines in " .. costPath)
2044
2045
2046
2047 end
2048
2049
2050
2051 costFile.close()
2052
2053
2054
2055 -- Reset prevItemName to nil after a successful transaction
2056
2057
2058
2059 prevItemName = nil
2060
2061
2062
2063 else
2064
2065
2066
2067 print("Error: Unable to open file " .. costPath)
2068
2069
2070
2071 end
2072
2073
2074
2075 else
2076
2077
2078
2079 print("Item in front has not changed. Skipping update.")
2080
2081
2082
2083 end
2084
2085
2086
2087 end
2088
2089
2090
2091end
2092
2093
2094
2095