-
Notifications
You must be signed in to change notification settings - Fork 5
/
UDMClient.pas
233 lines (194 loc) · 6.17 KB
/
UDMClient.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
unit UDMClient;
interface
uses System.Classes, DzSocket;
type
TDMClient = class(TDataModule)
C: TDzTCPClient;
procedure DataModuleCreate(Sender: TObject);
procedure CLoginRequest(Sender: TObject; Socket: TDzSocket;
var Data: string);
procedure CLoginResponse(Sender: TObject; Socket: TDzSocket;
Accepted: Boolean; const Data: string);
procedure CConnect(Sender: TObject; Socket: TDzSocket);
procedure CConnectionLost(Sender: TObject; Socket: TDzSocket);
procedure CDisconnect(Sender: TObject; Socket: TDzSocket;
const WasConnected: Boolean);
procedure CError(Sender: TObject; Socket: TDzSocket;
const Event: TErrorEvent; const ErrorCode: Integer;
const ErrorMsg: string);
procedure CRead(Sender: TObject; Socket: TDzSocket; const Cmd: Char;
const A: string);
private
procedure MessageReceived(const A: string);
procedure PlayersListReceived(const A: string);
procedure LettersReceived(const A: string);
procedure RulesReceived(const A: string; ToOne: Boolean);
public
procedure SendLetter(X, Y: Integer; const Letter: Char);
procedure SendAgreement(Agree: Boolean);
procedure SendContest(Accept: Boolean);
end;
var
DMClient: TDMClient;
implementation
{%CLASSGROUP 'Vcl.Controls.TControl'}
{$R *.dfm}
uses UVars, ULanguage, UDMServer,
UFrmMain, UFrmStart, UFrmGame, UFrmLog,
System.SysUtils, System.StrUtils;
procedure TDMClient.DataModuleCreate(Sender: TObject);
begin
C.Port := INT_TCP_PORT;
end;
procedure TDMClient.CConnect(Sender: TObject; Socket: TDzSocket);
begin
Log(Lang.Get('LOG_CONNECTED'));
end;
procedure TDMClient.CDisconnect(Sender: TObject; Socket: TDzSocket;
const WasConnected: Boolean);
begin
Log(Lang.Get('LOG_DISCONNECTED'));
if pubModeServer then DMServer.S.Close; //turn off server
FrmGame.Timer.Enabled := False;
FrmGame.Hide;
FrmMain.BoxConInfo.Visible := False;
FrmStart.Show;
FrmStart.EnableControls(True);
end;
procedure TDMClient.CConnectionLost(Sender: TObject; Socket: TDzSocket);
begin
Log(Lang.Get('LOG_CONNECTION_LOST'));
end;
procedure TDMClient.CError(Sender: TObject; Socket: TDzSocket;
const Event: TErrorEvent; const ErrorCode: Integer; const ErrorMsg: string);
begin
Log(Format(Lang.Get('LOG_SOCKET_ERROR'), [ErrorMsg]));
end;
procedure TDMClient.CLoginRequest(Sender: TObject; Socket: TDzSocket;
var Data: string);
begin
Data := ArrayToData([STR_VERSION, pubPlayerName, pubPlayerHash, pubPassword]);
end;
procedure TDMClient.CLoginResponse(Sender: TObject; Socket: TDzSocket;
Accepted: Boolean; const Data: string);
var
D: TMsgArray;
begin
if Accepted then
begin
Log(Lang.Get('LOG_LOGIN_ACCEPTED'));
FrmStart.Hide;
D := DataToArray(Data);
pubPlayerName := D[0];
FrmStart.EdHash.Text := D[1]; //auto-set for use when disconnect
FrmMain.LbMode.Caption := Lang.Get(IfThen(pubModeServer, 'MODE_SERVER', 'MODE_CLIENT'));
FrmMain.LbPlayer.Caption := pubPlayerName;
FrmMain.LbRules.Caption := string.Empty;
FrmMain.BoxConInfo.Visible := True;
FrmGame.Initialize(FrmStart.BoxReconnect.Visible);
FrmGame.Show;
if pubModeServer then
Log(Lang.Get('LOG_PREPARING_GAME_SERVER'))
else
if FrmStart.BoxReconnect.Visible then
Log(Lang.Get('LOG_RECONECTED_GAME_CLIENT'))
else
Log(Lang.Get('LOG_PREPARING_GAME_CLIENT'));
end else
begin
Log(Format(Lang.Get('LOG_LOGIN_REJECTED'), [Lang.Get('CONN_REJECT_'+Data)]));
end;
end;
procedure TDMClient.CRead(Sender: TObject; Socket: TDzSocket; const Cmd: Char;
const A: string);
begin
case Cmd of
'C': Log(Format(Lang.Get('LOG_PLAYER_JOIN'), [A]));
'D': Log(Format(Lang.Get('LOG_PLAYER_LEFT'), [A]));
'U', 'u': RulesReceived(A, Cmd='u');
'M': MessageReceived(A);
'L': PlayersListReceived(A);
'T': LettersReceived(A);
'R': FrmGame.GameStartedReceived;
'X': FrmGame.MatrixReceived(A);
'>': FrmGame.InitMyTurn;
'~': FrmGame.MyTurnTimeoutReceived;
'G': FrmGame.AgreementRequestReceived;
'K': FrmGame.AgreementFinishReceived(A);
'W': FrmGame.WaitValidationReceived;
'F': FrmGame.ValidationAcceptedReceived;
'J': FrmGame.ValidationRejectedReceived;
'O': FrmGame.OpenContestPeriodReceived;
'B': FrmGame.LettersExchangedReceived;
'E': FrmGame.GameOverReceived;
'P': FrmGame.ReceivedPreparingNewGame;
'?': FrmGame.ReceivedPausedByDrop;
'/': FrmGame.ReceivedDropContinue;
':': FrmGame.ReceivedTimerStart(A);
'.': FrmGame.ReceivedTimerStop;
'&': FrmGame.ReceivedAutoRejectedByInvalidLetters;
'Q': FrmGame.ReceivedContestResponse(A);
end;
end;
procedure TDMClient.RulesReceived(const A: string; ToOne: Boolean);
var
D: TMsgArray;
Dictionary: string;
SizeW, SizeH, HandLetters, GoalScore, TurnTimeout, AgreementTimeout: Integer;
begin
D := DataToArray(A);
Dictionary := D[0];
SizeW := D[1];
SizeH := D[2];
HandLetters := D[3];
GoalScore := D[4];
TurnTimeout := D[5];
AgreementTimeout := D[6];
FrmMain.LbRules.Caption :=
Format(Lang.Get('TITLE_RULES_DEFINITION'), [
Dictionary, SizeW, SizeH, HandLetters, GoalScore, TurnTimeout, AgreementTimeout]);
FrmGame.LbPosition.Caption := string.Empty;
FrmGame.PB.SetMatrixSize(SizeH, SizeW);
if not ToOne then
Log(Lang.Get('LOG_RULES_CHANGED'));
//When an user connects, it receive the game rules, so the log should be omitted.
end;
procedure TDMClient.MessageReceived(const A: string);
var
D: TMsgArray;
begin
D := DataToArray(A);
FrmGame.ChatLog(D[0], D[1], True);
DoSound('PLING');
end;
procedure TDMClient.PlayersListReceived(const A: string);
begin
FrmGame.LPlayers.Items.Text := A;
end;
procedure TDMClient.LettersReceived(const A: string);
var
Lst: TStringList;
Letter: Char;
begin
Lst := TStringList.Create;
try
for Letter in A do
Lst.Add(Letter);
FrmGame.LLetters.Items.Assign(Lst);
finally
Lst.Free;
end;
end;
procedure TDMClient.SendLetter(X, Y: Integer; const Letter: Char);
begin
C.Send('#', ArrayToData([X, Y, Letter]));
end;
procedure TDMClient.SendAgreement(Agree: Boolean);
begin
C.Send('H', ArrayToData([Agree]));
end;
procedure TDMClient.SendContest(Accept: Boolean);
begin
C.Send('Y', ArrayToData([Accept]));
end;
end.