source: tags/1.3.0/UGameServer.pas

Last change on this file was 231, checked in by chronos, 6 years ago
  • Modified: UGame unit was split to UPlayer, UMap and UClientGUI units to have better logical separation of game classes.
  • Modified: Drawing methods moved from TMap and TPlayerMap to TClientGUI. Generic TClient class and TComputer classes don't need have any drawing support.
File size: 6.9 KB
Line 
1unit UGameServer;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, UGame, DOM, XMLConf, fgl, UGameProtocol, UCommFrame, UPlayer;
9
10type
11 TServerMode = (smLocal, smNetworkServer, smNetworkClient);
12
13 { TServerClient }
14
15 TServerClient = class
16 private
17 procedure DoTurnEnd(Sender: TObject);
18 procedure DoSurrender(Sender: TObject);
19 public
20 Game: TGame;
21 Protocol: TGameProtocolServer;
22 Player: TPlayer;
23 CommFrame: TCommFrame;
24 procedure DoChange;
25 procedure TurnStart;
26 procedure GameEnd;
27 procedure NextPlayer;
28 constructor Create;
29 destructor Destroy; override;
30 end;
31
32 { TServerClients }
33
34 TServerClients = class(TFPGObjectList<TServerClient>)
35 Game: TGame;
36 function SearchByPlayer(Player: TPlayer): TServerClient;
37 end;
38
39 { TServer }
40
41 TServer = class
42 private
43 FActive: Boolean;
44 FGame: TGame;
45 FServerMode: TServerMode;
46 procedure SetActive(AValue: Boolean);
47 procedure SetGame(AValue: TGame);
48 procedure SetServerMode(AValue: TServerMode);
49 procedure GameStarted(Sender: TObject);
50 public
51 Clients: TServerClients;
52 LocalNetworkAddress: string;
53 LocalNetworkPort: Word;
54 RemoteNetworkAddress: string;
55 RemoteNetworkPort: Word;
56 procedure DoChange(Sender: TObject);
57 function GetNewServerClient: TServerClient;
58 procedure LoadConfig(Config: TXmlConfig; Path: string);
59 procedure SaveConfig(Config: TXmlConfig; Path: string);
60 procedure InitClients;
61 procedure GameEnd;
62 procedure NextPlayer;
63 procedure Clear;
64 constructor Create;
65 destructor Destroy; override;
66 property Game: TGame read FGame write SetGame;
67 property Mode: TServerMode read FServerMode write SetServerMode;
68 property Active: Boolean read FActive write SetActive;
69 end;
70
71const
72 DefaultServerPort = 40009;
73
74implementation
75
76{ TServerClients }
77
78function TServerClients.SearchByPlayer(Player: TPlayer): TServerClient;
79var
80 I: Integer;
81begin
82 I := 0;
83 while (I < Count) and (Items[I].Player <> Player) do Inc(I);
84 if I < Count then Result := Items[I]
85 else Result := nil;
86end;
87
88{ TServerClient }
89
90procedure TServerClient.DoChange;
91begin
92 Protocol.ParametersChanged;
93end;
94
95procedure TServerClient.TurnStart;
96begin
97 Protocol.TurnStart;
98end;
99
100procedure TServerClient.GameEnd;
101begin
102 Protocol.GameEnd;
103end;
104
105procedure TServerClient.NextPlayer;
106begin
107 Protocol.NextPlayer;
108end;
109
110constructor TServerClient.Create;
111begin
112 CommFrame := TCommFrame.Create(nil);
113 Protocol := TGameProtocolServer.Create;
114 Protocol.OnTurnEnd := DoTurnEnd;
115 Protocol.OnSurrender := DoSurrender;
116 Protocol.Pin.Connect(CommFrame.FrameDataPin);
117end;
118
119destructor TServerClient.Destroy;
120begin
121 Protocol.Free;
122 CommFrame.Free;
123 inherited Destroy;
124end;
125
126procedure TServerClient.DoTurnEnd(Sender: TObject);
127begin
128 if Game.Running then Game.NextPlayer;
129end;
130
131procedure TServerClient.DoSurrender(Sender: TObject);
132begin
133 Player.Surrender;
134 if Game.Running then Game.NextPlayer;
135end;
136
137{ TServer }
138
139procedure TServer.SetGame(AValue: TGame);
140var
141 I: Integer;
142begin
143 if FGame = AValue then Exit;
144 FGame := AValue;
145 for I := 0 to Clients.Count - 1 do
146 Clients[I].Game := FGame;
147 Clients.Game := Game;
148 if Assigned(FGame) then
149 FGame.OnChange := DoChange;
150end;
151
152procedure TServer.SetActive(AValue: Boolean);
153begin
154 if FActive = AValue then Exit;
155 FActive := AValue;
156end;
157
158procedure TServer.SetServerMode(AValue: TServerMode);
159var
160 LastActiveState: Boolean;
161begin
162 if FServerMode = AValue then Exit;
163 LastActiveState := Active;
164 Active := False;
165 FServerMode := AValue;
166{ FreeAndNil(GameSocket);
167 case FServerMode of
168 smLocal: GameSocket := TGameSocketDirect.Create;
169 smNetworkServer: begin
170 GameSocket := TGameSocketNetworkServer.Create;
171 TGameSocketNetworkServer(GameSocket).TCPServer.Address := LocalNetworkAddress;
172 TGameSocketNetworkServer(GameSocket).TCPServer.Port := LocalNetworkPort;
173 //TODO TGameSocketNetworkServer(GameSocket).TCPServer.Active := True;
174 end;
175 smNetworkClient: begin
176 GameSocket := TGameSocketNetworkClient.Create;
177 if (LocalNetworkAddress = RemoteNetworkAddress) and
178 (LocalNetworkPort = RemoteNetworkPort) then begin
179 // User wants to play on its own server
180 GameSocket := TGameSocketNetworkServer.Create;
181 TGameSocketNetworkServer(GameSocket).TCPServer.Address := LocalNetworkAddress;
182 TGameSocketNetworkServer(GameSocket).TCPServer.Port := LocalNetworkPort;
183 //TODO TGameSocketNetworkServer(GameSocket).TCPServer.Active := True;
184 end else
185 begin
186 // Do not create socket. User wants to connect elsewhere
187 end;
188 end;
189 end;
190 }
191 Active := LastActiveState;
192end;
193
194procedure TServer.DoChange(Sender: TObject);
195var
196 Client: TServerClient;
197begin
198 for Client in Clients do
199 Client.DoChange;
200end;
201
202procedure TServer.GameStarted(Sender: TObject);
203var
204 I: Integer;
205begin
206 for I := 0 to Clients.Count - 1 do
207 with TServerClient(Clients[I]) do begin
208 //TODO View.Clear;
209 end;
210end;
211
212function TServer.GetNewServerClient: TServerClient;
213begin
214 Result := TServerClient.Create;
215 Result.Game := Game;
216 Clients.Add(Result);
217end;
218
219procedure TServer.LoadConfig(Config: TXmlConfig; Path: string);
220begin
221 with Config do begin
222 LocalNetworkAddress := string(GetValue(DOMString(Path + '/LocalNetworkAddress'), 'localhost'));
223 LocalNetworkPort := GetValue(DOMString(Path + '/LocalNetworkPort'), DefaultServerPort);
224 RemoteNetworkAddress := string(GetValue(DOMString(Path + '/RemoteNetworkAddress'), 'localhost'));
225 RemoteNetworkPort := GetValue(DOMString(Path + '/RemoteNetworkPort'), DefaultServerPort);
226 Mode := TServerMode(GetValue(DOMString(Path + '/Mode'), Integer(smLocal)));
227 end;
228end;
229
230procedure TServer.SaveConfig(Config: TXmlConfig; Path: string);
231begin
232 with Config do begin
233 SetValue(DOMString(Path + '/LocalNetworkAddress'), DOMString(LocalNetworkAddress));
234 SetValue(DOMString(Path + '/LocalNetworkPort'), LocalNetworkPort);
235 SetValue(DOMString(Path + '/RemoteNetworkAddress'), DOMString(RemoteNetworkAddress));
236 SetValue(DOMString(Path + '/RemoteNetworkPort'), RemoteNetworkPort);
237 SetValue(DOMString(Path + '/Mode'), Integer(Mode));
238 end;
239end;
240
241procedure TServer.InitClients;
242begin
243 Clients.Clear;
244end;
245
246procedure TServer.GameEnd;
247var
248 I: Integer;
249begin
250 for I := 0 to Clients.Count - 1 do
251 Clients[I].GameEnd;
252end;
253
254procedure TServer.NextPlayer;
255var
256 I: Integer;
257begin
258 for I := 0 to Clients.Count - 1 do
259 Clients[I].NextPlayer;
260end;
261
262procedure TServer.Clear;
263begin
264 Clients.Clear;
265 Game.Clear;
266end;
267
268constructor TServer.Create;
269begin
270 FGame := nil;
271 Clients := TServerClients.Create;
272end;
273
274destructor TServer.Destroy;
275begin
276 FreeAndNil(Clients);
277 inherited Destroy;
278end;
279
280end.
281
Note: See TracBrowser for help on using the repository browser.