source: trunk/GameProtocol.pas

Last change on this file was 323, checked in by chronos, 6 months ago
File size: 5.7 KB
Line 
1unit GameProtocol;
2
3interface
4
5uses
6 Classes, SysUtils, Game, VarBlockSerializer, CommPin, SpecializedList;
7
8type
9 TCommand = (cmdTextMessage, cmdTurnStart, cmdTurnEnd, cmdGameStart, cmdGameEnd,
10 cmdSurrender, cmdNextPlayer, cmdParametersChanged);
11 TCommandEvent = procedure (Command: TCommand; DataIn, DataOut: TStream);
12
13 TCommandTextMessage = record
14 Text: string;
15 end;
16
17 { TGameProtocolClient }
18
19 TGameProtocolClient = class
20 private
21 FOnGameEnd: TNotifyEvent;
22 FOnGameStart: TNotifyEvent;
23 FOnNextPlayer: TNotifyEvent;
24 FOnParametersChanged: TNotifyEvent;
25 FOnTurnStart: TNotifyEvent;
26 procedure Receive(Sender: TCommPin; Stream: TListByte);
27 procedure SendCmd(Command: TCommand);
28 public
29 Pin: TCommPin;
30 procedure TurnEnd;
31 procedure Surrender;
32 procedure SendMessage(Text: string);
33 constructor Create;
34 destructor Destroy; override;
35 property OnGameStart: TNotifyEvent read FOnGameStart write FOnGameStart;
36 property OnGameEnd: TNotifyEvent read FOnGameEnd write FOnGameEnd;
37 property OnTurnStart: TNotifyEvent read FOnTurnStart write FOnTurnStart;
38 property OnNextPlayer: TNotifyEvent read FOnNextPlayer write FOnNextPlayer;
39 property OnParametersChanged: TNotifyEvent read FOnParametersChanged
40 write FOnParametersChanged;
41 end;
42
43 TSendMessageEvent = procedure (Text: string) of object;
44
45 { TGameProtocolServer }
46
47 TGameProtocolServer = class
48 private
49 FOnSendMessage: TSendMessageEvent;
50 FOnSurrender: TNotifyEvent;
51 FOnTurnEnd: TNotifyEvent;
52 procedure Receive(Sender: TCommPin; Stream: TListByte);
53 procedure SendCmd(Command: TCommand);
54 public
55 Pin: TCommPin;
56 constructor Create;
57 destructor Destroy; override;
58 procedure GameStart;
59 procedure GameEnd;
60 procedure TurnStart;
61 procedure ParametersChanged;
62 procedure NextPlayer;
63 property OnSendMessage: TSendMessageEvent read FOnSendMessage
64 write FOnSendMessage;
65 property OnTurnEnd: TNotifyEvent read FOnTurnEnd write FOnTurnEnd;
66 property OnSurrender: TNotifyEvent read FOnSurrender write FOnSurrender;
67 end;
68
69
70implementation
71
72{ TGameProtocolServer }
73
74procedure TGameProtocolServer.Receive(Sender: TCommPin; Stream: TListByte);
75var
76 Data: TVarBlockSerializer;
77 Command: Integer;
78begin
79 Data := TVarBlockSerializer.Create;
80 Data.WriteVarList(Stream);
81 Data.Stream.Position := 0;
82 Command := Data.ReadVarUInt;
83 try
84 if Command = Integer(cmdTextMessage) then begin
85 if Assigned(FOnSendMessage) then FOnSendMessage(Data.ReadVarString);
86 end else
87 if Command = Integer(cmdTurnEnd) then begin
88 if Assigned(FOnTurnEnd) then FOnTurnEnd(Self);
89 end else
90 if Command = Integer(cmdSurrender) then begin
91 if Assigned(FOnSurrender) then FOnSurrender(Self);
92 end;
93 finally
94 Data.Free;
95 end;
96end;
97
98procedure TGameProtocolServer.SendCmd(Command: TCommand);
99var
100 Data: TVarBlockSerializer;
101 Data2: TListByte;
102begin
103 Data := TVarBlockSerializer.Create;
104 Data2 := TListByte.Create;
105 try
106 Data.WriteVarUInt(Integer(Command));
107 Data.Stream.Position := 0;
108 Data.ReadVarList(Data2);
109 Pin.Send(Data2);
110 finally
111 Data2.Free;
112 Data.Free;
113 end;
114end;
115
116constructor TGameProtocolServer.Create;
117begin
118 Pin := TCommPin.Create;
119 Pin.OnReceive := Receive;
120end;
121
122destructor TGameProtocolServer.Destroy;
123begin
124 FreeAndNil(Pin);
125 inherited;
126end;
127
128procedure TGameProtocolServer.GameStart;
129begin
130 SendCmd(cmdGameStart);
131end;
132
133procedure TGameProtocolServer.GameEnd;
134begin
135 SendCmd(cmdGameEnd);
136end;
137
138procedure TGameProtocolServer.TurnStart;
139begin
140 SendCmd(cmdTurnStart);
141end;
142
143procedure TGameProtocolServer.ParametersChanged;
144begin
145 SendCmd(cmdParametersChanged);
146end;
147
148procedure TGameProtocolServer.NextPlayer;
149begin
150 SendCmd(cmdNextPlayer);
151end;
152
153{ TGameProtocol }
154
155procedure TGameProtocolClient.Receive(Sender: TCommPin; Stream: TListByte);
156var
157 Data: TVarBlockSerializer;
158 Command: Integer;
159begin
160 Data := TVarBlockSerializer.Create;
161 Data.WriteVarList(Stream);
162 Data.Stream.Position := 0;
163 Command := Data.ReadVarUInt;
164 try
165 if Command = Integer(cmdGameStart) then begin
166 if Assigned(FOnGameStart) then FOnGameStart(Self);
167 end else
168 if Command = Integer(cmdGameEnd) then begin
169 if Assigned(FOnGameEnd) then FOnGameEnd(Self);
170 end else
171 if Command = Integer(cmdTurnStart) then begin
172 if Assigned(FOnTurnStart) then FOnTurnStart(Self);
173 end else
174 if Command = Integer(cmdNextPlayer) then begin
175 if Assigned(FOnNextPlayer) then FOnNextPlayer(Self);
176 end else
177 if Command = Integer(cmdParametersChanged) then begin
178 if Assigned(FOnParametersChanged) then FOnParametersChanged(Self);
179 end;
180 finally
181 Data.Free;
182 end;
183end;
184
185procedure TGameProtocolClient.SendCmd(Command: TCommand);
186var
187 Data: TVarBlockSerializer;
188 Data2: TListByte;
189begin
190 Data := TVarBlockSerializer.Create;
191 Data2 := TListByte.Create;
192 try
193 Data.WriteVarUInt(Integer(Command));
194 Data.Stream.Position := 0;
195 Data.ReadVarList(Data2);
196 Pin.Send(Data2);
197 finally
198 Data2.Free;
199 Data.Free;
200 end;
201end;
202
203procedure TGameProtocolClient.TurnEnd;
204begin
205 SendCmd(cmdTurnEnd);
206end;
207
208procedure TGameProtocolClient.Surrender;
209begin
210 SendCmd(cmdSurrender);
211end;
212
213procedure TGameProtocolClient.SendMessage(Text: string);
214var
215 Data: TVarBlockSerializer;
216 Data2: TListByte;
217begin
218 Data := TVarBlockSerializer.Create;
219 Data2 := TListByte.Create;
220 try
221 Data.WriteVarUInt(Integer(cmdTextMessage));
222 Data.WriteVarString(Text);
223 Data.Stream.Position := 0;
224 Data.ReadVarList(Data2);
225 Pin.Send(Data2);
226 finally
227 Data2.Free;
228 Data.Free;
229 end;
230end;
231
232constructor TGameProtocolClient.Create;
233begin
234 Pin := TCommPin.Create;
235 Pin.OnReceive := Receive;
236end;
237
238destructor TGameProtocolClient.Destroy;
239begin
240 FreeAndNil(Pin);
241 inherited;
242end;
243
244end.
245
Note: See TracBrowser for help on using the repository browser.