-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUpdateForm.pas
341 lines (300 loc) · 9.73 KB
/
UpdateForm.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
unit UpdateForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls, Spin,
ConcurrentTasks,
InflatablesList_Item,
InflatablesList_ItemShop,
InflatablesList_Manager;
type
TILItemShopUpdateItem = record
Item: TILItem;
ItemTitle: String;
ItemShop: TILItemShop;
Done: Boolean;
end;
TILItemShopUpdateList = array of TILItemShopUpdateItem;
TfUpdateForm = class(TForm)
pnlInfo: TPanel;
btnAction: TButton;
pbProgress: TProgressBar;
seNumberOfThreads: TSpinEdit;
lblNumberOfThreads: TLabel;
bvlSplit: TBevel;
lblLog: TLabel;
meLog: TMemo;
tmrUpdate: TTimer;
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
procedure btnActionClick(Sender: TObject);
procedure seNumberOfThreadsChange(Sender: TObject);
procedure meLogKeyPress(Sender: TObject; var Key: Char);
procedure tmrUpdateTimer(Sender: TObject);
private
fILManager: TILManager;
fUpdateList: TILItemShopUpdateList;
fUpdater: TCNTSManager;
fCanContinue: Boolean;
fProcessedIndex: Integer; // index of next item to be processed
fLoggedIndex: Integer; // index of next item to be logged
fDoneCount: Integer;
fLastItemName: String;
fMaxShopNameLen: Integer;
protected
procedure MakeLog(Index: Integer);
procedure ContinueProcessing;
procedure TaskFinishHandler(Sender: TObject; TaskIndex: Integer);
public
procedure Initialize(ILManager: TILManager);
procedure Finalize;
Function ShowUpdate(var UpdateList: TILItemShopUpdateList): Boolean;
end;
var
fUpdateForm: TfUpdateForm;
implementation
uses
StrRect,
InflatablesList_Utils;
{$R *.dfm}
const
IL_NR_OF_THREADS_COEF = 2;
//==============================================================================
type
TILUpdateTask = class(TCNTSTask)
private
fProcessingIndex: Integer;
fItemShop: TILItemShop;
public
constructor Create(ILManager: TILManager; ItemShop: TILItemShop; ProcessingIndex: Integer);
destructor Destroy; override;
Function Main: Boolean; override;
property ProcessingIndex: Integer read fProcessingIndex;
property ItemShop: TILItemShop read fItemShop;
end;
//==============================================================================
constructor TILUpdateTask.Create(ILManager: TILManager; ItemShop: TILItemShop; ProcessingIndex: Integer);
var
Index: Integer;
begin
inherited Create;
fProcessingIndex := ProcessingIndex;
fItemShop := TILItemShop.CreateAsCopy(ItemShop,True);
// resolve reference and make local copy of processing settings
Index := ILManager.ShopTemplateIndexOf(fItemShop.ParsingSettings.TemplateReference);
If Index >= 0 then
fItemShop.ReplaceParsingSettings(ILManager.ShopTemplates[Index].ParsingSettings);
end;
//------------------------------------------------------------------------------
destructor TILUpdateTask.Destroy;
begin
fItemShop.Free;
inherited;
end;
//------------------------------------------------------------------------------
Function TILUpdateTask.Main: Boolean;
begin
If not Terminated then
Result := fItemShop.Update
else
Result := False;
Cycle;
end;
//==============================================================================
//------------------------------------------------------------------------------
//==============================================================================
procedure TfUpdateForm.MakeLog(Index: Integer);
var
TagStr: String;
begin
If not IL_SameText(fLastItemName,fUpdateList[Index].ItemTitle) then
begin
If meLog.Lines.Count > 0 then
meLog.Lines.Add(''); // empty line
meLog.Lines.Add(fUpdateList[Index].ItemTitle + sLineBreak);
fLastItemName := fUpdateList[Index].ItemTitle;
end;
If fUpdateList[Index].ItemShop.Selected then
TagStr := ' * '
else
TagStr := ' ';
meLog.Lines.Add(IL_Format('%s%s %s... %s',[TagStr,fUpdateList[Index].ItemShop.Name,
StringOfChar('.',fMaxShopNameLen - Length(fUpdateList[Index].ItemShop.Name)),
fUpdateList[Index].ItemShop.LastUpdateMsg]));
end;
//------------------------------------------------------------------------------
procedure TfUpdateForm.ContinueProcessing;
var
Index: Integer;
begin
If Assigned(fUpdater) and fCanContinue then
while (fProcessedIndex <= High(fUpdateList)) and
(fUpdater.GetActiveTaskCount < fUpdater.MaxConcurrentTasks) do
begin
Index := fUpdater.AddTask(
TILUpdateTask.Create(fILManager,fUpdateList[fProcessedIndex].ItemShop,fProcessedIndex));
fUpdater.StartTask(Index);
Inc(fProcessedIndex);
end;
end;
//------------------------------------------------------------------------------
procedure TfUpdateForm.TaskFinishHandler(Sender: TObject; TaskIndex: Integer);
begin
Inc(fDoneCount);
// retrieve results from the task
with TILUpdateTask(fUpdater.Tasks[TaskIndex].TaskObject) do
begin
fUpdateList[ProcessingIndex].ItemShop.SetValues(ItemShop.LastUpdateMsg,
ItemShop.LastUpdateRes,ItemShop.Available,ItemShop.Price);
fUpdateList[ProcessingIndex].Done := True;
end;
// log
while fLoggedIndex <= High(fUpdateList) do
begin
If fUpdateList[fLoggedIndex].Done then
begin
MakeLog(fLoggedIndex);
Inc(fLoggedIndex);
end
else Break{while...};
end;
//progress
If fDoneCount < Length(fUpdateList) then
begin
// show progress
If Length(fUpdateList) > 0 then
pbProgress.Position := Trunc((fDoneCount / Length(fUpdateList)) * pbProgress.Max)
else
pbProgress.Position := pbProgress.Max;
pnlInfo.Caption := IL_Format('%d item shops ready for update',[Length(fUpdateList) - fDoneCount]);
end
else
begin
pnlInfo.Caption := 'All shops updated, see log for details';
pbProgress.Position := pbProgress.Max;
btnAction.Tag := 2;
btnAction.Caption := 'Done';
end;
end;
//==============================================================================
procedure TfUpdateForm.Initialize(ILManager: TILManager);
begin
fILManager := ILManager;
fUpdater := nil;
seNumberOfThreads.Value := TCNTSManager.GetProcessorCount * IL_NR_OF_THREADS_COEF;
end;
//------------------------------------------------------------------------------
procedure TfUpdateForm.Finalize;
begin
// nothing to do here
end;
//------------------------------------------------------------------------------
Function TfUpdateForm.ShowUpdate(var UpdateList: TILItemShopUpdateList): Boolean;
var
i: Integer;
begin
Result := False;
If Length(UpdateList) > 0 then
begin
// init form
pnlInfo.Caption := IL_Format('%d item shops ready for update',[Length(UpdateList)]);
btnAction.Tag := 0;
btnAction.Caption := 'Start';
pbProgress.Position := 0;
meLog.Clear;
// init list of shops for processing
fUpdateList := UpdateList;
For i := Low(fUpdateList) to High(fUpdateList) do
fUpdateList[i].Done := False; // should be false atm, but to be sure
// init processing vars
fProcessedIndex := 0;
fLoggedIndex := 0;
fDoneCount := 0;
fLastItemName := '';
// calculate indentation correction
fMaxShopNameLen := 0;
For i := Low(fUpdateList) to High(fUpdateList) do
If Length(fUpdateList[i].ItemShop.Name) > fMaxShopNameLen then
fMaxShopNameLen := Length(fUpdateList[i].ItemShop.Name);
// create updater and show the window
tmrUpdate.Enabled := True;
fUpdater := TCNTSManager.Create(True);
try
fUpdater.MaxConcurrentTasks := seNumberOfThreads.Value;
fUpdater.OnTaskCompleted := TaskFinishHandler;
fCanContinue := False;
ShowModal;
fUpdater.WaitForRunningTasksToComplete;
finally
FreeAndNil(fUpdater);
end;
tmrUpdate.Enabled := False;
// save log
If (meLog.Lines.Count > 0) and not fILManager.StaticSettings.NoUpdateAutoLog then
meLog.Lines.SaveToFile(StrToRTL(fILManager.StaticSettings.ListPath + fILManager.StaticSettings.ListName + '.update.log'));
// return the changed list (done flag is used)
UpdateList := fUpdateList;
// indicate whether something was done
For i := Low(UpdateList) to High(UpdateList) do
If UpdateList[i].Done then
begin
Result := True;
Break{For i};
end;
end
else MessageDlg('No shop to update.',mtInformation,[mbOK],0);
end;
//==============================================================================
procedure TfUpdateForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
fCanContinue := False;
CanClose := True;
end;
//------------------------------------------------------------------------------
procedure TfUpdateForm.btnActionClick(Sender: TObject);
begin
case btnAction.Tag of
0: begin
// start
btnAction.Tag := 1;
btnAction.Caption := 'Stop';
fCanContinue := True;
ContinueProcessing;
end;
1: begin
// stop
btnAction.Tag := 0;
btnAction.Caption := 'Start';
fCanContinue := False;
end;
else
// finished, do nothing
Close;
end;
end;
//------------------------------------------------------------------------------
procedure TfUpdateForm.seNumberOfThreadsChange(Sender: TObject);
begin
If Assigned(fUpdater) then
fUpdater.MaxConcurrentTasks := seNumberOfThreads.Value;
end;
//------------------------------------------------------------------------------
procedure TfUpdateForm.meLogKeyPress(Sender: TObject; var Key: Char);
begin
If Key = ^A then
begin
meLog.SelectAll;
Key := #0;
end;
end;
//------------------------------------------------------------------------------
procedure TfUpdateForm.tmrUpdateTimer(Sender: TObject);
begin
If Assigned(fUpdater) then
begin
fUpdater.Update;
fUpdater.ClearCompletedTasks;
If fCanContinue then
ContinueProcessing;
end;
end;
end.