Changeset 179 for trunk/UGame.pas


Ignore:
Timestamp:
Feb 6, 2018, 3:47:23 PM (7 years ago)
Author:
chronos
Message:
  • Added: A preparation for gameplay over network.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UGame.pas

    r176 r179  
    88  Classes, SysUtils, ExtCtrls, Graphics, XMLConf, XMLRead, XMLWrite,
    99  DOM, Math, LazFileUtils, UXMLUtils, Dialogs, Types, LCLType, LCLIntf, fgl,
    10   UGeometry;
     10  UGeometry, UGameSocket;
    1111
    1212const
     
    409409  end;
    410410
     411  TCommand = (cmdTextMessage);
     412  TReceiveEvent = procedure (Command: TCommand; DataIn, DataOut: Pointer);
     413  TCommandTextMessage = record
     414    Text: string;
     415  end;
     416
    411417  { TClient }
    412418
     
    415421    FGame: TGame;
    416422    FControlPlayer: TPlayer;
     423    FOnReceive: TReceiveEvent;
    417424    procedure SetControlPlayer(AValue: TPlayer);
    418425    procedure SetGame(AValue: TGame);
     
    420427    Name: string;
    421428    View: TView;
     429    GameSocket: TGameSocket;
     430    procedure Send(Command: TCommand; DataOut, DataIn: Pointer);
    422431    constructor Create;
    423432    destructor Destroy; override;
    424433    property ControlPlayer: TPlayer read FControlPlayer write SetControlPlayer;
    425434    property Game: TGame read FGame write SetGame;
     435    property OnReceive: TReceiveEvent read FOnReceive write FOnReceive;
    426436  end;
    427437
     
    512522  end;
    513523
     524  TServerMode = (smLocal, smNetworkServer, smNetworkClient);
     525
    514526  { TServer }
    515527
    516528  TServer = class
    517529  private
     530    FActive: Boolean;
    518531    FGame: TGame;
     532    FServerMode: TServerMode;
     533    procedure SetActive(AValue: Boolean);
    519534    procedure SetGame(AValue: TGame);
     535    procedure SetServerMode(AValue: TServerMode);
    520536  public
    521537    Clients: TClients;
     538    LocalNetworkAddress: string;
     539    LocalNetworkPort: Word;
     540    RemoteNetworkAddress: string;
     541    RemoteNetworkPort: Word;
     542    GameSocket: TGameSocket;
     543    procedure LoadConfig(Config: TXmlConfig; Path: string);
     544    procedure SaveConfig(Config: TXmlConfig; Path: string);
    522545    procedure InitClients;
    523546    procedure Clear;
     
    525548    destructor Destroy; override;
    526549    property Game: TGame read FGame write SetGame;
     550    property Mode: TServerMode read FServerMode write SetServerMode;
     551    property Active: Boolean read FActive write SetActive;
    527552  end;
    528553
     
    648673  FGame := AValue;
    649674  View.Game := AValue;
     675end;
     676
     677procedure TClient.Send(Command: TCommand; DataOut, DataIn: Pointer);
     678begin
    650679end;
    651680
     
    31313160end;
    31323161
     3162procedure TServer.SetActive(AValue: Boolean);
     3163begin
     3164  if FActive = AValue then Exit;
     3165  FActive := AValue;
     3166end;
     3167
     3168procedure TServer.SetServerMode(AValue: TServerMode);
     3169var
     3170  LastActiveState: Boolean;
     3171begin
     3172  if FServerMode = AValue then Exit;
     3173  LastActiveState := Active;
     3174  Active := False;
     3175  FServerMode := AValue;
     3176  FreeAndNil(GameSocket);
     3177  case FServerMode of
     3178    smLocal: GameSocket := TGameSocketDirect.Create;
     3179    smNetworkServer: begin
     3180      GameSocket := TGameSocketNetworkServer.Create;
     3181      TGameSocketNetworkServer(GameSocket).TCPServer.Address := LocalNetworkAddress;
     3182      TGameSocketNetworkServer(GameSocket).TCPServer.Port := LocalNetworkPort;
     3183      //TODO TGameSocketNetworkServer(GameSocket).TCPServer.Active := True;
     3184    end;
     3185    smNetworkClient: begin
     3186      GameSocket := TGameSocketNetworkClient.Create;
     3187      if (LocalNetworkAddress = RemoteNetworkAddress) and
     3188        (LocalNetworkPort = RemoteNetworkPort) then begin
     3189          // User wants to play on its own server
     3190          GameSocket := TGameSocketNetworkServer.Create;
     3191          TGameSocketNetworkServer(GameSocket).TCPServer.Address := LocalNetworkAddress;
     3192          TGameSocketNetworkServer(GameSocket).TCPServer.Port := LocalNetworkPort;
     3193          //TODO TGameSocketNetworkServer(GameSocket).TCPServer.Active := True;
     3194        end else
     3195        begin
     3196          // Do not create socket. User wants to connect elsewhere
     3197        end;
     3198      end;
     3199  end;
     3200  Active := LastActiveState;
     3201end;
     3202
     3203procedure TServer.LoadConfig(Config: TXmlConfig; Path: string);
     3204begin
     3205  with Config do begin
     3206    LocalNetworkAddress := string(GetValue(DOMString(Path + '/LocalNetworkAddress'), 'localhost'));
     3207    LocalNetworkPort := GetValue(DOMString(Path + '/LocalNetworkPort'), 40009);
     3208    RemoteNetworkAddress := string(GetValue(DOMString(Path + '/RemoteNetworkAddress'), 'localhost'));
     3209    RemoteNetworkPort := GetValue(DOMString(Path + '/RemoteNetworkPort'), 40009);
     3210    Mode := TServerMode(GetValue(DOMString(Path + '/Mode'), Integer(smLocal)));
     3211  end;
     3212end;
     3213
     3214procedure TServer.SaveConfig(Config: TXmlConfig; Path: string);
     3215begin
     3216  with Config do begin
     3217    SetValue(DOMString(Path + '/LocalNetworkAddress'), DOMString(LocalNetworkAddress));
     3218    SetValue(DOMString(Path + '/LocalNetworkPort'), LocalNetworkPort);
     3219    SetValue(DOMString(Path + '/RemoteNetworkAddress'), DOMString(RemoteNetworkAddress));
     3220    SetValue(DOMString(Path + '/RemoteNetworkPort'), RemoteNetworkPort);
     3221    SetValue(DOMString(Path + '/Mode'), Integer(Mode));
     3222  end;
     3223end;
     3224
    31333225procedure TServer.InitClients;
    31343226var
Note: See TracChangeset for help on using the changeset viewer.