unit fEvents; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Generics.Collections, ShellAPI, uSoundManager, FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls, FMX.Edit, FMX.ListBox, FMX.Controls.Presentation, System.Rtti, FMX.Grid.Style, FMX.ScrollBox, FMX.Grid, uRecords, uDataBase, FMX.Menus, uKeyEvent; type TfrEvents = class(TFrame) Label1: TLabel; cbEventList: TComboBox; edtIF: TEdit; Label2: TLabel; Label3: TLabel; cbActions: TComboBox; cbRevards: TComboBox; sgEvents: TStringGrid; Label4: TLabel; edtParams: TEdit; StringColumn1: TStringColumn; StringColumn2: TStringColumn; StringColumn3: TStringColumn; StringColumn4: TStringColumn; btnAdd: TButton; btnDelete: TButton; PopupMenu1: TPopupMenu; MenuItem1: TMenuItem; OpenDialog1: TOpenDialog; pVKGenerate: TPanel; cbKey1: TComboBox; Label54: TLabel; cbKey3: TComboBox; cbKey2: TComboBox; Label56: TLabel; procedure cbEventListChange(Sender: TObject); procedure btnAddClick(Sender: TObject); procedure btnDeleteClick(Sender: TObject); procedure MenuItem1Click(Sender: TObject); procedure cbActionsChange(Sender: TObject); private { Private declarations } procedure toLog(aCode: integer; aMethod: string; aMess: string); public { Public declarations } ListEvents: TArray; CustomRewards: Tlist; CustomRewardEvents: Tlist; procedure UpdateGrid; procedure LoadCustomRevards(); procedure ESOnGetCustomReward(CustomReward: TCustomRewardEvent); procedure OnDonate(aNick, aMessage, aSum: string); end; implementation {$R *.fmx} uses uGeneral; procedure TfrEvents.btnAddClick(Sender: TObject); var NewRec: TEventGlobal; begin if (cbEventList.Text = '') or (cbActions.Text = '') then exit; NewRec.Event := cbEventList.ItemIndex; NewRec.Action := cbActions.ItemIndex; NewRec.Param := edtParams.Text; if edtIF.Visible then NewRec.Condition := edtIF.Text; if cbRevards.Visible then NewRec.Condition := cbRevards.Text; SetLength(ListEvents, Length(ListEvents) + 1); ListEvents[High(ListEvents)] := NewRec; DB.SaveRecordArray('ListEvents', ListEvents); UpdateGrid; end; procedure TfrEvents.btnDeleteClick(Sender: TObject); var i, RowIndex: integer; begin RowIndex := sgEvents.Row; if (RowIndex < 0) or (RowIndex > High(ListEvents)) then exit; for i := RowIndex to High(ListEvents) - 1 do ListEvents[i] := ListEvents[i + 1]; SetLength(ListEvents, Length(ListEvents) - 1); DB.SaveRecordArray('ListEvents', ListEvents); UpdateGrid; end; procedure TfrEvents.cbActionsChange(Sender: TObject); begin pVKGenerate.Visible := cbActions.ItemIndex = 0; end; procedure TfrEvents.cbEventListChange(Sender: TObject); begin edtIF.Visible := cbEventList.ItemIndex = 0; cbRevards.Visible := cbEventList.ItemIndex = 1; Label2.Visible := cbEventList.ItemIndex <= 1; end; procedure TfrEvents.ESOnGetCustomReward(CustomReward: TCustomRewardEvent); var i, i2: integer; req, s: string; hr: TCustomRewardEvent; sm: TSongMachine; myAction: integer; begin sm := TSongMachine.Create; try toLog(3, 'ESOnGetCustomReward', 'Начало обработки награды: ' + CustomReward.Event.revard.Title); s := '[' + CustomReward.Event.user_name + '] купил за баллы канала награду "' + CustomReward.Event.revard.Title + '" '; if CustomReward.Event.user_input <> '' then begin toLog(0, 'ESOnGetCustomReward', 'Пользовательский ввод: ' + CustomReward.Event.user_input); s := s + ' и передал строку ' + CustomReward.Event.user_input end; for i := 0 to high(ListEvents) do begin if ListEvents[i].Event <> 1 then continue; if ListEvents[i].Condition = CustomReward.Event.revard.Title then begin myAction := ListEvents[i].Action; { 0 Нажать нкопку на клавиатуре 1 Проиграть звук 2 Kandinsky 3 Запустить Web Event 4 Написать в чат 5 Запустить программу } case myAction of 0: begin // нажать кнопку toLog(0, 'ESOnGetCustomReward', 'Симуляция нажатия: ' + ListEvents[i].Param); kePoints.SimulateKeyPress(ListEvents[i].Param, 500); end; 1: begin // воспроизведение звука toLog(0, 'ESOnGetCustomReward', 'Воспроизведение звука: ' + ListEvents[i].Param); sm.PlayPublic(ListEvents[i].Param, '100'); end; 2: begin // Kandinsky toLog(0, 'ESOnGetCustomReward', 'Генерация Kandinsky для: ' + CustomReward.Event.user_input); Kandinsky.generate(CustomReward.Event.user_input, CustomReward.Event.user_login); end; 4: begin // Написать в чат toLog(0, 'ESOnGetCustomReward', 'Написать в чат: ' + CustomReward.Event.user_input); end; 5: begin // запуск файла toLog(0, 'ESOnGetCustomReward', 'Запуск файла: ' + ListEvents [i].Param); ShellExecute(0, 'open', pwidechar(ListEvents[i].Param), nil, nil, 0); end; end; end; end; finally sm.Free; end; end; procedure TfrEvents.LoadCustomRevards(); var cr: TCustomRevards; begin CustomRewards.Clear; cbRevards.Items.Clear; TTW_Bot.toLog('fEvents', 'LoadCustomRevards', 'Запрос наград с сервера', 3); ttw_API.getCustomReward(CustomRewards); for cr in CustomRewards do begin cbRevards.Items.Add(cr.Title); end; end; procedure TfrEvents.MenuItem1Click(Sender: TObject); begin if OpenDialog1.Execute then TEdit(PopupMenu1.PopupComponent).Text := OpenDialog1.FileName; end; procedure TfrEvents.OnDonate(aNick, aMessage, aSum: string); var i, i2: integer; req, s: string; hr: TCustomRewardEvent; sm: TSongMachine; myAction: integer; j: integer; aSumInt: integer; BestRow: integer; BestPriority: integer; BestValue: integer; Condition: string; CurrentPriority: integer; CurrentValue: integer; MinVal, MaxVal: integer; Parts: tstringlist; se: TStyleEvent; notify: TEventGlobal; begin aSumInt := StrToIntDef(aSum, 0); BestRow := -1; BestPriority := -1; BestValue := 0; for i := 0 to high(ListEvents) do begin if ListEvents[i].Event = 0 then begin Condition := ListEvents[i].Condition; CurrentPriority := -1; CurrentValue := 0; // Проверка на точное значение if Pos('-', Condition) = 0 then begin if Pos('>', Condition) = 0 then begin if Pos('<', Condition) = 0 then begin // Точное значение CurrentValue := StrToIntDef(Condition, -1); if aSumInt = CurrentValue then CurrentPriority := 4; end; end; end; if CurrentPriority = -1 then begin if Pos('-', Condition) > 0 then begin // Диапазон Parts := tstringlist.Create; try Parts.Delimiter := '-'; Parts.StrictDelimiter := True; Parts.DelimitedText := Condition; if Parts.Count = 2 then begin MinVal := StrToIntDef(Trim(Parts[0]), 0); MaxVal := StrToIntDef(Trim(Parts[1]), 0); if (aSumInt >= MinVal) and (aSumInt <= MaxVal) then begin CurrentPriority := 3; CurrentValue := MinVal; // Приоритет по MinVal end; end; finally Parts.Free; end; end else if Pos('>', Condition) > 0 then begin // Условие ">X" CurrentValue := StrToIntDef(Trim(StringReplace(Condition, '>', '', [rfReplaceAll])), 0); if aSumInt >= CurrentValue then CurrentPriority := 2; end else if Pos('<', Condition) > 0 then begin // Условие " -1 then begin // Определение наилучшего условия if (CurrentPriority > BestPriority) or (CurrentPriority = BestPriority) and ((CurrentPriority = 3) and (CurrentValue > BestValue) or (CurrentPriority = 2) and (CurrentValue > BestValue) or (CurrentPriority = 1) and (CurrentValue < BestValue)) then begin BestPriority := CurrentPriority; BestValue := CurrentValue; BestRow := i; notify := ListEvents[i]; end; end; if BestRow = -1 then Break; end; end; if BestRow = -1 then exit; sm := TSongMachine.Create; try myAction := ListEvents[i].Action; case myAction of 0: begin // нажать кнопку toLog(0, 'OnDonate', 'Симуляция нажатия: ' + ListEvents[i].Param); kePoints.SimulateKeyPress(ListEvents[i].Param, 500); end; 1: begin // воспроизведение звука toLog(0, 'OnDonate', 'Воспроизведение звука: ' + ListEvents[i].Param); sm.PlayPublic(ListEvents[i].Param, '100'); end; 2: begin // Kandinsky toLog(0, 'OnDonate', 'Генерация Kandinsky для: ' + aNick); Kandinsky.generate(aMessage, aNick); end; 4: begin // Написать в чат toLog(0, 'OnDonate', 'Написать в чат: ' + aNick); end; 5: begin // запуск файла toLog(0, 'OnDonate', 'Запуск файла: ' + ListEvents[i].Param); ShellExecute(0, 'open', pwidechar(ListEvents[i].Param), nil, nil, 0); end; end; finally sm.Free; end; end; procedure TfrEvents.toLog(aCode: integer; aMethod, aMess: string); begin TTW_Bot.toLog('fEvents', aMethod, aMess, aCode); end; procedure TfrEvents.UpdateGrid; var i: integer; begin sgEvents.BeginUpdate; try sgEvents.RowCount := 0; // Сбрасываем строки (оставляем только заголовки) for i := 0 to High(ListEvents) do begin sgEvents.RowCount := i + 1; sgEvents.Cells[0, i] := cbEventList.Items[ListEvents[i].Event]; sgEvents.Cells[1, i] := ListEvents[i].Condition; sgEvents.Cells[2, i] := cbActions.Items[ListEvents[i].Action]; sgEvents.Cells[3, i] := ListEvents[i].Param; end; finally sgEvents.EndUpdate; end; end; end.