Ignore:
Timestamp:
May 17, 2018, 5:41:47 PM (6 years ago)
Author:
chronos
Message:
  • Modified: AI player related code moved to UClientAI unit. It is now extension of TClient class to simulate regular human client.
  • Modified: More work on client-server architecture.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGameProtocol.pas

    r185 r202  
    2222    FOnGameEnd: TNotifyEvent;
    2323    FOnGameStart: TNotifyEvent;
     24    FOnTurnStart: TNotifyEvent;
    2425    procedure Receive(Sender: TCommPin; Stream: TListByte);
     26    procedure SendCmd(Command: TCommand);
    2527  public
    2628    Pin: TCommPin;
     29    procedure TurnEnd;
    2730    procedure SendMessage(Text: string);
    2831    constructor Create;
     
    3235    property OnGameEnd: TNotifyEvent read FOnGameEnd
    3336      write FOnGameEnd;
     37    property OnTurnStart: TNotifyEvent read FOnTurnStart
     38      write FOnTurnStart;
    3439  end;
    3540
     
    4146  private
    4247    FOnSendMessage: TSendMessageEvent;
     48    FOnTurnEnd: TNotifyEvent;
    4349    procedure Receive(Sender: TCommPin; Stream: TListByte);
     50    procedure SendCmd(Command: TCommand);
    4451  public
    4552    Pin: TCommPin;
     
    4855    procedure GameStart;
    4956    procedure GameEnd;
     57    procedure TurnStart;
    5058    property OnSendMessage: TSendMessageEvent read FOnSendMessage
    5159      write FOnSendMessage;
     60    property OnTurnEnd: TNotifyEvent read FOnTurnEnd
     61      write FOnTurnEnd;
    5262  end;
    5363
     
    6979    if Command = Integer(cmdTextMessage) then begin
    7080      if Assigned(FOnSendMessage) then FOnSendMessage(Data.ReadVarString);
     81    end else
     82    if Command = Integer(cmdTurnEnd) then begin
     83      if Assigned(FOnTurnEnd) then FOnTurnEnd(Self);
    7184    end;
    7285  finally
     
    7588end;
    7689
    77 constructor TGameProtocolServer.Create;
    78 begin
    79   Pin := TCommPin.Create;
    80   Pin.OnReceive := Receive;
    81 end;
    82 
    83 destructor TGameProtocolServer.Destroy;
    84 begin
    85   Pin.Free;
    86   inherited Destroy;
    87 end;
    88 
    89 procedure TGameProtocolServer.GameStart;
     90procedure TGameProtocolServer.SendCmd(Command: TCommand);
    9091var
    9192  Data: TVarBlockSerializer;
     
    9596  Data2 := TListByte.Create;
    9697  try
    97     Data.WriteVarUInt(Integer(cmdGameStart));
     98    Data.WriteVarUInt(Integer(Command));
    9899    Data.Stream.Position := 0;
    99100    Data.ReadVarList(Data2);
     
    105106end;
    106107
     108constructor TGameProtocolServer.Create;
     109begin
     110  Pin := TCommPin.Create;
     111  Pin.OnReceive := Receive;
     112end;
     113
     114destructor TGameProtocolServer.Destroy;
     115begin
     116  Pin.Free;
     117  inherited Destroy;
     118end;
     119
     120procedure TGameProtocolServer.GameStart;
     121begin
     122  SendCmd(cmdGameStart);
     123end;
     124
    107125procedure TGameProtocolServer.GameEnd;
    108 var
    109   Data: TVarBlockSerializer;
    110   Data2: TListByte;
    111 begin
    112   Data := TVarBlockSerializer.Create;
    113   Data2 := TListByte.Create;
    114   try
    115     Data.WriteVarUInt(Integer(cmdGameEnd));
    116     Data.Stream.Position := 0;
    117     Data.ReadVarList(Data2);
    118     Pin.Send(Data2);
    119   finally
    120     Data2.Free;
    121     Data.Free;
    122   end;
     126begin
     127  SendCmd(cmdGameEnd);
     128end;
     129
     130procedure TGameProtocolServer.TurnStart;
     131begin
     132  SendCmd(cmdTurnStart);
    123133end;
    124134
     
    131141begin
    132142  Data := TVarBlockSerializer.Create;
    133   Data.ReadVarList(Stream);
     143  Data.WriteVarList(Stream);
    134144  Data.Stream.Position := 0;
    135145  Command := Data.ReadVarSInt;
     
    140150    if Command = Integer(cmdGameEnd) then begin
    141151      if Assigned(FOnGameEnd) then FOnGameEnd(Self);
     152    end else
     153    if Command = Integer(cmdTurnStart) then begin
     154      if Assigned(FOnTurnStart) then FOnTurnStart(Self);
    142155    end;
    143156  finally
    144157    Data.Free;
    145158  end;
     159end;
     160
     161procedure TGameProtocolClient.SendCmd(Command: TCommand);
     162var
     163  Data: TVarBlockSerializer;
     164  Data2: TListByte;
     165begin
     166  Data := TVarBlockSerializer.Create;
     167  Data2 := TListByte.Create;
     168  try
     169    Data.WriteVarUInt(Integer(Command));
     170    Data.Stream.Position := 0;
     171    Data.ReadVarList(Data2);
     172    Pin.Send(Data2);
     173  finally
     174    Data2.Free;
     175    Data.Free;
     176  end;
     177end;
     178
     179procedure TGameProtocolClient.TurnEnd;
     180begin
     181  SendCmd(cmdTurnEnd);
    146182end;
    147183
Note: See TracChangeset for help on using the changeset viewer.