Rev 9 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 9 | Rev 21 | ||
---|---|---|---|
1 | unit Finish; |
1 | unit Finish; |
2 | 2 | ||
3 | interface |
3 | interface |
4 | 4 | ||
5 | uses |
5 | uses |
6 | Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, |
6 | Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, |
7 | Dialogs, StdCtrls, MMSystem, Math, ExtCtrls, Registry, Main; |
7 | Dialogs, StdCtrls, MMSystem, Math, ExtCtrls, Registry, LevelFunctions; |
8 | 8 | ||
9 | type |
9 | type |
10 | TFinishForm = class(TForm) |
10 | TFinishForm = class(TForm) |
11 | Label1: TLabel; |
11 | Label1: TLabel; |
12 | Label2: TLabel; |
12 | Label2: TLabel; |
13 | SaveBtn: TButton; |
13 | SaveBtn: TButton; |
14 | CancelBtn: TButton; |
14 | CancelBtn: TButton; |
15 | NameEdit: TEdit; |
15 | NameEdit: TEdit; |
16 | PerformanceMemo: TMemo; |
16 | PerformanceMemo: TMemo; |
17 | Label3: TLabel; |
17 | Label3: TLabel; |
18 | CupImage: TImage; |
18 | CupImage: TImage; |
19 | ReplayCheckBox: TCheckBox; |
19 | ReplayCheckBox: TCheckBox; |
20 | JumpHistoryLink: TLabel; |
20 | JumpHistoryLink: TLabel; |
21 | procedure CancelBtnClick(Sender: TObject); |
21 | procedure CancelBtnClick(Sender: TObject); |
22 | procedure SaveBtnClick(Sender: TObject); |
22 | procedure SaveBtnClick(Sender: TObject); |
23 | procedure JumpHistoryLinkClick(Sender: TObject); |
23 | procedure JumpHistoryLinkClick(Sender: TObject); |
24 | procedure FormCreate(Sender: TObject); |
24 | procedure FormCreate(Sender: TObject); |
25 | private |
25 | private |
26 | FLevel: String; |
26 | FLevel: String; |
27 | FScore: integer; |
27 | FScore: integer; |
28 | FStonesTotal: integer; |
28 | FStonesTotal: integer; |
29 | FStonesRemoved: integer; |
29 | FStonesRemoved: integer; |
30 | FSeconds: integer; |
30 | FSeconds: integer; |
31 | FHistory: TStringList; |
31 | FHistory: TStringList; |
32 | FGoalStatus: TGoalStatus; |
32 | FGoalStatus: TGoalStatus; |
33 | procedure SaveToJournal(PlayerName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer); |
33 | procedure SaveToJournal(PlayerName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer); |
34 | procedure LoadSettings; |
34 | procedure LoadSettings; |
35 | public |
35 | public |
36 | procedure SaveSettings; |
36 | procedure SaveSettings; |
37 | function Execute(LevelName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer; GoalStatus: TGoalStatus; JumpHistory: TStringList): Integer; |
37 | function Execute(LevelName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer; GoalStatus: TGoalStatus; JumpHistory: TStringList): Integer; |
38 | end; |
38 | end; |
39 | 39 | ||
40 | var |
40 | var |
41 | FinishForm: TFinishForm; |
41 | FinishForm: TFinishForm; |
42 | 42 | ||
43 | implementation |
43 | implementation |
44 | 44 | ||
45 | uses |
45 | uses |
46 | History, Constants, LevelFunctions; |
46 | History, Constants; |
47 | 47 | ||
48 | {$R *.dfm} |
48 | {$R *.dfm} |
49 | 49 | ||
50 | procedure TFinishForm.CancelBtnClick(Sender: TObject); |
50 | procedure TFinishForm.CancelBtnClick(Sender: TObject); |
51 | begin |
51 | begin |
52 | Close; |
52 | Close; |
53 | end; |
53 | end; |
54 | 54 | ||
55 | function TFinishForm.Execute(LevelName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer; GoalStatus: TGoalStatus; JumpHistory: TStringList): Integer; |
55 | function TFinishForm.Execute(LevelName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer; GoalStatus: TGoalStatus; JumpHistory: TStringList): Integer; |
- | 56 | resourcestring |
|
- | 57 | LNG_SCORE = 'Score: %d'; |
|
- | 58 | LNG_REMAINING = 'Remaining stones: %d (%f%%)'; |
|
- | 59 | LNG_TIME_SECONDS = 'Time: %d seconds'; |
|
- | 60 | LNG_POINTS_PER_MINUTE = '%d points per minute'; |
|
- | 61 | LNG_GOAL_RED = 'Red stone in target field (%d points)'; |
|
- | 62 | LNG_GOAL_YELLOW = 'Yellow stone in target field (%d points)'; |
|
- | 63 | LNG_GOAL_GREEN = 'Green stone in target field (%d points)'; |
|
- | 64 | LNG_GOAL_MISSED = 'No stone in target field (%d points)'; |
|
56 | var |
65 | var |
57 | ExtraPoints: Integer; |
66 | ExtraPoints: Integer; |
58 | begin |
67 | begin |
59 | FLevel := LevelName; |
68 | FLevel := LevelName; |
60 | FScore := Score; |
69 | FScore := Score; |
61 | FStonesTotal := StonesTotal; |
70 | FStonesTotal := StonesTotal; |
62 | FStonesRemoved := StonesRemoved; |
71 | FStonesRemoved := StonesRemoved; |
63 | FSeconds := Seconds; |
72 | FSeconds := Seconds; |
64 | FGoalStatus := GoalStatus; |
73 | FGoalStatus := GoalStatus; |
65 | FHistory := JumpHistory; |
74 | FHistory := JumpHistory; |
66 | 75 | ||
67 | PerformanceMemo.Lines.Clear; |
76 | PerformanceMemo.Lines.Clear; |
68 | PerformanceMemo.Lines.Add(''); |
77 | PerformanceMemo.Lines.Add(''); |
69 | PerformanceMemo.Lines.Add(Format(LNG_REMAINING, [FStonesTotal - 1 - FStonesRemoved, |
78 | PerformanceMemo.Lines.Add(Format(LNG_REMAINING, [FStonesTotal - 1 - FStonesRemoved, |
70 | RoundTo(((FStonesTotal - 1 - FStonesRemoved) / FStonesTotal * 100), -2)])); |
79 | RoundTo(((FStonesTotal - 1 - FStonesRemoved) / FStonesTotal * 100), -2)])); |
71 | PerformanceMemo.Lines.Add(Format(LNG_TIME_SECONDS, [FSeconds])); |
80 | PerformanceMemo.Lines.Add(Format(LNG_TIME_SECONDS, [FSeconds])); |
72 | PerformanceMemo.Lines.Add(Format(LNG_POINTS_PER_MINUTE, [Round(FScore / FSeconds * 60)])); |
81 | PerformanceMemo.Lines.Add(Format(LNG_POINTS_PER_MINUTE, [Round(FScore / FSeconds * 60)])); |
73 | 82 | ||
74 | if FGoalStatus = gsLastStoneInGoalRed then |
83 | if FGoalStatus = gsLastStoneInGoalRed then |
75 | begin |
84 | begin |
76 | ExtraPoints := FieldTypeWorth(ftRed) * 100; |
85 | ExtraPoints := FieldTypeWorth(ftRed) * 100; |
77 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_RED, [ExtraPoints])); |
86 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_RED, [ExtraPoints])); |
78 | Inc(FScore, ExtraPoints); |
87 | Inc(FScore, ExtraPoints); |
79 | end |
88 | end |
80 | else if FGoalStatus = gsLastStoneInGoalYellow then |
89 | else if FGoalStatus = gsLastStoneInGoalYellow then |
81 | begin |
90 | begin |
82 | ExtraPoints := FieldTypeWorth(ftYellow) * 100; |
91 | ExtraPoints := FieldTypeWorth(ftYellow) * 100; |
83 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_YELLOW, [ExtraPoints])); |
92 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_YELLOW, [ExtraPoints])); |
84 | Inc(FScore, ExtraPoints); |
93 | Inc(FScore, ExtraPoints); |
85 | end |
94 | end |
86 | else if FGoalStatus = gsLastStoneInGoalGreen then |
95 | else if FGoalStatus = gsLastStoneInGoalGreen then |
87 | begin |
96 | begin |
88 | ExtraPoints := FieldTypeWorth(ftGreen) * 100; |
97 | ExtraPoints := FieldTypeWorth(ftGreen) * 100; |
89 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_GREEN, [ExtraPoints])); |
98 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_GREEN, [ExtraPoints])); |
90 | Inc(FScore, ExtraPoints); |
99 | Inc(FScore, ExtraPoints); |
91 | end |
100 | end |
92 | else if FGoalStatus = gsLastStoneOutsideGoal then |
101 | else if FGoalStatus = gsLastStoneOutsideGoal then |
93 | begin |
102 | begin |
94 | ExtraPoints := 0; |
103 | ExtraPoints := 0; |
95 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_MISSED, [ExtraPoints])); |
104 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_MISSED, [ExtraPoints])); |
96 | Inc(FScore, ExtraPoints); |
105 | Inc(FScore, ExtraPoints); |
97 | end; |
106 | end; |
98 | 107 | ||
99 | PerformanceMemo.Lines.Strings[0] := Format(LNG_SCORE, [FScore]); |
108 | PerformanceMemo.Lines.Strings[0] := Format(LNG_SCORE, [FScore]); |
100 | 109 | ||
101 | result := ShowModal; |
110 | result := ShowModal; |
102 | end; |
111 | end; |
103 | 112 | ||
104 | procedure TFinishForm.SaveToJournal(PlayerName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer); |
113 | procedure TFinishForm.SaveToJournal(PlayerName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer); |
105 | var |
114 | var |
106 | f: textfile; |
115 | f: textfile; |
107 | tmp: string; |
116 | tmp: string; |
108 | begin |
117 | begin |
109 | tmp := Format(JNL_FILE, [FLevel]); |
118 | tmp := Format(JNL_FILE, [FLevel]); |
110 | 119 | ||
111 | AssignFile(f, tmp); |
120 | AssignFile(f, tmp); |
112 | if FileExists(tmp) then |
121 | if FileExists(tmp) then |
113 | Append(f) |
122 | Append(f) |
114 | else |
123 | else |
115 | ReWrite(f); |
124 | ReWrite(f); |
116 | 125 | ||
117 | // TODO: Maybe we should do much more details, like, how many green stones were removed, how many yellows etc., and which stone was in the goal? |
126 | // TODO: Maybe we should do much more details, like, how many green stones were removed, how many yellows etc., and which stone was in the goal? |
118 | WriteLn(f, Format(JNL_ENTRY, [DateTimeToStr(now()), NameEdit.Text, FScore, FSeconds, FStonesRemoved, FStonesTotal-1])); |
127 | WriteLn(f, Format(JNL_ENTRY, [DateTimeToStr(now()), NameEdit.Text, FScore, FSeconds, FStonesRemoved, FStonesTotal-1])); |
119 | 128 | ||
120 | CloseFile(f); |
129 | CloseFile(f); |
121 | end; |
130 | end; |
122 | 131 | ||
123 | procedure TFinishForm.SaveSettings; |
132 | procedure TFinishForm.SaveSettings; |
124 | var |
133 | var |
125 | reg: TRegistry; |
134 | reg: TRegistry; |
126 | begin |
135 | begin |
127 | reg := TRegistry.Create; |
136 | reg := TRegistry.Create; |
128 | try |
137 | try |
129 | reg.RootKey := HKEY_CURRENT_USER; |
138 | reg.RootKey := HKEY_CURRENT_USER; |
130 | if reg.OpenKey(REG_KEY, true) then |
139 | if reg.OpenKey(REG_KEY, true) then |
131 | begin |
140 | begin |
132 | reg.WriteString(REG_PLAYERNAME, NameEdit.Text); |
141 | reg.WriteString(REG_PLAYERNAME, NameEdit.Text); |
133 | reg.WriteBool(REG_REPLAY, ReplayCheckbox.Checked); |
142 | reg.WriteBool(REG_REPLAY, ReplayCheckbox.Checked); |
134 | reg.CloseKey; |
143 | reg.CloseKey; |
135 | end; |
144 | end; |
136 | finally |
145 | finally |
137 | reg.Free; |
146 | reg.Free; |
138 | end; |
147 | end; |
139 | end; |
148 | end; |
140 | 149 | ||
141 | procedure TFinishForm.LoadSettings; |
150 | procedure TFinishForm.LoadSettings; |
142 | var |
151 | var |
143 | reg: TRegistry; |
152 | reg: TRegistry; |
144 | begin |
153 | begin |
145 | NameEdit.Text := ''; |
154 | NameEdit.Text := ''; |
146 | reg := TRegistry.Create; |
155 | reg := TRegistry.Create; |
147 | try |
156 | try |
148 | reg.RootKey := HKEY_CURRENT_USER; |
157 | reg.RootKey := HKEY_CURRENT_USER; |
149 | if reg.OpenKeyReadOnly(REG_KEY) then |
158 | if reg.OpenKeyReadOnly(REG_KEY) then |
150 | begin |
159 | begin |
151 | if reg.ValueExists(REG_PLAYERNAME) then |
160 | if reg.ValueExists(REG_PLAYERNAME) then |
152 | NameEdit.Text := reg.ReadString(REG_PLAYERNAME); |
161 | NameEdit.Text := reg.ReadString(REG_PLAYERNAME); |
153 | if reg.ValueExists(REG_REPLAY) then |
162 | if reg.ValueExists(REG_REPLAY) then |
154 | ReplayCheckBox.Checked := reg.ReadBool(REG_REPLAY); |
163 | ReplayCheckBox.Checked := reg.ReadBool(REG_REPLAY); |
155 | reg.CloseKey; |
164 | reg.CloseKey; |
156 | end; |
165 | end; |
157 | finally |
166 | finally |
158 | reg.Free; |
167 | reg.Free; |
159 | end; |
168 | end; |
160 | end; |
169 | end; |
161 | 170 | ||
162 | procedure TFinishForm.SaveBtnClick(Sender: TObject); |
171 | procedure TFinishForm.SaveBtnClick(Sender: TObject); |
- | 172 | resourcestring |
|
- | 173 | LNG_ENTER_NAME = 'Please enter your name to get added to the high score lists.'; |
|
163 | begin |
174 | begin |
164 | if NameEdit.Text = '' then |
175 | if NameEdit.Text = '' then |
165 | begin |
176 | begin |
166 | MessageDlg(LNG_ENTER_NAME, mtWarning, [mbOk], 0); |
177 | MessageDlg(LNG_ENTER_NAME, mtWarning, [mbOk], 0); |
167 | NameEdit.SetFocus; |
178 | NameEdit.SetFocus; |
168 | Exit; |
179 | Exit; |
169 | end; |
180 | end; |
170 | 181 | ||
171 | SaveToJournal(NameEdit.Text, FScore, FStonesTotal, FStonesRemoved, FSeconds); |
182 | SaveToJournal(NameEdit.Text, FScore, FStonesTotal, FStonesRemoved, FSeconds); |
172 | ModalResult := mrOK; |
183 | ModalResult := mrOK; |
173 | end; |
184 | end; |
174 | 185 | ||
175 | procedure TFinishForm.JumpHistoryLinkClick(Sender: TObject); |
186 | procedure TFinishForm.JumpHistoryLinkClick(Sender: TObject); |
176 | begin |
187 | begin |
177 | HistoryForm.JumpMemo.Lines.Assign(FHistory); |
188 | HistoryForm.JumpMemo.Lines.Assign(FHistory); |
178 | HistoryForm.ShowModal(); |
189 | HistoryForm.ShowModal(); |
179 | end; |
190 | end; |
180 | 191 | ||
181 | procedure TFinishForm.FormCreate(Sender: TObject); |
192 | procedure TFinishForm.FormCreate(Sender: TObject); |
182 | begin |
193 | begin |
183 | if not ForceDirectories(ExtractFilePath(Application.ExeName) + JNL_PATH) then |
194 | if not ForceDirectories(ExtractFilePath(Application.ExeName) + JNL_PATH) then |
184 | begin |
195 | begin |
185 | MessageDlg(Format(LNG_COULD_NOT_CREATE_DIR, [JNL_PATH]), mtError, [mbOk], 0); |
196 | MessageDlg(Format(LNG_COULD_NOT_CREATE_DIR, [JNL_PATH]), mtError, [mbOk], 0); |
186 | end; |
197 | end; |
187 | 198 | ||
188 | LoadSettings; |
199 | LoadSettings; |
189 | end; |
200 | end; |
190 | 201 | ||
191 | end. |
202 | end. |
192 | 203 |