-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrader.lua
1007 lines (821 loc) · 25.8 KB
/
trader.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
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
------------------------
-- Collectable Trader --
------------------------
--- by my_hat_stinks ---
------------------------
-- If you're looking in this file, you probably want to know
-- all the possible formats for collectable item descriptions.
--
--
-- Details
-- Collectable <append> - This is REQUIRED on the first line of every collectable.
-- `Collectible` is also acceptable.
-- If you use the <append> value, it will be added to the end
-- of the collectable's name in the trade menu. This allows you
-- two have two separate listings for one item.
--
-- Cost <quantity> <name> - The cost for the item. If <quantity> is excluded, it will default
-- to one of the listed item. <name> must match the required item
-- name exactly, including formatting.
-- Multiple Cost lines are allowed, all items will be required.
-- If <name> matches a set, all items in that set are added to cost.
--
-- CostType <type> - Cost type, defaults to "all" if excluded. Only one CostType can be listed.
-- All - All listed Cost items (including sets) are required to purchase
-- this item.
-- Any - Any listed Cost items (matching <quantity>) can be used to purchase
-- this item.
-- If a set is listed in Cost, <quantity> is the number of items from the
-- set required to purchase this item.
--
-- Requires <name> - Items that are required, but NOT consumed, when purchasing this listing.
-- Completely optional. <name> must match the required item name exactly,
-- including formatting.
-- `Require` is also acceptable.
--
-- Limit <quantity> - The user can't purchase another of this item if they already have <quantity>.
-- If the item is part of a set, the limit counts for the entire set.
-- Limits will not be reliable for non-Blackjack tables.
--
--
-- SpawnAs <name> - Optional. Renames this item when it is spawned. Can include additional variables
-- for dynamic names, case sensitive:
-- {name} - Changes to the Steam name of the user.
-- {id} - Changes to the Steam ID of the user.
-- {color} - Changes to the color of the user at time of purchase.
-- os.date() formatting is also available, eg "%Y-%b-%d" to mark the purchase date.
--
-- Category <Directory> - The menu this item should appear under. `>` indicates a sub-menu.
-- Items without a Category will be listed on the main menu.
-- Only one Category can be listed per item. Use Collectable <append>
-- for additional listings.
--
-- Set <set name> - Optional. Marks this item as part of a set. Items do not need to be purchasable
-- to be part of a set.
--
--
-- Complete Example:
--
-- Collectable
-- CostType: All
-- Cost: 20 Collectable token
-- Cost: Reward token
-- Requires: [DEB444]Prestige 8[-]
-- Require: Bankruptcy token
-- Category: Main Directory > Sub Directory > Bottom Directory
--
TradeItems = {}
DirectoryPath = {}
ListData = {}
ListReference = {}
ListPage = 0
ListTitle = ""
AdminMode = false
SpawnPos = { 1.168, 3.01, 0.02 }
COST_ALL = 0
COST_ANY = 1
function onLoad()
refreshAllItems()
mainMenu()
end
function doNull() end
function getObjectName( obj )
return obj.getVar("__trader_ObjectName") or obj.getName():match("^%s*(.-)%s*$") or obj.getName()
end
-- Buy Item --
--------------
function traderSpawnObject( item, c, spawnAs )
local ourColor = c and self.getName():lower():find(c:lower())
local params = {}
params.position = self.positionToWorld(SpawnPos)
local clone
if item.tag=="Infinite" then
clone = item.takeObject(params)
else
clone = item.clone(params)
end
clone.interactable = true
clone.setLock(false)
clone.setPosition(params.position)
clone.setDescription( ourColor and ("%s - %s"):format( Player[c].steam_id, Player[c].steam_name) or "" )
local shouldReset = (clone.tag=="Bag" and item.tag~="Infinite")
Wait.frames(function()
if (not clone) or clone==nil then return end
if shouldReset then
clone.reset()
end
local formattedObjectName = (clone.getName():match("^%s*(.-)%s*$") or clone.getName()):gsub("[\\\"]", "\\%1")
if spawnAs and ourColor and Player[c].seated then
local newStr = os.date(spawnAs, os.time()):gsub("{name}", Player[c].steam_name):gsub("{id}", Player[c].steam_id):gsub("{color}", c)
clone.setName( newStr )
end
clone.setLuaScript( clone.getLuaScript() .. ("\n\n__trader_ObjectName = \"%s\""):format(formattedObjectName) )
clone.reload()
end, 0)
end
function buyItem( data, c )
if CooldownTime and CooldownTime>os.time() then return end
if not data.item then
broadcastToColor( "Something went wrong! (Item no longer exists)", c, {1,0.2,0.2} )
return
end
-- Cooldown to give objects a chance to be destroyed
CooldownTime = os.time() + 0.25
Wait.frames(function() CooldownTime = nil end, 3) -- Clear cooldown after 3 frames, time value is a failsafe
local ourColor = c and self.getName():lower():find(c:lower())
if AdminMode then
-- Spawn, no additional checks
traderSpawnObject( data.item, c, data.spawnAs )
elseif ourColor then
if Global.getVar("forwardFunction") and Global.getVar("findObjectSetFromColor") then -- Blackjack tables
local set = Global.call( "forwardFunction", {function_name="findObjectSetFromColor", data={c}} )
if not set then
broadcastToColor( "Something went wrong! (Your zones do not exist)", c, {1,0.2,0.2} )
return
end
local zoneObjects = {set.zone.getObjects(), set.tbl.getObjects(), set.prestige.getObjects()}
-- Requirements
if data.req then
if not processRequirements(c, data, zoneObjects) then return end
end
-- Costs
if data.cost then
if data.costType==COST_ANY then
if not processCostAny(c, data, zoneObjects) then return end
else
if not processCostAll(c, data, zoneObjects) then return end
end
end
-- Spawn
traderSpawnObject( data.item, c, data.spawnAs )
else -- Other tables
local traderScale = self.getScale()
-- Create search zone
local zone = spawnObject({
type = "ScriptingTrigger",
position = self.positionToWorld({1.2, 1.6, 0}), rotation = self.getRotation(), scale = {2.7*traderScale[1],3,3*traderScale[3]},
sound = false,
})
-- Add self-destruct script
zone.setLuaScript([[
function onLoad()
Wait.frames(function()
destroyObject(self)
end, 2)
end
]])
-- Wait for object initialisation
Wait.frames(function()
local zoneObjects = {zone.getObjects()}
-- Requirements
if data.req then
if not processRequirements(c, data, zoneObjects) then return end
end
-- Cost
if data.cost then
if data.costType==COST_ANY then
if not processCostAny(c, data, zoneObjects) then return end
else
if not processCostAll(c, data, zoneObjects) then return end
end
end
-- Spawn
traderSpawnObject( data.item, c, data.spawnAs )
end, 1)
end
else
broadcastToColor( "You can't spawn items from other people's Traders. Toggle Admin Mode first.", c, {1,0.2,0.2} )
return
end
-- traderSpawnObject( data.item, c, data.spawnAs )
end
-- Process Requirements --
--------------------------
function processRequirements( c, data, zoneObjects )
if not Player[c].seated then return false end -- Player gone
local req = {}
for i=1,#data.req do -- Copy table
req[data.req[i]] = true
end
for _,zone in pairs(zoneObjects) do
for _, obj in ipairs(zone) do
req[ getObjectName(obj) ] = nil -- Remove entry from table
end
end
for missing in pairs(req) do -- If at least one requirement isn't met
broadcastToColor( ("You need a %s on your table to buy this item."):format(tostring(missing)), c, {1,0.2,0.2} )
return false
end
return true
end
-- Process Costs --
-------------------
function processCostAll( c, data, zoneObjects )
if not Player[c].seated then return false end -- Player gone
local costData = data.cost
local missingCost = TranslateSetsToItems( CopyTable(costData) )
local foundStacks = {}
local sort = function(a,b)
return self.positionToLocal(a.getPosition()).z > self.positionToLocal(b.getPosition()).z
end
for i=1,#zoneObjects do
table.sort(zoneObjects[i], sort)
end
for _,zone in pairs(zoneObjects) do
-- for j, item in ipairs(zone) do
for i=1,#zone do
local item = zone[i]
-- local name = item.getName():match("^%s*(.-)%s*$") or item.getName()
local name = getObjectName(item)
if item and not (item==nil) and missingCost[name] and missingCost[name]>0 and item.interactable and not (item.getLock()) then
local count = item.getQuantity()
if count==-1 then count = 1 end
if item.tag=="Bag" then
count = 1
end
if count>missingCost[name] then
table.insert(foundStacks, {item, missingCost[name]})
missingCost[name] = nil
elseif count==missingCost[name] then
table.insert(foundStacks, {item})
missingCost[name] = nil
else
table.insert(foundStacks, {item})
missingCost[name] = (missingCost[name] or 0) - count
end
end
end
end
for missing,cost in pairs(missingCost) do -- Should only run if there's values left
broadcastToColor( ("You need %i more %s on your table to buy this item."):format(tonumber(cost) or 0, tostring(missing)), c, {1,0.2,0.2} )
return false
end
if exeedsLimits( data, zoneObjects, foundStacks ) then
broadcastToColor( "You have already reached your limit for this item.", c, {1,0.2,0.2} )
return false
end
local pos = self.getPosition()
pos.y = pos.y + 5
for i=1,#foundStacks do
local tbl = foundStacks[i]
if tbl[2] then
for i=1,tbl[2] do
local taken = tbl[1].takeObject( {position=pos, rotation={0,0,0}} )
destroyObject(taken)
end
else
destroyObject(tbl[1])
end
end
return true
end
function processCostAny( c, data, zoneObjects )
if not Player[c].seated then return false end -- Player gone
local tblCost = data.cost
local missingFromSets = {}
local foundInSets = {}
for i=1,#tblCost do
local setName = tblCost[i][1]
local setCount = tblCost[i][2] or 1
if TradeSets[setName] then
missingFromSets[i] = {
Required = setCount,
Objects = {}
}
local tbl = missingFromSets[i].Objects
for objName,objCount in pairs(TradeSets[setName]) do
tbl[objName] = true
end
else
missingFromSets[i] = {
Required = setCount,
Objects = {
[setName] = true,
}
}
end
end
local foundStacks
local sort = function(a,b)
return self.positionToLocal(a.getPosition()).z > self.positionToLocal(b.getPosition()).z
end
for i=1,#zoneObjects do
table.sort(zoneObjects[i], sort)
end
local done = false
for _,zone in pairs(zoneObjects) do
-- for j, item in ipairs(zone) do
for i=1,#zone do
local item = zone[i]
-- local name = item.getName():match("^%s*(.-)%s*$") or item.getName()
local name = getObjectName( item )
for i=1,#missingFromSets do
local missingCost = missingFromSets[i]
foundInSets[i] = foundInSets[i] or {}
if missingCost.Objects[name] and missingCost.Required>0 and item.interactable and not (item.getLock()) then
local count = item.getQuantity()
if count==-1 then count = 1 end
if count>missingCost.Required then
table.insert(foundInSets[i], {item, missingCost.Required})
missingCost.Required = nil
elseif count==missingCost.Required then
table.insert(foundInSets[i], {item})
missingCost.Required = nil
else
table.insert(foundInSets[i], {item})
missingCost.Required = (missingCost.Required or 0) - count
end
if (not missingCost.Required) or missingCost.Required<=0 then
done = true
foundStacks = foundInSets[i] or {}
break
end
end
end
if done then break end
end
if done then break end
end
if not foundStacks then
broadcastToColor( "You don't have the necessary items on your table to buy this item.", c, {1,0.2,0.2} )
return false
end
if exeedsLimits( data, zoneObjects, foundStacks ) then
broadcastToColor( "You have already reached your limit for this item.", c, {1,0.2,0.2} )
return false
end
local pos = self.getPosition()
pos.y = pos.y + 5
for i=1,#foundStacks do
local tbl = foundStacks[i]
if tbl[2] then
for i=1,tbl[2] do
local taken = tbl[1].takeObject( {position=pos, rotation={0,0,0}} )
destroyObject(taken)
end
else
destroyObject(tbl[1])
end
end
return true
end
-- Process Limits --
--------------------
function exeedsLimits( data, zones, skipObjects )
if not data.limit then return false end
local restrictedObjects = {}
if data.item.tag=="Infinite" then
local clone = data.item.takeObject(params)
if clone then
restrictedObjects[clone.getName() or "[ITEM]"] = true
end
destroyObject(clone)
else
restrictedObjects[data.item.getName() or "[ITEM]"] = true
end
local limitLeft = data.limit
if data.sets then
for i=1,#data.sets do
for name in pairs(TradeSets[data.sets[i]] or {}) do
restrictedObjects[name] = true
end
end
end
for _,zone in pairs(zones) do
for j, item in ipairs(zone) do
local name = item.getName()
if restrictedObjects[name] then
local count = item.getQuantity()
if count==-1 then count = 1 end
if item.tag=="Bag" then
count = 1
end
for i=1,#skipObjects do
local tbl = skipObjects[i]
if tbl[1]==item then
if tbl[2] then
count = count - tbl[2]
else
count = count - 1
end
end
end
if count>0 then
limitLeft = limitLeft - count
if limitLeft<=0 then return true end
end
end
end
end
if limitLeft<= 0 then return true end
return false
end
-- Register Items --
--------------------
function clickAdminMode(o,c)
if not Player[c].admin then
printToColor( "You can't do this.", c, {1,0,0} )
return
end
AdminMode = not AdminMode
doMenu( ListPage )
end
function clickRefresh(o,c)
if not Player[c].admin then
printToColor( "You can't do this.", c, {1,0,0} )
return
end
refreshAllItems()
end
function refreshAllItems()
TradeItems = {}
TradeSets = {}
DirectoryPath = {}
ListData = {}
ListReference = {}
ListPage = 0
for _,obj in pairs(getAllObjects()) do
if obj.getDescription():match("^[Cc]ollect[ai]ble") then
registerItem( obj, obj.getDescription():match("^[Cc]ollect[ai]ble *([^\n]*)") )
end
end
mainMenu()
end
local TextToCostType = {
any = COST_ANY,
all = COST_ALL,
}
function registerItem( obj, appendName )
local desc = obj.getDescription()
local name = obj.getName() or "[ITEM]"
if obj.tag=="Infinite" then
local clone = obj.takeObject(params)
if clone then
name = clone.getName() or name
end
destroyObject(clone)
end
if appendName then
name = name .. " " .. tostring(appendName)
end
local CostType = COST_ALL
local hasCost = false
for foundType in desc:gmatch("[Cc]ost[Tt]ype:? *([^\n]+)") do
foundType = foundType:match("^%s*(.-)%s*$") or foundType
CostType = TextToCostType[ foundType:lower() ] or CostType
end
local cost = {}
local hasCost = false
for num,item in desc:gmatch("[Cc]ost:? +(%d*)x? *([^\n]+)") do
num = tonumber(num) or 1
if item then
hasCost = true
item = item:match("^%s*(.-)%s*$") or item
if CostType==COST_ALL then
cost[item] = (cost[item] or 0) + num
elseif CostType==COST_ANY then
table.insert(cost, {item,num})
end
end
end
if not hasCost then -- No cost, can't buy
registerSet( obj )
return
end
local req = {}
for foundReq in desc:gmatch("[Rr]equires?:? *([^\n]+)") do
foundReq = foundReq:match("^%s*(.-)%s*$") or foundReq
table.insert(req, foundReq)
end
if #req==0 then
req = nil
end
local spawnAs = ""
for foundName in desc:gmatch("[Ss]pawn[Aa]s?:? *([^\n]+)") do
spawnAs = foundName:match("^%s*(.-)%s*$") or foundName
break
end
if #spawnAs==0 then
spawnAs = nil
end
local limit = nil
for foundLimit in desc:gmatch("[Ll]imit:? *(%d+)") do
limit = tonumber(foundLimit) or 0
break
end
local workingDir = TradeItems
local cat = desc:match("[Cc]ategory:? *([^\n]+)")
if cat then
local exploded = {}
local str = cat
local s,e,before,after = str:find("^([^>]*)>(.*)$")
while s and e do
before = before:match( "^%s*(.+)$" ) or before
-- before = before:match( "^(.-)%s*$" ) or before -- "Too complex", apparently.
local posTrailingSpaces = before:find("%s*$")
if posTrailingSpaces then
before = before:sub(1,posTrailingSpaces-1)
end
if #before>0 then table.insert(exploded, before) end
str = after
s,e,before,after = str:find("^([^>]*)>(.*)$")
end
str = str:match( "^%s*(.+)$" ) or str
local posTrailingSpaces = str:find("%s*$")
if posTrailingSpaces then
str = str:sub(1,posTrailingSpaces-1)
end
if #str>0 then table.insert(exploded, str) end
for i=1,#exploded do
if not exploded[i] then break end
if (not workingDir[exploded[i]]) or (workingDir[exploded[i]].IsTraderItem) then -- Empty or an item, replace with category
workingDir[exploded[i]] = {}
end
workingDir = workingDir[exploded[i]]
end
end
workingDir[ name ] = {
IsTraderItem = true,
item = obj,
costType = CostType,
cost = cost,
req = req,
spawnAs = spawnAs,
limit = limit,
sets = registerSet( obj ),
}
end
function registerSet( obj )
local desc = obj.getDescription()
local name = obj.getName() or "[ITEM]"
if obj.tag=="Infinite" then
local clone = obj.takeObject(params)
if clone then
name = clone.getName() or name
end
destroyObject(clone)
end
local sets = {}
local hasSets = false
for num,setName in desc:gmatch("Set:? *(%d*)x? *([^\n]+)") do
num = tonumber(num) or 1
if setName then
sets[setName] = (sets[setName] or 0) + num
hasSets = true
end
end
if not hasSets then return end -- Not part of a set
local inSets = {}
for setName,num in pairs(sets) do
TradeSets[setName] = TradeSets[setName] or {}
TradeSets[setName][name] = num
table.insert(inSets, setName)
end
return inSets
end
-- Cost To String --
--------------------
function getCostString( costTable, costType )
if not costTable then return "" end
local str = ""
local orderedCost = {}
if costType==COST_ANY then
orderedCost = CopyTable(costTable)
else
for item,num in pairs(costTable) do
table.insert( orderedCost, {item,num} )
end
end
table.sort( orderedCost, function(a,b)
if a[2]==b[2] then
return a[1]<b[1]
end
return a[2]<b[2]
end)
for i=1,#orderedCost do
str = str .. ("%s%i %s[b]%s[/b]"):format(
(i==1 and "") or (i==#orderedCost and (costType==COST_ANY and ", or " or ", and ")) or ", ",
orderedCost[i][2],
costType==COST_ANY and TradeSets[ orderedCost[i][1] ] and "from " or "",
orderedCost[i][1]
)
end
return str
end
-- Menu --
----------
function doListData()
for k in pairs(ListReference) do
table.insert(ListData, k)
end
table.sort(ListData, function(a,b)
if ListReference[a].IsTraderItem and not ListReference[b].IsTraderItem then return false end
if ListReference[b].IsTraderItem and not ListReference[a].IsTraderItem then return true end
return a<b
end)
end
function clickMainMenu(o,c)
local ourColor = c and self.getName():lower():find(c:lower())
if not (c and (Player[c].admin or ourColor)) then
broadcastToColor( "This does not belong to you.", c, {1,0.2,0.2} )
return
end
mainMenu()
end
function mainMenu()
ListTitle = "Collectable Trader"
ListReference = TradeItems
ListData = {}
DirectoryPath = {}
doListData()
doMenu( 1 )
end
function doMenu(page)
self.clearButtons()
self.clearInputs()
ListPage = page or 1
-- Title
self.createButton({
label=ListTitle, click_function="doNull", function_owner=self, scale = {0.5,0.5,0.5},
position={1.2, 0.25, -1.23}, rotation={0,0,0}, width=0, height=0, font_size=170,
font_color = {r=1,g=1,b=1},
})
Targets = {}
local displayFrom = (ListPage-1)*10
-- List Page
for i=1,10 do
local data = ListData[displayFrom + i]
if not (data and ListReference[data]) then break end
local zpos = -1.17 + (i * 0.21)
local col = AdminMode and {r=1,g=0.1,b=0.1} or {r=1,b=1,g=1}
-- Button
if ListReference[data].IsTraderItem then
self.createButton({
label= ("[b][u]%s[/u][/b]\n%s"):format( data, getCostString(ListReference[data].cost, ListReference[data].costType) ), click_function="doAction"..i, function_owner=self, scale = {0.5,0.5,0.5},
position={1.2, 0.25, zpos}, rotation={0,0,0}, width=1800, height=220, font_size=80,
color = col
})
else
self.createButton({
label= ("[b]%s[b] >"):format( data ), click_function="doAction"..i, function_owner=self, scale = {0.5,0.5,0.5},
position={1.2, 0.25, zpos}, rotation={0,0,0}, width=1800, height=220, font_size=80,
color = col
})
end
end
-- Page Navigaton
local pageStr = ("Page %i of %i"):format( ListPage, math.ceil(#ListData/10) )
self.createButton({
label=pageStr, click_function="doNull", function_owner=self, scale = {0.5,0.5,0.5},
position={1.2, 0.25, 1.12}, rotation={0,0,0}, width=0, height=0, font_size=100,
font_color = {r=1,g=1,b=1},
})
self.createButton({
label="<", click_function="PrevPage", function_owner=self, scale = {1,1,1}, scale = {0.5,0.5,0.5},
position={0.7, 0.25, 1.12}, rotation={0,0,0}, width=100, height=100, font_size=80,
color = ListPage==1 and {0.5,0.5,0.5} or {1,1,1}
})
self.createButton({
label=">", click_function="NextPage", function_owner=self, scale = {1,1,1}, scale = {0.5,0.5,0.5},
position={1.7, 0.25, 1.12}, rotation={0,0,0}, width=100, height=100, font_size=80,
color = ListPage>=math.ceil(#ListData/10) and {0.5,0.5,0.5} or {1,1,1}
})
if #DirectoryPath>0 then
-- Return
self.createButton({
label="Back", click_function="clickBack", function_owner=self, scale = {1,1,1}, scale = {0.5,0.5,0.5},
position={1.3, 0.25, 1.32}, rotation={0,0,0}, width=450, height=150, font_size=80,
})
self.createButton({
label="Main Menu", click_function="clickMainMenu", function_owner=self, scale = {1,1,1}, scale = {0.5,0.5,0.5},
position={1.8, 0.25, 1.32}, rotation={0,0,0}, width=450, height=150, font_size=80,
})
end
-- Admin
self.createButton({
label="Admin Mode", click_function="clickAdminMode", function_owner=self, scale = {1,1,1}, scale = {0.3,0.3,0.3},
position={0.2, 0.25, -1.33}, rotation={0,0,0}, width=450, height=50, font_size=70,
color = AdminMode and {r=1,g=0,b=0} or {r=0.25,g=0.25,b=0.25},
})
self.createButton({
label="Reload", click_function="clickRefresh", function_owner=self, scale = {1,1,1}, scale = {0.3,0.3,0.3},
position={0.2, 0.25, -1.23}, rotation={0,0,0}, width=450, height=50, font_size=70,
color = {r=0.25,g=0.25,b=0.25},
})
end
-- Actions --
-------------
function doAction( index, c )
local ourColor = c and self.getName():lower():find(c:lower())
if not (c and (Player[c].admin or ourColor)) then
broadcastToColor( "This does not belong to you.", c, {1,0.2,0.2} )
return
end
local trueIndex = index + (ListPage-1)*10
local data = ListData[trueIndex]
local ref = data and ListReference[data]
if not ref then
broadcastToColor( "Something went wrong! (Button reference is missing)", c, {1,0.2,0.2} )
mainMenu()
return
end
if ref.IsTraderItem then
buyItem( ref, c )
return
end
table.insert(DirectoryPath, data)
ListReference = ref
ListPage = 0
ListTitle = data
ListData = {}
doListData()
doMenu(1)
end
for i=1,10 do
_G["doAction"..i] = function(o,c) return doAction(i,c) end
end
function clickBack(o,c)
local ourColor = c and self.getName():lower():find(c:lower())
if not (c and (Player[c].admin or ourColor)) then
broadcastToColor( "This does not belong to you.", c, {1,0.2,0.2} )
return
end
table.remove(DirectoryPath)
if #DirectoryPath==0 then
mainMenu()
return
end
local workingDir = TradeItems
for i=1,#DirectoryPath do
local newDir = workingDir[DirectoryPath[i]]
if newDir and not newDir.IsTraderItem then
workingDir = newDir
else
while #DirectoryPath>=i do
table.remove(DirectoryPath)
end
break
end
end
if #DirectoryPath==0 then
mainMenu()
return
end
ListReference = workingDir
ListPage = 0
ListTitle = DirectoryPath[#DirectoryPath]
ListData = {}
doListData()
doMenu(1)
end
function NextPage(o,c)
local ourColor = c and self.getName():lower():find(c:lower())
if not (c and (Player[c].admin or ourColor)) then
broadcastToColor( "This does not belong to you.", c, {1,0.2,0.2} )
return
end
local maxPage = math.max( math.ceil(#ListData/10), 1 )
doMenu( math.min(ListPage+1, maxPage) )
end
function PrevPage(o,c)
local ourColor = c and self.getName():lower():find(c:lower())
if not (c and (Player[c].admin or ourColor)) then
broadcastToColor( "This does not belong to you.", c, {1,0.2,0.2} )
return
end
doMenu( math.max(ListPage-1, 1) )
end
-- Util --
----------
function CopyTable(from)
local to = {}
for k,v in pairs(from) do
to[k]=v
end
return to
end
function TranslateSetsToItems( tbl )
for setName,setCount in pairs(tbl) do
if TradeSets[setName] then
for objName,objCount in pairs(TradeSets[setName]) do
tbl[objName] = (tbl[objName] or 0) + ((objCount or 1) * (setCount or 1))
end
tbl[setName] = nil
end
end
return tbl
end
function PrintTable( tbl, indent, skipTables )
indent = indent or 0
skipTables = skipTables or {[tbl]=true}
for k,v in pairs(tbl) do
if type(v)=="table" and not skipTables[v] then
skipTables[v]=true
print( (" "):rep(indent+1), "\"",k,"\" = {" )