source: trunk/Score.pas

Last change on this file was 110, checked in by chronos, 16 months ago
  • Added: Sorting by individual columns in Score window.
File size: 3.5 KB
Line 
1unit Score;
2
3interface
4
5uses
6 Classes, SysUtils, Generics.Collections, RegistryEx, ListViewSort;
7
8type
9
10 { TScore }
11
12 TScore = class
13 StartTime: TDateTime;
14 Duration: TDateTime;
15 Score: Integer;
16 Moves: Integer;
17 UsedUndos: Integer;
18 BoardSize: string;
19 DisabledTiles: Integer;
20 UnmergeableTiles: Integer;
21 BiggestTile: Integer;
22 procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
23 procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
24 end;
25
26 { TScores }
27
28 TScores = class(TObjectList<TScore>)
29 function SearchByTime(StartTime: TDateTime): TScore;
30 function AddNew: TScore;
31 procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
32 procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
33 procedure AssignToList(List: TObjects);
34 end;
35
36
37implementation
38
39{ TScore }
40
41procedure TScore.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
42begin
43 with Reg do begin
44 CurrentContext := RegContext;
45 WriteDateTime('StartTime', StartTime);
46 WriteDateTime('Duration', Duration);
47 WriteInteger('Score', Score);
48 WriteInteger('Moves', Moves);
49 WriteInteger('UsedUndos', UsedUndos);
50 WriteString('BoardSize', BoardSize);
51 WriteInteger('DisabledTiles', DisabledTiles);
52 WriteInteger('UnmergeableTiles', UnmergeableTiles);
53 WriteInteger('BiggestTile', BiggestTile);
54 end;
55end;
56
57procedure TScore.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext
58 );
59begin
60 with Reg do begin
61 CurrentContext := RegContext;
62 StartTime := ReadDateTimeWithDefault('StartTime', StartTime);
63 Duration := ReadDateTimeWithDefault('Duration', Duration);
64 Score := ReadIntegerWithDefault('Score', Score);
65 Moves := ReadIntegerWithDefault('Moves', Moves);
66 UsedUndos := ReadIntegerWithDefault('UsedUndos', UsedUndos);
67 BoardSize := ReadStringWithDefault('BoardSize', BoardSize);
68 DisabledTiles := ReadIntegerWithDefault('DisabledTiles', DisabledTiles);
69 UnmergeableTiles := ReadIntegerWithDefault('UnmergeableTiles', UnmergeableTiles);
70 BiggestTile := ReadIntegerWithDefault('BiggestTile', BiggestTile);
71 end;
72end;
73
74{ TScores }
75
76function TScores.SearchByTime(StartTime: TDateTime): TScore;
77var
78 I: Integer;
79begin
80 I := 0;
81 while (I < Count) and (Items[I].StartTime <> StartTime) do Inc(I);
82 if I < Count then Result := Items[I]
83 else Result := nil;
84end;
85
86function TScores.AddNew: TScore;
87begin
88 Result := TScore.Create;
89 Add(Result);
90end;
91
92procedure TScores.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext
93 );
94var
95 I: Integer;
96begin
97 with Reg do begin
98 CurrentContext := RegContext;
99
100 WriteInteger('Count', Count);
101 for I := 0 to Count - 1 do
102 Items[I].SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Score' + IntToStr(I)));
103 end;
104end;
105
106procedure TScores.LoadFromRegistry(Reg: TRegistryEx;
107 RegContext: TRegistryContext);
108var
109 I: Integer;
110 C: Integer;
111 NewScore: TScore;
112begin
113 with Reg do begin
114 CurrentContext := RegContext;
115
116 C := ReadIntegerWithDefault('Count', 0);
117 for I := 0 to C - 1 do begin
118 NewScore := AddNew;
119 NewScore.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Score' + IntToStr(I)));
120 end;
121 end;
122end;
123
124procedure TScores.AssignToList(List: TObjects);
125var
126 I: Integer;
127begin
128 while List.Count > Count do List.Delete(List.Count - 1);
129 for I := 0 to List.Count - 1 do List[I] := Items[I];
130 while List.Count < Count do List.Add(Items[List.Count]);
131end;
132
133end.
134
Note: See TracBrowser for help on using the repository browser.