unit untImageDownloader;

{$mode Delphi}{$H+}

interface

uses
  Classes, SysUtils, ISCHttp, orca_scene2d;

const
  RETRY_TIMES = 5;

function DownloadCardImage(ACardId: Int64): Boolean;
procedure LoadCardImage(ACardId: Int64; AImg: TD2Image);

implementation


function DownloadCardImage(ACardId: Int64): Boolean;
var
  AFileName: string;
  AUrl: string;
  ATry: Integer = 0;
  ARet: Boolean = False;
begin
  AUrl:= Format('https://cdn.233.momobako.com/ygopro/pics/%d.jpg!half', [ACardId]);
  AFileName:= ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'cardimg' + DirectorySeparator + Format('%d.jpg', [ACardId]);
  while (ATry < RETRY_TIMES) do begin
    ARet := ISCDownloadFile(AUrl, AFileName);
    if (ARet) then Break;
    Inc(ATry);
  end;
  if (not ARet) then begin
    DeleteFile(AFileName);
  end;
  Exit(ARet);
end;

procedure LoadCardImage(ACardId: Int64; AImg: TD2Image);
var
  AFileName: string;
begin
  AFileName:= ExtractFilePath(ParamStr(0)) + DirectorySeparator + 'cardimg' + DirectorySeparator + Format('%d.jpg', [ACardId]);
  if (ACardId > 0) and (FileExists(AFileName)) then begin
    AImg.Bitmap.LoadFromFile(AFileName);
  end;
end;


end.

