-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUFrmGame.pas
459 lines (369 loc) · 11.2 KB
/
UFrmGame.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
unit UFrmGame;
interface
uses Vcl.Forms, System.ImageList, Vcl.ImgList, Vcl.Controls, Vcl.StdCtrls,
Vcl.Buttons, Vcl.ComCtrls, Vcl.ExtCtrls, System.Classes,
//
System.Types, URichEditUnicode, UMatrix;
type
TGameStatus = (
gsUnknown, gsPreparing,
gsPlaying, gsMyTurn, gsWaitValid, gsAgreement, gsContest,
gsPaused, gsGameOver);
TFrmGame = class(TForm)
BoxSide: TPanel;
BoxChat: TPanel;
EdChatLog: TRichEdit;
EdChatMsg: TEdit;
LPlayers: TListBox;
LLetters: TListBox;
BoxOperations: TPanel;
BtnStartGame: TBitBtn;
LbLetters: TLabel;
LbChat: TLabel;
IL: TImageList;
BtnDone: TBitBtn;
BtnAgree: TBitBtn;
BtnDisagree: TBitBtn;
BtnRules: TBitBtn;
BtnRestart: TBitBtn;
BoxGrid: TPanel;
SB: TScrollBox;
LbPosition: TLabel;
BoxStatus: TPanel;
BoxHeaderPlayers: TPanel;
LbHeaderPlayer: TLabel;
LbHeaderScore: TLabel;
BtnContestAccept: TBitBtn;
BtnContestReject: TBitBtn;
Timer: TTimer;
LbTimer: TLabel;
procedure EdChatMsgKeyPress(Sender: TObject; var Key: Char);
procedure BtnStartGameClick(Sender: TObject);
procedure LPlayersDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure FormCreate(Sender: TObject);
procedure LLettersDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure BtnDoneClick(Sender: TObject);
procedure BtnAgreeClick(Sender: TObject);
procedure BtnDisagreeClick(Sender: TObject);
procedure BtnRulesClick(Sender: TObject);
procedure BtnRestartClick(Sender: TObject);
procedure BtnContestAcceptClick(Sender: TObject);
procedure BtnContestRejectClick(Sender: TObject);
procedure TimerTimer(Sender: TObject);
public
Status: TGameStatus;
PB: TMatrixImage;
procedure Initialize(Reconnected: Boolean);
procedure MatrixReceived(const A: string);
procedure ChatLog(const Player, Text: string; Other: Boolean);
procedure GameStartedReceived;
procedure InitMyTurn;
procedure MyTurnTimeoutReceived;
procedure WaitValidationReceived;
procedure ValidationAcceptedReceived;
procedure ValidationRejectedReceived;
procedure AgreementRequestReceived;
procedure AgreementFinishReceived(const A: string);
procedure OpenContestPeriodReceived;
procedure ReceivedPausedByDrop;
procedure ReceivedDropContinue;
procedure GameOverReceived;
procedure ReceivedPreparingNewGame;
procedure ReceivedTimerStart(const A: string);
procedure ReceivedTimerStop;
procedure ReceivedAutoRejectedByInvalidLetters;
procedure ReceivedContestResponse(const A: string);
procedure LettersExchangedReceived;
private
TimerSeconds: Integer;
procedure InitTranslation;
procedure SetStatus(NewStatus: TGameStatus);
end;
var
FrmGame: TFrmGame;
implementation
{$R *.dfm}
uses System.SysUtils,
Vcl.Graphics, Winapi.Windows, Winapi.Messages,
UVars, UDams, ULanguage,
DzSocket, UDMClient, UDMServer,
UFrmLog, UFrmMain, UFrmRules;
procedure TFrmGame.FormCreate(Sender: TObject);
begin
PB := TMatrixImage.Create(Self);
PB.Parent := SB;
end;
procedure TFrmGame.InitTranslation;
begin
LbHeaderPlayer.Caption := Lang.Get('GAME_HEADER_PLAYER');
LbHeaderScore.Caption := Lang.Get('GAME_HEADER_SCORE');
LbLetters.Caption := Lang.Get('GAME_BOX_LETTERS');
LbChat.Caption := Lang.Get('GAME_BOX_CHAT');
BtnRestart.Caption := Lang.Get('GAME_BTN_RESTART');
BtnStartGame.Caption := Lang.Get('GAME_BTN_START');
BtnRules.Caption := Lang.Get('GAME_BTN_RULES');
BtnDone.Caption := Lang.Get('GAME_BTN_DONE');
BtnAgree.Caption := Lang.Get('GAME_BTN_AGREE');
BtnDisagree.Caption := Lang.Get('GAME_BTN_DISAGREE');
BtnContestAccept.Caption := Lang.Get('GAME_BTN_CONTEST_ACCEPT');
BtnContestReject.Caption := Lang.Get('GAME_BTN_CONTEST_REJECT');
end;
procedure TFrmGame.Initialize(Reconnected: Boolean);
begin
InitTranslation;
if Reconnected then
SetStatus(gsPaused)
else
SetStatus(gsPreparing);
PB.SetMatrixSize(0, 0);
LPlayers.Clear;
LLetters.Clear;
LbPosition.Caption := string.Empty;
LbTimer.Caption := string.Empty;
end;
procedure TFrmGame.SetStatus(NewStatus: TGameStatus);
procedure SetStatusLabel(const LangIdentSufix: string; Color: TColor);
begin
BoxStatus.Caption := Lang.Get('GAME_STATUS_'+LangIdentSufix);
BoxStatus.Color := Color;
end;
begin
Status := NewStatus;
BtnStartGame.Visible := (Status = gsPreparing) and pubModeServer;
BtnRules.Visible := (Status = gsPreparing) and pubModeServer;
BtnDone.Visible := (Status = gsMyTurn);
BtnAgree.Visible := (Status = gsAgreement);
BtnDisagree.Visible := (Status = gsAgreement);
BtnContestAccept.Visible := (Status = gsContest);
BtnContestReject.Visible := (Status = gsContest);
BtnRestart.Visible := (Status = gsGameOver) and pubModeServer;
FrmMain.BtnRestart.Visible := pubModeServer;
case Status of
gsUnknown:
begin
BoxStatus.Caption := string.Empty;
BoxStatus.Color := clBtnFace;
end;
gsPreparing: SetStatusLabel('PREPARING', clPurple);
gsPlaying: SetStatusLabel('PLAYING', clBlack);
gsMyTurn: SetStatusLabel('MYTURN', clGreen);
gsWaitValid: SetStatusLabel('WAITVALID', clWebBrown);
gsAgreement: SetStatusLabel('AGREEMENT', clBlue);
gsContest: SetStatusLabel('CONTEST', $00656225);
gsPaused: SetStatusLabel('PAUSED', $005B5B5B);
gsGameOver: SetStatusLabel('GAMEOVER', $002C075C);
else raise Exception.Create('Internal: Unsupported status');
end;
end;
procedure TFrmGame.MatrixReceived(const A: string);
begin
PB.UpdateData(A);
end;
procedure TFrmGame.ChatLog(const Player, Text: string; Other: Boolean);
var
Color: TColor;
begin
if Other then Color := clLime else Color := $000080FF;
RichEditIncLogLine(EdChatLog, Player+': ', Text, clGray, Color);
end;
procedure TFrmGame.EdChatMsgKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then
begin
Key := #0;
EdChatMsg.Text := Trim(EdChatMsg.Text);
if EdChatMsg.Text = string.Empty then Exit;
if not (LPlayers.Count>1) then
MsgRaise(Lang.Get('GAME_MSG_CHAT_WITH_NOBODY'));
ChatLog(pubPlayerName, EdChatMsg.Text, False);
DMClient.C.Send('M', EdChatMsg.Text);
EdChatMsg.Clear;
end;
end;
procedure TFrmGame.LPlayersDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure TextRight(X, Y: Integer; const A: string);
var
W: Integer;
begin
W := LPlayers.Canvas.TextWidth(A);
LPlayers.Canvas.TextOut(X-W, Y, A);
end;
var D: TMsgArray;
begin
LPlayers.Canvas.FillRect(Rect);
D := DataToArray(LPlayers.Items[Index]);
LPlayers.Canvas.TextOut(LbHeaderPlayer.Left, Rect.Top+2, D[0]); //player name
TextRight(LbHeaderScore.Left+LbHeaderScore.Width, Rect.Top+2, D[1]); //score
if D[2] then
IL.Draw(LPlayers.Canvas, 3, Rect.Top+1, 0); //this player turn
if D[3] then
IL.Draw(LPlayers.Canvas, 3, Rect.Top+1, 1); //agree
if D[4] then
IL.Draw(LPlayers.Canvas, LbHeaderScore.Left-25, Rect.Top+1, 2); //disconnected
end;
procedure TFrmGame.LLettersDrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
LLetters.Canvas.FillRect(Rect);
LLetters.Canvas.TextOut(Rect.Left+8, Rect.Top, LLetters.Items[Index]);
end;
procedure TFrmGame.BtnRestartClick(Sender: TObject);
begin
SetStatus(gsUnknown); //transition status to gsPreparing
DMServer.RestartGame;
end;
procedure TFrmGame.BtnStartGameClick(Sender: TObject);
begin
if LPlayers.Count<2 then
MsgRaise(Lang.Get('GAME_MSG_START_WITH_NOBODY'));
SetStatus(gsUnknown); //transition status to gsPlaying
DMServer.StartGame;
end;
procedure TFrmGame.BtnRulesClick(Sender: TObject);
begin
ShowGameRules;
end;
procedure TFrmGame.BtnDoneClick(Sender: TObject);
begin
if PB.Data.ContainsAnyInvalid then
MsgRaise(Lang.Get('GAME_MSG_INVALID_SEQUENCE'));
SetStatus(gsPlaying);
DMClient.C.Send('!');
end;
procedure TFrmGame.BtnAgreeClick(Sender: TObject);
begin
DMClient.SendAgreement(True);
Log(Lang.Get('LOG_AGREE_SENT'));
end;
procedure TFrmGame.BtnDisagreeClick(Sender: TObject);
begin
DMClient.SendAgreement(False);
Log(Lang.Get('LOG_DISAGREE_SENT'));
end;
procedure TFrmGame.BtnContestAcceptClick(Sender: TObject);
begin
SetStatus(gsPlaying);
DMClient.SendContest(True);
Log(Lang.Get('LOG_CONTEST_ACCEPT_SENT'));
end;
procedure TFrmGame.BtnContestRejectClick(Sender: TObject);
begin
SetStatus(gsPlaying);
DMClient.SendContest(False);
Log(Lang.Get('LOG_CONTEST_REJECT_SENT'));
end;
procedure TFrmGame.GameStartedReceived;
begin
SetStatus(gsPlaying);
Log(Lang.Get('LOG_GAME_STARTED'));
end;
procedure TFrmGame.InitMyTurn;
begin
SetStatus(gsMyTurn);
Log(Lang.Get('LOG_YOUR_TURN'));
DoSound('BELL');
end;
procedure TFrmGame.MyTurnTimeoutReceived;
begin
SetStatus(gsPlaying);
Log(Lang.Get('LOG_TURN_TIMEOUT'));
DoSound('TIMEOUT');
end;
procedure TFrmGame.LettersExchangedReceived;
begin
Log(Lang.Get('LOG_LETTERS_EXCHANGED'));
end;
procedure TFrmGame.ReceivedAutoRejectedByInvalidLetters;
begin
Log(Lang.Get('LOG_AUTO_REJECTED_BY_INVALID_LETTERS'));
end;
procedure TFrmGame.WaitValidationReceived;
begin
SetStatus(gsWaitValid);
Log(Lang.Get('LOG_WAIT_VALIDATION'));
end;
procedure TFrmGame.ValidationAcceptedReceived;
begin
SetStatus(gsPlaying);
Log(Lang.Get('LOG_WORDS_ACCEPTED'));
DoSound('DONE');
end;
procedure TFrmGame.ValidationRejectedReceived;
begin
SetStatus(gsMyTurn);
Log(Lang.Get('LOG_DISAGREE_RECEIVED'));
DoSound('REJECT');
end;
procedure TFrmGame.AgreementRequestReceived;
begin
SetStatus(gsAgreement);
Log(Lang.Get('LOG_AGREEMENT_PERIOD_START'));
DoSound('AGREEMENT');
end;
procedure TFrmGame.AgreementFinishReceived(const A: string);
begin
SetStatus(gsPlaying);
if DataToArray(A)[0] then
Log(Lang.Get('LOG_AGREEMENT_PERIOD_FINISH_ACCEPTED'))
else
Log(Lang.Get('LOG_AGREEMENT_PERIOD_FINISH_REJECTED'));
DoSound('AGREEMENT_END');
end;
procedure TFrmGame.OpenContestPeriodReceived;
begin
SetStatus(gsContest);
Log(Lang.Get('LOG_CONTEST_PERIOD_START'));
DoSound('CONTEST');
end;
procedure TFrmGame.ReceivedContestResponse(const A: string);
begin
if DataToArray(A)[0]{Accept} then
Log(Lang.Get('LOG_RECEIVED_CONTEST_RESPONSE_ACCEPT'))
else
Log(Lang.Get('LOG_RECEIVED_CONTEST_RESPONSE_REJECT'));
end;
procedure TFrmGame.ReceivedPausedByDrop;
begin
SetStatus(gsPaused);
Log(Lang.Get('LOG_PAUSE_BY_DROP'));
end;
procedure TFrmGame.ReceivedDropContinue;
begin
SetStatus(gsPlaying);
Log(Lang.Get('LOG_DROP_CONTINUE'))
end;
procedure TFrmGame.GameOverReceived;
begin
SetStatus(gsGameOver);
Log(Lang.Get('LOG_GAME_OVER'));
end;
procedure TFrmGame.ReceivedPreparingNewGame;
begin
SetStatus(gsPreparing);
Log(Lang.Get('LOG_RESTART_GAME'));
end;
procedure TFrmGame.ReceivedTimerStart(const A: string);
begin
TimerSeconds := A.ToInteger;
TimerTimer(nil);
Timer.Enabled := True;
end;
procedure TFrmGame.ReceivedTimerStop;
begin
Timer.Enabled := False;
LbTimer.Caption := string.Empty;
end;
procedure TFrmGame.TimerTimer(Sender: TObject);
begin
LbTimer.Caption := Format(Lang.Get('GAME_TIMER'), [TimerSeconds]);
if TimerSeconds=0 then
begin
Timer.Enabled := False;
Exit;
end;
Dec(TimerSeconds);
end;
end.