1 | unit GameClient;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, Game, Forms, Generics.Collections, GameProtocol,
|
---|
7 | GameServer, CommThread, Threading, CommFrame, Player;
|
---|
8 |
|
---|
9 | type
|
---|
10 | TClientConnectType = (ctLocal, ctNetwork);
|
---|
11 |
|
---|
12 | { TClient }
|
---|
13 |
|
---|
14 | TClient = class
|
---|
15 | private
|
---|
16 | FActive: Boolean;
|
---|
17 | FForm: TForm;
|
---|
18 | FGame: TGame;
|
---|
19 | FControlPlayer: TPlayer;
|
---|
20 | FOnChange: TNotifyEvent;
|
---|
21 | FOnDestroy: TNotifyEvent;
|
---|
22 | FOnGameEnd: TNotifyEvent;
|
---|
23 | FOnNextPlayer: TNotifyEvent;
|
---|
24 | FOnReceive: TCommandEvent;
|
---|
25 | FOnMove: TMoveEvent;
|
---|
26 | FOnTurnStart: TNotifyEvent;
|
---|
27 | CommThread: TCommThread;
|
---|
28 | CommFrame: TCommFrame;
|
---|
29 | procedure SetActive(AValue: Boolean);
|
---|
30 | procedure SetControlPlayer(AValue: TPlayer);
|
---|
31 | procedure SetForm(AValue: TForm);
|
---|
32 | procedure PlayerMove(CellFrom, CellTo: TPlayerCell; var CountOnce, CountRepeat: Integer;
|
---|
33 | Update: Boolean; var Confirm: Boolean);
|
---|
34 | procedure SetOnMove(AValue: TMoveEvent);
|
---|
35 | protected
|
---|
36 | procedure SetGame(AValue: TGame); virtual;
|
---|
37 | procedure ReceiveCmd(Command: TCommand; DataOut, DataIn: TStream); virtual;
|
---|
38 | procedure DoTurnStartHandler(Sender: TObject); virtual;
|
---|
39 | procedure DoTurnStart; virtual;
|
---|
40 | procedure DoGameEndHandler(Sender: TObject);
|
---|
41 | procedure DoGameEnd;
|
---|
42 | procedure DoNextPlayerHandler(Sender: TObject);
|
---|
43 | procedure DoNextPlayer;
|
---|
44 | procedure DoParametersChangedHandler(Sender: TObject);
|
---|
45 | public
|
---|
46 | Name: string;
|
---|
47 | LocalServer: TServer;
|
---|
48 | RemoteAddress: string;
|
---|
49 | RemotePort: Word;
|
---|
50 | ConnectType: TClientConnectType;
|
---|
51 | Protocol: TGameProtocolClient;
|
---|
52 | procedure DoChange;
|
---|
53 | constructor Create; virtual;
|
---|
54 | destructor Destroy; override;
|
---|
55 | property ControlPlayer: TPlayer read FControlPlayer write SetControlPlayer;
|
---|
56 | property Game: TGame read FGame write SetGame;
|
---|
57 | property Form: TForm read FForm write SetForm;
|
---|
58 | property Active: Boolean read FActive write SetActive;
|
---|
59 | property OnDestroy: TNotifyEvent read FOnDestroy write FOnDestroy;
|
---|
60 | property OnMove: TMoveEvent read FOnMove write SetOnMove;
|
---|
61 | property OnReceive: TCommandEvent read FOnReceive write FOnReceive;
|
---|
62 | property OnChange: TNotifyEvent read FOnChange write FOnChange;
|
---|
63 | property OnTurnStart: TNotifyEvent read FOnTurnStart write FOnTurnStart;
|
---|
64 | property OnGameEnd: TNotifyEvent read FOnGameEnd write FOnGameEnd;
|
---|
65 | property OnNextPlayer: TNotifyEvent read FOnNextPlayer write FOnNextPlayer;
|
---|
66 | end;
|
---|
67 |
|
---|
68 | { TClients }
|
---|
69 |
|
---|
70 | TClients = class(TObjectList<TClient>)
|
---|
71 | Game: TGame;
|
---|
72 | function New(Name: string): TClient;
|
---|
73 | function SearchPlayer(Player: TPlayer): TClient;
|
---|
74 | constructor Create(FreeObjects: Boolean = True);
|
---|
75 | end;
|
---|
76 |
|
---|
77 |
|
---|
78 | implementation
|
---|
79 |
|
---|
80 | uses
|
---|
81 | ClientGUI;
|
---|
82 |
|
---|
83 | { TClients }
|
---|
84 |
|
---|
85 | function TClients.New(Name: string): TClient;
|
---|
86 | begin
|
---|
87 | Result := TClientGUI.Create;
|
---|
88 | Result.Game := Game;
|
---|
89 | Result.Name := Name;
|
---|
90 | Add(Result);
|
---|
91 | end;
|
---|
92 |
|
---|
93 | function TClients.SearchPlayer(Player: TPlayer): TClient;
|
---|
94 | var
|
---|
95 | Client: TClient;
|
---|
96 | begin
|
---|
97 | Result := nil;
|
---|
98 | for Client in Self do
|
---|
99 | if Client.ControlPlayer = Player then begin
|
---|
100 | Result := Client;
|
---|
101 | Break;
|
---|
102 | end;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | constructor TClients.Create(FreeObjects: Boolean = True);
|
---|
106 | begin
|
---|
107 | Game := nil;
|
---|
108 | inherited;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | { TClient }
|
---|
112 |
|
---|
113 | procedure TClient.SetGame(AValue: TGame);
|
---|
114 | begin
|
---|
115 | if FGame = AValue then Exit;
|
---|
116 | FGame := AValue;
|
---|
117 | end;
|
---|
118 |
|
---|
119 | procedure TClient.DoChange;
|
---|
120 | begin
|
---|
121 | if Assigned(FOnChange) then
|
---|
122 | FOnChange(Self);
|
---|
123 | end;
|
---|
124 |
|
---|
125 | procedure TClient.PlayerMove(CellFrom, CellTo: TPlayerCell; var CountOnce,
|
---|
126 | CountRepeat: Integer; Update: Boolean; var Confirm: Boolean);
|
---|
127 | begin
|
---|
128 | if Assigned(FOnMove) then
|
---|
129 | FOnMove(CellFrom, CellTo, CountOnce, CountRepeat, Update, Confirm);
|
---|
130 | end;
|
---|
131 |
|
---|
132 | procedure TClient.SetOnMove(AValue: TMoveEvent);
|
---|
133 | begin
|
---|
134 | //if FOnMove = AValue then Exit;
|
---|
135 | FOnMove := AValue;
|
---|
136 | end;
|
---|
137 |
|
---|
138 | procedure TClient.ReceiveCmd(Command: TCommand; DataOut, DataIn: TStream);
|
---|
139 | begin
|
---|
140 | end;
|
---|
141 |
|
---|
142 | procedure TClient.DoTurnStartHandler(Sender: TObject);
|
---|
143 | begin
|
---|
144 | Synchronize(DoTurnStart);
|
---|
145 | end;
|
---|
146 |
|
---|
147 | procedure TClient.DoTurnStart;
|
---|
148 | begin
|
---|
149 | if Assigned(FOnTurnStart) then
|
---|
150 | FOnTurnStart(Self);
|
---|
151 | end;
|
---|
152 |
|
---|
153 | procedure TClient.DoGameEndHandler(Sender: TObject);
|
---|
154 | begin
|
---|
155 | Synchronize(DoGameEnd);
|
---|
156 | end;
|
---|
157 |
|
---|
158 | procedure TClient.DoGameEnd;
|
---|
159 | begin
|
---|
160 | if Assigned(FOnGameEnd) then FOnGameEnd(Self);
|
---|
161 | end;
|
---|
162 |
|
---|
163 | procedure TClient.DoNextPlayerHandler(Sender: TObject);
|
---|
164 | begin
|
---|
165 | Synchronize(DoNextPlayer);
|
---|
166 | end;
|
---|
167 |
|
---|
168 | procedure TClient.DoNextPlayer;
|
---|
169 | begin
|
---|
170 | if Assigned(FOnNextPlayer) then FOnNextPlayer(Self);
|
---|
171 | end;
|
---|
172 |
|
---|
173 | procedure TClient.DoParametersChangedHandler(Sender: TObject);
|
---|
174 | begin
|
---|
175 | Synchronize(DoChange);
|
---|
176 | end;
|
---|
177 |
|
---|
178 | procedure TClient.SetControlPlayer(AValue: TPlayer);
|
---|
179 | begin
|
---|
180 | if FControlPlayer = AValue then Exit;
|
---|
181 | if Assigned(FControlPlayer) then
|
---|
182 | FControlPlayer.OnMove := nil;
|
---|
183 | FControlPlayer := AValue;
|
---|
184 | if Assigned(FControlPlayer) then begin
|
---|
185 | FControlPlayer.OnMove := PlayerMove;
|
---|
186 | end;
|
---|
187 | end;
|
---|
188 |
|
---|
189 | procedure TClient.SetActive(AValue: Boolean);
|
---|
190 | var
|
---|
191 | ServerClient: TServerClient;
|
---|
192 | begin
|
---|
193 | if FActive = AValue then Exit;
|
---|
194 | if FActive then begin
|
---|
195 | case ConnectType of
|
---|
196 | ctLocal: begin
|
---|
197 | CommThread.Active := False;
|
---|
198 | CommThread.Ext.Disconnect;
|
---|
199 | CommThread.Pin.Disconnect;
|
---|
200 | end;
|
---|
201 | end;
|
---|
202 | end;
|
---|
203 | FActive := AValue;
|
---|
204 | if FActive then begin
|
---|
205 | case ConnectType of
|
---|
206 | ctLocal: if LocalServer.Active then begin
|
---|
207 | CommFrame.FrameDataPin.Connect(Protocol.Pin);
|
---|
208 | CommFrame.RawDataPin.Connect(CommThread.Pin);
|
---|
209 | CommThread.Active := True;
|
---|
210 | CommFrame.Active := True;
|
---|
211 | ServerClient := LocalServer.GetNewServerClient;
|
---|
212 | ServerClient.Player := ControlPlayer;
|
---|
213 | ServerClient.CommFrame.RawDataPin.Connect(CommThread.Ext);
|
---|
214 | end else raise Exception.Create('Local server is not active');
|
---|
215 | //ctNetwork: ;
|
---|
216 | end;
|
---|
217 | end else begin
|
---|
218 | Protocol.Pin.Disconnect;
|
---|
219 | end;
|
---|
220 | end;
|
---|
221 |
|
---|
222 | procedure TClient.SetForm(AValue: TForm);
|
---|
223 | begin
|
---|
224 | if FForm = AValue then Exit;
|
---|
225 | FForm := AValue;
|
---|
226 | end;
|
---|
227 |
|
---|
228 | constructor TClient.Create;
|
---|
229 | begin
|
---|
230 | CommThread := TCommThread.Create(nil);
|
---|
231 | CommFrame := TCommFrame.Create(nil);
|
---|
232 | FControlPlayer := nil;
|
---|
233 | Protocol := TGameProtocolClient.Create;
|
---|
234 | Protocol.OnTurnStart := DoTurnStartHandler;
|
---|
235 | Protocol.OnGameEnd := DoGameEndHandler;
|
---|
236 | Protocol.OnNextPlayer := DoNextPlayerHandler;
|
---|
237 | Protocol.OnParametersChanged := DoParametersChangedHandler;
|
---|
238 | end;
|
---|
239 |
|
---|
240 | destructor TClient.Destroy;
|
---|
241 | begin
|
---|
242 | Form := nil;
|
---|
243 | ControlPlayer := nil;
|
---|
244 | FreeAndNil(Protocol);
|
---|
245 | FreeAndNil(CommThread);
|
---|
246 | FreeAndNil(CommFrame);
|
---|
247 | inherited;
|
---|
248 | end;
|
---|
249 |
|
---|
250 | end.
|
---|
251 |
|
---|