カテゴリー別アーカイブ: テンプレート

スコアマネージャー


unit untScore;

interface

uses
  System.Classes,
  System.SysUtils,
  System.Generics.Collections,
  System.IOUtils,
  System.Types,
  FMX.Dialogs,
  System.UITypes;

type
  TScoreRecord = class
  private
    function getIsCorrect: boolean;

  const
    DELIMITER_CHAR = ',';

  public
    DateTime: TDateTime;
    SessionIndex: Integer;
    YomifudaSerial: Integer;
    TorifudaSerial: Integer;
    ResponceTime: Single;

    constructor Create();
    destructor Destroy; override;

    procedure DecodedString(aCodedString: String);
    function EncodeString(): String;

    property IsCorrect: boolean read getIsCorrect;
  end;

type
  TScoreList = class
  private

  public
    Items: TList<TScoreRecord>;

    constructor Create();
    destructor Destroy; override;

    procedure Init();
    procedure Load(aPathFileName: String);
    procedure Save(aPathFileName: String);

    procedure Add(aDateTime: TDateTime;
                  aSessionIndex: Integer;
                  aYomifudaSerial: Integer;
                  aTorifudaSerial: Integer;
                  aResponceTime: Single);


  end;

type
  TScoreManager = class
  const
    INITIAL_PLAYER_NAME = 'ゲスト';

  private
    FScoreRootPathName: String;

  public
    constructor Create(aScoreRootPathName: String);
    destructor Destroy; override;

    procedure PlayerList(vStringList: TStringList);
    procedure AddPlayer(aPlayerName: String);
    procedure DeletePlayer(aPlayerName: String);
    function IsPlayerExists(aPlayerName: String): Boolean;

  end;

var
  ScoreManager: TScoreManager;

const
  ON_SKIP    = -1;
  ON_TIMEOUT = -2;


implementation



// 要 System.IOUtils  System.Types
procedure fileListOfFolder(aFolderName: string; vFileList: TStringList);
var
  SearchPattern: string;
  Option: TSearchOption;
  FileNames: TStringDynArray;
  FileName: string;
begin
  // ファイル名に一致する検索パターン
  SearchPattern := '*.*';

  // ディレクトリの列挙モード
  Option := TSearchOption.soTopDirectoryOnly; // トップレベル列挙モード
  // Option := TSearchOption.soAllDirectories; // 再帰列挙モード

  //指定のディレクトリ内のファイルのリスト
  FileNames := TDirectory.GetFiles(aFolderName, SearchPattern, Option);
  vFileList.Clear;
  for FileName in FileNames do
    vFileList.Add(TPath.GetFileName(FileName));
end;





{ TScoreRecord }

constructor TScoreRecord.Create;
begin

end;

destructor TScoreRecord.Destroy;
begin

  inherited;
end;

procedure TScoreRecord.DecodedString(aCodedString: String);
var
  i: Integer;
  tmpFieldStringList: TStringList;
begin

  tmpFieldStringList := TStringList.Create;
  tmpFieldStringList.Delimiter := DELIMITER_CHAR;
  tmpFieldStringList.DelimitedText := aCodedString;

  Self.DateTime       := StrToDateTime(tmpFieldStringList[0]);
  Self.SessionIndex   := StrToInt     (tmpFieldStringList[1]);
  Self.YomifudaSerial := StrToInt     (tmpFieldStringList[2]);
  Self.TorifudaSerial := StrToInt     (tmpFieldStringList[3]);
  Self.ResponceTime   := StrToFloat   (tmpFieldStringList[4]);

  tmpFieldStringList.Free;
end;

function TScoreRecord.EncodeString: String;
var
  tmpFieldStringList: TStringList;
  tmpResult: string;
begin

  tmpFieldStringList := TStringList.Create;
  tmpFieldStringList.Delimiter := DELIMITER_CHAR;

  tmpFieldStringList.Add(DateTimeToStr(Self.DateTime));
  tmpFieldStringList.Add(IntToStr     (Self.SessionIndex));
  tmpFieldStringList.Add(IntToStr     (Self.YomifudaSerial));
  tmpFieldStringList.Add(IntToStr     (Self.TorifudaSerial));
  tmpFieldStringList.Add(FloatToStr   (Self.ResponceTime));

  tmpResult := tmpFieldStringList.DelimitedText;

  tmpFieldStringList.Free;

  Result := tmpResult;
end;

function TScoreRecord.getIsCorrect: boolean;
begin

end;

{ TScoreList }


constructor TScoreList.Create;
begin
  Items := TList<TScoreRecord>.Create;
  Init();
end;

destructor TScoreList.Destroy;
begin
  Init();
  Items.Free;
  inherited;
end;

procedure TScoreList.Init;
var
  i: Integer;
begin
  for i := 0 to Items.Count - 1 do
    Items[i].Free;
  Items.Clear;
end;

procedure TScoreList.Add(aDateTime: TDateTime;
                         aSessionIndex: Integer;
                         aYomifudaSerial,
                         aTorifudaSerial: Integer;
                         aResponceTime: Single);
var
  tmpScoreRecord: TScoreRecord;
begin
  tmpScoreRecord := TScoreRecord.Create;
  tmpScoreRecord.DateTime       := aDateTime;
  tmpScoreRecord.SessionIndex   := aSessionIndex;
  tmpScoreRecord.YomifudaSerial := aYomifudaSerial;
  tmpScoreRecord.TorifudaSerial := aTorifudaSerial;
  tmpScoreRecord.ResponceTime   := aResponceTime;

  Items.Add(tmpScoreRecord);
end;



procedure TScoreList.Load(aPathFileName: String);
var
  i: Integer;
  tmpRecordStringList: TStringList;
  tmpScoreRecord: TScoreRecord;
begin

  Self.Init();
  if FileExists(aPathFileName) then
  begin
    tmpRecordStringList := TStringList.Create;
    tmpRecordStringList.LoadFromFile(aPathFileName);

    for i := 0 to tmpRecordStringList.Count - 1 do
    begin
      tmpScoreRecord := TScoreRecord.Create();
      tmpScoreRecord.DecodedString(tmpRecordStringList[i]);
      Items.Add(tmpScoreRecord);
    end;
    tmpRecordStringList.Free;
  end;

end;

procedure TScoreList.Save(aPathFileName: String);
var
  i: Integer;
  tmpRecordStringList: TStringList;
begin

  tmpRecordStringList := TStringList.Create;
  tmpRecordStringList.Clear;
  for i := 0 to Self.Items.Count - 1 do
    tmpRecordStringList.Add(Self.Items[i].EncodeString);

  tmpRecordStringList.SaveToFile(aPathFileName);

  tmpRecordStringList.Free;
end;





{ TScoreManager }


constructor TScoreManager.Create(aScoreRootPathName: String);
begin
  FScoreRootPathName := aScoreRootPathName;

  ForceDirectories(aScoreRootPathName);
  if not IsPlayerExists(INITIAL_PLAYER_NAME) then AddPlayer(INITIAL_PLAYER_NAME);

end;

destructor TScoreManager.Destroy;
begin

  inherited;
end;

function TScoreManager.IsPlayerExists(aPlayerName: String): Boolean;
var
  tmpPathFileName: String;
begin
  tmpPathFileName := FScoreRootPathName + aPlayerName + '.txt';
  Result := TFile.Exists(tmpPathFileName);
end;

procedure TScoreManager.AddPlayer(aPlayerName: String);
var
  tmpPathFileName: String;
  tmpStringList: TStringList;
begin
  tmpPathFileName := FScoreRootPathName + aPlayerName + '.txt';
  tmpStringList := TStringList.Create;
  tmpStringList.Clear;
  tmpStringList.SaveToFile(tmpPathFileName);
  tmpStringList.Free;
end;

procedure TScoreManager.DeletePlayer(aPlayerName: String);
var
  tmpPathFileNameOld: String;
  tmpPathFileNameNew: String;
begin
  if aPlayerName = INITIAL_PLAYER_NAME then
  begin
    MessageDlg('このプレイヤーは削除できません。', TMsgDlgType.mtConfirmation,  [TMsgDlgBtn.mbOk], 0);
    Exit;
  end;
  tmpPathFileNameOld := FScoreRootPathName + aPlayerName + '.txt';
  tmpPathFileNameNew := FScoreRootPathName + aPlayerName + '.dum';
  RenameFile(tmpPathFileNameOld, tmpPathFileNameNew)
end;

procedure TScoreManager.PlayerList(vStringList: TStringList);
var
  i: Integer;
  tmpStringList: TStringList;
begin

  tmpStringList:= TStringList.Create;
  fileListOfFolder(FScoreRootPathName, tmpStringList);

  vStringList.Clear;
  for i := 0 to tmpStringList.Count - 1 do
    if ExtractFileExt(tmpStringList[i]) = '.txt' then
      vStringList.add(ChangeFileExt(ExtractFileName(tmpStringList[i]),''));

  tmpStringList.Free;

end;

end.