Rev 8 | Rev 21 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 8 | Rev 9 | ||
---|---|---|---|
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, Main; |
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, LevelFunctions; |
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 | var |
56 | var |
57 | ExtraPoints: Integer; |
57 | ExtraPoints: Integer; |
58 | begin |
58 | begin |
59 | FLevel := LevelName; |
59 | FLevel := LevelName; |
60 | FScore := Score; |
60 | FScore := Score; |
61 | FStonesTotal := StonesTotal; |
61 | FStonesTotal := StonesTotal; |
62 | FStonesRemoved := StonesRemoved; |
62 | FStonesRemoved := StonesRemoved; |
63 | FSeconds := Seconds; |
63 | FSeconds := Seconds; |
64 | FGoalStatus := GoalStatus; |
64 | FGoalStatus := GoalStatus; |
65 | FHistory := JumpHistory; |
65 | FHistory := JumpHistory; |
66 | 66 | ||
67 | PerformanceMemo.Lines.Clear; |
67 | PerformanceMemo.Lines.Clear; |
68 | PerformanceMemo.Lines.Add(''); |
68 | PerformanceMemo.Lines.Add(''); |
69 | PerformanceMemo.Lines.Add(Format(LNG_REMAINING, [FStonesTotal - 1 - FStonesRemoved, |
69 | PerformanceMemo.Lines.Add(Format(LNG_REMAINING, [FStonesTotal - 1 - FStonesRemoved, |
70 | RoundTo(((FStonesTotal - 1 - FStonesRemoved) / FStonesTotal * 100), -2)])); |
70 | RoundTo(((FStonesTotal - 1 - FStonesRemoved) / FStonesTotal * 100), -2)])); |
71 | PerformanceMemo.Lines.Add(Format(LNG_TIME_SECONDS, [FSeconds])); |
71 | PerformanceMemo.Lines.Add(Format(LNG_TIME_SECONDS, [FSeconds])); |
72 | PerformanceMemo.Lines.Add(Format(LNG_POINTS_PER_MINUTE, [Round(FScore / FSeconds * 60)])); |
72 | PerformanceMemo.Lines.Add(Format(LNG_POINTS_PER_MINUTE, [Round(FScore / FSeconds * 60)])); |
73 | 73 | ||
74 | if FGoalStatus = gsLastStoneInGoalRed then |
74 | if FGoalStatus = gsLastStoneInGoalRed then |
75 | begin |
75 | begin |
76 | ExtraPoints := FieldTypeWorth(ftRed) * 100; |
76 | ExtraPoints := FieldTypeWorth(ftRed) * 100; |
77 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_RED, [ExtraPoints])) |
77 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_RED, [ExtraPoints])); |
- | 78 | Inc(FScore, ExtraPoints); |
|
78 | end |
79 | end |
79 | else if FGoalStatus = gsLastStoneInGoalYellow then |
80 | else if FGoalStatus = gsLastStoneInGoalYellow then |
80 | begin |
81 | begin |
81 | ExtraPoints := FieldTypeWorth(ftYellow) * 100; |
82 | ExtraPoints := FieldTypeWorth(ftYellow) * 100; |
82 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_YELLOW, [ExtraPoints])) |
83 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_YELLOW, [ExtraPoints])); |
- | 84 | Inc(FScore, ExtraPoints); |
|
83 | end |
85 | end |
84 | else if FGoalStatus = gsLastStoneInGoalGreen then |
86 | else if FGoalStatus = gsLastStoneInGoalGreen then |
85 | begin |
87 | begin |
86 | ExtraPoints := FieldTypeWorth(ftGreen) * 100; |
88 | ExtraPoints := FieldTypeWorth(ftGreen) * 100; |
87 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_GREEN, [ExtraPoints])) |
89 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_GREEN, [ExtraPoints])); |
- | 90 | Inc(FScore, ExtraPoints); |
|
88 | end |
91 | end |
89 | else if FGoalStatus = gsLastStoneOutsideGoal then |
92 | else if FGoalStatus = gsLastStoneOutsideGoal then |
90 | begin |
93 | begin |
91 | ExtraPoints := 0; |
94 | ExtraPoints := 0; |
92 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_MISSED, [ExtraPoints])) |
95 | PerformanceMemo.Lines.Add(Format(LNG_GOAL_MISSED, [ExtraPoints])); |
- | 96 | Inc(FScore, ExtraPoints); |
|
93 | end; |
97 | end; |
94 | 98 | ||
95 | Inc(FScore, ExtraPoints); |
- | |
96 | PerformanceMemo.Lines.Strings[0] := Format(LNG_SCORE, [FScore]); |
99 | PerformanceMemo.Lines.Strings[0] := Format(LNG_SCORE, [FScore]); |
97 | 100 | ||
98 | result := ShowModal; |
101 | result := ShowModal; |
99 | end; |
102 | end; |
100 | 103 | ||
101 | procedure TFinishForm.SaveToJournal(PlayerName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer); |
104 | procedure TFinishForm.SaveToJournal(PlayerName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer); |
102 | var |
105 | var |
103 | f: textfile; |
106 | f: textfile; |
104 | tmp: string; |
107 | tmp: string; |
105 | begin |
108 | begin |
106 | tmp := Format(JNL_FILE, [FLevel]); |
109 | tmp := Format(JNL_FILE, [FLevel]); |
107 | 110 | ||
108 | AssignFile(f, tmp); |
111 | AssignFile(f, tmp); |
109 | if FileExists(tmp) then |
112 | if FileExists(tmp) then |
110 | Append(f) |
113 | Append(f) |
111 | else |
114 | else |
112 | ReWrite(f); |
115 | ReWrite(f); |
113 | 116 | ||
114 | // 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? |
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? |
115 | WriteLn(f, Format(JNL_ENTRY, [DateTimeToStr(now()), NameEdit.Text, FScore, FSeconds, FStonesRemoved, FStonesTotal-1])); |
118 | WriteLn(f, Format(JNL_ENTRY, [DateTimeToStr(now()), NameEdit.Text, FScore, FSeconds, FStonesRemoved, FStonesTotal-1])); |
116 | 119 | ||
117 | CloseFile(f); |
120 | CloseFile(f); |
118 | end; |
121 | end; |
119 | 122 | ||
120 | procedure TFinishForm.SaveSettings; |
123 | procedure TFinishForm.SaveSettings; |
121 | var |
124 | var |
122 | reg: TRegistry; |
125 | reg: TRegistry; |
123 | begin |
126 | begin |
124 | reg := TRegistry.Create; |
127 | reg := TRegistry.Create; |
125 | try |
128 | try |
126 | reg.RootKey := HKEY_CURRENT_USER; |
129 | reg.RootKey := HKEY_CURRENT_USER; |
127 | if reg.OpenKey(REG_KEY, true) then |
130 | if reg.OpenKey(REG_KEY, true) then |
128 | begin |
131 | begin |
129 | reg.WriteString(REG_PLAYERNAME, NameEdit.Text); |
132 | reg.WriteString(REG_PLAYERNAME, NameEdit.Text); |
130 | reg.WriteBool(REG_REPLAY, ReplayCheckbox.Checked); |
133 | reg.WriteBool(REG_REPLAY, ReplayCheckbox.Checked); |
131 | reg.CloseKey; |
134 | reg.CloseKey; |
132 | end; |
135 | end; |
133 | finally |
136 | finally |
134 | reg.Free; |
137 | reg.Free; |
135 | end; |
138 | end; |
136 | end; |
139 | end; |
137 | 140 | ||
138 | procedure TFinishForm.LoadSettings; |
141 | procedure TFinishForm.LoadSettings; |
139 | var |
142 | var |
140 | reg: TRegistry; |
143 | reg: TRegistry; |
141 | begin |
144 | begin |
142 | NameEdit.Text := ''; |
145 | NameEdit.Text := ''; |
143 | reg := TRegistry.Create; |
146 | reg := TRegistry.Create; |
144 | try |
147 | try |
145 | reg.RootKey := HKEY_CURRENT_USER; |
148 | reg.RootKey := HKEY_CURRENT_USER; |
146 | if reg.OpenKeyReadOnly(REG_KEY) then |
149 | if reg.OpenKeyReadOnly(REG_KEY) then |
147 | begin |
150 | begin |
148 | if reg.ValueExists(REG_PLAYERNAME) then |
151 | if reg.ValueExists(REG_PLAYERNAME) then |
149 | NameEdit.Text := reg.ReadString(REG_PLAYERNAME); |
152 | NameEdit.Text := reg.ReadString(REG_PLAYERNAME); |
150 | if reg.ValueExists(REG_REPLAY) then |
153 | if reg.ValueExists(REG_REPLAY) then |
151 | ReplayCheckBox.Checked := reg.ReadBool(REG_REPLAY); |
154 | ReplayCheckBox.Checked := reg.ReadBool(REG_REPLAY); |
152 | reg.CloseKey; |
155 | reg.CloseKey; |
153 | end; |
156 | end; |
154 | finally |
157 | finally |
155 | reg.Free; |
158 | reg.Free; |
156 | end; |
159 | end; |
157 | end; |
160 | end; |
158 | 161 | ||
159 | procedure TFinishForm.SaveBtnClick(Sender: TObject); |
162 | procedure TFinishForm.SaveBtnClick(Sender: TObject); |
160 | begin |
163 | begin |
161 | if NameEdit.Text = '' then |
164 | if NameEdit.Text = '' then |
162 | begin |
165 | begin |
163 | showmessage(LNG_ENTER_NAME); |
166 | MessageDlg(LNG_ENTER_NAME, mtWarning, [mbOk], 0); |
164 | NameEdit.SetFocus; |
167 | NameEdit.SetFocus; |
165 | Exit; |
168 | Exit; |
166 | end; |
169 | end; |
167 | 170 | ||
168 | SaveToJournal(NameEdit.Text, FScore, FStonesTotal, FStonesRemoved, FSeconds); |
171 | SaveToJournal(NameEdit.Text, FScore, FStonesTotal, FStonesRemoved, FSeconds); |
169 | ModalResult := mrOK; |
172 | ModalResult := mrOK; |
170 | end; |
173 | end; |
171 | 174 | ||
172 | procedure TFinishForm.JumpHistoryLinkClick(Sender: TObject); |
175 | procedure TFinishForm.JumpHistoryLinkClick(Sender: TObject); |
173 | begin |
176 | begin |
174 | HistoryForm.JumpMemo.Lines.Assign(FHistory); |
177 | HistoryForm.JumpMemo.Lines.Assign(FHistory); |
175 | HistoryForm.ShowModal(); |
178 | HistoryForm.ShowModal(); |
176 | end; |
179 | end; |
177 | 180 | ||
178 | procedure TFinishForm.FormCreate(Sender: TObject); |
181 | procedure TFinishForm.FormCreate(Sender: TObject); |
179 | begin |
182 | begin |
180 | if not ForceDirectories(ExtractFilePath(Application.ExeName) + JNL_PATH) then |
183 | if not ForceDirectories(ExtractFilePath(Application.ExeName) + JNL_PATH) then |
181 | begin |
184 | begin |
182 | ShowMessage(Format(LNG_COULD_NOT_CREATE_DIR, [JNL_PATH])); |
185 | MessageDlg(Format(LNG_COULD_NOT_CREATE_DIR, [JNL_PATH]), mtError, [mbOk], 0); |
183 | end; |
186 | end; |
184 | 187 | ||
185 | LoadSettings; |
188 | LoadSettings; |
186 | end; |
189 | end; |
187 | 190 | ||
188 | end. |
191 | end. |
189 | 192 |