Rev 8 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | daniel-mar | 1 | unit Finish; |
2 | |||
3 | interface |
||
4 | |||
5 | uses |
||
6 | Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, |
||
7 | Dialogs, StdCtrls, MMSystem, Math, ExtCtrls, Registry; |
||
8 | |||
9 | type |
||
10 | TFinishForm = class(TForm) |
||
11 | Label1: TLabel; |
||
12 | Label2: TLabel; |
||
13 | SaveBtn: TButton; |
||
14 | CancelBtn: TButton; |
||
15 | NameEdit: TEdit; |
||
16 | PerformanceMemo: TMemo; |
||
17 | Label3: TLabel; |
||
18 | CupImage: TImage; |
||
19 | ReplayCheckBox: TCheckBox; |
||
20 | JumpHistoryLink: TLabel; |
||
21 | procedure CancelBtnClick(Sender: TObject); |
||
22 | procedure SaveBtnClick(Sender: TObject); |
||
23 | procedure JumpHistoryLinkClick(Sender: TObject); |
||
24 | procedure FormCreate(Sender: TObject); |
||
25 | private |
||
26 | FLevel: String; |
||
27 | FScore: integer; |
||
28 | FStonesTotal: integer; |
||
29 | FStonesRemoved: integer; |
||
30 | FSeconds: integer; |
||
31 | FHistory: TStringList; |
||
32 | procedure Calculate; |
||
33 | procedure SaveToJournal(PlayerName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer); |
||
34 | procedure LoadSettings; |
||
35 | public |
||
36 | procedure SaveSettings; |
||
37 | function Execute(LevelName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer; JumpHistory: TStringList): Integer; |
||
38 | end; |
||
39 | |||
40 | var |
||
41 | FinishForm: TFinishForm; |
||
42 | |||
43 | implementation |
||
44 | |||
45 | uses |
||
46 | History, Constants; |
||
47 | |||
48 | {$R *.dfm} |
||
49 | |||
50 | procedure TFinishForm.CancelBtnClick(Sender: TObject); |
||
51 | begin |
||
52 | Close; |
||
53 | end; |
||
54 | |||
55 | procedure TFinishForm.Calculate; |
||
56 | begin |
||
57 | PerformanceMemo.Lines.Clear; |
||
58 | PerformanceMemo.Lines.Add(Format(LNG_SCORE, [FScore])); |
||
59 | PerformanceMemo.Lines.Add(Format(LNG_REMAINING, [FStonesTotal - FStonesRemoved, |
||
60 | RoundTo(((FStonesTotal - FStonesRemoved) / FStonesTotal * 100), -2)])); |
||
61 | PerformanceMemo.Lines.Add(Format(LNG_TIME_SECONDS, [FSeconds])); |
||
62 | PerformanceMemo.Lines.Add(Format(LNG_POINTS_PER_MINUTE, [Round(FScore / FSeconds * 60)])); |
||
63 | end; |
||
64 | |||
65 | function TFinishForm.Execute(LevelName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer; JumpHistory: TStringList): Integer; |
||
66 | begin |
||
67 | FLevel := LevelName; |
||
68 | FScore := Score; |
||
69 | FStonesTotal := StonesTotal; |
||
70 | FStonesRemoved := StonesRemoved; |
||
71 | FSeconds := Seconds; |
||
72 | FHistory := JumpHistory; |
||
73 | |||
74 | Calculate; |
||
75 | result := ShowModal; |
||
76 | end; |
||
77 | |||
78 | procedure TFinishForm.SaveToJournal(PlayerName: String; Score, StonesTotal, StonesRemoved, Seconds: Integer); |
||
79 | var |
||
80 | f: textfile; |
||
81 | tmp: string; |
||
82 | begin |
||
83 | tmp := Format(JNL_FILE, [FLevel]); |
||
84 | |||
85 | AssignFile(f, tmp); |
||
86 | if FileExists(tmp) then |
||
87 | Append(f) |
||
88 | else |
||
89 | ReWrite(f); |
||
90 | WriteLn(f, Format(JNL_ENTRY, [DateTimeToStr(now()), NameEdit.Text, FScore, FSeconds, FStonesRemoved, FStonesTotal])); |
||
91 | CloseFile(f); |
||
92 | end; |
||
93 | |||
94 | procedure TFinishForm.SaveSettings; |
||
95 | var |
||
96 | reg: TRegistry; |
||
97 | begin |
||
98 | reg := TRegistry.Create; |
||
99 | try |
||
100 | reg.RootKey := HKEY_CURRENT_USER; |
||
101 | if reg.OpenKey(REG_KEY, true) then |
||
102 | begin |
||
103 | reg.WriteString(REG_PLAYERNAME, NameEdit.Text); |
||
104 | reg.WriteBool(REG_REPLAY, ReplayCheckbox.Checked); |
||
105 | reg.CloseKey; |
||
106 | end; |
||
107 | finally |
||
108 | reg.Free; |
||
109 | end; |
||
110 | end; |
||
111 | |||
112 | procedure TFinishForm.LoadSettings; |
||
113 | var |
||
114 | reg: TRegistry; |
||
115 | begin |
||
116 | NameEdit.Text := ''; |
||
117 | reg := TRegistry.Create; |
||
118 | try |
||
119 | reg.RootKey := HKEY_CURRENT_USER; |
||
120 | if reg.OpenKeyReadOnly(REG_KEY) then |
||
121 | begin |
||
122 | if reg.ValueExists(REG_PLAYERNAME) then |
||
123 | NameEdit.Text := reg.ReadString(REG_PLAYERNAME); |
||
124 | if reg.ValueExists(REG_REPLAY) then |
||
125 | ReplayCheckBox.Checked := reg.ReadBool(REG_REPLAY); |
||
126 | reg.CloseKey; |
||
127 | end; |
||
128 | finally |
||
129 | reg.Free; |
||
130 | end; |
||
131 | end; |
||
132 | |||
133 | procedure TFinishForm.SaveBtnClick(Sender: TObject); |
||
134 | begin |
||
135 | if NameEdit.Text = '' then |
||
136 | begin |
||
137 | showmessage(LNG_ENTER_NAME); |
||
138 | NameEdit.SetFocus; |
||
139 | end |
||
140 | else |
||
141 | begin |
||
142 | SaveToJournal(NameEdit.Text, FScore, FStonesTotal, FStonesRemoved, FSeconds); |
||
143 | Close; |
||
144 | end; |
||
145 | |||
146 | ModalResult := mrOK; |
||
147 | end; |
||
148 | |||
149 | procedure TFinishForm.JumpHistoryLinkClick(Sender: TObject); |
||
150 | begin |
||
151 | HistoryForm.JumpMemo.Lines.Assign(FHistory); |
||
152 | HistoryForm.ShowModal(); |
||
153 | end; |
||
154 | |||
155 | procedure TFinishForm.FormCreate(Sender: TObject); |
||
156 | begin |
||
157 | if not ForceDirectories(ExtractFilePath(Application.ExeName) + JNL_PATH) then |
||
158 | begin |
||
159 | ShowMessage(Format(LNG_COULD_NOT_CREATE_DIR, [JNL_PATH])); |
||
160 | end; |
||
161 | |||
162 | LoadSettings; |
||
163 | end; |
||
164 | |||
165 | end. |