unit fEvents; interface uses System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Generics.Collections, winapi.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, fColorSettings, fFontSettings, fobs; 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 procedure toLog(aCode: integer; aMethod, aMess: string); public ListEvents: TArray; CustomRewardEvents: TList; procedure ExecuteAction(const aAction: integer; const aParam, aUserInput, aUserName, aUserLogin: string); procedure UpdateGrid; procedure ESOnGetCustomReward(CustomReward: TCustomRewardEvent); procedure OnDonate(aNick, aMessage, aSum: string); procedure OnTTVEvent(aNick: string); end; implementation {$R *.fmx} uses uGeneral; { --- UI --- } procedure TfrEvents.btnAddClick(Sender: TObject); var NewRec: TEventGlobal; begin if (cbEventList.ItemIndex < 0) or (cbActions.ItemIndex < 0) then Exit; NewRec.Event := cbEventList.ItemIndex; NewRec.Action := cbActions.ItemIndex; NewRec.Param := edtParams.Text; if edtIF.Visible then NewRec.Condition := edtIF.Text else if cbRevards.Visible then NewRec.Condition := cbRevards.Text; ListEvents := ListEvents + [NewRec]; DB.SaveRecordArray('ListEvents', ListEvents); UpdateGrid; end; procedure TfrEvents.btnDeleteClick(Sender: TObject); var RowIndex: integer; begin RowIndex := sgEvents.Row; if (RowIndex < 0) or (RowIndex > High(ListEvents)) then Exit; Delete(ListEvents, RowIndex, 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.MenuItem1Click(Sender: TObject); begin if OpenDialog1.Execute and (PopupMenu1.PopupComponent is TEdit) then TEdit(PopupMenu1.PopupComponent).Text := OpenDialog1.FileName; end; { --- Общая логика --- } procedure TfrEvents.ExecuteAction(const aAction: integer; const aParam, aUserInput, aUserName, aUserLogin: string); var sm: TSongMachine; f: TfrColorSettings; t: TfrFontSettings; i, j: integer; notify: TOBSNotify; se: TStyleEvent; ws: TEventWebServers; begin sm := TSongMachine.Create; try case aAction of 0: begin // Нажать кнопку toLog(0, 'ExecuteAction', 'Симуляция нажатия: ' + aParam); kePoints.SimulateKeyPress(aParam, 500); end; 1: begin // Воспроизведение звука toLog(0, 'ExecuteAction', 'Воспроизведение звука: ' + aParam); sm.PlayPublic(aParam, '100'); end; 2: begin // Kandinsky toLog(0, 'ExecuteAction', 'Генерация Kandinsky для: ' + aUserInput); Kandinsky.generate(aUserInput, aUserLogin); end; 3: begin // OBS Notify for i := 0 to TTW_Bot.frOBS1.EventWebServers.Count - 1 do begin if TTW_Bot.frOBS1.EventWebServers[i].typeEvent <> 5 then Continue; ws := TTW_Bot.frOBS1.EventWebServers[i]; var un: string; un := aUserName; if un = '' then un := aUserLogin; ws.se.Title := StringReplace(ws.se.Title, '[NICK]', un, [rfReplaceAll]); ws.se.Timestamp := now; ws.WebServerChat.AddMessage(ws.se); end; end; 4: toLog(0, 'ExecuteAction', 'Написать в чат: ' + aUserInput); 5: begin // Запуск файла toLog(0, 'ExecuteAction', 'Запуск файла: ' + aParam); ShellExecute(0, 'open', PWideChar(aParam), nil, nil, 0); end; end; finally sm.Free; end; end; procedure TfrEvents.ESOnGetCustomReward(CustomReward: TCustomRewardEvent); var ev: TEventGlobal; begin toLog(3, 'ESOnGetCustomReward', 'Начало обработки награды: ' + CustomReward.Event.revard.Title); for ev in ListEvents do if (ev.Event = 1) and (ev.Condition = CustomReward.Event.revard.Title) then ExecuteAction(ev.Action, ev.Param, CustomReward.Event.user_input, CustomReward.Event.user_name, CustomReward.Event.user_login); end; procedure TfrEvents.OnDonate(aNick, aMessage, aSum: string); var i, aSumInt, BestRow, BestPriority, BestValue, CurrentPriority, CurrentValue, MinVal, MaxVal: integer; Condition: string; Parts: TStringList; 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 Continue; Condition := ListEvents[i].Condition; CurrentPriority := -1; CurrentValue := 0; if TryStrToInt(Condition, CurrentValue) then begin if aSumInt = CurrentValue then CurrentPriority := 4; end else 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; end; end; finally Parts.Free; end; end else if (Condition.StartsWith('>')) then begin CurrentValue := StrToIntDef(Copy(Condition, 2), 0); if aSumInt >= CurrentValue then CurrentPriority := 2; end else if (Condition.StartsWith('<')) then begin CurrentValue := StrToIntDef(Copy(Condition, 2), 0); if aSumInt <= CurrentValue then CurrentPriority := 1; end; if (CurrentPriority <> -1) and ((CurrentPriority > BestPriority) or ((CurrentPriority = BestPriority) and (((CurrentPriority in [2, 3]) and (CurrentValue > BestValue)) or ((CurrentPriority = 1) and (CurrentValue < BestValue))))) then begin BestPriority := CurrentPriority; BestValue := CurrentValue; BestRow := i; end; end; if BestRow <> -1 then ExecuteAction(ListEvents[BestRow].Action, ListEvents[BestRow].Param, aMessage, aNick, aNick); end; procedure TfrEvents.OnTTVEvent(aNick: string); var ev: TEventGlobal; begin for ev in ListEvents do if ev.Event in ([2, 3, 4, 5]) then ExecuteAction(ev.Action, ev.Param, '', aNick, ''); end; procedure TfrEvents.UpdateGrid; var i: integer; begin sgEvents.BeginUpdate; try sgEvents.RowCount := Length(ListEvents); for i := 0 to High(ListEvents) do begin 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; procedure TfrEvents.toLog(aCode: integer; aMethod, aMess: string); begin TTW_Bot.toLog('fEvents', aMethod, aMess, aCode); end; end.