unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,ComObj;

type
  TMainForm = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Edit_DatiKey: TEdit;
    Button_GetKey: TButton;
    Edit_UserName: TEdit;
    Edit_UserPass: TEdit;
    Label3: TLabel;
    Button1: TButton;
    Label4: TLabel;
    Button_Report: TButton;
    Label5: TLabel;
    Label6: TLabel;
    Button_Check: TButton;
    Label7: TLabel;
    Button_SetSoftKey: TButton;
    Edit_SoftKey: TEdit;
    Label8: TLabel;
    Button_SetFather: TButton;
    Edit_FatherID: TEdit;
    Label9: TLabel;
    Button_Reg: TButton;
    Edit_RegName: TEdit;
    Edit_RegPass: TEdit;
    Edit_RegFather: TEdit;
    procedure Button_GetKeyClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button_ReportClick(Sender: TObject);
    procedure Button_CheckClick(Sender: TObject);
    procedure Button_SetSoftKeyClick(Sender: TObject);
    procedure Button_SetFatherClick(Sender: TObject);
    procedure Button_RegClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;
  QQQDaTi: OleVariant;
  Key:AnsiString; //答题密码串
  TID:AnsiString;//服务器返回的题目id
implementation

{$R *.dfm}
function RegisterOleFile(strOleFileName: STRING): BOOLEAN;
const
  RegisterOle = 1; // 注册
  UnRegisterOle = 0; // 卸载

type
  TOleRegisterFunction = function: HResult; // 注册或卸载函数的原型

var
  hLibraryHandle: THandle; // 由LoadLibrary返回的DLL或OCX句柄
  hFunctionAddress: TFarProc; // DLL或OCX中的函数句柄，由GetProcAddress返回
  RegFunction: TOleRegisterFunction; // 注册或卸载函数指针
begin
  Result := FALSE;
  // 打开OLE/DCOM文件，返回的DLL或OCX句柄
  hLibraryHandle := LoadLibrary(PCHAR(strOleFileName));
  if (hLibraryHandle > 0) then // DLL或OCX句柄正确
    try
      // 返回注册函数的指针
      hFunctionAddress := GetProcAddress(hLibraryHandle,
        PCHAR('DllRegisterServer'));
      if (hFunctionAddress <> NIL) then // 注册函数存在
      begin
        RegFunction := TOleRegisterFunction(hFunctionAddress); // 获取操作函数的指针
        if RegFunction >= 0 then // 执行注册或卸载操作，返回值>=0表示执行成功
          Result := true;
      end
      else
        MessageBox(0, '没找到DLL的接口DllRegisterServer!', '提示信息', 64)
    finally
      FreeLibrary(hLibraryHandle); // 关闭已打开的OLE/DCOM文件
    end
  else
  begin
    ShowMessage(strOleFileName + '没找到');

  end;

end;


procedure TMainForm.FormCreate(Sender: TObject);
begin

  if RegisterOleFile(ExtractFilePath(Application.Exename) + 'QQQDaTi.dll') then
   QQQDaTi := CreateOleObject('QQQDaTi.Reply');
end;


procedure TMainForm.Button1Click(Sender: TObject);
var
R:AnsiString;
GameID:Integer;
ImgPath:AnsiString;
OverTime:Integer;
T:integer;
Answer:AnsiString;
begin
  GameID:=2000;  //游戏类型id ,具体登录www.3QDaTi.com,查阅游戏类型id
  ImgPath:=ExtractFilePath(Application.Exename)+'Test.jpg'; //答题图片路径
  OverTime:=30; //超时时间
  

  T:=0;
  while (T<5)and(Length(R)<10) do
  begin
     //var SendFile(string UserKey, int GameId, string ImgPath, int OverTime)
    R:=QQQDaTi.SendFile(Key,GameID,ImgPath,OverTime);
    T:=T+1;
    Sleep(100);
  end;

  if Length(R)>10 then
  begin
    TID:=R;
    T:=0;
    //var GetAnswer(string Tid)
    R:=QQQDaTi.GetAnswer(TID);

    while (R='-99')and(T<(OverTime)) do
    begin
      R:=QQQDaTi.GetAnswer(TID);
      Sleep(1000);
    end;

    if StrToIntDef(R,0)>=0 then
    begin
      Answer:=R;
      ShowMessage('答题完成,返回答案='+Answer);
    end else
    begin
      ShowMessage('获取GetAnswer失败,返回错误代码='+R+',具体错误请参考插件说明中的返回值说明');
      exit;
    end;
    
    
  end else
  begin
    ShowMessage('获取SendFile失败,返回错误代码='+R+',具体错误请参考插件说明中的返回值说明');
  end;
end;

procedure TMainForm.Button_CheckClick(Sender: TObject);
var
 R:AnsiString;
begin

  //var Check(string UserKey)
  R:=QQQDaTi.Check(Key);
  if StrToIntDef(R,0)>=0 then
  begin

    ShowMessage('剩余题分='+R);
  end else
  begin
    ShowMessage('获取Check失败,返回错误代码='+R+',具体错误请参考插件说明中的返回值说明');
    exit;
  end;
  
end;

procedure TMainForm.Button_GetKeyClick(Sender: TObject);
var
  UserName:AnsiString;
  UserPass:AnsiString;
  R:AnsiString;
begin



   UserName:=Edit_UserName.Text;
   UserPass:=Edit_UserPass.Text;
   


   R:=QQQDaTi.LogIn(UserName,UserPass);

   if Length(R)>10 then
   begin
     Key:=R;
     Edit_DatiKey.Text:=Key;
     ShowMessage('获取key成功');
   end else
   begin
     ShowMessage('获取key失败,返回错误代码='+R+',具体错误请参考插件说明中的返回值说明');
   end;
end;

procedure TMainForm.Button_RegClick(Sender: TObject);
var
  R:AnsiString;
  RegUserName,RegUserPass,extUserName:AnsiString;
begin
  RegUserName:=Edit_RegName.Text;
  RegUserPass:=Edit_RegPass.Text;
  extUserName:=Edit_RegFather.Text;
  //var RegUser(string UserName, string UserPwd, string extUserName)
  R:=QQQDaTi.RegUser(RegUserName,RegUserPass,extUserName);
  if StrToIntDef(R,-1)>=0 then
  begin

    ShowMessage('注册成功');
  end else
  begin
    ShowMessage('获取RegUser失败,返回错误代码='+R+',具体错误请参考插件说明中的返回值说明');
    exit;
  end;
  
end;

procedure TMainForm.Button_ReportClick(Sender: TObject);
var
  R:AnsiString;
begin

    //var Report(string Tid, string UserKey)
  R:=QQQDaTi.Report(TID,Key);
  if R='0' then
  begin
    ShowMessage('申诉成功,等待管理员审核.');
  end else
  begin
   ShowMessage('获取Report失败,返回错误代码='+R+',具体错误请参考插件说明中的返回值说明');
  end;
end;



procedure TMainForm.Button_SetFatherClick(Sender: TObject);
var
 R:AnsiString;
begin
   //设置了父系,通过此软件答题的密码串必须是父系本身或者是其子账户的密码串,其余密码串无法答题.
   //父系ID就是父系的密码串前面的数字(例如密码是601|EA700D07-514E-4C3E-8888-C45600B6CBF1,那么父系ID就是601)
    
       
    //var SetFather(string FId)
  R:=QQQDaTi.SetFather(Edit_FatherID.Text);
  if StrToIntDef(R,-1)>=0 then
  begin

    ShowMessage('设置成功');
  end else
  begin
    ShowMessage('获取Edit_FatherID失败,返回错误代码='+R+',具体错误请参考插件说明中的返回值说明');
    exit;
  end;

end;

procedure TMainForm.Button_SetSoftKeyClick(Sender: TObject);
var
 R:AnsiString;
begin
   //设置作者返利key 只需要在注册插件成功后设置一次即可,无须重复设置.
   //请到官网登录作者账号获取作者KEY(作者KEY例如=A6E63EC2-13D4-4958-944C-1A1657171A43)

  //var SetSoftKey(string SoftKey)
  R:=QQQDaTi.SetSoftKey(Edit_SoftKey.Text);
  if StrToIntDef(R,-1)>=0 then
  begin

    ShowMessage('设置成功');
  end else
  begin
    ShowMessage('获取SetSoftKey失败,返回错误代码='+R+',具体错误请参考插件说明中的返回值说明');
    exit;
  end;
end;

end.
