unit highscores;

interface

uses
  SysUtils, Classes, Controls, Forms, StdCtrls, variables, creation;

type
  RHighScore = record
    hname : string;
    hdays, hhours, hminutes,
    hcredits, hscores,
    hwaves, hkilled, hmissed,
    hupgrades, hsmall, hmiddle, hbig,
    htotal : integer;
  end;

  Thighscores_ = class(TForm)
    list: TListBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    level: TComboBox;
    l1: TLabel;
    l2: TLabel;
    l3: TLabel;
    l5: TLabel;
    l6: TLabel;
    l7: TLabel;
    l8: TLabel;
    l9: TLabel;
    Label17: TLabel;
    l11: TLabel;
    bclose: TLabel;
    Label19: TLabel;
    l10: TLabel;
    Label21: TLabel;
    l4: TLabel;
    l0: TLabel;
    procedure FormShow(Sender: TObject);
    procedure bcloseClick(Sender: TObject);
    procedure levelChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure listClick(Sender: TObject);
    procedure FormHide(Sender: TObject);
    procedure bcloseMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure bcloseMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  highscores_: Thighscores_;
  hs : array[1..11] of RHighScore;
  new : RHighScore;
  old_index : integer;
  view_only : boolean = true;

implementation

uses launcher, new_record;

{$R *.DFM}

procedure SaveHighscores(file_name : string);
var
f : textfile;
s : string;
i : integer;
begin
assignfile(f, file_name);
rewrite(f);
for i := 1 to 10 do
  with hs[i] do
    begin
      if hname = '' then hname := '----------';
      writeln(f, hname);
      writeln(f, hdays);
      writeln(f, hhours);
      writeln(f, hminutes);
      writeln(f, hcredits);
      writeln(f, hscores);
      writeln(f, hwaves);
      writeln(f, hkilled);
      writeln(f, hmissed);
      writeln(f, hupgrades);
      writeln(f, hsmall);
      writeln(f, hmiddle);
      writeln(f, hbig);
      writeln(f, htotal);
    end;
closefile(f);
end;

procedure LoadHighscores(file_name : string);
var
f : textfile;
s : single;
i : integer;
begin
if not fileexists(file_name) then exit;
assignfile(f, file_name);
reset(f);
try
for i := 1 to 10 do
  with hs[i] do
    begin
      readln(f, hname);
      readln(f, hdays);
      readln(f, hhours);
      readln(f, hminutes);
      readln(f, hcredits);
      readln(f, hscores);
      readln(f, hwaves);
      readln(f, hkilled);
      readln(f, hmissed);
      readln(f, hsmall);
      readln(f, hmiddle);
      readln(f, hbig);
      readln(f, hupgrades);
      readln(f, htotal);
      highscores_.list.Items.Add(inttostr(i) + ']   ' + hname + '   :   '
                                 + inttostr(htotal));
    end;
except SaveHighscores(file_name);
end;
closefile(f);
end;

function CountTotal(h : RHighScore) : integer;
var
m : integer;
begin
result := 0;
with h do
  begin
    m := hminutes + hhours * 60 + hdays * 24 * 60;
    result := result + m * 2;
    result := result + hcredits div 150;
    result := result + hscores div 50;
    result := result + hwaves * 5;
    result := result + (hkilled * 2 - hmissed) * 5;
    result := result + hupgrades * 8;
    result := result + hsmall * 5;
    result := result + hmiddle * 10;
    result := result + hbig * 20;
  end;
end;

procedure Thighscores_.FormShow(Sender: TObject);
var
i, n, l : integer;
begin
TForm(sender).OnMouseMove(nil, [ssLeft], 1, 1);
level.ItemIndex := launcher_.level.ItemIndex;
list.clear;
LoadHighscores('levels/level' + inttostr(level.ItemIndex + 1) + '/scores.cfg');
old_index := level.ItemIndex + 1;
if view_only then exit;
new.hname := player_name;
new.hdays := days - start_days;
new.hhours := hours - start_hours;
new.hminutes := minutes;
new.hcredits := credits;
new.hscores := score;
new.hwaves := current_params;
new.hkilled := killed_enemys;
new.hmissed := missed_enemys;
new.hsmall := builded_towers[1];
new.hmiddle := builded_towers[2];
new.hbig := builded_towers[3];
new.hupgrades := builded_towers[0];
new.htotal := CountTotal(new);

n := 10;
for i := 11 downto 0 do
  begin
    if (new.htotal < hs[i].htotal) and (n < 11) then
      begin
        list.Clear;
        for l := 10 downto n do hs[l + 1] := hs[l];
        hs[n] := new;
        new_record_.show;
        new_record_.l1.caption := new.hname;
        new_record_.l2.caption := inttostr(n) + ' место';
        new_record_.l3.caption := inttostr(new.htotal);  
        for l := 1 to 10 do
          with hs[l] do
            highscores_.list.Items.Add(inttostr(l) + ']   ' +
              hname + '   :   ' + inttostr(htotal));
        exit;
      end;
    n := i;
  end;
end;

procedure Thighscores_.bcloseClick(Sender: TObject);
begin
close;
end;

procedure Thighscores_.levelChange(Sender: TObject);
begin
SaveHighscores('levels/level' + inttostr(old_index) + '/scores.cfg');
old_index := level.ItemIndex + 1;
list.Clear;
LoadHighscores('levels/level' + inttostr(old_index) + '/scores.cfg');

SaveHighscores('levels/level' + inttostr(old_index) + '/scores.cfg');
old_index := level.ItemIndex + 1;
list.Clear;
LoadHighscores('levels/level' + inttostr(old_index) + '/scores.cfg');
end;

procedure Thighscores_.FormCreate(Sender: TObject);
var
i : integer;
begin
for i := 1 to 11 do hs[i].hname := '----------';
end;

procedure Thighscores_.listClick(Sender: TObject);
begin
with hs[list.itemindex + 1] do
  begin
    l0.caption := hname;
    l1.caption := inttostr(hdays) + ' дней ' + inttostr(hhours) + ' часов ' +
                  inttostr(hminutes) + ' минут';
    l2.caption := inttostr(hcredits) + ' кр.';
    l3.caption := inttostr(hscores) + ' оч.';
    l4.caption := inttostr(hwaves);
    l5.caption := inttostr(hkilled);
    l6.caption := inttostr(hmissed);
    l7.caption := inttostr(hsmall);
    l8.caption := inttostr(hmiddle);
    l9.caption := inttostr(hbig);
    l10.caption := inttostr(hupgrades);
    l11.caption := inttostr(htotal);
  end;
end;

procedure Thighscores_.FormHide(Sender: TObject);
begin
SaveHighscores('levels/level' + inttostr(level.ItemIndex + 1) + '/scores.cfg');
new_record_.close;
launcher_.show;
end;

procedure Thighscores_.bcloseMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
LabelMouseMove(TLabel(sender), self);
end;

procedure Thighscores_.FormMouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
bclose.font.color := launcher_.font.color;
end;

procedure Thighscores_.bcloseMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
LabelMouseDown(TLabel(sender), self);
end;

end.
