source: trunk/GameServer.pas

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