-
Notifications
You must be signed in to change notification settings - Fork 25
/
mainloop.lua
2391 lines (2167 loc) · 74.6 KB
/
mainloop.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
require "cards"
require "brackets"
local recipes = recipes
local ceil = math.ceil
local xmutable = require "xmutable"
local generic_text_color = {155, 94, 33, 255}
function wait(n)
n = n or 1
for i=1,n do
coroutine.yield()
end
end
local main_select_boss, main_play, main_go_hard, main_login
local main_mxm, main_register, main_forgot_password
local main_modal_notice, main_select_faction, main_lobby
local main_fight, main_decks, main_xmute
local main_cafe, main_craft, main_dungeon
frames = {}
local frames = frames
local gobacktodungeon
function fmainloop()
local func, arg = main_login, nil
--local func, arg = main_go_hard, nil
while true do
func,arg = func(unpack(arg or {}))
collectgarbage("collect")
end
end
function str_to_deck(s)
s = s:sub(s:find("%d%d%d%d[%dDPC]+")):split("DPC")
local t = {}
t[1] = s[1] + 0
for i=2,#s,2 do
for j=1,s[i]+0 do
t[#t+1] = s[i+1]+0
end
end
return t
end
local file_to_deck = function(s)
file = love.filesystem.newFile("decks/"..s..".txt")
file:open("r")
s = file:read(file:getSize())
file:close()
return str_to_deck(s)
end
local char_ids = {}
local norm_ids = {}
for k,v in pairs(id_to_canonical_card) do
if v.type == "spell" or v.type == "follower" then
norm_ids[#norm_ids+1] = k
end
end
for k,_ in pairs(characters_func) do
char_ids[#char_ids+1] = k
end
local function get_deck()
local t = {}
t[1] = uniformly(char_ids)
for i=2,31 do
t[i] = uniformly(norm_ids)
while Card(t[i]).size == 0 do
t[i] = uniformly(norm_ids)
end
end
return t
end
local function go_hard()
Player.user_act = Player.ai_act
GO_HARD = true
BUFF_COUNTER = 0
wait = function() end
game = Game(get_deck(), get_deck())
end
function main_go_hard()
local strictness = require 'strictness'
for _,t in ipairs({skill_func, spell_func, characters_func}) do
for k,v in pairs(t) do
t[k] = strictness.strictf(v)
end
end
while true do
go_hard()
game:run()
coroutine.yield()
end
end
local from_login = nil
local doing_login = false
function main_login(email, password)
network_init()
email = email or GLOBAL_EMAIL or options.remember_me_email or ""
password = password or GLOBAL_PASSWORD or options.remember_me_password or ""
if not frames.login then
frames.login = {}
local frame = loveframes.Create("frame")
frame:SetName("Let's play the SG~")
frame:SetSize(300, 150)
frame:ShowCloseButton(false)
frame:SetDraggable(false)
frame:Center()
frame:SetState("login")
local text1 = loveframes.Create("text", frame)
text1:SetPos(5, 35)
text1:SetDefaultColor(generic_text_color)
text1:SetText("E-mail")
local textinput1 = loveframes.Create("textinput", frame)
textinput1:SetPos(80, 30)
textinput1:SetWidth(215)
frames.login.email_input = textinput1
local text2 = loveframes.Create("text", frame)
text2:SetPos(5, 65)
text2:SetDefaultColor(generic_text_color)
text2:SetText("Password")
local textinput2 = loveframes.Create("textinput", frame)
textinput2:SetPos(80, 60)
textinput2:SetWidth(215)
textinput2:SetMasked(true)
textinput2:SetMaskChar("*")
frames.login.password_input = textinput2
local loginbutton = loveframes.Create("button", frame)
loginbutton:SetPos(5, 90)
loginbutton:SetWidth(290)
loginbutton:SetText("Login to the SG~")
loginbutton.OnClick = function()
play_button_sound()
email = textinput1:GetText()
password = textinput2:GetText()
net_send({type="login",
email=email,
password=password})
doing_login = true
frames.login.login_button.enabled = false
frames.login.register_button.enabled = false
frames.login.forgot_button.enabled = false
end
frames.login.login_button = loginbutton
local donebutton = loveframes.Create("button", frame)
donebutton:SetPos(5, 120)
donebutton:SetWidth(143)
donebutton:SetText("Register")
donebutton.OnClick = function()
play_button_sound()
from_login = {main_register, {textinput1:GetText(),
textinput2:GetText()}}
end
frames.login.register_button = donebutton
local clearbutton = loveframes.Create("button", frame)
clearbutton:SetPos(152, 120)
clearbutton:SetWidth(143)
clearbutton:SetText("Forgot Password")
clearbutton.OnClick = function()
play_button_sound()
from_login = {main_forgot_password, {textinput1:GetText(),
textinput2:GetText()}}
end
frames.login.forgot_button = clearbutton
end
frames.login.email_input:SetText(email)
frames.login.password_input:SetText(password)
loveframes.SetState("login")
while true do
wait()
if doing_login then
if net_q:len() ~= 0 then
local resp = net_q:pop()
print(json.encode(resp))
if resp.type=="login_result" then
if resp.success then
while true do
wait()
if net_q:len() ~= 0 then
resp = net_q:pop()
if resp.type=="user_data" then
user_data = resp.value
if user_data.collection then
user_data.collection = fix_num_keys(user_data.collection)
end
if user_data.decks then
user_data.decks = map(fix_num_keys, user_data.decks)
end
if user_data.cafe then
user_data.cafe = fix_num_keys(user_data.cafe)
end
doing_login = false
options.remember_me_email = email
options.remember_me_password = password
set_file("options.json", json.encode(options))
from_login = {main_select_faction}
break
end
end
end
else
doing_login = false
from_login = {main_modal_notice,
{"Login failed "..(resp.reason or ":("),
{main_login, {frames.login.email_input:GetText(),
frames.login.password_input:GetText()}}}}
end
end
end
end
if (not doing_login) and from_login then
frames.login.login_button.enabled = true
frames.login.register_button.enabled = true
frames.login.forgot_button.enabled = true
local ret = from_login
from_login = nil
return unpack(ret)
end
end
end
local from_register = nil
local registering = false
function main_register(email, password)
email = email or ""
password = password or ""
if not frames.register then
frames.register = {}
local frame, text1, textinput1, text2, textinput2,
text3, textinput3, backbutton, registerbutton
frame = loveframes.Create("frame")
frame:SetName("Let's register for the SG~")
frame:SetSize(300, 150)
frame:ShowCloseButton(false)
frame:SetDraggable(false)
frame:Center()
frame:SetState("register")
text1 = loveframes.Create("text", frame)
text1:SetPos(5, 35)
text1:SetDefaultColor(generic_text_color)
text1:SetText("Username")
textinput1 = loveframes.Create("textinput", frame)
textinput1:SetPos(80, 30)
textinput1:SetWidth(215)
text2 = loveframes.Create("text", frame)
text2:SetPos(5, 65)
text2:SetDefaultColor(generic_text_color)
text2:SetText("E-mail")
textinput2 = loveframes.Create("textinput", frame)
textinput2:SetPos(80, 60)
textinput2:SetWidth(215)
frames.register.email_input = textinput2
text3 = loveframes.Create("text", frame)
text3:SetPos(5, 95)
text3:SetDefaultColor(generic_text_color)
text3:SetText("Password")
textinput3 = loveframes.Create("textinput", frame)
textinput3:SetPos(80, 90)
textinput3:SetWidth(215)
textinput3:SetMasked(true)
textinput3:SetMaskChar("*")
frames.register.password_input = textinput3
backbutton = loveframes.Create("button", frame)
backbutton:SetPos(5, 120)
backbutton:SetWidth(143)
backbutton:SetText("Back")
backbutton.OnClick = function()
play_cancel_sound()
from_register = {main_login, {textinput2:GetText(), textinput3:GetText()}}
end
frames.register.back_button = backbutton
registerbutton = loveframes.Create("button", frame)
registerbutton:SetPos(152, 120)
registerbutton:SetWidth(143)
registerbutton:SetText("Register forrealz")
registerbutton.OnClick = function(self)
play_button_sound()
net_send({type="register",
username=textinput1:GetText(),
email=textinput2:GetText(),
password=textinput3:GetText()})
registering = true
frames.register.register_button.enabled = false
frames.register.back_button.enabled = false
end
frames.register.register_button = registerbutton
end
frames.register.email_input:SetText(email)
frames.register.password_input:SetText(password)
loveframes.SetState("register")
while true do
wait()
if registering then
if net_q:len() ~= 0 then
local resp = net_q:pop()
print(json.encode(resp))
if resp.type=="register_result" then
frames.register.register_button.enabled = true
frames.register.back_button.enabled = true
registering = false
if resp.success then
from_register = {main_modal_notice, {"Registration succeeded~",
{main_login, {frames.register.email_input:GetText(),
frames.register.password_input:GetText()}}}}
else
from_register = {main_modal_notice, {"Registration failed :(\n" .. resp.why,
{main_register, {frames.register.email_input:GetText(),
frames.register.password_input:GetText()}}}}
end
end
end
elseif from_register then
local ret = from_register
from_register = nil
return unpack(ret)
end
end
end
local from_forgot_password = nil
function main_forgot_password(email, password)
email = email or ""
password = password or ""
if not frames.forgot_password then
frames.forgot_password = {}
local frame = loveframes.Create("frame")
frame:SetName("Too bad you forgot your password for the SG~")
frame:SetSize(300, 90)
frame:ShowCloseButton(false)
frame:SetDraggable(false)
frame:Center()
frame:SetState("forgot_password")
local text1 = loveframes.Create("text", frame)
text1:SetPos(5, 35)
text1:SetDefaultColor(generic_text_color)
text1:SetText("E-mail")
local textinput1 = loveframes.Create("textinput", frame)
textinput1:SetPos(80, 30)
textinput1:SetWidth(215)
frames.forgot_password.email_input = textinput1
local donebutton = loveframes.Create("button", frame)
donebutton:SetPos(5, 60)
donebutton:SetWidth(143)
donebutton:SetText("Back")
donebutton.OnClick = function()
play_cancel_sound()
from_forgot_password = {main_login, {textinput1:GetText(),
frames.forgot_password.password}}
end
local clearbutton = loveframes.Create("button", frame)
clearbutton:SetPos(152, 60)
clearbutton:SetWidth(143)
clearbutton:SetText("Request New Password")
clearbutton.OnClick = function()
play_button_sound()
local modal = loveframes.Create("frame")
modal:SetName("sorry-m9")
modal:SetSize(300, 120)
modal:ShowCloseButton(false)
modal:SetDraggable(false)
modal:Center()
modal:SetState("forgot_password")
modal:SetModal(true)
local modaltext = loveframes.Create("text", modal)
modaltext:SetDefaultColor(generic_text_color)
modaltext:SetText("Password reset is not implemented :(")
modaltext:Center()
modaltext:SetY(35)
local modaltext2 = loveframes.Create("text", modal)
modaltext2:SetDefaultColor(generic_text_color)
modaltext2:SetText("Email [email protected] for help")
modaltext2:Center()
modaltext2:SetY(65)
local loginbutton = loveframes.Create("button", modal)
loginbutton:SetPos(5, 90)
loginbutton:SetWidth(290)
loginbutton:SetText("Back")
loginbutton.OnClick = function()
play_cancel_sound()
modal:Remove()
from_forgot_password = {main_login, {textinput1:GetText(),
frames.forgot_password.password}}
end
end
end
frames.forgot_password.password = password
frames.forgot_password.email_input:SetText(email)
loveframes.SetState("forgot_password")
while true do
wait()
if from_forgot_password then
local ret = from_forgot_password
from_forgot_password = nil
return unpack(ret)
end
end
end
local from_modal_notice = nil
function main_modal_notice(text, to_ret)
if not frames.modal_notice then
frames.modal_notice = {}
local frame = loveframes.Create("frame")
frame:SetName("Notice~")
frame:SetSize(300, 100)
frame:ShowCloseButton(false)
frame:SetDraggable(false)
frame:Center()
frame:SetState("modal_notice")
local text1 = loveframes.Create("text", frame)
text1:SetDefaultColor(generic_text_color)
frames.modal_notice.text = text1
local okbutton = loveframes.Create("button", frame)
okbutton:SetPos(5, 70)
okbutton:SetWidth(290)
frames.modal_notice.ok_button = okbutton
end
frames.modal_notice.text:SetText(text)
frames.modal_notice.text:Center()
frames.modal_notice.text:SetY(35)
frames.modal_notice.ok_button:SetText("OK")
frames.modal_notice.ok_button.OnClick = function()
play_button_sound()
from_modal_notice = to_ret
end
loveframes.SetState("modal_notice")
while true do
wait()
if from_modal_notice then
local ret = from_modal_notice
from_modal_notice = nil
return unpack(ret)
end
end
end
function rewards(data)
local close = false
-- make outer frame
local frame = loveframes.Create("frame")
frame:SetState("playing")
play_bgm("rewards")
frame:SetName("Rewards!")
frame:SetSize(500, 300)
frame:ShowCloseButton(false)
frame:SetDraggable(false)
frame:SetModal(true)
loveframes.modalobject.modalbackground:SetState("playing")
frame:Center()
-- make text in frame
local text1 = loveframes.Create("text", frame)
text1:SetDefaultColor(generic_text_color)
text1:SetText("Rewards:")
text1:Center()
text1:SetY(35)
-- make okbutton that sets 'close' to true
local okbutton = loveframes.Create("button", frame)
okbutton:SetWidth(150)
okbutton:CenterX()
okbutton:SetY(250)
okbutton:SetText("OK!")
okbutton.OnClick = function()
play_button_sound()
close = true
end
-- spit out rewards received from msg. if there are too many rewards, let it scroll
local rewards_list = loveframes.Create("list", frame)
function rewards_list:Draw() end
local test_button = card_list_button(300001, false, 1, function() end)
local card_width = test_button:GetWidth()
local spacing = 5
local ncards = 0
rewards_list:SetHeight(test_button:GetHeight())
rewards_list:EnableHorizontalStacking(true)
rewards_list:SetSpacing(spacing)
for i, v in pairs(data) do
ncards = ncards + 1
end
if ncards < 1 then
rewards_list:Remove()
end
local width = math.min(ncards * card_width + (ncards - 1) * spacing, spacing * 4 + card_width * 5 + 15) -- 15 is scrollbar width
rewards_list:SetWidth(width)
rewards_list:CenterX()
rewards_list:CenterY()
for i, v in pairs(data) do
rewards_list:AddItem(card_list_button(i, false, v, function() end))
end
-- sit around and wait until 'close' is true, then remove this frame
while true do
if close == true then
frame:Remove()
break
end
wait()
end
end
function main_select_faction()
loveframes.SetState("select_faction")
if user_data.active_deck then
return main_lobby
end
if not frames.select_faction then
frames.select_faction = {}
for idx,faction in ipairs({"D","V","A","C"}) do
idx = idx - 1
local button = faction_button(faction, 16+(180+16)*idx, 165)
button:SetState("select_faction")
end
end
loveframes.SetState("select_faction")
while true do
wait()
if user_data.active_deck then
return main_lobby
end
end
end
local from_lobby = nil
function main_lobby()
if not frames.lobby then
frames.lobby = {}
local frame, text, textinput
local chatWidth = 470
local chatHeight = 560
frame = loveframes.Create("frame")
frame:SetName("Let's talk about the SG~~")
frame:SetSize(chatWidth, chatHeight)
frame:SetPos(20,20)
frame:ShowCloseButton(false)
frame:SetDraggable(false)
frame:SetState("lobby")
text = loveframes.Create("textinput", frame)
text:SetMultiline(true)
text:SetAutoScroll(true)
text.linenumbers = false
text:SetSize(chatWidth-10, chatHeight-65)
text:SetPos(5, 30)
text:SetText("")
text:SetLimit(200)
text:SetEditable(false)
frames.lobby.text = text
textinput = loveframes.Create("textinput", frame)
textinput:SetWidth(chatWidth-10)
textinput:Center()
textinput:SetY(chatHeight-30)
function textinput:OnEnter()
local text = self:GetText()
self:Clear()
net_send({type="general_chat",text=text})
end
make_player_info(frame)
-- === Create Menubar and Lobby Buttons === --
frames.lobby.game_buttons = {}
--tried4's TODO: don't hardcode frame position and size
local menuX = 495
local menuY = 0
local offsetX = 13
local offsetY = 105
local spacing = 57
local button = make_menubar(menuX,menuY)
--table.insert(frames.lobby.game_buttons, button)
local button = menu_dungeon_button(menuX+offsetX,menuY+offsetY)
button.OnClick = function()
play_button_sound()
from_lobby = {main_dungeon}
end
table.insert(frames.lobby.game_buttons, button)
local button = menu_fight_button(menuX+offsetX,menuY+offsetY+spacing)
button.OnClick = function()
play_button_sound()
net_send({type="join_fight"})
local bracket = deck_to_bracket(user_data.decks[user_data.active_deck])
net_send({type="general_chat",text="[ Public Msg ] " .. user_data.username .. " is looking for a " .. bracket .. "DP fite!"})
end
table.insert(frames.lobby.game_buttons, button)
--[[
local button = loveframes.Create("button")
button:SetPos(50,0)
button:SetSize(50, 50)
button:SetText("NOLD")
button:SetState("lobby")
button.OnClick = function()
net_send({type="dungeon", idx=1})
end
table.insert(frames.lobby.game_buttons, button)
local button = loveframes.Create("button")
button:SetPos(100,0)
button:SetSize(50, 50)
button:SetText("BUNNY")
button:SetState("lobby")
button.OnClick = function()
net_send({type="dungeon", idx=2})
end
table.insert(frames.lobby.game_buttons, button)
local button = loveframes.Create("button")
button:SetPos(150,0)
button:SetSize(50, 50)
button:SetText("GARTS")
button:SetState("lobby")
button.OnClick = function()
net_send({type="dungeon", idx=3})
end
table.insert(frames.lobby.game_buttons, button)
local button = loveframes.Create("button")
button:SetPos(200,0)
button:SetSize(50, 50)
button:SetText("GINGER")
button:SetState("lobby")
button.OnClick = function()
net_send({type="dungeon", idx=4})
end
table.insert(frames.lobby.game_buttons, button)
local button = loveframes.Create("button")
button:SetPos(250,0)
button:SetSize(50, 50)
button:SetText("LAEV")
button:SetState("lobby")
button.OnClick = function()
net_send({type="dungeon", idx=5})
end
table.insert(frames.lobby.game_buttons, button)
local button = loveframes.Create("button")
button:SetPos(300,0)
button:SetSize(50, 50)
button:SetText("SIGMA")
button:SetState("lobby")
button.OnClick = function()
net_send({type="dungeon", idx=7})
end
table.insert(frames.lobby.game_buttons, button)
]]
local button = loveframes.Create("button")
button:SetPos(menuX, 530)
button:SetSize(92, 50)
button:SetText("OPTIONS")
button:SetState("lobby")
button.OnClick = function()
play_button_sound()
from_lobby = {main_options}
end
-- table.insert(frames.lobby.game_buttons, button)
-- == Lobby Buttons, continued == --
local button = menu_cafe_button(menuX+offsetX-3,menuY+offsetY+spacing*2)
button.OnClick = function()
play_button_sound()
if frames.cafe then
frames.cafe.populate_cafe_card_list()
frames.cafe.update_feeding_list()
frames.cafe.refresh_stats_pane()
end
from_lobby = {main_cafe}
end
-- table.insert(frames.lobby.game_buttons, button)
local button = menu_deck_button(menuX+offsetX-2,menuY+offsetY+spacing*3-5)
button.OnClick = function()
play_button_sound()
from_lobby = {main_decks}
end
-- table.insert(frames.lobby.game_buttons, button)
local button = menu_craft_button(menuX+offsetX-2,menuY+offsetY+spacing*4-10)
button.OnClick = function()
play_button_sound()
from_lobby = {main_craft}
end
-- table.insert(frames.lobby.game_buttons, button)
local button = menu_xmute_button(menuX+offsetX-2,menuY+offsetY+spacing*5-15)
button.OnClick = function()
play_button_sound()
if frames.xmute then
frames.xmute.xmute_type = nil
frames.xmute.populate_xmutable_card_list()
end
from_lobby = {main_xmute}
end
-- table.insert(frames.lobby.game_buttons, button)
local deck_warn_text = loveframes.Create("text")
deck_warn_text:SetText({{color = {215, 0, 0, 255}}, "Active deck is invalid! Needs 31 cards!"})
deck_warn_text:SetY(5)
deck_warn_text:CenterX()
deck_warn_text:SetVisible(false)
deck_warn_text:SetState("lobby")
frames.lobby.deck_warn_text = deck_warn_text
end
local enable_buttons = check_active_deck()
for _,button in ipairs(frames.lobby.game_buttons) do
button:SetEnabled(enable_buttons)
end
frames.lobby.deck_warn_text:SetVisible(not enable_buttons)
loveframes.SetState("lobby")
play_bgm("lobby")
-- goes back to dungeon select screen after a dungeon battle
if gobacktodungeon then
gobacktodungeon = false
from_lobby = {main_dungeon}
end
while true do
wait()
if net_q:len() ~= 0 then
local msg = net_q:pop()
if msg.type=="game_start" then
-- check if dungeon, prepare to return to dungeon select screen if so
if from_dungeon then
gobacktodungeon = true
frames.dungeon = from_dungeon
from_dungeon = false
end
from_lobby = {main_fight, {msg}}
end
end
if from_lobby then
local ret = from_lobby
from_lobby = nil
return unpack(ret)
end
end
end
local function update_deck(deck, diff)
for k,v in pairs(diff) do
deck[k] = (deck[k] or 0) + v
if deck[k] == 0 then
deck[k] = nil
end
end
end
local function collection_ex_deck(coll, deck)
local ret = {}
for k,v in pairs(coll) do
ret[k] = v - (deck[k] or 0)
if ret[k] == 0 then
ret[k] = nil
end
end
return ret
end
local function deck_cmp(a, b)
-- a<b
a, b = tostring(a), tostring(b)
if a[1]==b[1] then
return tonumber(a)<tonumber(b)
end
if a[1] == "1" then return true end
if b[1] == "1" then return false end
if a[1] == "3" then return true end
return false
end
local function name_cmp(a, b)
return id_to_canonical_card[a].name:lower() <
id_to_canonical_card[b].name:lower()
end
local from_craft = nil
function main_craft()
if not frames.craft then
frames.craft = {}
frames.craft.page_num = 1
frames.craft.stack = {}
local list, name, stats, text, quote = get_hover_list_text("craft")
frames.craft.card_text_list = list
frames.craft.card_text = {name, stats, text, quote}
local lobby_button = loveframes.Create("button")
lobby_button:SetState("craft")
lobby_button:SetY(list:GetY()+list:GetHeight()+5)
lobby_button:SetX(list:GetX())
lobby_button:SetWidth(list:GetWidth())
lobby_button:SetText("Lobby")
lobby_button:SetHeight(600-field_y-5-lobby_button:GetY())
function lobby_button:OnClick()
play_cancel_sound()
from_craft = {main_lobby}
end
local craft_pane = loveframes.Create("frame")
craft_pane:SetState("craft")
local x,y,w,h = left_hover_frame_pos()
craft_pane:SetPos(x,y)
craft_pane:SetSize(w,h)
craft_pane:ShowCloseButton(false)
craft_pane:SetDraggable(false)
craft_pane.Draw = function(self)
draw_hover_frame(self.x, self.y, self.width, self.height, "Lab")
end
local text_card_list = loveframes.Create("list", craft_pane)
text_card_list:SetWidth(w-12)
text_card_list:Center()
text_card_list:SetY(64)
text_card_list:SetHeight(508)
text_card_list:SetPadding(2)
text_card_list:SetSpacing(0)
function text_card_list:Draw() end
function frames.craft.update_list()
local substr = ""
if craft_search_bar then substr = craft_search_bar:GetText() end
if substr ~= "" then
frames.craft.populate_text_card_list(recipes, substr, true)
frames.craft.populate_card_list(recipes, substr)
else
frames.craft.populate_text_card_list(recipes)
frames.craft.populate_card_list(recipes)
end
end
function frames.craft.spawn_craft_frame(id)
local str_len_limit = 32
if frames.craft.craft_frame then
return
end
local name = id_to_canonical_card[id].name
name = string.len(name) < str_len_limit and name
or string.sub(name, 1, str_len_limit).."…"
local frame = loveframes.Create("frame")
frames.craft.craft_frame = frame
frame:SetName("Let's craft the "..name)
frame:SetSize(400, 400)
frame:ShowCloseButton(false)
frame:SetDraggable(false)
frame:SetState("craft")
frame:SetModal(true)
loveframes.modalobject.modalbackground:SetState("craft")
frame:Center()
local in_list = loveframes.Create("list", frame)
local test_button = card_list_button(300001, false, 1, function() end)
local card_width = test_button:GetWidth()
local card_height = test_button:GetHeight()
local spacing = 5
local ncards = 0
in_list:EnableHorizontalStacking(true)
in_list:SetSpacing(spacing)
local width = spacing * 3 + card_width * 4
local height = spacing + card_height * 2
in_list:SetWidth(width)
in_list:SetHeight(height)
frame:SetWidth(width+2*spacing+2)
frame:SetHeight(30 + height + spacing+card_height+spacing+1)
frame:Center()
for i, v in pairs(recipes[id]) do
local coll_amt = frames.craft.collection[i] or 0
local gray = coll_amt < v
in_list:AddItem(card_list_button(i, gray, coll_amt.."/"..v,
function()
if recipes[i] then
local stack = frames.craft.stack
stack[#stack+1] = id
frame:SetModal(false)
frame:Remove()
frames.craft.craft_frame = nil
frames.craft.spawn_craft_frame(i)
end
end))
ncards = ncards + 1
end
in_list:CenterX()
in_list:SetY(30)
function in_list.Draw() end
local out_card = card_list_button(id, false, nil, function() end)
out_card:SetParent(frame)
out_card:SetY(30+2*spacing+2*card_height)
out_card:SetX(math.floor(spacing+1+(card_width+spacing)/2))
local back_button
local craft_button = loveframes.Create("button", frame)
craft_button:SetText("Craft!")
craft_button:SetWidth(card_width)
craft_button:SetHeight(card_height/2)
craft_button:SetX(out_card:GetStaticX()+spacing+card_width)
craft_button:SetY(out_card:GetStaticY()+card_height/4)
craft_button.OnClick = function()
play_button_sound()
net_send({type="craft", id=id})
function frames.craft.enable_buttons()
craft_button:SetEnabled(true)
back_button:SetEnabled(true)
frames.craft.collection = collection_ex_deck(
user_data.collection, union_counters(user_data.decks))
for _,button in pairs(in_list.children) do
button:SetEnabled(true)
local have_amt = frames.craft.collection[button.card_id] or 0
local req_amt = recipes[id][button.card_id]
button:set_count(have_amt.."/"..req_amt)
button:set_gray(have_amt < req_amt)
end
for k,v in pairs(recipes[id]) do
if (frames.craft.collection[k] or 0) < v then
craft_button:SetEnabled(false)
break
end
end
frames.craft.enable_buttons = nil
end
craft_button:SetEnabled(false)
back_button:SetEnabled(false)
for _,button in pairs(in_list.children) do
button:SetEnabled(false)
end
end
for k,v in pairs(recipes[id]) do
if (frames.craft.collection[k] or 0) < v then
craft_button:SetEnabled(false)
break
end
end
back_button = loveframes.Create("button", frame)
back_button:SetText("Close")
back_button:SetWidth(card_width)
back_button:SetHeight(card_height/2)
back_button:SetX(out_card:GetStaticX()+2*spacing+2*card_width)
back_button:SetY(out_card:GetStaticY()+card_height/4)
back_button.OnClick = function()
play_cancel_sound()