Subversion Repositories jumper

Rev

Rev 1 | Rev 9 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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