-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathBaseConflict.Api.Quests.pas
588 lines (512 loc) · 18 KB
/
BaseConflict.Api.Quests.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
unit BaseConflict.Api.Quests;
interface
uses
// System
System.SysUtils,
System.Math,
System.Classes,
System.Generics.Collections,
// Engine
Engine.dXML,
Engine.Helferlein,
Engine.Helferlein.Windows,
Engine.Helferlein.Threads,
Engine.Helferlein.DataStructures,
Engine.Network.RPC,
Engine.DataQuery,
// Game
BaseConflict.Api,
BaseConflict.Api.Types,
BaseConflict.Api.Shared,
BaseConflict.Api.Shop;
type
{$RTTI EXPLICIT METHODS([vcPublished, vcPublic, vcPrivate]) FIELDS([vcPublic, vcProtected]) PROPERTIES([vcPublic, vcPublished])}
{$M+}
TQuestManager = class;
TQuest = class
protected
FQuestManager : TQuestManager;
strict private
FIdentifier : string;
FReward : TLootlist;
FCompleted : boolean;
FRerollable : boolean;
FID, FTargetProgress, FCurrentProgress : integer;
FInvisible : boolean;
procedure SetCurrentProgress(const Value : integer); virtual;
procedure SetCompleted(const Value : boolean); virtual;
published
property CurrentProgress : integer read FCurrentProgress write SetCurrentProgress;
property TargetProgress : integer read FTargetProgress;
property Identifier : string read FIdentifier;
property Invisible : boolean read FInvisible;
property Reward : TLootlist read FReward;
property Rerollable : boolean read FRerollable;
[dXMLDependency('.Reward.Loot')]
function ShownReward : TLootboxContent;
[dXMLDependency('.Reward.Loot')]
function ShownSecondaryReward : TLootboxContent;
property Completed : boolean read FCompleted write SetCompleted;
procedure Complete; virtual;
/// <summary> After Quest is completed player can collect reward by calling method.
/// This will also remove quest from quests list. Because quest count as completed after reward is collected.</summary>
procedure CollectReward;
procedure ReRoll; virtual;
public
property ID : integer read FID;
/// <summary> Currently hacked to autocollect starter decks. </summary>
function ShouldBeAutoCollected : boolean;
constructor Create(QuestManager : TQuestManager; const ProgressData : RQuestProgress);
destructor Destroy; override;
end;
TTutorialQuest = class(TQuest)
public const
// a list of all tutorial tasks
{$REGION 'TASKS'}
PLAYER_BUY_CARD = 'PLAYER_BUY_CARD';
PLAYER_START_GAME = 'PLAYER_START_GAME';
PLAYER_NAVIGATE_PLAY = 'PLAYER_NAVIGATE_PLAY';
PLAYER_NAVIGATE_DECKBUILDER = 'PLAYER_NAVIGATE_DECKBUILDER';
PLAYER_NAVIGATE_COLLECTION = 'PLAYER_NAVIGATE_COLLECTION';
PLAYER_NAVIGATE_SHOP = 'PLAYER_NAVIGATE_SHOP';
PLAYER_NAVIGATE_SHOP_CRYSTALS = 'PLAYER_NAVIGATE_SHOP_CRYSTALS';
PLAYER_COLLECT_REWARD = 'PLAYER_COLLECT_REWARD';
PLAYER_REROLL_QUEST = 'PLAYER_REROLL_QUEST';
PLAYER_REROLL_TUTORIAL_QUEST = 'PLAYER_REROLL_TUTORIAL_QUEST';
PLAYER_PLAY_TUTORIAL = 'PLAYER_PLAY_TUTORIAL';
PLAYER_CARD_REACH_MAX_LEVEL = 'PLAYER_CARD_REACH_MAX_LEVEL';
PLAYER_CARD_ASCEND = 'PLAYER_CARD_ASCEND';
PLAYER_CARD_ASCEND_BRONZE = 'PLAYER_CARD_ASCEND_BRONZE';
PLAYER_CARD_ASCEND_SILVER = 'PLAYER_CARD_ASCEND_SILVER';
PLAYER_CARD_ASCEND_GOLD = 'PLAYER_CARD_ASCEND_GOLD';
PLAYER_CARD_ASCEND_CRYSTAL = 'PLAYER_CARD_ASCEND_CRYSTAL';
PLAYER_CARD_UNLOCK = 'PLAYER_CARD_UNLOCK';
PLAYER_DECK_ADD_CARD = 'PLAYER_DECK_ADD_CARD';
PLAYER_ADD_FRIEND = 'PLAYER_ADD_FRIEND';
PLAYER_CARD_LEVEL_TRIBUTE = 'PLAYER_CARD_LEVEL_TRIBUTE';
PLAYER_SET_PROFILE_ICON = 'PLAYER_SET_PROFILE_ICON';
PLAYER_SET_DECK_ICON = 'PLAYER_SET_DECK_ICON';
PLAYER_SET_DECK_NAME = 'PLAYER_SET_DECK_NAME';
PLAYER_OPEN_DIALOG_PREFIX = 'PLAYER_OPEN_DIALOG_';
PLAYER_OPEN_URL_PREFIX = 'PLAYER_OPEN_URL_';
{$ENDREGION}
strict private
FPlayerActionTask : string;
published
procedure ReRoll; override;
procedure Complete; override;
public
property PlayerActionTask : string read FPlayerActionTask;
constructor Create(QuestManager : TQuestManager; const ProgressData : RQuestProgress);
end;
TDailyQuest = class(TQuest)
end;
TWeeklyQuest = class(TQuest)
end;
TEventQuest = class(TQuest)
end;
TQuestManager = class(TInterfacedObject, IQuestBackchannel)
protected
procedure LoadQuests(const Data : RQuestData);
function CreateQuest(const Data : RQuestProgress) : TQuest;
strict private
FQuests : TUltimateObjectList<TQuest>;
FMaxReRolls, FOpenReRolls, FMaxDailyQuests, FFinishedQuests : integer;
procedure SetFinishedQuests(const Value : integer); virtual;
procedure SetMaxDailyQuests(const Value : integer); virtual;
procedure SetMaxReRolls(const Value : integer); virtual;
procedure SetOpenReRolls(const Value : integer); virtual;
// ======================== Backchannels ===================
procedure QuestUpdate(quest_data : RQuestProgress; created : boolean);
published
property Quests : TUltimateObjectList<TQuest> read FQuests;
property OpenReRolls : integer read FOpenReRolls write SetOpenReRolls;
property MaxReRolls : integer read FMaxReRolls write SetMaxReRolls;
property MaxDailyQuests : integer read FMaxDailyQuests write SetMaxDailyQuests;
property FinishedQuests : integer read FFinishedQuests write SetFinishedQuests;
[dXMLDependency('.Quests')]
function EventQuests : TArray<TEventQuest>;
[dXMLDependency('.EventQuests')]
function EventQuestsCount : integer;
[dXMLDependency('.Quests')]
function TutorialQuests : TArray<TTutorialQuest>;
[dXMLDependency('.TutorialQuests')]
function TutorialQuestsCount : integer;
[dXMLDependency('.Quests')]
function WeeklyQuests : TArray<TWeeklyQuest>;
[dXMLDependency('.WeeklyQuests')]
function WeeklyQuestsCount : integer;
[dXMLDependency('.Quests')]
function DailyQuests : TArray<TDailyQuest>;
[dXMLDependency('.DailyQuests')]
function DailyQuestsCount : integer;
[dXMLDependency('.Quests.Completed')]
function IsAnyQuestCompleted : boolean;
public
constructor Create;
/// <summary> Game signals quest manager an action player does, to push quest progress
/// of any quests that have player actions as task.
/// <param name="EventIdentifier"> Action player has done from TTutorialQuest task constant list.</param>
procedure SignalPlayerAction(const ActionIdentifier : string; ForceSend : boolean = False);
destructor Destroy; override;
end;
{$REGION 'Actions'}
TQuestAction = class(TPromiseAction)
private
FQuestManager : TQuestManager;
public
property QuestManager : TQuestManager read FQuestManager;
constructor Create(QuestManager : TQuestManager);
end;
[AQCriticalAction]
TQuestActionLoadCurrentQuests = class(TQuestAction)
public
function Execute : boolean; override;
procedure Rollback; override;
end;
TQuestActionSendPlayerAction = class(TQuestAction)
private
FActionIdentifier : string;
public
constructor Create(QuestManager : TQuestManager; const ActionIdentifier : string);
function Execute : boolean; override;
end;
[AQCriticalAction]
TQuestActionCollectReward = class(TQuestAction)
private
FQuest : TQuest;
FIndex : integer;
public
constructor Create(QuestManager : TQuestManager; Quest : TQuest);
procedure Emulate; override;
function Execute : boolean; override;
function ExecuteSynchronized : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TQuestActionReRoll = class(TQuestAction)
private
FQuest : TQuest;
FIndex : integer;
FOpenReRolls : integer;
public
constructor Create(QuestManager : TQuestManager; Quest : TQuest);
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
QuestManager : TQuestManager;
implementation
{ TQuest }
procedure TQuest.CollectReward;
begin
assert(Completed);
MainActionQueue.DoAction(TQuestActionCollectReward.Create(FQuestManager, self));
FQuestManager.SignalPlayerAction(TTutorialQuest.PLAYER_COLLECT_REWARD);
end;
procedure TQuest.Complete;
begin
Completed := True;
end;
constructor TQuest.Create(QuestManager : TQuestManager; const ProgressData : RQuestProgress);
begin
FIdentifier := ProgressData.Quest.Identifier;
FInvisible := ProgressData.Quest.Invisible;
if length(ProgressData.Quest.Reward.loot) > 0 then
FReward := TLootlist.Create(ProgressData.Quest.Reward);
FID := ProgressData.ID;
FRerollable := ProgressData.Quest.Rerollable;
FTargetProgress := ProgressData.Quest.target_count;
FCurrentProgress := ProgressData.counter;
Completed := ProgressData.Completed;
FQuestManager := QuestManager;
end;
destructor TQuest.Destroy;
begin
FReward.Free;
inherited;
end;
procedure TQuest.ReRoll;
begin
MainActionQueue.DoAction(TQuestActionReRoll.Create(self.FQuestManager, self));
FQuestManager.SignalPlayerAction(TTutorialQuest.PLAYER_REROLL_QUEST);
end;
procedure TQuest.SetCompleted(const Value : boolean);
begin
FCompleted := Value;
if FCompleted then
begin
CurrentProgress := TargetProgress;
if assigned(FQuestManager) then
FQuestManager.FinishedQuests := FQuestManager.FinishedQuests + 1;
end;
end;
procedure TQuest.SetCurrentProgress(const Value : integer);
begin
FCurrentProgress := Value;
end;
function TQuest.ShouldBeAutoCollected : boolean;
begin
Result := assigned(Reward) and (Reward.Content.Count = 1) and (Reward.Content.First.ShopItem.ItemType = itLootList);
end;
function TQuest.ShownReward : TLootboxContent;
begin
if assigned(Reward) and (Reward.Content.Count >= 1) then
Result := Reward.Content.First
else
Result := nil;
end;
function TQuest.ShownSecondaryReward : TLootboxContent;
begin
if assigned(Reward) and (Reward.Content.Count >= 2) and not(Reward.Content[1].ShopItem.ItemType = itCurrency) then
Result := Reward.Content[1]
else
Result := nil;
end;
{ TQuestManager }
constructor TQuestManager.Create;
begin
FQuests := TUltimateObjectList<TQuest>.Create;
RPCHandlerManager.SubscribeHandler(self);
MainActionQueue.DoAction(TQuestActionLoadCurrentQuests.Create(self));
FOpenReRolls := 1;
FMaxReRolls := 1;
FMaxDailyQuests := 3;
end;
function TQuestManager.CreateQuest(const Data : RQuestProgress) : TQuest;
begin
case Data.Quest.quest_type of
qtTutorial : Result := TTutorialQuest.Create(self, Data);
qtDaily : Result := TDailyQuest.Create(self, Data);
qtWeekly : Result := TWeeklyQuest.Create(self, Data);
qtEvent : Result := TEventQuest.Create(self, Data);
else raise ENotFoundException.CreateFmt('TQuestManager.CreateQuest: Questtype "%s" not implemented.', [HRtti.EnumerationToString<EnumQuestType>(Data.Quest.quest_type)]);
end;
Data.Quest.custom_task_data.Free;
end;
function TQuestManager.DailyQuests : TArray<TDailyQuest>;
begin
Result := HArray.Cast<TQuest, TDailyQuest>(Quests.Query.Filter((F('.') = TDailyQuest) and not F('Invisible')).ToArray);
end;
function TQuestManager.DailyQuestsCount : integer;
begin
Result := length(DailyQuests);
end;
destructor TQuestManager.Destroy;
begin
RPCHandlerManager.UnsubscribeHandler(self);
FQuests.Free;
inherited;
end;
function TQuestManager.EventQuests : TArray<TEventQuest>;
begin
Result := HArray.Cast<TQuest, TEventQuest>(Quests.Query.Filter((F('.') = TEventQuest) and not F('Invisible')).ToArray);
end;
function TQuestManager.EventQuestsCount : integer;
begin
Result := length(EventQuests);
end;
function TQuestManager.IsAnyQuestCompleted : boolean;
begin
Result := Quests.Query.Filter(F('Completed') and not F('Invisible')).Exists;
end;
procedure TQuestManager.LoadQuests(const Data : RQuestData);
var
ProgressDataItem : RQuestProgress;
AddedQuests : TList<TQuest>;
begin
MaxReRolls := Data.max_rerolls;
OpenReRolls := Data.rerolls;
AddedQuests := TList<TQuest>.Create;
Quests.Clear;
// load progress data
for ProgressDataItem in Data.Quests do
AddedQuests.add(CreateQuest(ProgressDataItem));
Quests.AddRange(AddedQuests);
AddedQuests.Free;
end;
procedure TQuestManager.SetFinishedQuests(const Value : integer);
begin
FFinishedQuests := Value;
end;
procedure TQuestManager.SetMaxDailyQuests(const Value : integer);
begin
FMaxDailyQuests := Value;
end;
procedure TQuestManager.SetMaxReRolls(const Value : integer);
begin
FMaxReRolls := Value;
end;
procedure TQuestManager.SetOpenReRolls(const Value : integer);
begin
FOpenReRolls := Value;
end;
procedure TQuestManager.QuestUpdate(quest_data : RQuestProgress; created : boolean);
var
Quest : TQuest;
begin
if created and not Quests.Query.Filter(F('ID') = quest_data.ID).Exists() then
begin
Quest := CreateQuest(quest_data);
Quests.add(Quest);
end
else
begin
Quest := Quests.Query.Get(F('ID') = quest_data.ID, True);
if assigned(Quest) then
begin
if quest_data.Completed then
Quest.Complete
else
Quest.Completed := quest_data.Completed;
Quest.CurrentProgress := quest_data.counter;
end;
end;
end;
procedure TQuestManager.SignalPlayerAction(const ActionIdentifier : string; ForceSend : boolean);
begin
if ForceSend or (Quests.Query.Filter((F('.') = TTutorialQuest) and (F('PlayerActionTask') = ActionIdentifier)).Exists) then
MainActionQueue.DoAction(TQuestActionSendPlayerAction.Create(self, ActionIdentifier));
end;
function TQuestManager.TutorialQuests : TArray<TTutorialQuest>;
begin
Result := HArray.Cast<TQuest, TTutorialQuest>(Quests.Query.Filter((F('.') = TTutorialQuest) and not F('Invisible')).ToArray);
end;
function TQuestManager.TutorialQuestsCount : integer;
begin
Result := length(TutorialQuests);
end;
function TQuestManager.WeeklyQuests : TArray<TWeeklyQuest>;
begin
Result := HArray.Cast<TQuest, TWeeklyQuest>(Quests.Query.Filter((F('.') = TWeeklyQuest) and not F('Invisible')).ToArray);
end;
function TQuestManager.WeeklyQuestsCount : integer;
begin
Result := length(WeeklyQuests);
end;
{ TTutorialQuest }
procedure TTutorialQuest.Complete;
begin
if not Completed then
begin
inherited;
if self.FPlayerActionTask = TTutorialQuest.PLAYER_REROLL_TUTORIAL_QUEST then
CollectReward;
end;
end;
constructor TTutorialQuest.Create(QuestManager : TQuestManager; const ProgressData : RQuestProgress);
begin
inherited Create(QuestManager, ProgressData);
if ProgressData.Quest.custom_task_data.HasField('player_action_task') then
FPlayerActionTask := ProgressData.Quest.custom_task_data['player_action_task'].AsValue.AsString
else
FPlayerActionTask := self.Identifier;
end;
procedure TTutorialQuest.ReRoll;
begin
FQuestManager.SignalPlayerAction(TTutorialQuest.PLAYER_REROLL_QUEST);
FQuestManager.SignalPlayerAction(TTutorialQuest.PLAYER_REROLL_TUTORIAL_QUEST);
end;
{ TQuestAction }
constructor TQuestAction.Create(QuestManager : TQuestManager);
begin
FQuestManager := QuestManager;
inherited Create;
end;
{ TQuestActionLoadCurrentQuests }
function TQuestActionLoadCurrentQuests.Execute : boolean;
var
QuestProgressPromise : TPromise<RQuestData>;
begin
QuestProgressPromise := QuestApi.GetCurrentQuestData;
QuestProgressPromise.WaitForData;
if QuestProgressPromise.WasSuccessful then
begin
DoSynchronized(
procedure()
begin
QuestManager.LoadQuests(QuestProgressPromise.Value);
end);
end
else
HandlePromiseError(QuestProgressPromise);
Result := QuestProgressPromise.WasSuccessful;
QuestProgressPromise.Free;
end;
procedure TQuestActionLoadCurrentQuests.Rollback;
begin
QuestManager.Quests.Clear;
end;
{ TQuestActionSendPlayerAction }
constructor TQuestActionSendPlayerAction.Create(QuestManager : TQuestManager; const ActionIdentifier : string);
begin
FActionIdentifier := ActionIdentifier;
inherited Create(QuestManager);
end;
function TQuestActionSendPlayerAction.Execute : boolean;
var
promise : TPromise<boolean>;
begin
promise := QuestApi.SendPlayerAction(FActionIdentifier);
Result := self.HandlePromise(promise);
end;
{ TQuestActionCollectReward }
constructor TQuestActionCollectReward.Create(QuestManager : TQuestManager; Quest : TQuest);
begin
FQuest := Quest;
inherited Create(QuestManager);
end;
procedure TQuestActionCollectReward.Emulate;
begin
FIndex := QuestManager.Quests.IndexOf(FQuest);
QuestManager.Quests.Extract(FQuest);
end;
function TQuestActionCollectReward.Execute : boolean;
var
promise : TPromise<boolean>;
begin
promise := QuestApi.CollectReward(FQuest.ID);
// currently nothing todo, because backchannels will do the unlock
Result := self.HandlePromise(promise);
end;
function TQuestActionCollectReward.ExecuteSynchronized : boolean;
begin
Shop.CallRewardGained(FQuest.Reward.Content.Query.Filter((F('ShopItem') = TShopItemBuyCurrency) or (F('ShopItem') = TShopItemUnlockIcon) or (F('ShopItem') = TShopItemLootList)).ToArray());
FQuest.Free;
Result := True;
end;
procedure TQuestActionCollectReward.Rollback;
begin
QuestManager.Quests.Insert(FIndex, FQuest);
end;
{ TQuestActionReRoll }
constructor TQuestActionReRoll.Create(QuestManager : TQuestManager; Quest : TQuest);
begin
FQuest := Quest;
inherited Create(QuestManager);
end;
procedure TQuestActionReRoll.Emulate;
begin
FOpenReRolls := QuestManager.OpenReRolls;
QuestManager.OpenReRolls := QuestManager.OpenReRolls - 1;
FIndex := QuestManager.Quests.IndexOf(FQuest);
QuestManager.Quests.Extract(FQuest);
end;
function TQuestActionReRoll.Execute : boolean;
var
promise : TPromise<boolean>;
begin
promise := QuestApi.ReRoll(FQuest.ID);
// currently nothing todo, because backchannels will do the unlock
Result := self.HandlePromise(promise);
end;
procedure TQuestActionReRoll.Rollback;
begin
QuestManager.OpenReRolls := FOpenReRolls;
QuestManager.Quests.Insert(FIndex, FQuest);
end;
end.