Forum in READ ONLY mode! All questions and discussions on Discord official server, invite link: https://discord.gg/VxsGzJ7

Работа с динамическими массивами объектов. Delphi.

тут можно задать вопрос по скриптингу
Post Reply
drabadan
Expert
Expert
Posts: 730
Joined: 13.12.2012 17:35
Contact:

Работа с динамическими массивами объектов. Delphi.

Post by drabadan »

Прошу людей с опытом прочесть мой код, он рабочий. Но я бы хотел увидеть, если есть какие-то замечания, упрощения и решения оптимальней тех которые принял я. Код умышленно не коментирован - это для того, чтобы я своими "рассуждениями" не ввел в заблуждение людей которые захотят мне помочь!

Цель моего поста, в том - что я хотел бы получить совета от профессионалов. Наступать 100 раз на одни и те же грабли - глупо, по-этому подскажите пожалуйста в правильном ли направлении я двигаюсь.

Если будет необходимость приложить файлы к проекту - приложу.
Код моей программы

Code: Select all

unit RegBotOOP;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, types_const_import, Vcl.StdCtrls, inifiles;

//объявляем новый класс
type
  TBuyPoint = class(TObject)
    private
      vendor_Name : String;
      //vendor_ID, rune_ID : Integer;
      tmpID : Array[0..1] of Integer;
      function GetName : String;
      procedure SetName(Value : String);
      function GetTmpID(Index : Integer) : Integer;
      procedure SetTmpID(Index : Integer; Value : Integer);
    public
      Property Name : String read GetName write SetName;
      Property VendorID : Integer Index 0 read GetTmpID write SetTmpID;
      Property RuneID : Integer Index 1 read GetTmpID write SetTmpID;
      constructor Create;
  end;


type
  TRegBotOOPForm = class(TForm)
    Label1: TLabel;
    Add_Button: TButton;
    Statistics_Memo: TMemo;
    AddToMemo_Button: TButton;
    SaveConfig_Button: TButton;
    LoadConfig_Button: TButton;
    procedure Add_ButtonClick(Sender: TObject);
    procedure AddToMemo_ButtonClick(Sender: TObject);
    procedure SaveConfig_ButtonClick(Sender: TObject);
    procedure LoadConfig_ButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;



var
  RegBotOOPForm: TRegBotOOPForm;
  BuyPoint : Array of TBuyPoint;//объявляем динамический массив объектов.
  //BuyPoint : TBuyPoint;

implementation

{$R *.dfm}

function GetItemInfo : TTargetInfo;
  var
    tInfo : TClient;
  begin
    tInfo.RequestObjectTarget;
    tInfo.WaitForTargetResponse(30000);
    if tInfo.TargetResponsePresent then Result := tInfo.TargetResponse;
  end;

constructor TBuyPoint.Create;
begin
  Name := 'No name found.';
  VendorID := -1;
  RuneID := -1;
end;

function TBuyPoint.GetName : String;
begin
  Result := vendor_Name;
end;

function TBuyPoint.GetTmpID(Index: Integer) : Integer;
begin
  Result := tmpID[index];
end;

procedure TBuyPoint.SetName(Value: String);
begin
  vendor_Name := Value;
end;

procedure TBuyPoint.SetTmpID(Index: Integer; Value: Integer);
begin
  tmpID[Index] := Value;
end;

procedure addElement;
  var
    tt : TTargetInfo;
    currElem : Integer;
  begin
    SetLength(BuyPoint, Length(BuyPoint)+1);
    currElem := High(BuyPoint);
    Script.Client.Print('Добавь вендора!');
    tt := GetItemInfo;
    BuyPoint[currElem] := TBuyPoint.Create;
    BuyPoint[currElem].SetName(TUOObject.GetName(tt.ID));
    BuyPoint[currElem].VendorID := tt.ID;
    Script.Client.Print('Добавь руну!');
    tt := GetItemInfo;
    BuyPoint[currElem].RuneID := tt.ID;
  end;

procedure setElementsToIni;
var
  i, points_Count : Integer;
  tFile : TIniFile;
begin
  tFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  try
    tFile.WriteInteger('General', 'Points_Count', Length(BuyPoint));
    for i := 0 to Length(BuyPoint)-1 do
      begin
        tFile.WriteString('Vendor_' + IntToStr(i), 'Vendor_Name', BuyPoint[i].GetName);
        tFile.WriteInteger('Vendor_' + IntToStr(i), 'Vendor_ID', BuyPoint[i].VendorID);
        tFile.WriteInteger('Vendor_' + IntToStr(i), 'Rune_ID', BuyPoint[i].RuneID);
      end;
  finally
    tFile.Free;
  end;
end;

procedure LoadConfig;
var
  i, points_Count : Integer;
  tFile : TIniFile;
begin
  tFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini'));
  try
    points_Count := tFile.ReadInteger('General', 'Points_Count', 0);
    SetLength(BuyPoint, points_Count);
    for i := 0 to points_Count-1 do
      begin
        BuyPoint[i] := TBuyPoint.Create;
        BuyPoint[i].SetName(tFile.ReadString('Vendor_' + IntToStr(i), 'Vendor_Name', 'No name found!'));
        BuyPoint[i].VendorID := tFile.ReadInteger('Vendor_' + IntToStr(i), 'Vendor_ID', -1);
        BuyPoint[i].RuneID := tFile.ReadInteger('Vendor_' + IntToStr(i), 'Rune_ID', -1);
      end;
  finally
    tFile.Free;
  end;
end;

procedure TRegBotOOPForm.Add_ButtonClick(Sender: TObject);
begin
  addElement;
end;


procedure TRegBotOOPForm.LoadConfig_ButtonClick(Sender: TObject);
begin
  LoadConfig;
end;

procedure TRegBotOOPForm.AddToMemo_ButtonClick(Sender: TObject);
  var
    i : Integer;
    st : TStringList;
  begin
    st := TStringList.Create;
    try
      for i := Low(BuyPoint) to Length(BuyPoint)-1 do st.Add(BuyPoint[i].GetName);
        RegBotOOPForm.Statistics_Memo.Text := st.Text;
    finally
      st.Free;
    end;
  end;

procedure TRegBotOOPForm.SaveConfig_ButtonClick(Sender: TObject);
begin
  setElementsToIni;
end;

end.
Post Reply