-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathBaseConflict.Api.Deckbuilding.pas
839 lines (743 loc) · 22.6 KB
/
BaseConflict.Api.Deckbuilding.pas
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
unit BaseConflict.Api.Deckbuilding;
interface
uses
// System
System.SysUtils,
System.Math,
Generics.Defaults,
// Engine
Engine.Log,
Engine.Helferlein,
Engine.Helferlein.Windows,
Engine.Helferlein.DataStructures,
Engine.Helferlein.Threads,
Engine.Network.RPC,
Engine.DataQuery,
Engine.dXML,
// Game
BaseConflict.Constants.Cards,
BaseConflict.Api,
BaseConflict.Api.Types,
BaseConflict.Api.Cards;
type
{$RTTI EXPLICIT METHODS([vcPublished, vcPublic, vcPrivate]) FIELDS([vcPublic, vcProtected]) PROPERTIES([vcPublic, vcPublished])}
{$M+}
TDeckManager = class;
TDeck = class;
ProcDeckChanged = reference to procedure(const Sender : TDeck);
TDeckCard = class
strict private
FOwner : TDeck;
FCardInstance : TCardInstance;
FSkin : TCardSkin;
FID : integer;
procedure SetSkin(const Value : TCardSkin); virtual;
protected
function Clone : TDeckCard;
function GetData : RDeckCard;
class function GetEmptyData : RDeckCard;
published
property ID : integer read FID;
property CardInstance : TCardInstance read FCardInstance;
property Skin : TCardSkin read FSkin write SetSkin;
public
function CardInfo : TCardInfo;
function SlotIndex : integer;
constructor Create(Owner : TDeck; const DeckCard : RDeckCard); overload;
constructor Create(Owner : TDeck; CardInstance : TCardInstance); overload;
end;
TDeck = class
private const
DECKSLOT_COUNT = 12;
private
FID : integer;
FCardslots : TUltimateObjectList<TDeckCard>;
FDeckbuildingManager : TDeckManager;
FOnChange : ProcDeckChanged;
FFormerDeck : TUltimateObjectList<TDeckCard>;
// methods to load data from api and save to
function GetDeckData : RDeck;
procedure SignalChange();
constructor CreateWithData(const DeckData : RDeck; DeckbuildingManager : TDeckManager);
constructor CreateEmpty(DeckbuildingManager : TDeckManager);
procedure SortDeckList(DeckList : TUltimateObjectList<TDeckCard>);
function CloneCardslots : TUltimateObjectList<TDeckCard>;
function StartDeckUpdate : TUltimateObjectList<TDeckCard>;
procedure SubmitDeckUpdate(const NewDeck : TUltimateObjectList<TDeckCard>);
strict private
FName, FIcon : string;
FNew : boolean;
procedure SetName(const Value : string); virtual;
procedure SetIcon(const Value : string); virtual;
procedure SetNew(const Value : boolean); virtual;
published
/// <summary> Name of the deck.</summary>
property name : string read FName write SetName;
/// <summary> Iconidentifier of the deck.</summary>
property Icon : string read FIcon write SetIcon;
/// <summary> Is the deck shown as new.</summary>
property New : boolean read FNew write SetNew;
property Cards : TUltimateObjectList<TDeckCard> read FCardslots;
/// <summary> The different colors used in this deck. </summary>
[dXMLDependency('.Cards')]
function Colors : SetEntityColor;
/// <summary> The number of different colors used in this deck. </summary>
[dXMLDependency('.Colors')]
function ColorCount : integer;
/// <summary> The league of this deck, which depends on the maximum card league. </summary>
[dXMLDependency('.Cards.CardInstance.League')]
function League : integer;
/// <summary> Adds this card to the deck and resort the deck. If card is already in the deck, nothing happens </summary>
procedure AddCard(const Card : TCardInstance);
/// <summary> Removes this card from the deck and resort the deck. If card is not part of the deck, nothing happens. </summary>
procedure RemoveCard(const Card : TDeckCard);
public
/// <summary> Called if any data of the deck has changed.</summary>
property OnChange : ProcDeckChanged read FOnChange write FOnChange;
/// <summary> Unique ID of this deck, this id connect the deck to instance on server.</summary>
property ID : integer read FID;
/// <summary> Returns whether this card card could be put into the deck or not. </summary>
[dXMLDependency('.Cards')]
function CanAddCard(const CardInstance : TCardInstance) : boolean;
/// <summary> Returns whether this card is in the deck or not. </summary>
[dXMLDependency('.Cards')]
function ContainsCard(const CardInstance : TCardInstance) : boolean;
/// <summary> Returns whether this card can be added regarding the color restriction. </summary>
[dXMLDependency('.Cards')]
function ColorCheckCard(const CardInstance : TCardInstance) : boolean;
[dXMLDependency('.Cards')]
/// <summary> Returns whether this deck already have an epic card. </summary>
function EpicCheckCard(const CardInstance : TCardInstance) : boolean;
/// <summary> Returns whether this deck is filled up with cards. </summary>
[dXMLDependency('.Cards')]
function IsFull : boolean;
/// <summary> Returns whether this deck has no cards in it. </summary>
[dXMLDependency('.Cards')]
function IsEmpty : boolean;
/// <summary> Returns whether this slot is free. Returns false for slots not in range. </summary>
function IsSlotFree(SlotIndex : integer) : boolean;
/// <summary> Returns the count of cardslots. The count is predefinded and controlled by user and can't changed.</summary>
function CardSlotCount : integer;
/// <summary> Deletes the deck on server and remove it from deckmanager.</summary>
procedure Delete();
/// <summary> Sets new to false. </summary>
procedure HasBeenSeen;
destructor Destroy; override;
end;
TDeckManager = class(TInterfacedObject, IDeckbuildingBackchannel)
private
procedure LoadDecks(const Decks : ARDeck);
// backchannel
procedure DeckCreated(deck_data : RDeck);
strict private
FDecks : TUltimateObjectList<TDeck>;
published
property Decks : TUltimateObjectList<TDeck> read FDecks;
[dXMLDependency('.Decks.New')]
function HasAnyNewDeck : boolean;
public
/// <summary> Created a new deck and return instance. Any changes on deck will
/// displayed instantly but will not send to server until deck creation is finsihed. Will automtically set
/// deck to editing mode. </summary>
function CreateNewDeck : TDeck;
function IsCardInAnyDeck(const CardInstance : TCardInstance) : boolean;
/// <summary> Returns the first non-empty deck of the user. Returns nil if none found. </summary>
function GetDefaultDeck : TDeck;
constructor Create;
destructor Destroy; override;
end;
{$REGION 'Actions'}
/// <summary> Parent class for any action targeting deckmanager.</summary>
TDeckManagerAction = class(TPromiseAction)
private
FDeckManager : TDeckManager;
public
constructor Create(DeckManager : TDeckManager);
end;
[AQCriticalAction]
TDeckManagerActionLoadDecks = class(TDeckManagerAction)
public
function Execute : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TDeckManagerActionCreateDeck = class(TDeckManagerAction)
private
FCreatedDeck : TDeck;
public
property CreatedDeck : TDeck read FCreatedDeck;
procedure Emulate; override;
function Execute : boolean; override;
procedure Rollback; override;
end;
/// <summary> Parent class for any action targeting deck. Will save deck on execute to server.</summary>
TDeckAction = class(TPromiseAction)
private
FDeck : TDeck;
FCleanedName : string;
public
constructor Create(deck : TDeck);
/// <summary> Sends the complete deck to server.</summary>
function Execute : boolean; override;
function ExecuteSynchronized : boolean; override;
end;
TDeckActionChangeName = class(TDeckAction)
private
FOldName, FNewName : string;
public
constructor Create(deck : TDeck; NewName : string);
procedure Emulate; override;
procedure Rollback; override;
end;
TDeckActionChangeIcon = class(TDeckAction)
private
FOldIcon, FNewIcon : string;
public
constructor Create(deck : TDeck; NewIcon : string);
procedure Emulate; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TDeckActionChangeDeck = class(TDeckAction)
private
FOldDeck : TUltimateObjectList<TDeckCard>;
FNewDeck : TUltimateObjectList<TDeckCard>;
public
/// <summary> Both lists are managed by the action. </summary>
constructor Create(deck : TDeck; OldDeck, NewDeck : TUltimateObjectList<TDeckCard>);
procedure Emulate; override;
procedure Rollback; override;
destructor Destroy; override;
end;
[AQCriticalAction]
TDeckActionDelete = class(TDeckAction)
public
procedure Emulate; override;
function Execute : boolean; override;
procedure Rollback; override;
end;
{$ENDREGION}
{$M-}
{$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished]) FIELDS([vcPrivate, vcProtected, vcPublic])}
var
Deckbuilding : TDeckManager;
implementation
{ TDeck }
constructor TDeck.CreateWithData(const DeckData : RDeck; DeckbuildingManager : TDeckManager);
var
i : integer;
begin
CreateEmpty(DeckbuildingManager);
FName := DeckData.Name;
FID := DeckData.ID;
FIcon := DeckData.icon_identifier;
// fill deck with content, because content has to be unlocked by user, get a copy from
// deckbuildingmanager
assert(Cards.Count = length(DeckData.Cards));
for i := 0 to min(Cards.Count, length(DeckData.Cards)) - 1 do
begin
if DeckData.Cards[i].card_id > 0 then
begin
FCardslots[i] := TDeckCard.Create(self, DeckData.Cards[i]);
end
else
FCardslots[i] := nil;
end;
end;
function TDeck.ColorCheckCard(const CardInstance : TCardInstance) : boolean;
var
Color : EnumEntityColor;
NewDeckColors : SetEntityColor;
ColorCount : integer;
begin
NewDeckColors := Colors + CardInstance.CardInfo.CardColors;
NewDeckColors := NewDeckColors - [ecColorless];
ColorCount := 0;
for Color in NewDeckColors do
ColorCount := ColorCount + 1;
Result := ColorCount <= 2;
end;
function TDeck.ColorCount : integer;
var
Color : EnumEntityColor;
begin
Result := 0;
for Color in Colors do
if Color <> ecColorless then
Result := Result + 1;
end;
function TDeck.Colors : SetEntityColor;
var
i : integer;
begin
Result := [];
for i := 0 to FCardslots.Count - 1 do
if assigned(FCardslots[i]) and not(FCardslots[i].CardInstance.CardInfo.CardColors <= Result) then
Result := Result + FCardslots[i].CardInstance.CardInfo.CardColors;
end;
function TDeck.ContainsCard(const CardInstance : TCardInstance) : boolean;
begin
Result := Cards.Extra.Any(
function(Instance : TDeckCard) : boolean
begin
Result := assigned(Instance) and (Instance.CardInstance = CardInstance);
end);
end;
constructor TDeck.CreateEmpty(DeckbuildingManager : TDeckManager);
var
i : integer;
begin
FDeckbuildingManager := DeckbuildingManager;
FCardslots := TUltimateObjectList<TDeckCard>.Create;
for i := 0 to DECKSLOT_COUNT - 1 do FCardslots.Add(nil);
FID := -1;
end;
procedure TDeck.Delete;
begin
MainActionQueue.DoAction(TDeckActionDelete.Create(self));
end;
destructor TDeck.Destroy;
begin
FCardslots.Free;
inherited;
end;
function TDeck.EpicCheckCard(const CardInstance : TCardInstance) : boolean;
begin
Result := True;
if CardInstance.CardInfo.IsEpic then
begin
Result := not Cards.Extra.Any(
function(Instance : TDeckCard) : boolean
begin
Result := assigned(Instance) and Instance.CardInfo.IsEpic;
end);
end;
end;
procedure TDeck.AddCard(const Card : TCardInstance);
var
NewDeck : TUltimateObjectList<TDeckCard>;
i : integer;
begin
if assigned(Card) and not IsFull and CanAddCard(Card) then
begin
NewDeck := StartDeckUpdate;
for i := 0 to NewDeck.Count - 1 do
if NewDeck[i] = nil then
begin
NewDeck[i] := TDeckCard.Create(self, Card);
break;
end;
SubmitDeckUpdate(NewDeck);
end;
end;
function TDeck.CanAddCard(const CardInstance : TCardInstance) : boolean;
begin
Result := not ContainsCard(CardInstance) and ColorCheckCard(CardInstance) and EpicCheckCard(CardInstance);
end;
function TDeck.CardSlotCount : integer;
begin
Result := Cards.Count;
end;
function TDeck.CloneCardslots : TUltimateObjectList<TDeckCard>;
var
i : integer;
begin
Result := TUltimateObjectList<TDeckCard>.Create();
for i := 0 to Cards.Count - 1 do
begin
if assigned(Cards[i]) then
Result.Add(Cards[i].Clone)
else
Result.Add(nil);
end;
end;
function TDeck.GetDeckData : RDeck;
var
i : integer;
begin
assert(FID <> -1);
Result.ID := FID;
Result.Name := name;
Result.icon_identifier := Icon;
assert(CardSlotCount = FCardslots.Count);
setlength(Result.Cards, CardSlotCount);
for i := 0 to CardSlotCount - 1 do
begin
if FCardslots[i] <> nil then
Result.Cards[i] := FCardslots[i].GetData
else
Result.Cards[i] := TDeckCard.GetEmptyData;
end;
end;
procedure TDeck.HasBeenSeen;
begin
self.New := False;
end;
function TDeck.IsEmpty : boolean;
begin
Result := not Cards.Extra.Any(
function(Instance : TDeckCard) : boolean
begin
Result := assigned(Instance);
end);
end;
function TDeck.IsFull : boolean;
begin
Result := Cards.Extra.All(
function(Instance : TDeckCard) : boolean
begin
Result := assigned(Instance);
end);
end;
function TDeck.IsSlotFree(SlotIndex : integer) : boolean;
begin
Result := InRange(SlotIndex, 0, CardSlotCount - 1) and not assigned(FCardslots[SlotIndex]);
end;
function TDeck.League : integer;
var
i : integer;
begin
Result := 1; // if no card contained take 1
for i := 0 to Cards.Count - 1 do
if assigned(Cards[i]) then Result := Max(Result, Cards[i].CardInstance.League);
end;
procedure TDeck.RemoveCard(const Card : TDeckCard);
var
index : integer;
NewDeck : TUltimateObjectList<TDeckCard>;
begin
if assigned(Card) and Cards.Query.TryGetIndex(F('.') = Card, index) then
begin
NewDeck := StartDeckUpdate;
NewDeck[index] := nil;
SubmitDeckUpdate(NewDeck);
end;
end;
procedure TDeck.SetIcon(const Value : string);
begin
if MainActionQueue.IsActive then FIcon := Value
else MainActionQueue.DoAction(TDeckActionChangeIcon.Create(self, Value));
end;
procedure TDeck.SetName(const Value : string);
begin
if MainActionQueue.IsActive then FName := Value
else MainActionQueue.DoAction(TDeckActionChangeName.Create(self, Value));
end;
procedure TDeck.SetNew(const Value : boolean);
begin
FNew := Value;
end;
procedure TDeck.SignalChange;
begin
FDeckbuildingManager.Decks.SignalItemChanged(self);
if assigned(OnChange) then OnChange(self);
end;
procedure TDeck.SortDeckList(DeckList : TUltimateObjectList<TDeckCard>);
begin
DeckList.Sort(TComparer<TDeckCard>.Construct(
function(const Left, Right : TDeckCard) : integer
var
LCardInfo, RCardInfo : TCardInfo;
begin
if assigned(Left) then
LCardInfo := Left.CardInfo
else
LCardInfo := nil;
if assigned(Right) then
RCardInfo := Right.CardInfo
else
RCardInfo := nil;
Result := TCardInfo.Compare(LCardInfo, RCardInfo);
end));
end;
function TDeck.StartDeckUpdate : TUltimateObjectList<TDeckCard>;
begin
FFormerDeck := CloneCardslots;
Result := CloneCardslots;
end;
procedure TDeck.SubmitDeckUpdate(const NewDeck : TUltimateObjectList<TDeckCard>);
begin
SortDeckList(NewDeck);
MainActionQueue.DoAction(TDeckActionChangeDeck.Create(self, FFormerDeck, NewDeck));
end;
{ TDeckbuildingManager }
constructor TDeckManager.Create;
begin
assert(assigned(CardManager), 'DeckManager depends on CardManager');
FDecks := TUltimateObjectList<TDeck>.Create;
RPCHandlerManager.SubscribeHandler(self);
MainActionQueue.DoAction(TDeckManagerActionLoadDecks.Create(self));
end;
function TDeckManager.CreateNewDeck : TDeck;
var
Action : TDeckManagerActionCreateDeck;
begin
Action := TDeckManagerActionCreateDeck.Create(self);
// this will create the deck, but first emulate the creation
MainActionQueue.DoAction(Action);
Result := Action.CreatedDeck;
end;
procedure TDeckManager.DeckCreated(deck_data : RDeck);
var
deck : TDeck;
begin
if not Decks.Query.Filter(F('ID') = deck_data.ID).Exists then
begin
deck := TDeck.CreateWithData(deck_data, self);
deck.New := True;
deck.SortDeckList(deck.Cards);
Decks.Add(deck);
// this will save the deck translated deckname and sorted cards
deck.Name := HInternationalizer.TranslateTextRecursive('§deck_preset_title_' + deck.Name);
end;
end;
destructor TDeckManager.Destroy;
begin
RPCHandlerManager.UnsubscribeHandler(self);
FDecks.Free;
inherited;
end;
function TDeckManager.GetDefaultDeck : TDeck;
var
i : integer;
begin
for i := 0 to Decks.Count - 1 do
if not Decks[i].IsEmpty then exit(Decks[i]);
Result := nil;
end;
function TDeckManager.HasAnyNewDeck : boolean;
begin
Result := Decks.Query.Filter(F('New') = True).Exists;
end;
function TDeckManager.IsCardInAnyDeck(const CardInstance : TCardInstance) : boolean;
var
i : integer;
begin
Result := False;
for i := 0 to Decks.Count - 1 do
if Decks[i].ContainsCard(CardInstance) then exit(True);
end;
procedure TDeckManager.LoadDecks(const Decks : ARDeck);
var
deck : RDeck;
begin
FDecks.Clear;
for deck in Decks do
begin
FDecks.Add(TDeck.CreateWithData(deck, self));
end;
end;
{ TCreateDeckAction }
procedure TDeckManagerActionCreateDeck.Emulate;
begin
FCreatedDeck := TDeck.CreateEmpty(FDeckManager);
FDeckManager.Decks.Add(FCreatedDeck);
end;
function TDeckManagerActionCreateDeck.Execute : boolean;
var
promise : TPromise<integer>;
begin
promise := DeckBuildingAPI.CreateDeck;
promise.WaitForData;
if promise.WasSuccessful then
FCreatedDeck.FID := promise.Value
else
HandlePromiseError(promise);
Result := promise.WasSuccessful;
promise.Free;
end;
procedure TDeckManagerActionCreateDeck.Rollback;
begin
// Remove it from list and free deck
FDeckManager.Decks.Remove(FCreatedDeck);
end;
{ TChangeNameDeckAction }
constructor TDeckActionChangeName.Create(deck : TDeck; NewName : string);
begin
inherited Create(deck);
FNewName := NewName;
end;
procedure TDeckActionChangeName.Emulate;
begin
FOldName := FDeck.Name;
FDeck.Name := FNewName;
FDeck.SignalChange;
end;
procedure TDeckActionChangeName.Rollback;
begin
FDeck.Name := FOldName;
FDeck.SignalChange;
end;
{ TDeckAction }
constructor TDeckAction.Create(deck : TDeck);
begin
inherited Create();
FDeck := deck;
end;
function TDeckAction.Execute : boolean;
var
promise : TPromise<string>;
begin
promise := DeckBuildingAPI.ChangeDeck(FDeck.ID, FDeck.GetDeckData);
promise.WaitForData;
if not promise.WasSuccessful then
HandlePromiseError(promise)
else
FCleanedName := promise.Value;
Result := promise.WasSuccessful;
promise.Free;
end;
function TDeckAction.ExecuteSynchronized : boolean;
begin
Result := True;
if assigned(FDeck) and (FCleanedName <> FDeck.Name) then
FDeck.Name := FCleanedName;
end;
{ TDeckActionDelete }
procedure TDeckActionDelete.Emulate;
begin
FDeck.FDeckbuildingManager.Decks.Extract(FDeck)
end;
function TDeckActionDelete.Execute : boolean;
var
promise : TPromise<boolean>;
begin
promise := DeckBuildingAPI.DeleteDeck(FDeck.ID);
promise.WaitForData;
if promise.WasSuccessful or (promise.ErrorAsErrorCode = ecUnknownDeck) then
FreeAndNil(FDeck)
else
HandlePromiseError(promise);
Result := promise.WasSuccessful;
promise.Free;
end;
procedure TDeckActionDelete.Rollback;
begin
FDeck.FDeckbuildingManager.Decks.Add(FDeck);
end;
{ TDeckActionChangeDeck }
constructor TDeckActionChangeDeck.Create(deck : TDeck; OldDeck, NewDeck : TUltimateObjectList<TDeckCard>);
begin
inherited Create(deck);
FOldDeck := OldDeck;
FNewDeck := NewDeck;
end;
destructor TDeckActionChangeDeck.Destroy;
begin
FOldDeck.Free;
FNewDeck.Free;
inherited;
end;
procedure TDeckActionChangeDeck.Emulate;
begin
FDeck.Cards.Clear;
FDeck.Cards.AddRange(FNewDeck);
// FNewDeck item ownership is taken over by deck
FNewDeck.OwnsObjects := False;
FDeck.SignalChange;
end;
procedure TDeckActionChangeDeck.Rollback;
begin
FDeck.Cards.Clear;
FDeck.Cards.AddRange(FOldDeck);
// FOldDeck item ownership is taken over by deck
FOldDeck.OwnsObjects := False;
FDeck.SignalChange;
end;
{ TDeckActionChangeIcon }
constructor TDeckActionChangeIcon.Create(deck : TDeck; NewIcon : string);
begin
inherited Create(deck);
FNewIcon := NewIcon;
end;
procedure TDeckActionChangeIcon.Emulate;
begin
FOldIcon := FDeck.Icon;
FDeck.Icon := FNewIcon;
FDeck.SignalChange;
end;
procedure TDeckActionChangeIcon.Rollback;
begin
FDeck.Icon := FOldIcon;
FDeck.SignalChange;
end;
{ TDeckManagerAction }
constructor TDeckManagerAction.Create(DeckManager : TDeckManager);
begin
inherited Create();
FDeckManager := DeckManager;
end;
{ TDeckManagerLoadDecks }
function TDeckManagerActionLoadDecks.Execute : boolean;
var
promise : TPromise<ARDeck>;
begin
promise := DeckBuildingAPI.GetDecks;
promise.WaitForData;
if promise.WasSuccessful then
begin
DoSynchronized(
procedure()
begin
FDeckManager.LoadDecks(promise.Value);
end);
end
else
HandlePromiseError(promise);
Result := promise.WasSuccessful;
promise.Free;
end;
procedure TDeckManagerActionLoadDecks.Rollback;
begin
FDeckManager.Decks.Clear;
end;
{ TDeckCard }
function TDeckCard.CardInfo : TCardInfo;
begin
if assigned(Skin) then
Result := Skin.CardInfo
else
Result := CardInstance.CardInfo;
end;
function TDeckCard.Clone : TDeckCard;
begin
Result := TDeckCard.Create(FOwner, self.GetData);
end;
constructor TDeckCard.Create(Owner : TDeck; const DeckCard : RDeckCard);
begin
FOwner := Owner;
FCardInstance := CardManager.PlayerCards.Query.Get(F('ID') = DeckCard.card_id);
if DeckCard.skin_id > 0 then
FSkin := CardInstance.OriginCard.Skins.Query.Get(F('ID') = DeckCard.skin_id);
end;
constructor TDeckCard.Create(Owner : TDeck; CardInstance : TCardInstance);
begin
FOwner := Owner;
FCardInstance := CardInstance;
end;
function TDeckCard.GetData : RDeckCard;
begin
Result.card_id := CardInstance.ID;
if assigned(Skin) then
Result.skin_id := Skin.ID
else
Result.skin_id := -1;
end;
class function TDeckCard.GetEmptyData : RDeckCard;
begin
Result.card_id := -1;
Result.skin_id := -1;
end;
procedure TDeckCard.SetSkin(const Value : TCardSkin);
var
NewDeck : TUltimateObjectList<TDeckCard>;
begin
NewDeck := FOwner.StartDeckUpdate;
NewDeck[SlotIndex].FSkin := Value;
FOwner.SubmitDeckUpdate(NewDeck);
end;
function TDeckCard.SlotIndex : integer;
begin
Result := FOwner.Cards.IndexOf(self);
end;
end.