-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathBaseConflict.Api.Shop.pas
2149 lines (1880 loc) · 62.2 KB
/
BaseConflict.Api.Shop.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
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
unit BaseConflict.Api.Shop;
interface
uses
// System
System.SysUtils,
System.Math,
System.Classes,
System.Generics.Defaults,
System.Generics.Collections,
// 3rd party
steam_api,
steamtypes,
isteamuser_,
isteamapps_,
// Engine
Engine.Helferlein,
Engine.Helferlein.Windows,
Engine.Helferlein.DataStructures,
Engine.Helferlein.Threads,
Engine.Network.RPC,
Engine.DataQuery,
Engine.Serializer.JSON,
Engine.GUI,
Engine.dXML,
Engine.Math,
// Game
BaseConflict.Constants.Cards,
BaseConflict.Constants.Client,
BaseConflict.Api,
BaseConflict.Api.Types,
BaseConflict.Api.Shared,
BaseConflict.Api.Cards;
const
CURRENCY_GOLD = 'currency_gold';
CURRENCY_DIAMONDS = 'currency_diamonds';
CURRENCY_FREE_XP = 'currency_free_exp';
type
{$RTTI EXPLICIT METHODS([vcPublished, vcPublic, vcPrivate]) FIELDS([vcPublic, vcProtected]) PROPERTIES([vcPublic, vcPublished])}
{$M+}
EnumShopCategory = (scCards, scCurrency, scCredits, scDiamonds, scPremiumTime, scIcons, scBonuscode, scBundles, scSkins, scBooster);
SetShopCategory = set of EnumShopCategory;
EnumDefaultCurrency = (dcGold, dcDiamonds, dcFreeXP);
TShop = class;
TShopItem = class;
TInventoryItem = class;
TLootbox = class;
TDraftbox = class;
TLootboxContent = class;
/// <summary> There maybe some more shopoffers on serverside, but on clientside only shopoffers that are currently active
/// will be displayed.</summary>
TShopItemOffer = class
private
FID : integer;
FShopItem : TShopItem;
/// <summary> Inidicates if offer is for real money of ingame money.</summary>
FRealMoney, FHasHardCurrency : boolean;
procedure EmulateBuy(const Amount : integer);
procedure RollbackBuy(const Amount : integer);
strict private
FCosts : TUltimateList<RCost>;
published
property Costs : TUltimateList<RCost> read FCosts;
/// <summary> Returns True, if this offer can be payed with the current amount of currency the
/// player owns.</summary>
[dXMLDependency('shop.Shop.Balances.Balance')]
function IsPayable : boolean;
[dXMLDependency('shop.Shop.Balances.Balance')]
function IsPayableTimes10 : boolean;
/// <summary> True if costs contain any diamonds. </summary>
property HasHardCurrency : boolean read FHasHardCurrency;
public
/// <summary> The owning shop item. </summary>
property ShopItem : TShopItem read FShopItem;
property RealMoney : boolean read FRealMoney;
/// <summary> Only valid if real money is true. </summary>
function RealMoneyString : string;
/// <summary> Buy offer and spend currency to pay cost. Will automatically add items (e.g. currency or cards)
/// to profile. Will buy item linked to offer amount times.</summary>
procedure Buy(Amount : integer);
function IsPayableTimes(Times : integer) : boolean;
function BalanceAfterPurchase : TArray<RCost>;
constructor Create(ShopItem : TShopItem; const Data : ROffer);
destructor Destroy; override;
end;
EnumShopItemType = (
itInvalid,
itPremiumTime,
itSkin,
itCard,
itDraftbox,
itLootbox,
itIcon,
itDeckSlot,
itLootList,
itCredits,
itDiamonds,
itCurrency,
itPlayerXP,
itRandomCard,
itDisenchantedCard);
TShopItem = class abstract
private
FName : string;
FID : integer;
FShop : TShop;
FPurchasesCount : integer;
FPurchasesLimitedTo : integer;
FTimeToBuy : integer;
/// <summary> Emulates the impact of buying this shophitem count times.
/// Data returned is saved by action and passed to another methods (execute or rollback) on call.</summary>
function EmulateBuy(Count : integer) : TObject; virtual; abstract;
/// <summary> After successful call, maybe data has to applied on emulated data.</summary>
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); virtual; abstract;
/// <summary> Rollback buy when failed.</summary>
procedure RollbackBuy(const AObject : TObject; Count : integer); virtual; abstract;
function GetCategories : SetShopCategory; virtual; abstract;
function GetDisplayName : string; virtual;
procedure SetPurchasesCount(const Value : integer); virtual;
strict private
FOffers : TUltimateObjectList<TShopItemOffer>;
published
property Offers : TUltimateObjectList<TShopItemOffer> read FOffers;
property PurchasesCount : integer read FPurchasesCount write SetPurchasesCount;
property PurchasesLimitedTo : integer read FPurchasesLimitedTo;
property TimeToBuy : integer read FTimeToBuy;
[dXMLDependency('.PurchasesCount', '.PurchasesLimitedTo')]
function MaxPurchasesReached : boolean; virtual;
public
property ID : integer read FID;
property name : string read FName;
property DisplayName : string read GetDisplayName;
property Categories : SetShopCategory read GetCategories;
function IsTimeLimited : boolean;
function PurchasableBySoftcurrency : boolean; virtual;
function ItemType : EnumShopItemType; virtual;
function CostOrderValue(CurrencyUID : string) : integer;
/// <summary> Returns whether an item should be displayed within shop. </summary>
function IsVisible : boolean; virtual;
constructor Create(Shop : TShop; Data : TApiShopItem);
destructor Destroy; override;
end;
TShopItemBuyCardInstance = class(TShopItem)
private
FCard : TCard;
FLeague : integer;
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
published
/// <summary> Target card which player will buy a instance.</summary>
property Card : TCard read FCard;
public
function CardInfo : TCardInfo;
function League : integer;
[dXMLDependency('.Card.Unlocked')]
function PurchasableBySoftcurrency : boolean; override;
function IsVisible : boolean; override;
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemBuyCard);
end;
TShopItemUnlockSkin = class(TShopItem)
private
FSkin : TCardSkin;
FCard : TCard;
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
published
property Skin : TCardSkin read FSkin;
property Card : TCard read FCard;
[dXMLDependency('.Skin.Unlocked')]
function MaxPurchasesReached : boolean; override;
public
function CardInfo : TCardInfo;
function League : integer;
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemUnlockSkin);
end;
TShopItemRandomUnlockedCard = class(TShopItem)
private
FLeague : integer;
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
public
property League : integer read FLeague;
function IsVisible : boolean; override;
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemRandomCard);
end;
TShopItemBuyCurrency = class(TShopItem)
private
FCurrency : TCurrency;
FAmount : integer;
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
function GetDisplayName : string; override;
public
property Currency : TCurrency read FCurrency;
property Amount : integer read FAmount;
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemBuyCurrency);
end;
TShopItemGainPlayerExperience = class(TShopItem)
private
FAmount : integer;
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
public
property Amount : integer read FAmount;
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemGainPlayerExperience);
end;
TShopItemLootbox = class(TShopItem)
private
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
public
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemLootbox);
end;
TShopItemDraftbox = class(TShopItem)
private
FLeague : integer;
FTypeIdentifier : string;
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
published
property TypeIdentifier : string read FTypeIdentifier;
public
function IsCardBox : boolean;
function IsSkinBox : boolean;
property League : integer read FLeague;
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemDraftbox);
end;
TShopItemUnlockIcon = class(TShopItem)
private
FIconIdentifier : string;
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
public
property Icon : string read FIconIdentifier;
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemUnlockIcon);
end;
TShopItemPremiumAccount = class(TShopItem)
private
FDays : integer;
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
public
property Days : integer read FDays;
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemPremiumAccount);
end;
TShopItemDeckSlot = class(TShopItem)
private
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
public
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemDeckSlot);
end;
TShopItemLootList = class(TShopItem)
private
function GetCategories : SetShopCategory; override;
function EmulateBuy(Count : integer) : TObject; override;
procedure ExecuteBuy(const AObject : TObject; const Data : TJSONData; Count : integer); override;
procedure RollbackBuy(const AObject : TObject; Count : integer); override;
public
function ItemType : EnumShopItemType; override;
constructor Create(Shop : TShop; Data : TApiShopItemLootList);
end;
/// <summary> Describes the current balance for an currency the player owns.</summary>
TBalance = class(TVersionedItem)
strict private
FBalance : integer;
FCurrency : TCurrency;
procedure SetBalance(const Value : integer); virtual;
private
// update balance from serverdata and return True if balance has changed
// else false
function UpdateBalance(balance_data : RBalance) : boolean;
// update balance using clientdata
procedure ChargeBalance(BalanceDifference : integer);
// rollback the charge of balance
procedure RollbackChargeBalance(BalanceDifference : integer);
published
property Balance : integer read FBalance write SetBalance;
property Currency : TCurrency read FCurrency;
public
constructor Create(Balance : integer; Currency : TCurrency);
end;
EnumTransactionState = (tsNone, tsProcessing, tsSuccessful, tsAborted, tsFailed);
/// <summary> Observing steam for changed dlc ownership and inform shop, if new dlc was installed.</summary>
TShopDLCObserverThread = class(TThread)
private const
POLLING_RATE = 1000;
strict private
FNotOwnedDLCList : TList<AppId_t>;
FTimer : TTimer;
protected
procedure Execute; override;
public
constructor Create;
destructor Destroy; override;
end;
RReward = record
ShopItem : TShopItem;
Amount : integer;
end;
ARReward = TArray<RReward>;
ProcRewardGained = procedure(Rewards : TArray<RReward>) of object;
TShop = class(TInterfacedObject, IShop, ILoot)
private
FLastRealMoneyTransactionState : EnumTransactionState;
FOnInventoryLoaded : ProcOfObject;
FOnRewardGained : ProcRewardGained;
FObserverThread : TShopDLCObserverThread;
FPlayerCurrencyCode : string;
procedure LoadBalance(const Data : RBalanceData);
procedure LoadShopItems(const Data : ATApiShopItem; const PurchaseData : ARShopPurchase);
procedure LoadInventory(const DraftData : ARDraftBox; const LootData : ARLootbox);
procedure ChargeAccount(Costs : array of RCost; Sign : integer; Rollback : boolean);
/// <summary> Will called by steamapi when player gets DLC ownership and install it.</summary>
procedure SteamDLCInstalledCallback(const Data : DlcInstalled_t);
// backchannels
procedure BalanceUpdate(balance_data : RBalance);
procedure NewDraftBox(draftbox_data : RDraftBox);
strict private
FItems : TUltimateObjectList<TShopItem>;
FBalances : TUltimateObjectList<TBalance>;
FInventory : TUltimateObjectList<TInventoryItem>;
published
property PlayerCurrencyCode : string read FPlayerCurrencyCode;
property Balances : TUltimateObjectList<TBalance> read FBalances;
property Items : TUltimateObjectList<TShopItem> read FItems;
property Inventory : TUltimateObjectList<TInventoryItem> read FInventory;
public
property OnInventoryLoaded : ProcOfObject read FOnInventoryLoaded write FOnInventoryLoaded;
property OnRewardGained : ProcRewardGained read FOnRewardGained write FOnRewardGained;
property LastRealMoneyTransactionState : EnumTransactionState read FLastRealMoneyTransactionState;
[dXMLDependency('.Balances')]
function Balance(const Currency : EnumDefaultCurrency) : TBalance;
[dXMLDependency('.Balances')]
function BalanceByUID(const CurrencyUID : string) : TBalance;
[dXMLDependency('.Balances')]
function BalanceByCurrency(const Currency : TCurrency) : TBalance;
[dXMLDependency('.Balances')]
function CanPayCosts(Costs : TUltimateList<RCost>) : boolean;
/// <summary> Charge account by subtract the costs from accounts. This will only applied locally
/// and is not submitted to server.</summary>
procedure PayCosts(Costs : array of RCost);
/// <summary> Charge account by adding the costs to account. Only locally. Method is used
/// for rollback payed costs.</summary>
procedure RollbackPayedCosts(Costs : array of RCost);
/// <summary> Charge account by give player some money.</summary>
procedure CreditCurrency(CreditItems : array of RCost);
/// <summary> Take them the money away.</summary>
procedure RollbackCreditCurrency(CreditItems : array of RCost);
procedure CallRewardGained(Rewards : TArray<RReward>); overload;
procedure CallRewardGained(Rewards : TArray<TLootboxContent>); overload;
procedure RedeemKeycode(const Key : string);
function ResolveShopItemByID(const ID : integer) : TShopItem;
function TryResolveShopItemByID(const ID : integer; out ShopItem : TShopItem) : boolean;
function ResolveShopItemByName(const Name : string) : TShopItem;
function TryResolveShopItemByName(const Name : string; out ShopItem : TShopItem) : boolean;
function ResolveShopItemBuyCardInstance(const Card : TCard) : TShopItemBuyCardInstance;
function TryResolveShopItemBuyCardInstance(const Card : TCard; out ShopItem : TShopItemBuyCardInstance) : boolean;
constructor Create;
destructor Destroy; override;
end;
TInventoryItem = class
// + Level
private const
LID_LEVEL_REWARD = 'player_level_reward_box_';
strict private
FOpened : boolean;
procedure SetOpened(const Value : boolean); virtual;
protected
FID : integer;
FTypeIdentifier : string;
published
property Opened : boolean read FOpened write SetOpened;
property TypeIdentifier : string read FTypeIdentifier;
public
/// <summary> Returns whether this box is the reward to reach a certain level. </summary>
function IsLevelReward : boolean;
/// <summary> Returns the level which was this box rewarded for. Only valid in conjunction with IsLevelReward, otherwise returns -1.</summary>
function RewardForPlayerLevel : integer;
end;
TLootboxContent = class
strict protected
FShopItem : TShopItem;
FAmount : integer;
published
/// <summary> The shopitem the player will receive from lootbox.</summary>
property ShopItem : TShopItem read FShopItem;
/// <summary> The amount of shopitems the player will receive from lootbox.</summary>
property Amount : integer read FAmount;
public
constructor Create(ShopItem : TShopItem; Amount : integer);
end;
TLootbox = class(TInventoryItem)
private
FContent : TUltimateObjectList<TLootboxContent>;
published
property Content : TUltimateObjectList<TLootboxContent> read FContent;
procedure OpenAndReceiveLoot;
public
constructor Create(Data : RLootbox);
destructor Destroy; override;
end;
TLootList = class
private
FLoot : TUltimateObjectList<TLootboxContent>;
published
property Content : TUltimateObjectList<TLootboxContent> read FLoot;
public
constructor Create(Data : RLootList);
destructor Destroy; override;
end;
TDraftBoxChoice = class(TLootboxContent)
strict private
FChoiceID : integer;
public
property ChoiceID : integer read FChoiceID;
constructor Create(ShopItem : TShopItem; Amount : integer; ChoiceID : integer);
end;
TDraftbox = class(TInventoryItem)
private
FLeague : integer;
FChoices : TUltimateObjectList<TDraftBoxChoice>;
published
property Choices : TUltimateObjectList<TDraftBoxChoice> read FChoices;
procedure DraftItem(Item : TDraftBoxChoice);
public
function IsCardBox : boolean;
function IsSkinBox : boolean;
property League : integer read FLeague;
constructor Create(Data : RDraftBox);
destructor Destroy; override;
end;
{$REGION 'Actions'}
TShopAction = class(TPromiseAction)
private
FShop : TShop;
public
property Shop : TShop read FShop;
constructor Create(Shop : TShop);
end;
[AQCriticalAction]
TShopActionLoadBalance = class(TShopAction)
public
function Execute : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TShopActionLoadShopItems = class(TShopAction)
public
function Execute : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TShopActionLoadInventory = class(TShopAction)
public
function Execute : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TShopItemOfferActionBuy = class(TPromiseAction)
private
FOffer : TShopItemOffer;
FAmount : integer;
FEmulatedData : TObject;
public
property Offer : TShopItemOffer read FOffer;
property Amount : integer read FAmount;
property EmulatedData : TObject read FEmulatedData;
constructor Create(Offer : TShopItemOffer; Amount : integer);
procedure Emulate; override;
function Execute : boolean; override;
function ExecuteSynchronized : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TShopItemOfferActionBuyForRealMoney = class(TShopItemOfferActionBuy)
private
FBuyAuthorized : integer;
FOrderID : UInt64;
procedure SteamMicroTxnAuthorizationResponseCallback(const Data : MicroTxnAuthorizationResponse_t);
public
procedure Emulate; override;
function Execute : boolean; override;
function ExecuteSynchronized : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TLootboxActionOpen = class(TPromiseAction)
private
FLootbox : TLootbox;
public
constructor Create(Lootbox : TLootbox);
procedure Emulate; override;
function Execute : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TDraftBoxActionDraftItem = class(TPromiseAction)
private
FDraftBox : TDraftbox;
FChoiceID : integer;
public
property DraftBox : TDraftbox read FDraftBox;
constructor Create(DraftBox : TDraftbox; ChoiceID : integer);
procedure Emulate; override;
function Execute : boolean; override;
procedure Rollback; override;
end;
TShopActionUpdateDLCOwnership = class(TPromiseAction)
public
function Execute : boolean; override;
end;
[AQCriticalAction]
TShopActionRedeemKeycode = class(TPromiseAction)
private
FKeycode : string;
public
constructor Create(const Keycode : string);
function Execute : boolean; override;
function ExecuteSynchronized : boolean; override;
end;
{$ENDREGION}
{$M-}
{$RTTI EXPLICIT METHODS([vcPublic, vcPublished]) PROPERTIES([vcPublic, vcPublished]) FIELDS([vcPrivate, vcProtected, vcPublic])}
var
Shop : TShop;
const
DLC_SYSTEM_ENABLED = False;
implementation
uses
{$IF not defined(MAPEDITOR)}
BaseConflictMainUnit,
{$ENDIF}
BaseConflict.Globals.Client,
BaseConflict.Classes.Gamestates.GUI,
BaseConflict.Api.Profile;
{ TShopOffer }
function TShopItemOffer.BalanceAfterPurchase : TArray<RCost>;
var
i : integer;
begin
setLength(Result, FCosts.Count);
for i := 0 to FCosts.Count - 1 do
begin
Result[i].Currency := FCosts[i].Currency;
Result[i].Amount := Shop.BalanceByCurrency(FCosts[i].Currency).Balance - FCosts[i].Amount;
end;
end;
procedure TShopItemOffer.Buy(Amount : integer);
begin
if IsPayable then
begin
if FRealMoney then
MainActionQueue.DoAction(TShopItemOfferActionBuyForRealMoney.Create(self, Amount))
else MainActionQueue.DoAction(TShopItemOfferActionBuy.Create(self, Amount));
end;
end;
constructor TShopItemOffer.Create(ShopItem : TShopItem; const Data : ROffer);
var
cost_data : RCostRaw;
begin
FCosts := TUltimateList<RCost>.Create();
FShopItem := ShopItem;
FID := Data.ID;
FRealMoney := Data.real_money;
for cost_data in Data.Costs do
begin
FCosts.Add(RCost.Create(cost_data.Amount, CurrencyManager.GetCurrencyByUID(cost_data.currency_uid)));
if FCosts.Last.Currency.UID = CURRENCY_DIAMONDS then FHasHardCurrency := True;
end;
end;
destructor TShopItemOffer.Destroy;
begin
FCosts.Free;
inherited;
end;
procedure TShopItemOffer.EmulateBuy(const Amount : integer);
begin
FShopItem.FShop.PayCosts(Costs.ToArray);
end;
function TShopItemOffer.IsPayable : boolean;
begin
Result := IsPayableTimes(1);
end;
function TShopItemOffer.IsPayableTimes(Times : integer) : boolean;
var
cost : RCost;
begin
if FRealMoney then
Result := True
else
begin
Result := True;
for cost in FCosts do
begin
Result := Result and (FShopItem.FShop.BalanceByUID(cost.Currency.UID).Balance >= cost.Amount * Times);
end;
end;
end;
function TShopItemOffer.IsPayableTimes10 : boolean;
begin
Result := IsPayableTimes(10);
end;
function TShopItemOffer.RealMoneyString : string;
begin
if RealMoney and not Costs.IsEmpty then
begin
Result := Inttostr(Costs[0].Amount div 100) + FormatSettings.DecimalSeparator + Inttostr(Costs[0].Amount mod 100);
if Costs[0].Currency.UID = 'EUR' then Result := Result + ' €'
else if Costs[0].Currency.UID = 'GBP' then Result := '£' + Result
else if Costs[0].Currency.UID = 'USD' then Result := '$' + Result
else if Costs[0].Currency.UID = 'BRL' then Result := 'R$' + Result
else if Costs[0].Currency.UID = 'RUB' then Result := '₽ ' + Result
else if Costs[0].Currency.UID = 'PLN' then Result := Result + ' zł'
else Result := '? ' + Result;
end
else Result := '';
end;
procedure TShopItemOffer.RollbackBuy(const Amount : integer);
begin
FShopItem.FShop.RollbackPayedCosts(Costs.ToArray);
end;
{ TShopItem }
function TShopItem.CostOrderValue(CurrencyUID : string) : integer;
var
i, j : integer;
begin
Result := MaxInt;
for i := 0 to Offers.Count - 1 do
for j := 0 to Offers[i].Costs.Count - 1 do
if Offers[i].Costs[j].Currency.UID = CurrencyUID then
Result := Min(Result, Offers[i].Costs[j].Amount);
end;
constructor TShopItem.Create(Shop : TShop; Data : TApiShopItem);
var
offer_data, offer_data_copy : ROffer;
DefaultCost, cost : RCostRaw;
CostQuery : IDataQuery<RCostRaw>;
begin
FOffers := TUltimateObjectList<TShopItemOffer>.Create();
FShop := Shop;
FName := Data.Name;
FID := Data.ID;
FPurchasesLimitedTo := Data.purchases_limited_to;
FTimeToBuy := Data.time_to_buy;
Data.Offers := HArray.Sort<ROffer>(Data.Offers,
function(const L, R : ROffer) : integer
begin
if (length(L.Costs) > 0) and (length(R.Costs) > 0) then
Result := -CompareStr(L.Costs[0].currency_uid, R.Costs[0].currency_uid)
else
Result := L.ID - R.ID;
end);
for offer_data in Data.Offers do
begin
if offer_data.real_money then
begin
offer_data_copy := offer_data;
CostQuery := TDelphiDataQuery<RCostRaw>.CreateInterface(offer_data.Costs);
DefaultCost := CostQuery.Get(F('currency_uid') = 'USD');
if CostQuery.TryGet(F('currency_uid') = Shop.PlayerCurrencyCode, cost) then
offer_data_copy.Costs := [cost]
else
offer_data_copy.Costs := [DefaultCost];
FOffers.Add(TShopItemOffer.Create(self, offer_data_copy))
end
else
FOffers.Add(TShopItemOffer.Create(self, offer_data))
end;
end;
destructor TShopItem.Destroy;
begin
FOffers.Free;
inherited;
end;
function TShopItem.GetDisplayName : string;
begin
Result := Format('%s', [HInternationalizer.TranslateTextRecursive('§shop_item_name_' + name)]);;
end;
function TShopItem.IsTimeLimited : boolean;
begin
Result := FTimeToBuy >= 0;
end;
function TShopItem.IsVisible : boolean;
begin
// only show items which have offers for
Result := Offers.Count > 0;
end;
function TShopItem.ItemType : EnumShopItemType;
begin
Result := itInvalid;
end;
function TShopItem.MaxPurchasesReached : boolean;
begin
Result := (PurchasesLimitedTo > 0) and (PurchasesCount >= PurchasesLimitedTo);
end;
function TShopItem.PurchasableBySoftcurrency : boolean;
begin
Result := True;
end;
procedure TShopItem.SetPurchasesCount(const Value : integer);
begin
FPurchasesCount := Value;
end;
{ TShop }
function TShop.Balance(const Currency : EnumDefaultCurrency) : TBalance;
begin
case Currency of
dcGold : Result := BalanceByUID(CURRENCY_GOLD);
dcDiamonds : Result := BalanceByUID(CURRENCY_DIAMONDS);
dcFreeXP : Result := BalanceByUID(CURRENCY_FREE_XP);
else
raise ENotImplemented.Create('TShop.Balance: Missing implementation of default currency ' + HRtti.EnumerationToString<EnumDefaultCurrency>(Currency));
end;
end;
function TShop.BalanceByCurrency(const Currency : TCurrency) : TBalance;
begin
Result := FBalances.Query.Get(F('Currency') = Currency);
end;
function TShop.BalanceByUID(const CurrencyUID : string) : TBalance;
begin
Result := FBalances.Query.Get(F('Currency.UID') = CurrencyUID, True);
end;
procedure TShop.BalanceUpdate(balance_data : RBalance);
var
Balance : TBalance;
begin
Balance := Balances.Query.Get(F('Currency.UID') = balance_data.currency_uid);
if Balance.UpdateBalance(balance_data) then
Balances.SignalItemChanged(Balance);
end;
procedure TShop.CallRewardGained(Rewards : TArray<RReward>);
begin
if assigned(OnRewardGained) and (length(Rewards) > 0) then
OnRewardGained(Rewards);
end;
procedure TShop.CallRewardGained(Rewards : TArray<TLootboxContent>);
var
RawRewards : TArray<RReward>;
i : integer;
begin
setLength(RawRewards, length(Rewards));
for i := 0 to length(Rewards) - 1 do
begin
RawRewards[i].ShopItem := Rewards[i].ShopItem;
RawRewards[i].Amount := Rewards[i].Amount;
end;
RawRewards := HArray.Filter<RReward>(RawRewards,
function(const Value : RReward) : boolean
begin
Result := Value.ShopItem.IsVisible or (Value.ShopItem.ItemType <> itCurrency);
end);
RawRewards := HArray.Sort<RReward>(RawRewards,
function(const L, R : RReward) : integer
begin
Result := -(ord(L.ShopItem.IsVisible) - ord(R.ShopItem.IsVisible));
if Result = 0 then
Result := L.ShopItem.ID - R.ShopItem.ID;
end);
CallRewardGained(RawRewards);
end;
function TShop.CanPayCosts(Costs : TUltimateList<RCost>) : boolean;
var
i : integer;
begin
Result := True;
if not assigned(Costs) then exit;
for i := 0 to Costs.Count - 1 do
Result := Result and (Costs[i].Amount <= BalanceByCurrency(Costs[i].Currency).Balance)
end;
procedure TShop.ChargeAccount(Costs : array of RCost; Sign : integer; Rollback : boolean);
var
cost : RCost;
Balance : TBalance;
begin
for cost in Costs do
begin
Balance := self.BalanceByCurrency(cost.Currency);
if not Rollback then
Balance.ChargeBalance(cost.Amount * Sign)
else
Balance.RollbackChargeBalance(cost.Amount * Sign);
Balances.SignalItemChanged(Balance);
end;
end;
procedure TShop.PayCosts(Costs : array of RCost);
begin
ChargeAccount(Costs, -1, False);
end;
constructor TShop.Create;
begin
FItems := TUltimateObjectList<TShopItem>.Create;
FBalances := TUltimateObjectList<TBalance>.Create();
FInventory := TUltimateObjectList<TInventoryItem>.Create();
RPCHandlerManager.SubscribeHandler(self);
{$IFDEF STEAM}
if DLC_SYSTEM_ENABLED then
FObserverThread := TShopDLCObserverThread.Create;
{$ENDIF}
MainActionQueue.DoAction(TShopActionLoadBalance.Create(self));
MainActionQueue.DoAction(TShopActionLoadShopItems.Create(self));
MainActionQueue.DoAction(TShopActionLoadInventory.Create(self));
end;
procedure TShop.CreditCurrency(CreditItems : array of RCost);
begin
ChargeAccount(CreditItems, +1, False);
end;
destructor TShop.Destroy;
begin
RPCHandlerManager.UnsubscribeHandler(self);
FObserverThread.Free;
FInventory.Free;
FItems.Free;
FBalances.Free;
inherited;
end;
procedure TShop.RedeemKeycode(const Key : string);
begin
MainActionQueue.DoAction(TShopActionRedeemKeycode.Create(Key));
end;
procedure TShop.RollbackCreditCurrency(CreditItems : array of RCost);
begin
ChargeAccount(CreditItems, +1, True);
end;
procedure TShop.RollbackPayedCosts(Costs : array of RCost);
begin
ChargeAccount(Costs, -1, True);
end;
procedure TShop.SteamDLCInstalledCallback(const Data : DlcInstalled_t);
begin
noop;
end;
function TShop.ResolveShopItemBuyCardInstance(const Card : TCard) : TShopItemBuyCardInstance;
begin
if not TryResolveShopItemBuyCardInstance(Card, Result) then
Result := nil;
end;
function TShop.ResolveShopItemByID(const ID : integer) : TShopItem;
begin
if not TryResolveShopItemByID(ID, Result) then
raise ENotFoundException.CreateFmt('TShop.ResolveShopItemByID: Did not found shopitem with id %d!', [ID]);
end;
function TShop.ResolveShopItemByName(const Name : string) : TShopItem;
begin
if not TryResolveShopItemByName(name, Result) then
raise ENotFoundException.CreateFmt('TShop.ResolveShopItemByName: Did not found shopitem with name %s!', [name]);
end;
function TShop.TryResolveShopItemBuyCardInstance(const Card : TCard; out ShopItem : TShopItemBuyCardInstance) : boolean;
var
Item : TShopItem;
begin
Item := Shop.Items.Query.Get((F('ItemType') = RQuery.From<EnumShopItemType>(itCard)) and F('IsVisible') and (F('Card') = Card), True);
if assigned(Item) then
begin
Result := True;
ShopItem := Item as TShopItemBuyCardInstance;
end
else
Result := False;
end;
function TShop.TryResolveShopItemByID(const ID : integer; out ShopItem : TShopItem) : boolean;
var
Item : TShopItem;
begin
Item := Shop.Items.Query.Get(F('ID') = ID, True);
if assigned(Item) then
begin
Result := True;
ShopItem := Item;
end
else
Result := False;
end;