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