-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathKT-Command-Node.lua
More file actions
1321 lines (1186 loc) · 32.9 KB
/
Copy pathKT-Command-Node.lua
File metadata and controls
1321 lines (1186 loc) · 32.9 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
-- icons by game-icons.net
triangle = "(1\")"
circle = "(2\")"
square = "(3\")"
pentagon = "(6\")"
pickModelForId = nil
panelToggles = {}
teamApiUrl = [[https://datateamapp.azurewebsites.net/api/toTTS/]]
teamApiCode = ""
versionUrl = [[https://datateamapp.azurewebsites.net/api/Scripts/Version]]
scriptUrl = [[https://datateamapp.azurewebsites.net/api/Scripts/]]
--anything from special profiles with these typenames will be added to the names of operatives
specialNames = {
"Boon of Tzeentch"
}
specialSelections = {
"Tzeentch",
"Khorne",
"Nurgle",
"Slaanesh",
"Undivided"
}
state = {
teamkey = nil,
name = "New Team",
playerid = nil,
models = nil,
modelOrder = nil,
positions = {},
catalogues = {},
step = nil,
version={
model = 1,
node = 1
},
modelscript = [[]]
}
baseDimensions = {
{x = 25, z = 25},
{x = 28.5, z = 28.5},
{x = 32, z = 32},
{x = 40, z = 40},
{x = 50, z = 50},
{x = 55, z = 55},
{x = 60, z = 60},
{x = 100, z = 100},
{x = 25, z = 75},
{x = 75, z = 25},
{x = 120, z = 92},
{x = 92, z = 120},
{x = 170, z = 105},
{x = 105, z = 170}
}
function panelToggleCallback(player, value, id)
-- print(id.." toggle buton pressed")
local pid = panelToggles[id]
if pid then
-- print("toggling "..pid[1])
if pid[2] then
pid[2] = false
-- print("OFF")
self.UI.hide(pid[1])
else
pid[2] = true
-- print("ON")
self.UI.show(pid[1])
end
end
end
function panelToggle(btnid, panelid, df)
panelToggles[btnid] = {panelid, df}
-- print(string.format("toggle button: %s => %s", btnid, panelid))
return "panelToggleCallback"
end
function makeGuiid( tbl )
return "ktcnid-"..table.concat(tbl,"-")
end
function fieldGuiid(t, name)
return makeGuiid({name, "field"})
end
function readGuiid( guiid )
return splitString(guiid, '%-')
end
function textColorXml( color, text )
return string.format("<textcolor color=\"#%s\">%s</textcolor>", color, text)
end
function textColorMd( color, text )
return string.format("[%s]%s[-]", color, text)
end
function textAttr( text, attr )
return {
tag="Text",
attributes=attr,
value=text
}
end
function xt(tag, attributes, children, value)
return {
tag=tag,
attributes=attributes,
children=children,
value=value
}
end
function rcall(target, fname, args)
if target.getVar(fname) then
target.call(fname, args)
end
end
text_subs = {
["1&&"] = textColorXml("000000", triangle),
["2&&"] = textColorXml("ffffff", circle),
["3&&"] = textColorXml("1E87FF", square),
["6&&"] = textColorXml("DA1A18", pentagon),
["%(R%)"] = textColorXml("1E87FF", "R"),
["%(M%)"] = textColorXml("F4641D", "M")
}
md_subs = {
["1&&"] = textColorMd("000000", triangle),
["2&&"] = textColorMd("ffffff", circle),
["3&&"] = textColorMd("1E87FF", square),
["6&&"] = textColorMd("DA1A18", pentagon),
["%(R%)"] = textColorMd("1E87FF", "R"),
["%(M%)"] = textColorMd("F4641D", "M")
}
function subsymbol(s, tbl)
local st = s
for o, sub in pairs(tbl) do
st = string.gsub(st, o, sub)
end
return st
end
function startsWith(st, match)
return string.sub(st, 1, string.len(match)) == match
end
function checkOwner(p)
if p.steam_id == state.playerid then
return true
else
p.broadcast("Only the command node's owner can do that")
return false
end
end
function checkHost(p)
if p.host then
return true
else
p.broadcast("Only the host can do that")
end
end
updateButtonId = makeGuiid({"update","script","button"})
remoteVersion = {
model=0,
node=0
}
function findBase(obj)
local base = obj.getTable("modelBase")
if base == nil then
local bounds = obj.getBoundsNormalized()
local baseX = 0
local baseZ = 0
if bounds.size.x == 0 then
bounds = obj.getBounds()
end
if bounds.size.x > 0 then
local boundsX = bounds.size.x * 25.4
local boundsZ = bounds.size.z * 25.4
local baseError = 999999
for i, dim in pairs(baseDimensions) do
local difx = (dim.x - boundsX)
local difz = (dim.z - boundsZ)
local dimError = difx*difx + difz*difz
if dimError < baseError then
baseError = dimError
baseX = dim.x
baseZ = dim.z
end
end
else
printToOwner("Could not detect base size for this model. You will need to set it manually.")
baseX = 32
baseZ = 32
end
base = {x = baseX, z = baseZ}
end
return base
end
function needsUpdate()
return remoteVersion.model > state.version.model or remoteVersion.node > state.version.node
end
function getVersions()
WebRequest.get(versionUrl, function(req)
if req.is_error then
log(req.error)
else
remoteVersion = JSON.decode(req.text)
if needsUpdate() then
self.UI.show(updateButtonId)
broadcastToOwner("A new version of the Command Node is available!\nRight click the node and select [b]Update Scripts[/b] to update")
self.addContextMenuItem("Update Scripts", tryUpdate)
end
end
end)
end
function broadcastToOwner(s)
if state.playerid == nil then
broadcastToAll(s)
return
end
for _,player in pairs(Player.getPlayers()) do
if player.steam_id == state.playerid then
player.broadcast(s)
return
end
end
end
function onPlayerChangeColor(pc)
if pc ~= "Grey" and state.playerid and Player[pc].steam_id == state.playerid then -- error when player leaves server
self.setColorTint(Color.fromString(pc))
end
end
function callback_claimNode(player, value, id)
state.playerid = player.steam_id
self.setColorTint(Color.fromString(player.color))
player.broadcast(string.format("Welcome, %s.\nSelect your roster to get started.", player.steam_name))
saveState()
generateGui()
end
function generateUIDefaults()
return xt("Defaults",{},{
xt("Text",{
class="mainTitle",
fontSize="30",
fontStyle="BoldAndItalic"
}),
xt("Text",{
class="inputTitle",
fontSize="20",
fontStyle="Bold"
}),
xt("Panel",{
class="helpPanel",
color="#8B8B8B",
showAnimation="FadeIn",
hideAnimation="FadeOut",
active=false
}),
xt("Button",{
class="helpButton",
width=30, height=30,
resizeTextForBestFit=true,
text="?"
})
})
end
function generateClaimUI(active, id)
return xt("Panel", {
active=active,
id=id,
width=150,
height=60,
position="0 120 -10",
rotation="0 0 180"
},
{
xt("Button", {
resizeTextForBestFit=true,
onClick="callback_claimNode",
text="New team"
})
})
end
function callback_teamCode( player, value, id )
teamApiCode = value
end
loadTeamButtonId = makeGuiid({"team", "load", "button"})
function loadTeamRequest(request)
if request.is_error then
log(request.error)
broadcastToAll("Failed to load team - check the system log")
self.UI.setXmlTable({generateUIDefaults(), generateTeamSelectUI(true, false)})
else
local status, dc = pcall(JSON.decode, request.text)
if status then
state.name = dc["roster"]["@name"]
self.setName(state.name)
local force = dc["roster"]["forces"]["force"]
local models = {}
local unpackModels = function(l)
for _, v in pairs(l) do
local vt = v.categories.category["@name"]
if vt == nil or (vt ~= "Configuration" and vt ~= "Reference") then
models[v["@id"]] = v
end
end
end
if force["@id"] then
--roster mode
unpackModels(force.selections.selection)
else
--fire team mode
for _, v in pairs(force) do
unpackModels(v.selections.selection)
end
end
state.models = models
saveState()
-- log(generateModelScriptUI(true, models))
self.UI.setXmlTable({generateUIDefaults(), generateModelScriptUI(true, models), generateTeamSelectUI(true, true)})
else
broadcastToOwner("That code is not valid. Please copy your roster code from [b]https://datateamapp.azurewebsites.net/Encode[/b].")
self.UI.setXmlTable({generateUIDefaults(), generateTeamSelectUI(true, false)})
end
end
end
function callback_loadTeam( player, value, id )
if state.teamkey and state.teamkey == teamApiCode then
player.broadcast("That team is already loaded")
return
end
self.UI.setAttribute(id, "interactable", false)
self.UI.setAttribute(id, "text", "LOADING...")
state.teamkey = teamApiCode
saveState()
WebRequest.get(teamApiUrl .. teamApiCode, loadTeamRequest)
end
function modelSelections(model)
local snames = {}
sfloop(model.selections.selection, function(v)
table.insert(snames, v["@name"])
end)
return table.concat(snames, ", ")
end
function callback_pickModel( player, value, id )
if player.steam_id == state.playerid then
pickModelForId = id
local model = state.models[id]
player.broadcast(string.format("Choose a model for [b]%s[/b] with %s", model["@name"], modelSelections(model)))
else
player.broadcast("Only the team's owner can pick models.")
end
end
function callback_tweakBase( player, value, id )
local idi = readGuiid(id)
local base = baseDimensions[tonumber(idi[4])]
local so = player.getSelectedObjects()
if next(so) ~= nil then
for k,v in pairs(so) do
rcall(v, "comSetBase", base)
end
else
player.broadcast("Select some operatives first")
end
end
function callback_autoScale( player, value, id )
local so = player.getSelectedObjects()
if next(so) ~= nil then
for k,v in pairs(so) do
rcall(v, "comAutoSize")
end
else
player.broadcast("Select some operatives first")
end
end
function callback_finishLayout( player, value, id )
if checkOwner(player) then
local np = {}
for k,v in pairs(state.positions) do
local m = getObjectsWithTag(k)
if next(m) ~= nil then
local o = m[1]
local vr = o.getRotation().y - self.getRotation().y
np[k] = {
position=self.positionToLocal(o.getPosition()),
rotation=vr
}
o.call("comSetUIAngle", {uiAngle=vr})
end
end
state.positions = np
saveState()
generateGui()
end
end
function generateTeamLayoutUI(active, id)
local tweakPanel = function()
local bh = 25
local sep = 4
local border = 6
local width = 150
local height = border - sep
local baseButton = function(t, h, k, v)
local btext = (v.x == v.z) and string.format("%d", v.x) or string.format("%d by %d", v.x, v.z)
table.insert(t,
xt("Button", {
text=btext,
width=width-border*2,
height=bh,
rectAlignment="UpperCenter",
id=makeGuiid({"tweak","base",tostring(k)}),
onClick="callback_tweakBase",
offsetXY=string.format("0 %d", -h)
}))
return h + bh
end
local pchildren = {}
table.insert(pchildren,
xt("Text", {
class="inputTitle",
resizeTextForBestFit=true,
text="Adjust base size",
width=width-border*2,
height=30,
rectAlignment="UpperCenter",
offsetXY="0 "..(-border)
}))
height = height + 30
for k,v in pairs(baseDimensions) do
height=baseButton(pchildren, height+sep, k, v)
end
height = height + 50 + border
table.insert(pchildren,
xt("Button", {
text="AUTO SCALE",
resizeTextForBestFit=true,
width=width-border*2,
height=35,
rectAlignment="LowerCenter",
offsetXY="0 "..border,
onClick="callback_autoScale"
}))
return xt("Panel", {
color="#ffffff",
width=width,
height=height,
position=string.format("%d %d -50", -(width*0.5 + 200), -(height*0.5)),
rotation="0 0 180"
}, pchildren)
end
local layoutArea = function(w, h, t, o)
return xt("Panel", {
width=w,
height=h,
position=string.format("0 %d -10", h*0.5 + o),
rotation="0 0 180"
}, {
xt("Panel", {
color="#F4641D",
height=t,
rectAlignment="UpperCenter"
}),
xt("Panel", {
color="#F4641D",
height=t,
rectAlignment="LowerCenter"
}),
xt("Panel", {
color="#F4641D",
height=(h-t*2),
width=t,
rectAlignment="MiddleLeft"
}),
xt("Panel", {
color="#F4641D",
height=(h-t*2),
width=t,
rectAlignment="MiddleRight"
}),
xt("Text", {
color="#F4641D",
text="Arrange your team here",
fontSize=40
})
})
end
return xt("Panel", {},{
tweakPanel(),
layoutArea(1600, 900, 15, 75),
xt("Panel", {
active=active, id=id,
width=310, height=225,
position="0 -100 -200",
color="#FFFFFF",
rotation="45 0 180"
},
{
xt("VerticalLayout",{
width=300, height=120,
offsetXY="0 -5",
rectAlignment="UpperCenter"
},{
xt("Text",{class="inputTitle", text="Finish Your Team"}),
xt("Text",{text="Make sure all your operatives are on the right bases (see the <b>Adjust base size</b> panel)"}),
xt("Text",{text="When you're done, arrange your team in the orange area. When you're happy with the team's layout, click the FINISH button."})
}),
xt("Button",{
width=300, height=40,
resizeTextForBestFit=true,
rectAlignment="UpperCenter",
text="FINISH",
offsetXY="0 -180",
onClick="callback_finishLayout"
})
})
})
end
function generateModelScriptUI(active, models, id)
local mpw = 250
local mph = 200
local th = 50
local bh = 50
local mps = 25
local mcw = mpw+mps
local mch = mph+mps
local hmps = mps*0.5
local vofs = 200
local mids = {}
for k, m in pairs(models) do
table.insert(mids, {guid=k, sel=modelSelections(m), name=m["@name"]})
end
table.sort( mids, function(A, B)
if A.name ~= B.name then
return A.name < B.name
end
if A.sel ~= B.sel then
return A.sel < B.sel
end
return A.guid < B.guid
end)
local mcount = #mids
local mcs = math.floor(math.sqrt(mcount))
local mw = math.ceil(mcount/mcs)
local tcx = 0
local tcy = 0
local modelPanel = function(tbl, x, y, mid)
local guid = mids[mid].guid
local em = getObjectsWithTag(guid)
local mod = models[guid]
local lx = x*(mpw + mps)+hmps
local ly = -y*(mph + mps)-hmps
if next(em) ~= nil then
makeOperative(em[1], guid)
end
table.insert(tbl,
xt("Panel", {
class="modelPanel",
color= (#em > 0) and "#808080" or "#8F5757",
rectAlignment="UpperLeft",
width=mpw, height = mph,
offsetXY=string.format("%d %d", lx, ly),
id=guid.."_panel"
},{
xt("Text",{
height=th,
alignment="MiddleCenter",
rectAlignment="UpperCenter",
resizeTextForBestFit=true,
text=mod["@customName"] or mod["@name"]
}),
xt("Text",{
height=mph - th - bh,
rectAlignment="UpperCenter",
offsetXY=string.format("0 %d", -th),
text=mids[mid].sel
}),
xt("Button",{
height=bh,
rectAlignment="LowerCenter",
text="Choose Model",
onClick="callback_pickModel",
id=guid
})
}))
end
state.positions = {}
local mpanels = {}
local panelw = (mpw+mps)*mw
local panelh = (mph+mps)*mcs
local i = 1
local mi = 0
while i <= mcount do
local mx = math.floor(mi%mw)
local my = math.floor(mi/mw)
while not pcall(modelPanel, mpanels, mx, my, i) do
i = i + 1
end
if i <= mcount then
state.positions[mids[i].guid] = {
position=Vector(
(panelw*0.5 - mx*mcw - (mcw)*0.5)*0.01,
1,
(vofs + my*mch + th + mps*0.5)*0.01),
rotation=0
}
end
i = i + 1
mi = mi + 1
end
state.models = models
saveState()
return xt("Panel",{
active=active, id=id,
-- color="#ffffff",
width=panelw,
height=panelh,
position=string.format("0 %d -10", panelh/2 + vofs),
rotation="0 0 180"
}, mpanels)
end
function callback_doTeamLayout( player, value, id )
if checkOwner(player) then
self.UI.setXmlTable({generateUIDefaults(), generateTeamLayoutUI(true)})
resetOperativePositions()
end
end
function callback_doLoadNewRoster( player, value, id )
if checkOwner(player) then
self.UI.setXmlTable({generateUIDefaults(), generateTeamSelectUI(true, true), generateModelScriptUI(true, state.models)})
resetOperativePositions()
end
end
function generateTeamSelectUI(active, allowFinish, id)
local selectHelpButton = makeGuiid({"team","select","help","button"})
local selectHelpPanel = makeGuiid({"team","select","help","panel"})
return xt("Panel", {
active=active, id=id,
width=310, height=225,
position="0 0 -100",
color="#FFFFFF",
rotation="0 0 180"
},
{
xt("VerticalLayout",{
width=300, height=120,
offsetXY="0 -5",
rectAlignment="UpperCenter"
},{
xt("Text",{class="inputTitle", text="Enter Team Code"}),
xt("InputField",{
placeholder="Team Code",
alignment="MiddleCenter",
fontSize=20,
onValueChanged="callback_teamCode",
characterLimit=16,
text=teamApiCode
})
}),
xt("Button",{
width=300, height=40,
resizeTextForBestFit=true,
rectAlignment="UpperCenter",
text="LOAD TEAM",
offsetXY="0 -135",
id=loadTeamButtonId,
onClick="callback_loadTeam"
}),
xt("Button",{
width=300, height=40,
resizeTextForBestFit=true,
rectAlignment="UpperCenter",
text="FINISH",
offsetXY="0 -180",
interactable=allowFinish,
onClick="callback_doTeamLayout"
}),
xt("Panel",{
class="helpPanel",
id=selectHelpPanel,
width=350, height=400,
rectAlignment="LowerRight",
offsetXY="355 5"
},{
xt("Text",{
alignment="UpperLeft",
width=344, height=394
},{},[[<textsize size="18"><b>How To Make a Team</b></textsize>\n
<b>STEP 1)</b> Create your roster in battlescribe\n
<b>STEP 2)</b> Go to https://datateamapp.azurewebsites.net/Encode\n
<b>STEP 3)</b> Upload your team's ".rosz" file"\n
<b>STEP 4)</b> Copy your team code\n
<b>STEP 5)</b> Paste your team code into the Command Node and press the <b>LOAD TEAM</b> button\n
<b>STEP 6)</b> Select models for your operatives\n
<b>STEP 7)</b> Press the <b>FINISH</b> button\n
<b>STEP 8)</b> Your team is ready to play!]])
}),
xt("Button",{
width=50, height=50,
rectAlignment="UpperRight",
offsetXY="-5 -5",
class="helpButton",
id=selectHelpButton,
onClick=panelToggle(selectHelpButton, selectHelpPanel, false)
})
})
end
function splitString(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
table.insert(t, str)
end
return t
end
function getKeys(tbl)
local r = {}
local n = 1
for k, v in pairs(tbl) do
r[n] = k
n = n + 1
end
return r, n-1
end
function callback_saveAll( player, value, id )
if checkOwner(player) then
allOperatives(function(op)
for i,v in ipairs(op) do
v.call("comSavePosition", {})
end
end)
end
end
function callback_loadAll( player, value, id )
if checkOwner(player) then
allOperatives(function(op)
for i,v in ipairs(op) do
v.call("comLoadPosition")
end
end)
end
end
function callback_backToMain( player, value, id )
if checkOwner(player) then
generateGui()
end
end
function generatePlayUI( active )
local width = 350
local border = 6
local cw = width-border*2
local sep = 15
local bh = 40
local h = border
local pc = {}
local button = function(text, callback, id)
table.insert(pc, xt("Button", {
width = cw,
height = bh,
onClick = callback,
text=text,
fontSize=12,
id=id,
rectAlignment="UpperCenter",
offsetXY="0 "..(-h)
}))
h = h + bh + sep
end
button("Save all positions", "callback_saveAll")
button("Load all positions", "callback_loadAll")
button("Back to main menu", "callback_backToMain")
return xt("Panel", {
color="#ffffff",
height = h + border - sep,
width=width,
position=string.format("0 %d -100", -h/2),
rotation="0 0 180"
}, pc)
end
function callback_resetModelPositions( player, value, id )
if checkOwner(player) then
resetOperativePositions()
end
end
function callback_updateScripts( player, value, id )
if checkOwner(player) then
tryUpdate(player.color)
end
end
function callback_play( player, value, id )
if checkOwner(player) then
self.UI.setXmlTable({generateUIDefaults(), generatePlayUI(true)})
end
end
function generateMainMenuUI( active )
local width = 350
local border = 6
local cw = width-border*2
local sep = 15
local bh = 40
local h = border
local pc = {}
local button = function(text, callback, id)
table.insert(pc, xt("Button", {
width = cw,
height = bh,
onClick = callback,
text=text,
fontSize=12,
id=id,
rectAlignment="UpperCenter",
offsetXY="0 "..(-h)
}))
h = h + bh + sep
end
table.insert(pc, xt("Text", {
class="mainTitle",
resizeTextForBestFit=true,
text=state.name,
width = cw,
height=30,
rectAlignment="UpperCenter",
offsetXY="0 "..(-h)
}))
h = h + 30 + sep
button("Play", "callback_play")
button("Recall models", "callback_resetModelPositions")
button("Adjust Team", "callback_doTeamLayout")
button("Load new roster", "callback_doLoadNewRoster")
table.insert(pc, xt("Button", {
resizeTextForBestFit=true,
text="Update Scripts",
width=200, height=80,
rectAlignment="UpperCenter",
offsetXY="0 90",
color="#F4641D",
id=updateButtonId,
showAnimation="Grow",
onClick="callback_updateScripts",
active=needsUpdate()
}))
return xt("Panel", {
color="#ffffff",
height = h + border - sep,
width=width,
position=string.format("0 %d -100", -h/2),
rotation="0 0 180"
}, pc)
end
function generateGui()
local defaults = generateUIDefaults()
if state.playerid then
if state.models then
self.UI.setXmlTable({defaults, generateMainMenuUI(true)})
else
self.UI.setXmlTable({defaults, generateTeamSelectUI(true, false)})
end
else
-- self.UI.setXmlTable({defaults, generateClaimUI(true)})
self.UI.setXmlTable({defaults, generateClaimUI(true)})
end
end
function saveState()
self.script_state = JSON.encode(state)
end
function loadState()
local ds = JSON.decode(self.script_state)
if ds then state = ds end
end
function tryUpdate(pc)
if needsUpdate() then
local rq = scriptUrl
local updateModel = remoteVersion.model > state.version.model
local updateNode = remoteVersion.node > state.version.node
if updateModel then rq = rq .. "Model"end
if updateNode then rq = rq .. "Node"end
WebRequest.get(rq, function(req)
if req.is_error then
log(req.error)
broadcastToOwner("Failed to update scripts. Check the log.")
else
state.version = remoteVersion
local data = JSON.decode(req.text)
self.clearContextMenu()
if updateModel then
broadcastToOwner("Updating models...")
state.modelscript = data.model
allOperatives(function(ops)
for i=2,#ops do
ops[i].destruct()
end
ops[1].setLuaScript(data.model)
ops[1].reload()
end)
end
saveState()
if updateNode then
broadcastToOwner("Updating node...")
self.setLuaScript(data.node)
end
resetOperativePositions()
self.reload()
broadcastToOwner("Finished update. You should save your team again.")
end
end)
end
end
function sfloop(cat, func)
if cat then
if cat["@id"] then
func(cat)
else
for i,v in ipairs(cat) do
func(v)