-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.pas
188 lines (168 loc) · 5.03 KB
/
client.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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit client;
interface
uses
corenetwork, corewebsocket, world, player;
type
TCuddlyWorldClient = class(TWebSocket)
protected
FPlayer: TPlayer;
FWorld: TWorld;
procedure HandleMessage(Message: UTF8String); override;
procedure HandleAvatarMessage(Message: UTF8String);
procedure HandleForceDisconnect();
procedure TryLogin(Message: UTF8String);
procedure TryCommand(Message: UTF8String);
public
constructor Create(AListener: TListenerSocket; AWorld: TWorld);
destructor Destroy(); override;
end;
TCuddlyWorldServer = class(TNetworkServer)
protected
FOrigin: UTF8String;
FHostName: UTF8String;
FResource: UTF8String;
FWorld: TWorld;
function CreateNetworkSocket(AListenerSocket: TListenerSocket): TNetworkSocket; override;
public
constructor Create(APort: Word; AWorld: TWorld);
end;
{$IFOPT C+}
procedure TestClient();
{$ENDIF}
implementation
uses
sysutils;
const
ResponseFrameHeader = #$01; // first byte of sequences of frames that represent responses; first frame is echo.
EndOfResponse = #$02; // contents of trailing frame after response
constructor TCuddlyWorldClient.Create(AListener: TListenerSocket; AWorld: TWorld);
begin
inherited Create(AListener);
FWorld := AWorld;
end;
destructor TCuddlyWorldClient.Destroy();
begin
if (Assigned(FPlayer)) then
FPlayer.Abandon();
inherited;
end;
procedure TCuddlyWorldClient.HandleMessage(Message: UTF8String);
begin
if (not Assigned(FPlayer)) then
TryLogin(Message)
else
TryCommand(Message);
end;
procedure TCuddlyWorldClient.TryLogin(Message: UTF8String);
var
PotentialPlayer: TPlayer;
Index: Cardinal;
Username, Password: UTF8String;
begin
Index := Pos(' ', Message); {BOGUS Warning: Type size mismatch, possible loss of data / range check error}
if (Index = 0) then
begin
WriteFrame('First frame must be username and password, separated by a space character.');
Disconnect();
Exit;
end;
Username := Copy(Message, 1, Index-1);
if (Username = '') then
begin
WriteFrame('Username cannot be blank.');
Disconnect();
Exit;
end;
Password := Copy(Message, Index+1, Length(Message)-Index);
try
PotentialPlayer := FWorld.GetPlayer(Username) as TPlayer;
if (Assigned(PotentialPlayer)) then
begin
if (PotentialPlayer.GetPassword() = Password) then
begin
FPlayer := PotentialPlayer;
FPlayer.Adopt(@Self.HandleAvatarMessage, @Self.HandleForceDisconnect);
WriteFrame('Welcome back to Cuddly World, ' + Username + '!');
WriteFrame('');
FPlayer.DoLook();
FPlayer.DoInventory();
WriteFrame('');
end
else
begin
WriteFrame('Wrong password.');
Disconnect();
Exit;
end;
end
else
begin
FPlayer := TPlayer.Create(Username, Password, pSingularThey);
FPlayer.Adopt(@Self.HandleAvatarMessage, @Self.HandleForceDisconnect);
FWorld.AddPlayer(FPlayer); { this puts it into the world }
FPlayer.AnnounceAppearance();
WriteFrame('Welcome to Cuddly World, ' + Username + '!');
WriteFrame('');
FPlayer.DoLook();
WriteFrame('');
end;
except
on E: EExternal do raise;
on E: Exception do
begin // for debugging
WriteFrame('You feel a disturbance in the force that whispers "' + E.Message + '".');
WriteFrame('');
Writeln('');
Writeln('While connecting user "' + Username + '", the following exception was raised:');
Writeln(E.Message);
DumpExceptionBackTrace(Output);
end;
end;
end;
procedure TCuddlyWorldClient.TryCommand(Message: UTF8String);
begin
WriteFrame(ResponseFrameHeader + '> ' + Message);
try
FWorld.SetDirty();
FWorld.Perform(Message, FPlayer);
except
on EExternal do raise;
on E: Exception do
begin // for debugging
WriteFrame('You feel a disturbance in the force that whispers "' + E.Message + '".');
Writeln('');
Writeln('While processing "' + Message + '", the following exception was raised:');
Writeln(E.Message);
DumpExceptionBackTrace(Output);
end;
end;
WriteFrame(EndOfResponse);
end;
procedure TCuddlyWorldClient.HandleAvatarMessage(Message: UTF8String);
begin
WriteFrame(Message);
end;
procedure TCuddlyWorldClient.HandleForceDisconnect();
begin
WriteFrame('Switching to new connection...');
FPlayer.Abandon();
FPlayer := nil;
Disconnect();
end;
constructor TCuddlyWorldServer.Create(APort: Word; AWorld: TWorld);
begin
inherited Create(APort);
FWorld := AWorld;
end;
function TCuddlyWorldServer.CreateNetworkSocket(AListenerSocket: TListenerSocket): TNetworkSocket;
begin
Result := TCuddlyWorldClient.Create(AListenerSocket, FWorld);
end;
{$IFOPT C+}
procedure TestClient();
begin
end;
{$ENDIF}
end.