Subversion Repositories jumper

Rev

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