1 | unit GameServer;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Game, DOM, XMLConf, Generics.Collections, GameProtocol,
|
---|
7 | CommFrame, Player;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
71 | const
|
---|
72 | DefaultServerPort = 40009;
|
---|
73 |
|
---|
74 |
|
---|
75 | implementation
|
---|
76 |
|
---|
77 | { TServerClients }
|
---|
78 |
|
---|
79 | function TServerClients.SearchByPlayer(Player: TPlayer): TServerClient;
|
---|
80 | var
|
---|
81 | I: Integer;
|
---|
82 | begin
|
---|
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;
|
---|
87 | end;
|
---|
88 |
|
---|
89 | { TServerClient }
|
---|
90 |
|
---|
91 | procedure TServerClient.DoChange;
|
---|
92 | begin
|
---|
93 | Protocol.ParametersChanged;
|
---|
94 | end;
|
---|
95 |
|
---|
96 | procedure TServerClient.TurnStart;
|
---|
97 | begin
|
---|
98 | Protocol.TurnStart;
|
---|
99 | end;
|
---|
100 |
|
---|
101 | procedure TServerClient.GameEnd;
|
---|
102 | begin
|
---|
103 | Protocol.GameEnd;
|
---|
104 | end;
|
---|
105 |
|
---|
106 | procedure TServerClient.NextPlayer;
|
---|
107 | begin
|
---|
108 | Protocol.NextPlayer;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | constructor TServerClient.Create;
|
---|
112 | begin
|
---|
113 | CommFrame := TCommFrame.Create(nil);
|
---|
114 | Protocol := TGameProtocolServer.Create;
|
---|
115 | Protocol.OnTurnEnd := DoTurnEnd;
|
---|
116 | Protocol.OnSurrender := DoSurrender;
|
---|
117 | Protocol.Pin.Connect(CommFrame.FrameDataPin);
|
---|
118 | end;
|
---|
119 |
|
---|
120 | destructor TServerClient.Destroy;
|
---|
121 | begin
|
---|
122 | FreeAndNil(Protocol);
|
---|
123 | FreeAndNil(CommFrame);
|
---|
124 | inherited;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure TServerClient.DoTurnEnd(Sender: TObject);
|
---|
128 | begin
|
---|
129 | if Game.Running then Game.NextPlayer;
|
---|
130 | end;
|
---|
131 |
|
---|
132 | procedure TServerClient.DoSurrender(Sender: TObject);
|
---|
133 | begin
|
---|
134 | Player.Surrender;
|
---|
135 | if Game.Running then Game.NextPlayer;
|
---|
136 | end;
|
---|
137 |
|
---|
138 | { TServer }
|
---|
139 |
|
---|
140 | procedure TServer.SetGame(AValue: TGame);
|
---|
141 | var
|
---|
142 | I: Integer;
|
---|
143 | begin
|
---|
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;
|
---|
153 | end;
|
---|
154 |
|
---|
155 | procedure TServer.SetActive(AValue: Boolean);
|
---|
156 | begin
|
---|
157 | if FActive = AValue then Exit;
|
---|
158 | FActive := AValue;
|
---|
159 | end;
|
---|
160 |
|
---|
161 | procedure TServer.SetServerMode(AValue: TServerMode);
|
---|
162 | var
|
---|
163 | LastActiveState: Boolean;
|
---|
164 | begin
|
---|
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;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | procedure TServer.DoChange(Sender: TObject);
|
---|
198 | var
|
---|
199 | Client: TServerClient;
|
---|
200 | begin
|
---|
201 | for Client in Clients do
|
---|
202 | Client.DoChange;
|
---|
203 | end;
|
---|
204 |
|
---|
205 | procedure TServer.GameStarted(Sender: TObject);
|
---|
206 | var
|
---|
207 | I: Integer;
|
---|
208 | begin
|
---|
209 | for I := 0 to Clients.Count - 1 do
|
---|
210 | with TServerClient(Clients[I]) do begin
|
---|
211 | //TODO View.Clear;
|
---|
212 | end;
|
---|
213 | end;
|
---|
214 |
|
---|
215 | function TServer.GetNewServerClient: TServerClient;
|
---|
216 | begin
|
---|
217 | Result := TServerClient.Create;
|
---|
218 | Result.Game := Game;
|
---|
219 | Clients.Add(Result);
|
---|
220 | end;
|
---|
221 |
|
---|
222 | procedure TServer.LoadConfig(Config: TXmlConfig; Path: string);
|
---|
223 | begin
|
---|
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;
|
---|
231 | end;
|
---|
232 |
|
---|
233 | procedure TServer.SaveConfig(Config: TXmlConfig; Path: string);
|
---|
234 | begin
|
---|
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;
|
---|
242 | end;
|
---|
243 |
|
---|
244 | procedure TServer.InitClients;
|
---|
245 | begin
|
---|
246 | Clients.Clear;
|
---|
247 | end;
|
---|
248 |
|
---|
249 | procedure TServer.GameEnd;
|
---|
250 | var
|
---|
251 | I: Integer;
|
---|
252 | begin
|
---|
253 | for I := 0 to Clients.Count - 1 do
|
---|
254 | Clients[I].GameEnd;
|
---|
255 | end;
|
---|
256 |
|
---|
257 | procedure TServer.NextPlayer;
|
---|
258 | var
|
---|
259 | I: Integer;
|
---|
260 | begin
|
---|
261 | for I := 0 to Clients.Count - 1 do
|
---|
262 | Clients[I].NextPlayer;
|
---|
263 | end;
|
---|
264 |
|
---|
265 | procedure TServer.Clear;
|
---|
266 | begin
|
---|
267 | Clients.Clear;
|
---|
268 | Game.Clear;
|
---|
269 | end;
|
---|
270 |
|
---|
271 | constructor TServer.Create;
|
---|
272 | begin
|
---|
273 | FGame := nil;
|
---|
274 | Clients := TServerClients.Create;
|
---|
275 | end;
|
---|
276 |
|
---|
277 | destructor TServer.Destroy;
|
---|
278 | begin
|
---|
279 | FreeAndNil(Clients);
|
---|
280 | inherited;
|
---|
281 | end;
|
---|
282 |
|
---|
283 | procedure TServer.Assign(Source: TServer);
|
---|
284 | begin
|
---|
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;
|
---|
292 | end;
|
---|
293 |
|
---|
294 | end.
|
---|
295 |
|
---|