-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.lua
More file actions
3552 lines (3231 loc) · 123 KB
/
Copy pathcode.lua
File metadata and controls
3552 lines (3231 loc) · 123 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
--[[ Lua code. See documentation: https://api.tabletopsimulator.com/ --]]
--TTS开发守则
--1.给ai以需求
--2.能拿则拿
--3.注意拼写!!!
--4.通读API文档
--[[ The onLoad event is called after the game save finishes loading. --]]
function onLoad(save_state)
WebRequest.get("http://www.justgan.cn/datas/cards.csv", function(request)
data = processCSVData(request)
end
)
canAddButton = false
cardInstances = {}
tokenInstances = {}
gameInfo = {
stage = "",
redealA = 3,
redealB = 2,
meleeA = {},
rangeA = {},
meleeB = {},
rangeB = {},
deckA = nil,
deckB = nil,
originalDeckA = nil,
originalDeckB = nil,
round = 0,
turn = 0,
allTurn = 0,
stopA = false,
stopB = false,
jumpA = false,
jumpB = false,
playedA = false,
playedB = false,
pointA = 0,
pointB = 0,
board = nil
}
count = 0
countCards = 0
countAdjust = 0
globalButtons = {}
clipBoards = {}
scale = 2
size_card = {9.25, 0, 12} -- 假设这是单张卡牌的尺寸(宽、无意义、高)
Turns.enable = true
Turns.pass_turns = true
flagODA = false
flagODB = false
choosing = false
chosenCard = nil
chooseCards = {} -- 存储选择的卡牌GUID列表
chooseNum = 0 -- 需要选择的卡牌数量
chooseUIObjects = {} -- 存储UI对象引用,用于清理
objectUI = {}
objectAsset = {}
-- FollowCursor相关变量
followCursorData = {} -- 存储每个玩家的追踪数据 {playerColor = {cardObj, isTracking}}
-- 出牌区列表(FollowCursor功能使用)
PLAY_ZONES = {
"zoneMeleeA",
"zoneMeleeB",
"zoneRangeA",
"zoneRangeB"
}
-- 旋转检测相关变量
cardRotated = {} -- 存储卡牌是否被旋转过 {cardGuid = true/false}
-- =========================================================
-- 指令系统:Order 按钮状态追踪与目标选择模式
-- =========================================================
-- 追踪每张卡牌的指令按钮状态
-- {cardGuid -> {turnEntered=N, isDormant=true/false, filter="...", selectType="..."}}
orderButtonState = {}
-- target_field 交互模式的选择状态
targetSelectMode = false -- 是否处于目标选择模式
targetSelectData = { -- 当前目标选择的配置和进度
filter = nil, -- 筛选范围 ("allyRows"/"enemyRows"/"battleGround")
min = 1, -- 最少选择数量
max = 1, -- 最多选择数量
orderCardGuid = nil, -- 发起 Order 的卡牌 GUID(可用于排除自身)
selected = {} -- 已选中的目标 GUID 列表
}
end
function deepCopyItable(t)
local newTable = {}
if t then
for k, v in pairs(t) do
if type(v) == 'table' then
newTable[k] = deepCopyItable(v) -- 递归复制子表
else
newTable[k] = v -- 直接复制非表类型的数据
end
end
end
return newTable
end
-- 处理单个牌组的通用函数
function processDeck(deck)
if deck then
local tempDeck = {}
for _, object in ipairs(deck.getObjects()) do
local instance = getGUIDInstance(object.guid)
local instanceCopy = deepCopyItable(instance) -- 创建 instance 的深拷贝
instanceCopy.inGameObj = instanceCopy.inGameObjGUID -- 修改拷贝,不影响原始对象
instanceCopy.owner = nil
instanceCopy.buttonDisplay = nil
table.insert(tempDeck, instanceCopy)
end
return tempDeck
end
return nil
end
function processDeckWithOutGetObjects(deck)
if deck then
local tempDeck = {}
for _, object in ipairs(deck) do
local instance = getGUIDInstance(object.guid)
local instanceCopy = deepCopyItable(instance) -- 创建 instance 的深拷贝
instanceCopy.inGameObj = nil -- 修改拷贝,不影响原始对象
instanceCopy.owner = nil
instanceCopy.buttonDisplay = nil
table.insert(tempDeck, instanceCopy)
end
return tempDeck
end
return nil
end
function processCards(cardListName, processedGameInfo)
local temp = {}
for _, card in ipairs(processedGameInfo[cardListName]) do
local instance = getObjectInstance(card)
if instance then
-- 清理 userdata 字段,只保留可序列化的数据
local safeInstance = deepCopyItable(instance)
safeInstance.inGameObj = nil
safeInstance.inGameObjGuid = instance.inGameObjGuid
safeInstance.owner = instance.owner and instance.owner.color or nil
safeInstance.buttonDisplay = nil
-- 清理 statuses 中的 userdata
if safeInstance.statuses then
local safeStatuses = {}
for _, status in ipairs(safeInstance.statuses) do
if type(status) == "table" then
table.insert(safeStatuses, {
inGameObjGuid = status.inGameObjGuid,
type = status.type,
charge = status.charge
})
else
table.insert(safeStatuses, tostring(status))
end
end
safeInstance.statuses = safeStatuses
end
table.insert(temp, safeInstance)
end
end
processedGameInfo[cardListName] = temp
end
-- 修改后的 emit 函数
function emit(event)
local url = "http://www.justgan.cn/server"
-- 不使用 deepCopyItable(gameInfo),因为它会复制 TTS userdata(Deck/Card 对象)导致 JSON 报错
-- 手动构建一个完全可序列化的 gameInfo 表
local processedGameInfo = {
stage = gameInfo.stage or "",
redealA = gameInfo.redealA or 3,
redealB = gameInfo.redealB or 2,
round = gameInfo.round or 0,
turn = gameInfo.turn or 0,
allTurn = gameInfo.allTurn or 0,
stopA = gameInfo.stopA or false,
stopB = gameInfo.stopB or false,
jumpA = gameInfo.jumpA or false,
jumpB = gameInfo.jumpB or false,
playedA = gameInfo.playedA or false,
playedB = gameInfo.playedB or false,
pointA = gameInfo.pointA or 0,
pointB = gameInfo.pointB or 0,
originalDeckA = nil,
originalDeckB = nil
}
-- 安全处理牌库(deckA/deckB 是 TTS userdata,不能直接序列化)
-- 注意:这里发送的是当前牌库中的完整卡牌信息,用于服务器实时判定
local function processCurrentDeck(deckRef, fieldName)
if not deckRef then
processedGameInfo[fieldName] = {}
return
end
local temp = {}
-- 获取牌库中的所有卡牌对象
local deckObjects = deckRef.getObjects and deckRef.getObjects() or {}
for _, cardObj in ipairs(deckObjects) do
if cardObj and cardObj.guid then
local instance = getGUIDInstance(cardObj.guid)
if instance then
-- 使用白名单方式,只提取基本类型数据,避免任何 userdata 泄露
local safeInstance = {
dataId = tostring(instance.dataId or ""),
power = tonumber(instance.power) or 0,
basePower = tonumber(instance.basePower) or 0,
armor = tonumber(instance.armor) or 0,
provision = tonumber(instance.provision) or 0,
guid = instance.guid or cardObj.guid,
inGameObjGuid = instance.guid or cardObj.guid,
owner = instance.owner and instance.owner.color or nil,
statuses = {}
}
-- 安全地处理 statuses,只保留基本字段
if instance.statuses then
for _, status in ipairs(instance.statuses) do
if type(status) == "table" then
table.insert(safeInstance.statuses, {
inGameObjGuid = tostring(status.inGameObjGuid or ""),
type = tostring(status.type or ""),
charge = tonumber(status.charge) or 0
})
end
end
end
table.insert(temp, safeInstance)
else
-- 如果找不到实例,至少保留 guid 和 dataId
table.insert(temp, {guid = cardObj.guid, dataId = "", inGameObjGuid = cardObj.guid})
end
end
end
processedGameInfo[fieldName] = temp
end
processCurrentDeck(gameInfo.deckA, "deckA")
processCurrentDeck(gameInfo.deckB, "deckB")
-- board 只保留 GUID
if gameInfo.board then
processedGameInfo.board = {
zones = {
meleeA = gameInfo.board.zones.meleeA.getGUID(),
meleeB = gameInfo.board.zones.meleeB.getGUID(),
rangeA = gameInfo.board.zones.rangeA.getGUID(),
rangeB = gameInfo.board.zones.rangeB.getGUID(),
deckA = gameInfo.board.zones.deckA.getGUID(),
deckB = gameInfo.board.zones.deckB.getGUID(),
graveA = gameInfo.board.zones.graveA.getGUID(),
graveB = gameInfo.board.zones.graveB.getGUID()
}
}
else
processedGameInfo.board = {zones = {}}
end
-- 处理战场上的卡牌列表:发送完整的卡牌实例数据
local function processFieldCards(fieldName)
local cards = gameInfo[fieldName] or {}
local temp = {}
for _, card in ipairs(cards) do
if card then
local guid = ""
if type(card) == "userdata" and card.guid then
guid = card.guid
elseif type(card) == "table" and card.inGameObjGuid then
guid = card.inGameObjGuid
else
guid = tostring(card)
end
-- 获取完整的卡牌实例数据
local instance = getGUIDInstance(guid)
if instance then
-- 使用白名单方式,只提取基本类型数据,避免任何 userdata 泄露
local safeInstance = {
dataId = tostring(instance.dataId or ""),
power = tonumber(instance.power) or 0,
basePower = tonumber(instance.basePower) or 0,
armor = tonumber(instance.armor) or 0,
provision = tonumber(instance.provision) or 0,
guid = instance.guid or guid,
inGameObjGuid = instance.guid or guid,
owner = instance.owner and instance.owner.color or nil,
statuses = {}
}
-- 安全地处理 statuses,只保留基本字段
if instance.statuses then
for _, status in ipairs(instance.statuses) do
if type(status) == "table" then
table.insert(safeInstance.statuses, {
inGameObjGuid = tostring(status.inGameObjGuid or ""),
type = tostring(status.type or ""),
charge = tonumber(status.charge) or 0
})
end
end
end
table.insert(temp, safeInstance)
else
-- 如果找不到实例,至少保留 GUID
table.insert(temp, {inGameObjGuid = guid, dataId = ""})
end
end
end
processedGameInfo[fieldName] = temp
end
processFieldCards("meleeA")
processFieldCards("rangeA")
processFieldCards("meleeB")
processFieldCards("rangeB")
-- 处理墓场中的卡牌列表:发送完整的卡牌实例数据
local function processGraveCards(fieldName)
local graveZone = gameInfo.board and gameInfo.board.zones and gameInfo.board.zones[fieldName]
if not graveZone then
processedGameInfo[fieldName] = {}
return
end
local temp = {}
-- 获取墓场区域中的所有对象
local graveObjects = graveZone.getObjects and graveZone.getObjects() or {}
for _, cardObj in ipairs(graveObjects) do
if cardObj and cardObj.guid then
-- 获取完整的卡牌实例数据
local instance = getGUIDInstance(cardObj.guid)
if instance then
-- 使用白名单方式,只提取基本类型数据,避免任何 userdata 泄露
local safeInstance = {
dataId = tostring(instance.dataId or ""),
power = tonumber(instance.power) or 0,
basePower = tonumber(instance.basePower) or 0,
armor = tonumber(instance.armor) or 0,
provision = tonumber(instance.provision) or 0,
guid = instance.guid or cardObj.guid,
inGameObjGuid = instance.guid or cardObj.guid,
owner = instance.owner and instance.owner.color or nil,
statuses = {}
}
-- 安全地处理 statuses,只保留基本字段
if instance.statuses then
for _, status in ipairs(instance.statuses) do
if type(status) == "table" then
table.insert(safeInstance.statuses, {
inGameObjGuid = tostring(status.inGameObjGuid or ""),
type = tostring(status.type or ""),
charge = tonumber(status.charge) or 0
})
end
end
end
table.insert(temp, safeInstance)
else
-- 如果找不到实例,至少保留 GUID
table.insert(temp, {inGameObjGuid = cardObj.guid, dataId = ""})
end
end
end
processedGameInfo[fieldName] = temp
end
processGraveCards("graveA")
processGraveCards("graveB")
-- 处理 originalDeckA/B:只在 game_start 事件时发送完整的初始卡组信息
-- originalDeck 是游戏开始时的快照,不需要随时更新状态
local function processOriginalDeck(originalDeckRef, fieldName)
if not originalDeckRef then
processedGameInfo[fieldName] = {}
return
end
local temp = {}
for _, cardObj in ipairs(originalDeckRef) do
if cardObj and cardObj.guid then
local instance = getGUIDInstance(cardObj.guid)
if instance then
-- 使用白名单方式,只提取基本类型数据,避免任何 userdata 泄露
local safeInstance = {
dataId = tostring(instance.dataId or ""),
power = tonumber(instance.power) or 0,
basePower = tonumber(instance.basePower) or 0,
armor = tonumber(instance.armor) or 0,
provision = tonumber(instance.provision) or 0,
guid = instance.guid or cardObj.guid,
inGameObjGuid = instance.guid or cardObj.guid,
owner = instance.owner and instance.owner.color or nil,
statuses = {}
}
-- 安全地处理 statuses,只保留基本字段
if instance.statuses then
for _, status in ipairs(instance.statuses) do
if type(status) == "table" then
table.insert(safeInstance.statuses, {
inGameObjGuid = tostring(status.inGameObjGuid or ""),
type = tostring(status.type or ""),
charge = tonumber(status.charge) or 0
})
end
end
end
table.insert(temp, safeInstance)
else
-- 如果找不到实例,至少保留 guid 和 dataId
table.insert(temp, {guid = cardObj.guid, dataId = "", inGameObjGuid = cardObj.guid})
end
end
end
processedGameInfo[fieldName] = temp
end
processOriginalDeck(gameInfo.originalDeckA, "originalDeckA")
processOriginalDeck(gameInfo.originalDeckB, "originalDeckB")
-- 白名单处理 event 参数中的 card 字段,避免 userdata 泄露
local safeEvent = {
type = event.type or "",
color = event.color or "",
round = event.round or nil,
row = event.row or nil,
index = event.index or nil,
args = event.args or nil
}
-- 如果 event 包含 card 字段,使用白名单方式安全处理
if event.card then
local cardData = event.card
if type(cardData) == "userdata" then
-- cardData 本身就是 TTS userdata,尝试获取 GUID
safeEvent.card = {
inGameObjGuid = cardData.guid or "",
dataId = ""
}
elseif type(cardData) == "table" then
-- cardData 是卡牌实例表,使用白名单提取安全字段
safeEvent.card = {
dataId = tostring(cardData.dataId or ""),
power = tonumber(cardData.power) or 0,
basePower = tonumber(cardData.basePower) or 0,
armor = tonumber(cardData.armor) or 0,
provision = tonumber(cardData.provision) or 0,
guid = cardData.guid or cardData.inGameObjGuid or "",
inGameObjGuid = cardData.guid or cardData.inGameObjGuid or "",
owner = cardData.owner and cardData.owner.color or nil,
faction = cardData.faction or "",
color = cardData.color or "",
cardType = cardData.type or "",
rarity = cardData.rarity or "",
placed = cardData.placed or false,
charge = cardData.charge or 0,
statuses = {}
}
-- 安全处理 statuses
if cardData.statuses then
for _, status in ipairs(cardData.statuses) do
if type(status) == "table" then
table.insert(safeEvent.card.statuses, {
inGameObjGuid = tostring(status.inGameObjGuid or ""),
type = tostring(status.type or ""),
charge = tonumber(status.charge) or 0
})
else
table.insert(safeEvent.card.statuses, tostring(status))
end
end
end
end
end
local body = {
gameInfo = JSON.encode(processedGameInfo),
event = safeEvent
}
local headers = {
["Content-Type"]="application/x-www-form-urlencoded"
}
WebRequest.custom(url, "POST", true, JSON.encode(body), headers, handleResponse)
end
function handleResponse(request)
if request.is_error then
print(request.error)
else
print(request.text)
local actions = JSON.decode(request.text)
if type(actions) ~= "table" then
print("handleResponse: invalid or nil actions from server, skipping.")
return
end
for i, action in ipairs(actions) do
if action["action"] == "power" then
card = getGUIDInstance(action["guid"])
if card then
card.power = card.power + action["args"][1]
end
elseif action["action"] == "jumpTo" then
card = getObjectFromGUID(action["guid"])
to = action["args"][1]
index = action["args"][2]
if card then
for _, zone in ipairs(card.getZones()) do
if zone == gameInfo.board.zones.meleeA or zone == gameInfo.board.zones.meleeB or zone == gameInfo.board.zones.rangeA or zone == gameInfo.board.zones.rangeB then
insertCard(card, to, index)
break
elseif zone == gameInfo.board.zones.deckA then
for _, c in ipairs(gameInfo.deckA.getObjects()) do
if c.guid == card.guid then
_card = gameInfo.board.zones.deckA.takeObject({
index = c.index
})
insertCard(_card, to, index)
break
end
end
elseif zone == gameInfo.board.zones.deckB then
for _, c in ipairs(gameInfo.deckB.getObjects()) do
if c.guid == card.guid then
_card = gameInfo.board.zones.deckB.takeObject({
index = c.index
})
insertCard(_card, to, index)
break
end
end
elseif zone == gameInfo.board.zones.graveA then
objsInDeck = {}
deck = nil
for _, c in ipairs(gameInfo.board.zones.graveA.getObjects()) do
if c.type == "Deck" then
objsIndeck = c.getObjects()
deck = c
break
elseif c.type == "Card" then
objsInDeck[#objsInDeck + 1] = c
end
end
for _, c in ipairs(objsInDeck) do
print(c.guid, card.guid)
if c.guid == card.guid then
if deck then
_card = deck.takeObject({
index = c.index
})
else
_card = objsInDeck[1]
end
insertCard(_card, to, index)
break
end
end
print("graveA")
elseif zone == gameInfo.board.zones.graveB then
for _, c in ipairs(gameInfo.board.zones.graveB.getObjects()) do
if c.guid == card.guid then
_card = gameInfo.board.zones.graveB.takeObject({
index = c.index
})
insertCard(_card, to, index)
break
end
end
end
end
card = getObjectInstance(card)
end
elseif action["action"] == "Choose" then
choosing = true
Wait.condition(function()
local url = "http://www.justgan.cn/choose"
-- 白名单处理chosenCard
local safeCard = {
dataId = tostring(chosenCard.dataId or ""),
power = tonumber(chosenCard.power) or 0,
basePower = tonumber(chosenCard.basePower) or 0,
armor = tonumber(chosenCard.armor) or 0,
provision = tonumber(chosenCard.provision) or 0,
inGameObjGuid = tostring(chosenCard.guid or chosenCard.inGameObjGuid or ""),
owner = chosenCard.owner and chosenCard.owner.color or nil,
faction = tostring(chosenCard.faction or ""),
cardType = tostring(chosenCard.type or ""),
rarity = tostring(chosenCard.rarity or ""),
placed = chosenCard.placed or false,
charge = tonumber(chosenCard.charge) or 0,
statuses = {}
}
if chosenCard.statuses then
for _, status in ipairs(chosenCard.statuses) do
if type(status) == "table" then
table.insert(safeCard.statuses, {
inGameObjGuid = tostring(status.inGameObjGuid or ""),
type = tostring(status.type or ""),
charge = tonumber(status.charge) or 0
})
else
table.insert(safeCard.statuses, tostring(status))
end
end
end
event = {
type = "ChooseEnd",
args = {
card = safeCard
}
}
local body = {
gameInfo = nil,
event = event
}
local headers = {
["Content-Type"]="application/x-www-form-urlencoded"
}
WebRequest.custom(url, "POST", true, JSON.encode(body), headers, handleResponse)
end, function()
return not choosing
end)
elseif action["action"] == "showChooseUI" then
-- 显示新的选择UI
chooseNum = action["num"] or 1
chooseCards = {}
local cards = action["cards"] or {}
print("[DEBUG-ChooseUI] 收到showChooseUI请求")
print("[DEBUG-ChooseUI] 需要选择数量: " .. chooseNum)
print("[DEBUG-ChooseUI] 卡牌数量: " .. #cards)
-- 检查cards是否为空
if #cards == 0 then
print("[ERROR-ChooseUI] 卡牌列表为空!")
UI.setAttribute("GlobalUI", "active", false)
return
end
-- 为每张卡牌创建缩略图按钮(带战力、护甲、人口显示)
local gridXML = ""
for i, cardData in ipairs(cards) do
local imageUrl = string.format("http://www.justgan.cn/images/card_image_%s.png", cardData.dataId)
local descUrl = string.format("http://www.justgan.cn/images/card_description_%s.png", cardData.dataId)
-- 计算战力颜色
local powerColor = "#FFFFFF"
if cardData.power < cardData.basePower then
powerColor = "#FF0000"
elseif cardData.power > cardData.basePower then
powerColor = "#00FF00"
end
-- 放大到200%:从140,190变为280,380
gridXML = gridXML .. [[
<Button id="card_]] .. cardData.guid .. [["
onClick="onChooseCardClick"
onHover="onChooseCardHover"
tooltip="]] .. cardData.name .. [[ 战力: ]] .. cardData.power .. [[/]] .. cardData.basePower .. [[ 护甲: ]] .. (cardData.armor or 0) .. [[ 人口: ]] .. (cardData.provision or 0) .. [[\n]] .. (cardData.description or "") .. [["
image="]] .. imageUrl .. [["
width="280"
height="380"
data-guid="]] .. cardData.guid .. [["
data-desc="]] .. descUrl .. [["
data-name="]] .. cardData.name .. [["
data-description="]] .. (cardData.description or "") .. [["
data-power="]] .. cardData.power .. [["
data-basePower="]] .. cardData.basePower .. [["
data-armor="]] .. (cardData.armor or 0) .. [["
color="#FFFFFF">
<Panel>
<!-- 🔴 左上角:战力 -->
<Text text="]] .. cardData.power .. [["
fontSize="42"
color="]] .. powerColor .. [["
offsetXY="-100,150"
fontStyle="Bold"
textOutline="#000000"
textOutlineSize="2" />
<!-- 🟡 右上角:护甲 -->
<Text text="]] .. (cardData.armor or 0) .. [["
fontSize="42"
active="]] .. ((cardData.armor or 0) > 0 and "true" or "false") .. [["
color="#FFD700"
offsetXY="100,150"
fontStyle="Bold"
textOutline="#000000"
textOutlineSize="2" />
<!-- 🔵 右下角:人口 -->
<Text text="]] .. (cardData.provision or 0) .. [["
fontSize="42"
color="#FFD700"
offsetXY="100,-150"
fontStyle="Bold"
textOutline="#000000"
textOutlineSize="2" />
</Panel>
</Button>
]]
end
print("[DEBUG-ChooseUI] 已生成gridXML,长度: " .. string.len(gridXML))
-- 使用GridLayout配合固定尺寸,确保卡牌正确排列
-- ScrollView使用固定高度而非百分比
local fullXML = [[
<VerticalLayout width="100%" height="100%" color="#CC000000" padding="20" spacing="10" Id="choosePanel">
<Text text="请选择 ]] .. chooseNum .. [[ 张卡" fontSize="40" color="#FFFFFF" alignment="MiddleCenter" height="60" />
<VerticalScrollView width="100%" height="700" scrollbarBackgroundColor="#404040">
<GridLayout width="100%" cellSize="280,380" spacing="10 10" padding="10" color="#00000000">
]] .. gridXML .. [[
</GridLayout>
</VerticalScrollView>
<HorizontalLayout width="100%" height="80" color="#00000000">
<Button id="confirmBtn" text="确定 (0/]] .. chooseNum .. [[)" onClick="confirmChoose" width="200" height="60" fontSize="42" color="#404040" activeColor="#4CAF50" />
</HorizontalLayout>
</VerticalLayout>
]]
-- UI.setAttribute("GlobalUI", "active", true)
UI.setXml(fullXML)
print("[DEBUG-ChooseUI] UI已设置完成")
-- 初始化按钮状态
updateConfirmButton()
elseif action["action"] == "addButton" then
-- 动态给卡牌添加 Order 按钮(支持图标和筛选条件)
-- BUG修复:原代码在有 icon 时调用一次 createButton,无 icon 时调用两次,现已统一。
-- 设计变更:首次加入战场时始终添加"休眠指令"按钮(不可用),
-- 回合推进后由 updateOrderButtons() 自动切换为可用指令按钮。
local card = getObjectFromGUID(action["card"])
if card then
local buttonType = action["args"][1] -- "Order", "Deploy", "Charge" 等
if buttonType == "Order" then
local filter = action["filter"] or "allyRows" -- 目标筛选范围
local selectType = action["type"] or "unit" -- 目标类型
local cardGuid = action["card"]
-- 清理已有的 Order 相关按钮,防止重复添加(例如卡牌被 jumpTo 后重新触发 Deploy)
for _, button in ipairs(card.getButtons() or {}) do
if button.click_function == "clickOrderButton" or
button.click_function == "clickDormantOrderButton" then
card.removeButton(button.index)
end
end
-- 添加"休眠指令"按钮(第一回合不可用,灰色样式)
local dormantParams = {
click_function = "clickDormantOrderButton",
function_owner = self,
label = "Order",
position = {0, 0.1, 0},
rotation = {0, 0, 0},
width = 400,
height = 200,
font_size = 200,
color = {0.5, 0.5, 0.5}, -- 灰色:不可用
font_color = {0.8, 0.8, 0.8},
tooltip = "休眠指令\n本回合无法使用,下一回合起可激活。\n筛选: " .. filter
}
card.createButton(dormantParams)
-- 设置休眠图标贴花(休眠指令图标)
card.setDecals({{
name = "order_dormant",
url = "http://www.justgan.cn/icons/休眠指令",
position = {0, 0.05, -0.5},
rotation = {90, 0, 0},
scale = {0.15, 0.15, 1}
}})
-- 记录该卡牌的指令按钮状态,供 updateOrderButtons() 使用
orderButtonState[cardGuid] = {
turnEntered = gameInfo.turn, -- 入场回合数
isDormant = true, -- 当前为休眠状态
filter = filter,
selectType = selectType
}
print("[AddButton] 添加休眠指令按钮: card=" .. cardGuid
.. ",入场回合=" .. gameInfo.turn
.. ",筛选=" .. filter)
end
end
elseif action["action"] == "removeButton" then
-- 移除卡牌上的按钮,并清理对应的指令状态追踪
local card = getObjectFromGUID(action["guid"])
if card then
local buttonLabel = action["args"][1] -- 要移除的按钮标签
for _, button in ipairs(card.getButtons()) do
if button.label == buttonLabel then
card.removeButton(button.index)
print("Removed button '" .. buttonLabel .. "' from card: " .. action["guid"])
break
end
end
end
-- 移除该卡牌的指令状态追踪(防止残留状态影响其他逻辑)
orderButtonState[action["guid"]] = nil
elseif action["action"] == "awaitTarget" then
-- target_field 交互模式:进入目标选择状态
-- 服务端指示 Lua 端让玩家在战场上点选卡牌,选完后通过 /choose 提交
local filter = action["filter"] or "allyRows"
local minCount = action["min"] or 1
local maxCount = action["max"] or 1
local orderGuid = action["cardGuid"] -- 发起 Order 的卡牌 GUID
print("[AwaitTarget] 进入目标选择模式: filter=" .. filter
.. " min=" .. minCount .. " max=" .. maxCount
.. " orderCard=" .. tostring(orderGuid))
enterTargetSelectMode(filter, minCount, maxCount, orderGuid)
elseif action["action"] == "followCursor" then
-- 让卡牌跟随玩家光标
local cardGuid = action["guid"]
local playerColor = action["args"][1]
-- 🔴 关键修复:getGUIDInstance 返回的是 cardInstance 表,需要获取其中的 inGameObj
local cardInstance = getGUIDInstance(cardGuid)
local cardObj = nil
if cardInstance and cardInstance.inGameObj then
-- cardInstance 是包含 inGameObj 字段的表,提取实际对象
cardObj = cardInstance.inGameObj
print("[FollowCursor] 从 cardInstance 提取卡牌对象: " .. cardGuid)
end
if not cardObj then
-- 如果找不到卡牌实例,说明可能在卡组中,直接从 TTS 获取
print("[FollowCursor] 警告: 未找到卡牌实例 " .. cardGuid .. ",尝试直接从 TTS 获取")
cardObj = getObjectFromGUID(cardGuid)
if not cardObj then
print("[FollowCursor] 错误: 无法从 TTS 找到卡牌 " .. cardGuid)
return
end
end
-- 🔴 关键修复:如果卡牌在卡组中,必须先取出才能操作
print("[FollowCursor] 尝试取出卡牌: " .. cardGuid)
local unpackedCard = ensureCardIsUnpacked(cardObj, cardGuid)
if unpackedCard == nil then
print("[FollowCursor] 错误: 无法从卡组中取出卡牌 " .. cardGuid)
return
end
print(unpackedCard)
print("[FollowCursor] 开始追踪卡牌: " .. unpackedCard.guid .. " 玩家: " .. playerColor)
startFollowCursor(unpackedCard, playerColor)
elseif action["action"] == "spawnCard" then
-- 生成一张新卡并可选地跟随光标
local dataId = action["dataId"]
local playerColor = action["args"][1] -- 如果提供,则跟随该玩家光标
print("[SpawnCard] 生成卡牌: " .. dataId)
-- 查找卡牌数据
local cardData = nil
for _, card in ipairs(data) do
if card.id == dataId then
cardData = card
break
end
end
if not cardData then
print("[SpawnCard] 错误: 找不到卡牌数据 " .. dataId)
return
end
-- 确定生成位置
local spawnPos
if playerColor then
-- 如果有playerColor,生成在该玩家手牌附近
spawnPos = (playerColor == "Purple") and {50, 2, -26} or {48, 2, 21}
else
-- 否则默认生成在紫方手牌区
spawnPos = {50, 2, -26}
end
-- 生成卡牌对象
local obj = spawnObject({
type = "CardCustom",
position = spawnPos,
scale = {2*scale, 1, 2*scale},
sound = false,
})
-- 设置卡牌图片
local image = string.format("http://www.justgan.cn/images/card_image_%s.png", dataId)
local desc = string.format("http://www.justgan.cn/images/card_description_%s.png", dataId)
obj.setCustomObject({
face = image,
back = desc
})
obj.addTag("GwentCard")
-- 创建卡牌实例
local player = Player[playerColor or "Purple"]
createCardInstance(obj, cardData, player)
print("[SpawnCard] 卡牌生成成功: " .. obj.guid)
-- 如果指定了playerColor,让卡牌跟随光标
if playerColor then
Wait.frames(function()
print("[SpawnCard] 启动跟随效果")
startFollowCursor(obj, playerColor)
end, 3) -- 等待几帧确保卡牌完全生成
end
end
if action["guid"] then
-- 重新获取 card 变量,避免使用过期的引用
local cardObj = getObjectFromGUID(action["guid"])
local displayCard = cardObj and getGUIDInstance(cardObj.guid)
local displayObj = cardObj
if displayCard and displayObj then
updateDisplay(displayCard, displayObj)
end
end
end
end
end
function insertCard(card, to, index)
if to == "MeleeA" then
table.insert(gameInfo.meleeA, index+1, card) -- 插入到位置+1(lua的index...)
print("inserted")
print(index)
print(gameInfo.meleeA[index+1])
adjustCardsPlace(gameInfo.meleeA, gameInfo.board.zones.meleeA)
elseif to == "RangeA" then
table.insert(gameInfo.rangeA, index+1, card)
print(table[index+1])
adjustCardsPlace(gameInfo.rangeA, gameInfo.board.zones.rangeA)
elseif to == "MeleeB" then
table.insert(gameInfo.meleeB, index+1, card)
adjustCardsPlace(gameInfo.meleeB, gameInfo.board.zones.meleeB)
elseif to == "RangeB" then
table.insert(gameInfo.rangeB, index+1, card)
adjustCardsPlace(gameInfo.rangeB, gameInfo.board.zones.rangeB)
end
end
-- 用于打印表格的辅助函数
function printTable(t, indent)
indent = indent or 0
for k, v in pairs(t) do
if type(v) == 'table' then
print(string.rep(' ', indent) .. k .. ':')
printTable(v, indent + 2)
else
print(string.rep(' ', indent) .. k .. ': ' .. tostring(v))
end
end
end
function loadCardDeck(link, player)
WebRequest.get(link, function(request)
if request.is_error then
print(request.error)
else
count = count+1
print('Deck fetched successfully')
local str = request.text local card_ids = {}
for line in str:gmatch("%S+") do -- 修改正则表达式以匹配非空白字符序列
table.insert(card_ids, line)
end
local cards = data
-- 创建一个从ID到卡片信息的映射表
idToCard = {}
for _, card in ipairs(cards) do
idToCard[card.id] = card
end
-- 遍历 card_ids,只处理那些存在于 cards 中的卡牌
for index, id in ipairs(card_ids) do
if idToCard[id] then
local card = idToCard[id]