summaryrefslogtreecommitdiff
path: root/plugins/!NotAdopted/Chess4Net/MI/ManagerUnit.MI.pas
blob: c50aa58e4d658831662dc7e228d1035ba4601836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
////////////////////////////////////////////////////////////////////////////////
// All code below is exclusively owned by author of Chess4Net - Pavel Perminov
// (packpaul@mail.ru, packpaul1@gmail.com).
// Any changes, modifications, borrowing and adaptation are a subject for
// explicit permition from the owner.

unit ManagerUnit.MI;

interface

uses
  SysUtils,
  //
  ControlUnit, ManagerUnit, ConnectorUnit, ModalForm, NonRefInterfacedObjectUnit;

type
  TManagerMIFactory = class(TNonRefInterfacedObject, IMirandaPlugin)
  private
    m_Connector: TConnector;
    m_Manager: IMirandaPlugin;
    m_Dialogs: TDialogs;
    m_bOwnExceptionHandler: boolean;
    FErrorDuringPluginStart: TProcedure;
    procedure FDialogsHandler(modSender: TModalForm; msgDlgID: TModalFormID);
    function FGetDialogs: TDialogs;
    procedure FStartGaming;
    procedure FStartTransmitting; overload;
    procedure FStartTransmitting(ManagerForTransmition: TManager); overload;
    function FCanStartTransmitting: boolean;
    procedure FHandleStartException;
    property Dialogs: TDialogs read FGetDialogs;
  protected
    procedure Start;
    procedure Stop;
    procedure ConnectorHandler(ce: TConnectorEvent; d1: pointer = nil; d2: pointer = nil);
  public
    constructor Create(Connector: TConnector; AErrorDuringPluginStart: TProcedure); reintroduce;
    destructor Destroy; override;
  end;

procedure StopAllPlugins;

implementation

uses
  Types, StrUtils, Classes, Dialogs, Controls,
  //
  LocalizerUnit, TransmitGameSelectionUnit, GlobalsLocalUnit, ChessBoardUnit,
  GameChessBoardUnit;

type
  TManagerMI = class(TManager, IMirandaPlugin) // abstract
  protected
    procedure Start;
    procedure Stop;
    procedure RSendData(const cmd: string); override;
    procedure ROnDestroy; override;

    procedure ConnectorHandler(e: TConnectorEvent; d1: pointer = nil; d2: pointer = nil); override;
    procedure RSetOpponentClientVersion(lwVersion: LongWord); override;

  public
    constructor Create(Connector: TConnector); reintroduce;
    destructor Destroy; override;
  end;

  TTransmittingManagerMI = class;

  EGamingManagerMI = class(Exception);
  TGamingManagerMI = class(TManagerMI, IMirandaPlugin)
  private
    m_lstTransmittingManagers: TList;

    procedure FAddTransmitter(ATransmitter: TTransmittingManagerMI);
    function FRemoveTransmitter(ATransmitter: TTransmittingManagerMI): boolean;
    procedure FSetGameContextToTransmitter(ATransmitter: TTransmittingManagerMI);
    function FContainsContactIDInTransmitters(iContactID: integer): boolean;
    procedure FUpdateChessBoardCaptions;

  protected
    procedure Start;
    procedure ROnCreate; override;
    procedure ROnDestroy; override;
    procedure ConnectorHandler(e: TConnectorEvent; d1: pointer = nil; d2: pointer = nil); override;
    procedure RSetConnectionOccured; override;
    procedure RHandleConnectorDataCommand(sl: string); override;
    procedure RRetransmit(const strCmd: string); override;
    function RGetGameName: string; override;
  end;

  ETransmittingManagerMI = class(Exception);
  TTransmittingManagerMI = class(TManagerMI, IMirandaPlugin) // TODO: move to separate hierarchy
  private
    m_GamingManager: TGamingManagerMI;
    m_bReady: boolean; // ready for transmition
    property Ready: boolean read m_bReady;
  protected
    procedure Start;
    procedure ROnCreate; override;
    procedure ROnDestroy; override;

    procedure ConnectorHandler(e: TConnectorEvent; d1: pointer = nil; d2: pointer = nil); override;
    procedure RHandleConnectorDataCommand(sl: string); override;
//    procedure RWriteSettings; override; // no need because m_bConnectionOccured is always FALSE

  public
    constructor Create(Connector: TConnector; GamingManager: TGamingManagerMI); reintroduce;
  end;

var
  g_lstGamingManagers: TList = nil;
  g_Plugins: TInterfaceList = nil;

procedure AddToPlugins(Plugin: IMirandaPlugin);
begin
  if (not Assigned(g_Plugins)) then
    g_Plugins := TInterfaceList.Create;
  g_Plugins.Add(Plugin);
end;

procedure RemoveFromPlugins(Plugin: IMirandaPlugin);
begin
  if (not Assigned(g_Plugins)) then
    exit;
  g_Plugins.Remove(Plugin);
  if (g_Plugins.Count = 0) then
    FreeAndNil(g_Plugins);
end;


procedure StopAllPlugins;
var
  i: integer;
  Plugin: IMirandaPlugin;
begin
  if (not Assigned(g_Plugins)) then
    exit;
  i := g_Plugins.Count - 1;
  while ((i >= 0) and Assigned(g_Plugins)) do
  begin
    Plugin := g_Plugins[i] as IMirandaPlugin;
    Plugin.Stop;
    Pointer(Plugin) := nil;
    dec(i);
  end;
  FreeAndNil(g_Plugins);
end;

////////////////////////////////////////////////////////////////////////////////
// TGamingManagerMI

procedure TGamingManagerMI.FAddTransmitter(ATransmitter: TTransmittingManagerMI);
begin
  if (not Assigned(m_lstTransmittingManagers)) then
    m_lstTransmittingManagers := TList.Create;
  m_lstTransmittingManagers.Add(ATransmitter);
end;


function TGamingManagerMI.FRemoveTransmitter(ATransmitter: TTransmittingManagerMI): boolean;
var
  i: integer;
begin
  Result := FALSE;

  if (not Assigned(m_lstTransmittingManagers)) then
    exit;

  for i := 0 to m_lstTransmittingManagers.Count - 1 do
  begin
    if (m_lstTransmittingManagers[i] = ATransmitter) then
    begin
      m_lstTransmittingManagers.Delete(i);
      if (m_lstTransmittingManagers.Count = 0) then
        FreeAndNil(m_lstTransmittingManagers);

      Result := TRUE;
      exit;
    end;
  end; // for
end;


procedure TGamingManagerMI.FSetGameContextToTransmitter(ATransmitter: TTransmittingManagerMI);
begin
  if (not (Assigned(ATransmitter) and ATransmitter.Ready)) then
    exit;

  ATransmitter.RSendData(CMD_NICK_ID + ' ' + PlayerNickId + ' ' + OpponentNickId + ' ' + OpponentNick);
  ATransmitter.RSendData(CMD_GAME_CONTEXT + ' ' + RGetGameContextStr);

  if (ChessBoard.Mode = mGame) then
    ATransmitter.RSendData(CMD_CONTINUE_GAME);
end;


function TGamingManagerMI.FContainsContactIDInTransmitters(iContactID: integer): boolean;
var
  i: integer;
  ATransmitter: TTransmittingManagerMI;
begin
  Result := FALSE;
  if (not Assigned(m_lstTransmittingManagers)) then
    exit;
  for i := 0 to m_lstTransmittingManagers.Count - 1 do
  begin
    ATransmitter := m_lstTransmittingManagers[i];
    if (Assigned(ATransmitter) and (ATransmitter.Connector.ContactID = iContactID)) then
    begin
      Result := TRUE;
      exit;
    end;
  end;
end;


procedure TGamingManagerMI.Start;
begin
  if (Assigned(ChessBoard)) then
  begin
    Show;
    exit;
  end;

  if (not Connector.Opened) then
  begin
    if (not Connector.Open(FALSE)) then
      raise EGamingManagerMI.Create('ERROR: Cannot open connector!');
  end;

  RCreateChessBoardAndDialogs;
  RSetChessBoardToView;

  RReadPrivateSettings;

  RShowConnectingForm;
end;


procedure TGamingManagerMI.ROnCreate;
begin
  RCreateAndPopulateExtBaseList;

  // Nicks initialization
  PlayerNick := Connector.OwnerNick;
  OpponentNick := Connector.ContactNick;
  OpponentId := IntToStr(Connector.ContactID);

  TLocalizer.Instance.AddSubscriber(self); // TODO: -> TManager.ROnCreate
  RLocalize;
end;


procedure TGamingManagerMI.ROnDestroy;

  procedure NRemoveFromGamings;
  var
    lstTmp: TList;
    i: integer;
    ATransmitter: TTransmittingManagerMI;
  begin
    if (Assigned(g_lstGamingManagers)) then
      g_lstGamingManagers.Remove(self);

    if (not Assigned(m_lstTransmittingManagers)) then
      exit;

    lstTmp := m_lstTransmittingManagers;
    m_lstTransmittingManagers := nil;

    for i := 0 to lstTmp.Count - 1 do
    begin
      ATransmitter := lstTmp[i];
      if (Assigned(ATransmitter)) then
      begin
        lstTmp[i] := nil;
        ATransmitter.Stop;
      end;
    end;

    lstTmp.Free;
  end;

begin // TGamingManagerMI.ROnDestroy
  NRemoveFromGamings;
  FUpdateChessBoardCaptions;
  inherited ROnDestroy;
end;


procedure TGamingManagerMI.ConnectorHandler(e: TConnectorEvent;
  d1: pointer = nil; d2: pointer = nil);
begin
  case e of
    ceError:
    begin
      Connector.Close;
    end;
  end; // case

  inherited ConnectorHandler(e, d1, d2);
end;


procedure TGamingManagerMI.RSetConnectionOccured;
var
  iIndex: integer;
begin
  inherited RSetConnectionOccured;

  if (not Assigned(g_lstGamingManagers)) then
    g_lstGamingManagers := TList.Create;

  iIndex := g_lstGamingManagers.IndexOf(self);
  if (iIndex < 0) then
    g_lstGamingManagers.Add(self);

  FUpdateChessBoardCaptions;
end;


procedure TGamingManagerMI.FUpdateChessBoardCaptions;
var
  i: integer;
  GM: TGamingManagerMI;
begin
  if (not Assigned(g_lstGamingManagers)) then
    exit;
  for i := 0 to g_lstGamingManagers.Count - 1 do
  begin
    GM := g_lstGamingManagers[i];
    if (Assigned(GM)) then
      GM.RUpdateChessBoardCaption;
  end;
end;


procedure TGamingManagerMI.RRetransmit(const strCmd: string);
var
  i: integer;
  ATransmitter: TTransmittingManagerMI;
begin
  if (Transmittable or (not Assigned(m_lstTransmittingManagers))) then
    exit;

  for i := 0 to m_lstTransmittingManagers.Count - 1 do
  begin
    ATransmitter := m_lstTransmittingManagers[i];
    if (Assigned(ATransmitter) and (ATransmitter.Ready)) then
      ATransmitter.RSendData(strCmd);
  end;
end;


function TGamingManagerMI.RGetGameName: string;
var
  i: integer;
  iIndex: integer;
  iWithSameConnectorCount: integer;
  GM: TGamingManagerMI;
begin
  Result := inherited RGetGameName;

  if (not Assigned(g_lstGamingManagers)) then
    exit;

  iWithSameConnectorCount := 0;
  iIndex := 0;

  for i := 0 to g_lstGamingManagers.Count - 1 do
  begin
    GM := g_lstGamingManagers[i];
    if (Assigned(GM) and (GM.Connector.ContactID = self.Connector.ContactID)) then
    begin
      inc(iWithSameConnectorCount);
      if (GM = self) then
        iIndex := iWithSameConnectorCount;
    end; // if
  end; // for

  if (iWithSameConnectorCount > 1) then
    Result := Result + ' (' + IntToStr(iIndex) + ')';
end;


procedure TGamingManagerMI.RHandleConnectorDataCommand(sl: string);
var
  strCmdSaved, sr: string;
begin
  strCmdSaved := sl;

  RSplitStr(sl, sl, sr);

  if (sl = CMD_TRANSMITTING) then
  begin
    Transmittable := TRUE;
    exit;
  end;

  if (not Transmittable) then
  begin
    inherited RHandleConnectorDataCommand(strCmdSaved);
    exit;
  end;

  if (sl = CMD_WELCOME) then
  begin
    if (Assigned(ChessBoard)) then
      ChessBoard.InitPosition;
    RSetConnectionOccured;
  end
  else if (sl = CMD_NICK_ID) then
  begin
    // sr ::= <PlayerNickId><OpponentNickId><OpponentNick>
    PlayerNick := OpponentNick; // change for transmittion

    RSplitStr(sr, sl, sr);
    PlayerNickId := sl;

    RSplitStr(sr, sl, sr);
    OpponentNickId := sl;

    RSplitStr(sr, sl, sr);
    OpponentNick := sl;

    ChessBoard.Caption := RGetGameName;
  end
  else if (sl = CMD_GAME_CONTEXT) then
  begin
    RSetGameContext(sr);
  end
  else
    inherited RHandleConnectorDataCommand(strCmdSaved);
end;

////////////////////////////////////////////////////////////////////////////////
// TManagerMIFactory

constructor TManagerMIFactory.Create(Connector: TConnector; AErrorDuringPluginStart: TProcedure);
begin
  inherited Create;
  m_Connector := Connector;
  FErrorDuringPluginStart := AErrorDuringPluginStart;
  AddToPlugins(self);
end;


destructor TManagerMIFactory.Destroy;
begin
  RemoveFromPlugins(self);
  m_Dialogs.Free;
  inherited;
end;


procedure TManagerMIFactory.Start;
begin
  if (FCanStartTransmitting) then
  begin
    Dialogs.MessageDlg(TLocalizer.Instance.GetMessage(28), mtCustom,
      [mbYes, mbNo], mfTransmitting); // You are currently playing some games. Do you want to start broadcasting?
    m_bOwnExceptionHandler := TRUE;
    exit;
  end;

  FStartGaming;
end;


function TManagerMIFactory.FCanStartTransmitting: boolean;
var
  i: integer;
  GM: TGamingManagerMI;
begin
  Result := FALSE;

  if (not (Assigned(g_lstGamingManagers) and Assigned(m_Connector))) then
    exit;

  for i := 0 to g_lstGamingManagers.Count - 1 do
  begin
    GM := g_lstGamingManagers[i];
    Result := (Assigned(GM) and (not GM.Transmittable) and
               (m_Connector.ContactID <> GM.Connector.ContactID) and
               (not GM.FContainsContactIDInTransmitters(m_Connector.ContactID)));
    if (Result) then
      exit;
  end; // for
end;


procedure TManagerMIFactory.FStartGaming;
begin
  m_Manager := TGamingManagerMI.Create(m_Connector);
  m_Connector.SetPlugin(m_Manager);
  m_Connector := nil;

  try
    m_Manager.Start;
    Pointer(m_Manager) := nil;
  except
    if (m_bOwnExceptionHandler) then
      FHandleStartException
    else
      raise;
  end;

  Stop;
end;


procedure TManagerMIFactory.FHandleStartException;
begin
  if (Assigned(FErrorDuringPluginStart)) then
    FErrorDuringPluginStart;
end;


procedure TManagerMIFactory.FStartTransmitting(ManagerForTransmition: TManager);
begin
  m_Manager := TTransmittingManagerMI.Create(m_Connector, ManagerForTransmition as TGamingManagerMI);
  m_Connector.SetPlugin(m_Manager);
  m_Connector := nil;

  try
    m_Manager.Start;
    Pointer(m_Manager) := nil;
  except
    if (m_bOwnExceptionHandler) then
      FHandleStartException
    else
      raise;
  end;

  Stop;
end;


procedure TManagerMIFactory.FStartTransmitting;
var
  strlGames: TStringList;
  i: integer;
  GM: TGamingManagerMI;
  ATransmitGameSelectionForm: TTransmitGameSelectionForm;
begin
  if (not Assigned(g_lstGamingManagers)) then
  begin
    Stop; // Don't do anything
    exit;
  end;

  strlGames := TStringList.Create;
  try
    for i := 0 to g_lstGamingManagers.Count - 1 do
    begin
      GM := g_lstGamingManagers[i];
      if (Assigned(GM) and (not GM.Transmittable) and
          (m_Connector.ContactID <> GM.Connector.ContactID) and
          (not GM.FContainsContactIDInTransmitters(m_Connector.ContactID))) then
        strlGames.AddObject(GM.RGetGameName, GM);
    end;

    if (strlGames.Count > 1) then
    begin
      ATransmitGameSelectionForm :=
        Dialogs.CreateDialog(TTransmitGameSelectionForm) as TTransmitGameSelectionForm;
      ATransmitGameSelectionForm.SetGames(strlGames);
      ATransmitGameSelectionForm.Show;
    end
    else if (strlGames.Count = 1) then
    begin
      GM := strlGames.Objects[0] as TGamingManagerMI;
      FStartTransmitting(GM);
    end
    else // = 0
      Stop;

  finally
    strlGames.Free;
  end;
end;


procedure TManagerMIFactory.Stop;
begin
  if (Assigned(m_Connector)) then
  begin
    m_Connector.SetPlugin(nil); 
    m_Connector.Free;
  end;
  if (Assigned(m_Manager)) then
  begin
    m_Manager.Stop;
    Pointer(m_Manager) := nil;
  end;
  Free;
end;


procedure TManagerMIFactory.ConnectorHandler(ce: TConnectorEvent;
  d1: pointer = nil; d2: pointer = nil);
begin
end;


function TManagerMIFactory.FGetDialogs: TDialogs;
begin
  if (not Assigned(m_Dialogs)) then
    m_Dialogs := TDialogs.Create(nil, FDialogsHandler);
  Result := m_Dialogs;
end;


procedure TManagerMIFactory.FDialogsHandler(modSender: TModalForm; msgDlgID: TModalFormID);
var
  modRes: TModalResult;
  GM: TGamingManagerMI;
begin
  modRes := modSender.ModalResult;
  case msgDlgID of
    mfNone:
      ;
    mfTransmitting:
    begin
      if (modRes = mrYes) then
        FStartTransmitting
      else // mrNo
        FStartGaming;
    end;

    mfTransmitGame:
    begin
      if (modRes = mrOk) then
      begin
        with modSender as TTransmitGameSelectionForm do
        begin
          GM := GetSelected as TGamingManagerMI;
          FStartTransmitting(GM); 
        end;
      end
      else
        Stop;
    end;
  end; // case
end;

////////////////////////////////////////////////////////////////////////////////
// TTransmittingManagerMI

constructor TTransmittingManagerMI.Create(Connector: TConnector; GamingManager: TGamingManagerMI);
begin
  inherited Create(Connector);
  m_GamingManager := GamingManager;
end;

procedure TTransmittingManagerMI.Start;
begin
  if (not Connector.Opened) then
  begin
    if (not Connector.Open(FALSE)) then
      raise ETransmittingManagerMI.Create('ERROR: Cannot open connector!');
  end;
  m_GamingManager.FAddTransmitter(self);
end;


procedure TTransmittingManagerMI.ROnDestroy;

  procedure NRemoveFromTransmittings;
  var
    i: integer;
    GM: TGamingManagerMI;
  begin
    if (not Assigned(g_lstGamingManagers)) then
      exit;

    for i := 0 to g_lstGamingManagers.Count - 1 do
    begin
      GM := g_lstGamingManagers[i];
      if (Assigned(GM) and GM.FRemoveTransmitter(self)) then
        break;
    end;
  end;

begin // TTransmittingManagerMI.ROnDestroy
  NRemoveFromTransmittings;
  inherited ROnDestroy;
end;


procedure TTransmittingManagerMI.ROnCreate;
begin
//  PlayerNick := m_GamingManager.PlayerNick;
//  OpponentNick := m_GamingManager.OpponentNick;
//  OpponentId := m_GamingManager.OpponentId;
end;


procedure TTransmittingManagerMI.ConnectorHandler(e: TConnectorEvent; d1: pointer = nil; d2: pointer = nil);
begin
  case e of
    ceConnected:
    begin
      RSendData(CMD_VERSION + ' ' + IntToStr(CHESS4NET_VERSION));
      RSendData(CMD_TRANSMITTING);
    end;

    ceError, ceDisconnected:
    begin
      Connector.Close;
      Stop;
    end;

  else
    inherited ConnectorHandler(e, d1, d2);
  end; // case
end;


procedure TTransmittingManagerMI.RHandleConnectorDataCommand(sl: string);
var
  sr: string;
  lwOpponentClientVersion: Longword;
begin
  RSplitStr(sl, sl, sr);
  if (sl = CMD_VERSION) then
  begin
    RSplitStr(sr, sl, sr);
    lwOpponentClientVersion := StrToIntDef(sl, CHESS4NET_VERSION);

    if (lwOpponentClientVersion < 201000) then
    begin
      RSendData(CMD_GOODBYE);
      RReleaseWithConnectorGracefully;
    end;

    RSetOpponentClientVersion(lwOpponentClientVersion);
  end
  else if (sl = CMD_TRANSMITTING) then
  begin
    RSendData(CMD_GOODBYE); // TODO: some message or output to log
    RReleaseWithConnectorGracefully;
  end
  else if (sl = CMD_GOODBYE) then
  begin
    Stop;
  end
  else if (sl = CMD_WELCOME) then
  begin
    RSendData(CMD_WELCOME);
    m_bReady := TRUE;
    m_GamingManager.FSetGameContextToTransmitter(self);
  end;
end;


////////////////////////////////////////////////////////////////////////////////
// TManagerMI

constructor TManagerMI.Create(Connector: TConnector);
begin
  self.Connector := Connector;
  RCreate;
  AddToPlugins(self);
end;


destructor TManagerMI.Destroy;
begin
  RemoveFromPlugins(self);
  inherited;
end;


procedure TManagerMI.Start;
begin
  Assert(FALSE);
end;


procedure TManagerMI.Stop;
begin
  Release;
end;


procedure TManagerMI.RSendData(const cmd: string);
begin
  Connector.SendData(cmd);
end;


procedure TManagerMI.ROnDestroy;
begin
  if (Assigned(Connector)) then
  begin
    Connector.Free;
    Connector := nil;
  end;

  inherited ROnDestroy;
end;


procedure TManagerMI.ConnectorHandler(e: TConnectorEvent; d1: pointer = nil; d2: pointer = nil);
var
  iData: integer;
  strCmd: string;
  strLeft: string;
begin
  case e of
    ceData:
    begin
      Assert(High(TStringDynArray(d1)) >= 0);
      iData := 0;
      repeat
        strLeft := TStringDynArray(d1)[iData];
        inc(iData);
        strCmd := IfThen((iData <= High(TStringDynArray(d1))), '*');

        RHandleConnectorDataCommand(strLeft);
      until (strCmd = '');
    end; // ceData

  else
    inherited ConnectorHandler(e, d1, d2);
  end; // case
end;


procedure TManagerMI.RSetOpponentClientVersion(lwVersion: LongWord);
begin
  inherited RSetOpponentClientVersion(lwVersion);

  if (lwVersion >= 200901) then
    Connector.MultiSession := TRUE;
end;


initialization

finalization
  FreeAndNil(g_lstGamingManagers);

end.