unit Score;

interface

uses
  Classes, SysUtils, Generics.Collections, RegistryEx, ListViewSort;

type

  { TScore }

  TScore = class
    StartTime: TDateTime;
    Duration: TDateTime;
    Score: Integer;
    Moves: Integer;
    UsedUndos: Integer;
    BoardSize: string;
    DisabledTiles: Integer;
    UnmergeableTiles: Integer;
    BiggestTile: Integer;
    procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
  end;

  { TScores }

  TScores = class(TObjectList<TScore>)
    function SearchByTime(StartTime: TDateTime): TScore;
    function AddNew: TScore;
    procedure SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    procedure LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
    procedure AssignToList(List: TObjects);
  end;


implementation

{ TScore }

procedure TScore.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext);
begin
  with Reg do begin
    CurrentContext := RegContext;
    WriteDateTime('StartTime', StartTime);
    WriteDateTime('Duration', Duration);
    WriteInteger('Score', Score);
    WriteInteger('Moves', Moves);
    WriteInteger('UsedUndos', UsedUndos);
    WriteString('BoardSize', BoardSize);
    WriteInteger('DisabledTiles', DisabledTiles);
    WriteInteger('UnmergeableTiles', UnmergeableTiles);
    WriteInteger('BiggestTile', BiggestTile);
  end;
end;

procedure TScore.LoadFromRegistry(Reg: TRegistryEx; RegContext: TRegistryContext
  );
begin
  with Reg do begin
    CurrentContext := RegContext;
    StartTime := ReadDateTimeWithDefault('StartTime', StartTime);
    Duration := ReadDateTimeWithDefault('Duration', Duration);
    Score := ReadIntegerWithDefault('Score', Score);
    Moves := ReadIntegerWithDefault('Moves', Moves);
    UsedUndos := ReadIntegerWithDefault('UsedUndos', UsedUndos);
    BoardSize := ReadStringWithDefault('BoardSize', BoardSize);
    DisabledTiles := ReadIntegerWithDefault('DisabledTiles', DisabledTiles);
    UnmergeableTiles := ReadIntegerWithDefault('UnmergeableTiles', UnmergeableTiles);
    BiggestTile := ReadIntegerWithDefault('BiggestTile', BiggestTile);
  end;
end;

{ TScores }

function TScores.SearchByTime(StartTime: TDateTime): TScore;
var
  I: Integer;
begin
  I := 0;
  while (I < Count) and (Items[I].StartTime <> StartTime) do Inc(I);
  if I < Count then Result := Items[I]
    else Result := nil;
end;

function TScores.AddNew: TScore;
begin
  Result := TScore.Create;
  Add(Result);
end;

procedure TScores.SaveToRegistry(Reg: TRegistryEx; RegContext: TRegistryContext
  );
var
  I: Integer;
begin
  with Reg do begin
    CurrentContext := RegContext;

    WriteInteger('Count', Count);
    for I := 0 to Count - 1 do
      Items[I].SaveToRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Score' + IntToStr(I)));
  end;
end;

procedure TScores.LoadFromRegistry(Reg: TRegistryEx;
  RegContext: TRegistryContext);
var
  I: Integer;
  C: Integer;
  NewScore: TScore;
begin
  with Reg do begin
    CurrentContext := RegContext;

    C := ReadIntegerWithDefault('Count', 0);
    for I := 0 to C - 1 do begin
      NewScore := AddNew;
      NewScore.LoadFromRegistry(Reg, TRegistryContext.Create(RegContext.RootKey, RegContext.Key + '\Score' + IntToStr(I)));
    end;
  end;
end;

procedure TScores.AssignToList(List: TObjects);
var
  I: Integer;
begin
  while List.Count > Count do List.Delete(List.Count - 1);
  for I := 0 to List.Count - 1 do List[I] := Items[I];
  while List.Count < Count do List.Add(Items[List.Count]);
end;

end.

