Subversion Repositories jumper

Rev

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