Changeset 179 for trunk/UGame.pas
- Timestamp:
- Feb 6, 2018, 3:47:23 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UGame.pas
r176 r179 8 8 Classes, SysUtils, ExtCtrls, Graphics, XMLConf, XMLRead, XMLWrite, 9 9 DOM, Math, LazFileUtils, UXMLUtils, Dialogs, Types, LCLType, LCLIntf, fgl, 10 UGeometry ;10 UGeometry, UGameSocket; 11 11 12 12 const … … 409 409 end; 410 410 411 TCommand = (cmdTextMessage); 412 TReceiveEvent = procedure (Command: TCommand; DataIn, DataOut: Pointer); 413 TCommandTextMessage = record 414 Text: string; 415 end; 416 411 417 { TClient } 412 418 … … 415 421 FGame: TGame; 416 422 FControlPlayer: TPlayer; 423 FOnReceive: TReceiveEvent; 417 424 procedure SetControlPlayer(AValue: TPlayer); 418 425 procedure SetGame(AValue: TGame); … … 420 427 Name: string; 421 428 View: TView; 429 GameSocket: TGameSocket; 430 procedure Send(Command: TCommand; DataOut, DataIn: Pointer); 422 431 constructor Create; 423 432 destructor Destroy; override; 424 433 property ControlPlayer: TPlayer read FControlPlayer write SetControlPlayer; 425 434 property Game: TGame read FGame write SetGame; 435 property OnReceive: TReceiveEvent read FOnReceive write FOnReceive; 426 436 end; 427 437 … … 512 522 end; 513 523 524 TServerMode = (smLocal, smNetworkServer, smNetworkClient); 525 514 526 { TServer } 515 527 516 528 TServer = class 517 529 private 530 FActive: Boolean; 518 531 FGame: TGame; 532 FServerMode: TServerMode; 533 procedure SetActive(AValue: Boolean); 519 534 procedure SetGame(AValue: TGame); 535 procedure SetServerMode(AValue: TServerMode); 520 536 public 521 537 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); 522 545 procedure InitClients; 523 546 procedure Clear; … … 525 548 destructor Destroy; override; 526 549 property Game: TGame read FGame write SetGame; 550 property Mode: TServerMode read FServerMode write SetServerMode; 551 property Active: Boolean read FActive write SetActive; 527 552 end; 528 553 … … 648 673 FGame := AValue; 649 674 View.Game := AValue; 675 end; 676 677 procedure TClient.Send(Command: TCommand; DataOut, DataIn: Pointer); 678 begin 650 679 end; 651 680 … … 3131 3160 end; 3132 3161 3162 procedure TServer.SetActive(AValue: Boolean); 3163 begin 3164 if FActive = AValue then Exit; 3165 FActive := AValue; 3166 end; 3167 3168 procedure TServer.SetServerMode(AValue: TServerMode); 3169 var 3170 LastActiveState: Boolean; 3171 begin 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; 3201 end; 3202 3203 procedure TServer.LoadConfig(Config: TXmlConfig; Path: string); 3204 begin 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; 3212 end; 3213 3214 procedure TServer.SaveConfig(Config: TXmlConfig; Path: string); 3215 begin 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; 3223 end; 3224 3133 3225 procedure TServer.InitClients; 3134 3226 var
Note:
See TracChangeset
for help on using the changeset viewer.