-
Notifications
You must be signed in to change notification settings - Fork 3
/
History.pas
428 lines (407 loc) · 11.5 KB
/
History.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
unit History;
{$MODE Delphi}
interface
uses
LCLIntf, LCLType, LMessages, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, ComCtrls, {JvExGrids, JvStringGrid,} Contnrs, ExtCtrls,
StdCtrls, Buttons;
type
TPStat = class(TObject)
public
player: string; // player's name
games, // number of games played
won, // number of games won
points: integer; // points
end;
type
TfHistory = class(TForm)
pagHistory: TPageControl;
tbsHistory: TTabSheet;
lstHistory: TListView;
tbsRanking: TTabSheet;
grdRanking: TStringGrid;
tbsCompare: TTabSheet;
cboPlayer1: TComboBox;
Label1: TLabel;
Label2: TLabel;
cboPlayer2: TComboBox;
Label3: TLabel;
cmdAnalyze: TBitBtn;
Bevel1: TBevel;
Bevel2: TBevel;
panAnalyze: TPanel;
prbPlayer2: TProgressBar;
prbPlayer1: TProgressBar;
lblAnalyze: TLabel;
lblPlayer1: TLabel;
lblPlayer2: TLabel;
function SplitStr(var theString: string; delimiter: string): string;
procedure FormShow(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure grdRankingCaptionClick(Sender: TStringGrid;
AColumn, ARow: integer);
procedure grdRankingDrawCell(Sender: TObject; ACol, ARow: integer;
Rect: TRect; State: TGridDrawState);
procedure GridCompareCells(Sender: TObject; ACol, ARow, BCol, BRow: Integer; var Result: integer);
procedure cmdAnalyzeClick(Sender: TObject);
procedure lstHistoryColumnClick(Sender: TObject; Column: TListColumn);
procedure lstHistoryCompare(Sender: TObject; Item1, Item2: TListItem;
Data: integer; var Compare: integer);
private
uHRanking: TObjectList;
iColumnClicked, iSortCol, iTotGames: integer;
bAscending: boolean;
procedure LoadHistoryFile;
function SearchPlayer(const sKey: string; uList: TObjectList): integer;
public
{ Public declarations }
end;
var
fHistory: TfHistory;
implementation
{$R *.lfm}
uses Globals{, StdPas};
procedure TfHistory.FormCreate(Sender: TObject);
var
i: integer;
begin
with lstHistory.Columns.Add do begin
Caption := 'Date';
Width := 65;
end;
with lstHistory.Columns.Add do begin
Caption := 'Players';
Width := 50;
end;
with lstHistory.Columns.Add do begin
Caption := 'Winner';
Width := 80;
end;
with lstHistory.Columns.Add do begin
Caption := '2nd';
Width := 80;
end;
with lstHistory.Columns.Add do begin
Caption := '3rd';
Width := 80;
end;
for i := 4 to 10 do begin
with lstHistory.Columns.Add do begin
Caption := IntToStr(i) + 'th';
Width := 80;
end;
end;
with grdRanking do begin
RowCount := 1;
ColCount := 6;
Cells[0, 0] := 'Player';
Cells[1, 0] := 'Games';
Cells[2, 0] := 'Won';
Cells[3, 0] := '% Won';
Cells[4, 0] := 'Points';
Cells[5, 0] := 'P.ts/Game';
ColWidths[0] := 100;
ColWidths[1] := 70;
ColWidths[2] := 70;
ColWidths[3] := 70;
ColWidths[4] := 70;
ColWidths[5] := 70;
end;
iSortCol := 0;
end;
function TfHistory.SplitStr(var theString: string; delimiter: string): string;
var
i: integer;
begin
Result:= '';
if theString <> '' then
begin
i:= Pos(delimiter, theString);
if i > 0 then
begin
Result:= Copy(theString, 1, i-1);
theString:= Copy(theString, i+Length(delimiter), maxLongInt);
end
else
begin
Result:= theString;
theString:= '';
end;
end;
end;
procedure TfHistory.FormShow(Sender: TObject);
begin
Screen.Cursor := crHourGlass;
iColumnClicked := -1;
LoadHistoryFile;
bAscending := true;
panAnalyze.Visible := false;
Screen.Cursor := crDefault;
end;
procedure TfHistory.LoadHistoryFile;
var
fHistory: textfile;
iMaxRank, iP, iL: integer;
sRec, sTmp: string;
dDate: TDate;
begin
iTotGames := 0;
// open history file
if not FileExists(sG_AppPath + 'history.txt') then
exit;
AssignFile(fHistory, sG_AppPath + 'history.txt');
Reset(fHistory);
// prepare list
lstHistory.Clear;
// create object for statistics
uHRanking := TObjectList.Create;
// load list
try
// read history file
while not Eof(fHistory) do begin
Readln(fHistory, sRec);
inc(iTotGames);
// date
sTmp := SplitStr(sRec, ',');
dDate := EncodeDate(StrToInt(copy(sTmp, 1, 4)),
StrToInt(copy(sTmp, 5, 2)), StrToInt(copy(sTmp, 7, 2)));
// max rank
sTmp := SplitStr(sRec, ',');
iMaxRank := StrToInt(sTmp);
// list item
with lstHistory.Items.Add do begin
Caption := FormatDateTime('yyyy-mm-dd', dDate);
SubItems.Add(IntToStr(iMaxRank));
for iP := 1 to MAXPLAYERS do begin
if iP <= iMaxRank then begin
// add player to list item
sTmp := lowercase(SplitStr(sRec, ','));
SubItems.Add(sTmp);
// search player in ranking list
iL := SearchPlayer(sTmp, uHRanking);
if iL < 0 then begin
// not found, add new player
iL := uHRanking.Count;
uHRanking.Add(TPStat.Create);
with uHRanking[iL] as TPStat do begin
player := sTmp;
games := 1;
if iP = 1 then
won := 1
else
won := 0;
points := iMaxRank - iP + 1;
end;
end
else begin
// found, increment counters
with uHRanking[iL] as TPStat do begin
inc(games);
if iP = 1 then
inc(won);
points := points + iMaxRank - iP + 1;
end;
end;
end else begin
SubItems.Add('');
end;
end;
end;
end;
// load ranking grid
with grdRanking do begin
RowCount := uHRanking.Count + 1;
for iL := 0 to uHRanking.Count - 1 do begin
with uHRanking[iL] as TPStat do begin
Cells[0, iL + 1] := player;
Cells[1, iL + 1] := FormatFloat('#,##0', games);
Cells[2, iL + 1] := FormatFloat('#,##0', won);
Cells[4, iL + 1] := FormatFloat('#,##0', points);
if games > 0 then begin
Cells[3, iL + 1] := FormatFloat('0.0"%"', (100.0 * won) / games);
Cells[5, iL + 1] := FormatFloat('0.0', points / games);
end
else begin
Cells[3, iL + 1] := '-';
Cells[5, iL + 1] := '-';
end;
end;
end;
if RowCount > 1 then
FixedRows := 1;
// SortGrid(0, true);
iSortCol := 0;
end;
// load compare combos
cboPlayer1.Clear;
cboPlayer2.Clear;
for iL := 0 to uHRanking.Count - 1 do begin
cboPlayer1.Items.Add((uHRanking[iL] as TPStat).player);
cboPlayer2.Items.Add((uHRanking[iL] as TPStat).player);
end;
finally
CloseFile(fHistory);
uHRanking.Free;
end;
end;
procedure TfHistory.lstHistoryColumnClick(Sender: TObject; Column: TListColumn);
begin
if Column.Index = iColumnClicked then begin
bAscending := not bAscending
end
else begin
bAscending := true;
iColumnClicked := Column.Index;
end;
Screen.Cursor := crHourGlass;
try (Sender as TListView)
.AlphaSort;
finally
Screen.Cursor := crDefault;
end;
end;
procedure TfHistory.lstHistoryCompare(Sender: TObject; Item1, Item2: TListItem;
Data: integer; var Compare: integer);
begin
case iColumnClicked of
0:
Compare := CompareStr(Item1.Caption, Item2.Caption);
else
Compare := CompareStr(Item1.SubItems[iColumnClicked - 1],
Item2.SubItems[iColumnClicked - 1]);
end;
if not bAscending then
Compare := -Compare;
end;
function TfHistory.SearchPlayer(const sKey: string;
uList: TObjectList): integer;
var
i: integer;
begin
for i := 0 to uList.Count - 1 do begin
if TPStat(uList[i]).player = sKey then begin
result := i;
exit;
end;
end;
result := -1;
end;
procedure TfHistory.grdRankingCaptionClick(Sender: TStringGrid;
AColumn, ARow: integer);
begin
Screen.Cursor := crHourGlass;
with (Sender as TStringGrid) do begin
iSortCol := AColumn;
{ if AColumn = 0 then
SortGrid(0, true)
else
SortGrid(AColumn, false, false, stNumeric, false);
if RowCount > 0 then
Row := 1
else
Row := 0; }
end;
Screen.Cursor := crDefault;
end;
procedure TfHistory.GridCompareCells(Sender: TObject; ACol, ARow, BCol, BRow: Integer; var Result: integer);
begin
// Result will be either <0, =0, or >0 for normal order.
if (ACol = 1) or (ACol = 2) or (ACol = 4) then begin
result := StrToIntDef(grdRanking.Cells[ACol,ARow],0)-StrToIntDef(grdRanking.Cells[BCol,BRow],0);
end
else if ACol = 3 then begin
result := round((StrToFloatDef(StringReplace(grdRanking.Cells[ACol,ARow],'%','',[rfReplaceAll,rfIgnoreCase]),0)-StrToFloatDef(StringReplace(grdRanking.Cells[BCol,BRow],'%','',[rfReplaceAll,rfIgnoreCase]),0))*10);
end
else if ACol = 5 then begin
result := round((StrToFloatDef(grdRanking.Cells[ACol,ARow],0)-StrToFloatDef(grdRanking.Cells[BCol,BRow],0))*10);
end;
// For inverse order, just negate the result (eg. based on grid's SortOrder).
if grdRanking.SortOrder = soDescending then
result := -result;
end;
procedure TfHistory.grdRankingDrawCell(Sender: TObject; ACol, ARow: integer;
Rect: TRect; State: TGridDrawState);
begin
with grdRanking.Canvas do begin
if ARow = 0 then begin
Brush.color := clBtnFace;
Font.color := clBlack;
Font.Style := [fsBold];
FillRect(Rect);
end
else begin
if ACol = iSortCol then
Brush.color := clCream
else
Brush.color := clWhite;
Font.color := clBlack;
Font.Style := [];
FillRect(Rect);
end;
if ACol = 0 then begin
TextOut(Rect.Left + 1, Rect.Top + 1, grdRanking.Cells[ACol, ARow]);
end
else begin
TextOut(Rect.Right - TextWidth(grdRanking.Cells[ACol, ARow]) - 3,
Rect.Top + 1, grdRanking.Cells[ACol, ARow]);
end;
end;
end;
procedure TfHistory.cmdAnalyzeClick(Sender: TObject);
var
i, j, iR1, iR2, iT1, iT2, iTT: integer;
sP1, sP2: string;
begin
// check for legal case
if (cboPlayer1.ItemIndex < 0) or (cboPlayer2.ItemIndex < 0) or
(cboPlayer1.ItemIndex = cboPlayer2.ItemIndex) then
exit;
// analyze
Screen.Cursor := crHourGlass;
sP1 := cboPlayer1.Text;
sP2 := cboPlayer2.Text;
iT1 := 0;
iT2 := 0;
// scan history
for i := 0 to lstHistory.Items.Count - 1 do begin
iR1 := 0;
iR2 := 0;
// scan game
with lstHistory.Items[i] do begin
for j := 1 to SubItems.Count - 1 do begin
if SubItems[j] = sP1 then
iR1 := j
else if SubItems[j] = sP2 then
iR2 := j;
end;
end;
if (iR1 > 0) and (iR2 > 0) then begin
if iR1 < iR2 then
inc(iT1)
else
inc(iT2);
end;
end;
// show results
iTT := iT1 + iT2;
if iTT > 0 then begin
prbPlayer1.Position := trunc(100.0 * iT1 / iTT + 0.5);
prbPlayer2.Position := trunc(100.0 * iT2 / iTT + 0.5);
lblAnalyze.Caption := 'Results of ' + IntToStr(iTT)
+ ' games including ' + sP1 + ' and ' + sP2;
lblPlayer1.Caption := sP1 + ' was better ' + IntToStr(iT1)
+ ' times (' + IntToStr(prbPlayer1.Position) + '%)';
lblPlayer2.Caption := sP2 + ' was better ' + IntToStr(iT2)
+ ' times (' + IntToStr(prbPlayer2.Position) + '%)';
end
else begin
prbPlayer1.Position := 0;
prbPlayer2.Position := 0;
lblAnalyze.Caption := 'No games including both ' + sP1 + ' and ' + sP2;
lblPlayer1.Caption := '';
lblPlayer2.Caption := '';
end;
panAnalyze.Visible := true;
Screen.Cursor := crDefault;
end;
end.