-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathBaseConflict.Api.Messages.pas
238 lines (205 loc) · 6.17 KB
/
BaseConflict.Api.Messages.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
unit BaseConflict.Api.Messages;
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+}
TMessageInbox = class;
TMessageAttachment = class(TLootboxContent);
TMessage = class
strict private
FID : integer;
FMessageBox : TMessageInbox;
FSubject : string;
FText : string;
FAttachments : TUltimateObjectList<TMessageAttachment>;
published
property Subject : string read FSubject;
property Text : string read FText;
property Attachments : TUltimateObjectList<TMessageAttachment> read FAttachments;
[dXMLDependency('.Attachments')]
function HasWideItems : boolean;
procedure MarkAsReadAndCollectItems;
public
property ID : integer read FID;
property MessageBox : TMessageInbox read FMessageBox;
constructor Create(MessageBox : TMessageInbox; const MessageData : RMessage);
destructor Destroy; override;
end;
ProcOnNewMessage = procedure(const Msg : TMessage) of object;
TMessageInbox = class(TInterfacedObject, IMessageBackchannel)
protected
FOnNewMessage : ProcOnNewMessage;
procedure LoadMessages(const Data : ARMessage);
strict private
FMessages : TUltimateObjectList<TMessage>;
// ======================== Backchannels ===================
procedure NewMessage(message_data : RMessage);
published
property Messages : TUltimateObjectList<TMessage> read FMessages;
public
property OnNewMessage : ProcOnNewMessage read FOnNewMessage write FOnNewMessage;
constructor Create;
destructor Destroy; override;
end;
{$REGION 'Actions'}
[AQCriticalAction]
TMessageActionLoadUnreadMessages = class(TPromiseAction)
private
FMessageBox : TMessageInbox;
public
constructor Create(MessageBox : TMessageInbox);
function Execute : boolean; override;
procedure Rollback; override;
end;
[AQCriticalAction]
TMessageActionReadMessageAndCollectItems = class(TPromiseAction)
private
FMessage : TMessage;
FIndex : integer;
public
constructor Create(AMessage : TMessage);
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
MessageInbox : TMessageInbox;
implementation
{ TMessageInbox }
constructor TMessageInbox.Create;
begin
FMessages := TUltimateObjectList<TMessage>.Create;
MainActionQueue.DoAction(TMessageActionLoadUnreadMessages.Create(self));
RPCHandlerManager.SubscribeHandler(self);
end;
destructor TMessageInbox.Destroy;
begin
RPCHandlerManager.SubscribeHandler(self);
FMessages.Free;
inherited;
end;
procedure TMessageInbox.LoadMessages(const Data : ARMessage);
begin
Messages.AddRange(HArray.Map<RMessage, TMessage>(Data,
function(const message_data : RMessage) : TMessage
begin
Result := TMessage.Create(self, message_data);
end));
end;
procedure TMessageInbox.NewMessage(message_data : RMessage);
begin
Messages.add(TMessage.Create(self, message_data));
if assigned(OnNewMessage) then OnNewMessage(Messages.Last);
end;
{ TMessage }
constructor TMessage.Create(MessageBox : TMessageInbox; const MessageData : RMessage);
var
i : integer;
ShopItem : TShopItem;
begin
FAttachments := TUltimateObjectList<TMessageAttachment>.Create;
for i := 0 to length(MessageData.Attachments) - 1 do
begin
if shop.TryResolveShopItemByID(MessageData.Attachments[i].shopitem_id, ShopItem) then
FAttachments.add(TMessageAttachment.Create(ShopItem, MessageData.Attachments[i].amount))
end;
FMessageBox := MessageBox;
FID := MessageData.ID;
FSubject := MessageData.Subject;
FText := MessageData.Text;
end;
destructor TMessage.Destroy;
begin
FAttachments.Free;
inherited;
end;
function TMessage.HasWideItems : boolean;
var
i : integer;
begin
Result := False;
for i := 0 to FAttachments.Count - 1 do
if FAttachments[i].ShopItem.ItemType in [itCard, itSkin, itDeckSlot] then
Result := True;
end;
procedure TMessage.MarkAsReadAndCollectItems;
begin
MainActionQueue.DoAction(TMessageActionReadMessageAndCollectItems.Create(self));
end;
{ TMessageActionLoadUnreadMessages }
constructor TMessageActionLoadUnreadMessages.Create(MessageBox : TMessageInbox);
begin
FMessageBox := MessageBox;
inherited Create;
end;
function TMessageActionLoadUnreadMessages.Execute : boolean;
var
Promise : TPromise<ARMessage>;
begin
Promise := MessageApi.GetUnreadMessages;
Promise.WaitForData;
if Promise.WasSuccessful then
begin
DoSynchronized(
procedure()
begin
FMessageBox.LoadMessages(Promise.Value);
end);
end
else
HandlePromiseError(Promise);
Result := Promise.WasSuccessful;
Promise.Free;
end;
procedure TMessageActionLoadUnreadMessages.Rollback;
begin
FMessageBox.Messages.Clear;
end;
{ TMessageActionReadMessageAndCollectItems }
constructor TMessageActionReadMessageAndCollectItems.Create(AMessage : TMessage);
begin
FMessage := AMessage;
inherited Create;
end;
procedure TMessageActionReadMessageAndCollectItems.Emulate;
begin
FIndex := FMessage.MessageBox.Messages.IndexOf(FMessage);
FMessage.MessageBox.Messages.Extract(FMessage);
end;
function TMessageActionReadMessageAndCollectItems.Execute : boolean;
var
Promise : TPromise<boolean>;
begin
Promise := MessageApi.ReadMessageAndCollectItems(FMessage.ID);
// currently nothing todo, because backchannels will do the unlock
Result := self.HandlePromise(Promise);
if Result then
FMessage.Free;
end;
procedure TMessageActionReadMessageAndCollectItems.Rollback;
begin
FMessage.MessageBox.Messages.Insert(FIndex, FMessage);
end;
end.