-
Notifications
You must be signed in to change notification settings - Fork 1
/
nfkPlanet.pas
427 lines (356 loc) · 16.3 KB
/
nfkPlanet.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
{###############################################################################
#
# Original NFKPlanet code
#
###############################################################################}
unit nfkPlanet;
interface //--------------------------------------------------------------------
function NFKPLANET_AutoUpdate() :boolean;
procedure NFKPLANET_CheckProxies;
procedure NFKPLANET_IWantJoinProxy(IP : string);
procedure NFKPLANET_KeepAlive;
procedure NFKPLANET_PingAllServers;
procedure NFKPLANET_PingLastServer;
procedure NFKPLANET_proxyd;
procedure NFKPLANET_Register(HostName_, MapName_ : string; Players_, MaxPlayers_, GameType_ : byte );
procedure NFKPLANET_ShowNewsDeliveryScreen;
procedure NFKPLANET_SortList(ID:Byte);
procedure NFKPLANET_UpdateCurrentUsers(Players_ : byte);
procedure NFKPLANET_UpdateGameType(GameType_ : byte);
procedure NFKPLANET_UpdateHostName(HostName_ : string);
procedure NFKPLANET_UpdateMapName(MapName_ : string);
procedure NFKPLANET_UpdateMaxUsers(MaxPlayers_ : byte);
procedure NFKPLANET_UpdatePlayersCount;
procedure NFKPLANET_UpdateServerList;
procedure NFKPLANET_UpdateServerPing(IP: ShortString);
implementation //---------------------------------------------------------------
uses unit1, demounit, sysutils, windows, classes, bnet, winsock;
procedure NFKPLANET_PingAllServers;
var i : word;
PingPacket : TMP_Warmupis2;
begin
if MP_Sessions.Count = 0 then exit;
if not mainform.Lobby.active then exit;
// if BNET1.Active = false then BNET1.Active := true;
for i := 0 to MP_Sessions.Count-1 do begin
addmessage('"'+strpar_np (MP_Sessions[i],6) + '"');
if strpar_np (MP_Sessions[i],6) = '' then begin
PingPacket.DATA := MMP_LOBBY_PING;
Mainform.BNETSendData2IP_ (strpar_np(MP_Sessions[i],5), BNET_GAMEPORT, PingPacket, sizeof(TMP_Warmupis2),0);
MP_Sessions[i] := MP_Sessions[i] + #0 + inttostr(gettickcount);
end;
end;
end;
procedure NFKPLANET_PingLastServer;
var i : word;
PingPacket : TMP_Warmupis2;
begin
if MP_Sessions.Count = 0 then exit;
if (not mainform.Lobby.active) and (MP_STEP <> 4) then exit;
// if BNET1.Active = false then BNET1.Active := true;
if strpar_np (MP_Sessions[i],5) = '' then exit;
i := MP_Sessions.Count-1;
// addmessage('"'+strpar_np (MP_Sessions[i],6) + '"');
// Server is FULL. we don't ping this.
if strpar_np (MP_Sessions[i],3) = strpar_np (MP_Sessions[i],4) then begin
MP_Sessions[i] := MP_Sessions[i] +#0 + '0' + #0 + 'XXX';
exit;
end;
if strpar_np (MP_Sessions[i],6) = '' then begin
PingPacket.DATA := MMP_LOBBY_PING;
Mainform.BNETSendData2IP_ (strpar_np(MP_Sessions[i],5),BNET_GAMEPORT, PingPacket, sizeof(TMP_Warmupis2),0);
if BNET_GAMEPORT<>BNET_SERVERPORT then
Mainform.BNETSendData2IP_ (strpar_np(MP_Sessions[i],5),BNET_SERVERPORT, PingPacket, sizeof(TMP_Warmupis2),0);
MP_Sessions[i] := MP_Sessions[i] + #0 + inttostr(gettickcount);
end;
end;
procedure NFKPLANET_UpdateServerPing(IP: ShortString);
var i : word;
prevTICK : longword;
ping : word;
begin
// addmessage('^2NFKPLANET_UpdateServerPing');
if MP_Sessions.Count = 0 then exit;
if (not mainform.Lobby.active) and (MP_STEP<>4) then exit;
for i := 0 to MP_Sessions.Count-1 do
if strpar_np(MP_Sessions[i],5)=IP then begin
try prevTICK := strtoint ( strpar_np(MP_Sessions[i],6) );
except exit; end;
ping := (gettickcount-prevTICK) div 2;
if strpar_np(MP_Sessions[i],7) <> '' then
ping := (ping + strtoint ( strpar_np(MP_Sessions[i],7)));
if ping > 999 then ping := 999;
MP_Sessions[i] := MP_Sessions[i] +#0 + inttostr( PING );
exit;
end;
end;
procedure NFKPLANET_UpdateMapName(MapName_ : string);
var Command : TNFKPLANET_CMD;
begin
if not mainform.Lobby.active then exit;
Command._cmd := 'm'; // update map name
FillCharEx(Command._data, MapName_);
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
end;
procedure NFKPLANET_UpdateHostName(HostName_ : string);
var Command : TNFKPLANET_CMD;
begin
if not mainform.Lobby.active then exit;
Command._cmd := 'N'; // update map name
FillCharEx(Command._data, HostName_);
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
end;
procedure NFKPLANET_UpdateCurrentUsers(Players_ : byte);
var Command : TNFKPLANET_CMD;
begin
if not mainform.Lobby.active then exit;
Command._cmd := 'C'; // update current users
FillCharEx(Command._data, inttostr(Players_) );
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
end;
procedure NFKPLANET_UpdateMaxUsers(MaxPlayers_ : byte);
var Command : TNFKPLANET_CMD;
MsgSize:word;
msg:TMP_Svcommand_ex;
begin
if ismultip=1 then begin
MsgSize := SizeOf(TMP_Svcommand_ex);
msg.data := MMP_SV_COMMANDEX;
msg.maxplayers := OPT_SV_MAXPLAYERS;
msg.net_predict := OPT_NETPREDICT;
msg.reserved1 := 0;
msg.powerup := OPT_SV_POWERUP;
mainform.BNETSendData2All(Msg,MsgSize,ttGuaranteed);
end;
if not mainform.Lobby.active then exit;
Command._cmd := 'M'; // update maxuzerz
FillCharEx(Command._data, inttostr(MaxPlayers_));
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
end;
procedure NFKPLANET_UpdateGameType(GameType_ : byte);
var Command : TNFKPLANET_CMD;
begin
if not mainform.Lobby.active then exit;
Command._cmd := 'P'; // update gametype
FillCharEx(Command._data, inttostr(GameType_));
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
end;
procedure NFKPLANET_Register(HostName_, MapName_ : string; Players_, MaxPlayers_, GameType_ : byte );
var Command : TNFKPLANET_CMD;
begin
if not mainform.Lobby.active then begin
// showmessage('NFKPLANET: Can''t register server. Not connected.');
exit;
end;
Command._cmd := 'N'; // update host name
FillCharEx(Command._data, HostName_);
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
Command._cmd := 'm'; // update map name
FillCharEx(Command._data, MapName_);
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
Command._cmd := 'C'; // update current users
FillCharEx(Command._data, inttostr(Players_) );
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
Command._cmd := 'M'; // update maxuzerz
FillCharEx(Command._data, inttostr(MaxPlayers_));
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
Command._cmd := 'P'; // update gametype
FillCharEx(Command._data, inttostr(GameType_));
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
Command._cmd := 'R'; // register server
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
end;
// NFKPLANET_KeepAlive
procedure NFKPLANET_KeepAlive;
var Command : TNFKPLANET_CMD;
begin
if not mainform.Lobby.active then exit;
Command._cmd := '#';
FillChar(Command._data,0,sizeof(Command._data));
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
// addmessage('NFKPLANET: Keep alive');
end;
// -------------------------------------+---------------------------------------
// NFKPLANET_CheckProxies \
// Check If SomeBody want to connect me. /
// -------------------------------------/
procedure NFKPLANET_CheckProxies;
var Command : TNFKPLANET_CMD;
begin
if not mainform.Lobby.active then exit;
if ismultip <> 1 then exit;
if GetNumberOfPlayers >= OPT_SV_MAXPLAYERS then exit;
Command._cmd := 'x';
FillCharEx(Command._data, MainForm.GlobalIP); // connect $ELF.
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
// addmessage('NFKPLANET_CheckProxies');
end;
// -----------------------------------------------------------------------------
procedure NFKPLANET_IWantJoinProxy(IP : string);
var Command : TNFKPLANET_CMD;
i : byte;
zz : longint;
begin
if not mainform.Lobby.active then exit;
Command._cmd := 'X';
for i := 0 to 14 do Command._data [i] := #0;
zz := inet_addr(pchar(IP));
move(zz, Command._data, 4);
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
// addmessage('NFKPLANET: NFKPLANET_IWantJoinProxy: '+IP);
end;
procedure NFKPLANET_proxyd;
var i : byte;
begin
addmessage('NFKPLANET_proxyd');
for i := 0 to random(10) do begin
if random(3)=0 then NFKPLANET_IWantJoinProxy (MainForm.GlobalIP)
else NFKPLANET_IWantJoinProxy(inttostr(255)+'.'+inttostr(255)+'.'+inttostr(255)+'.'+inttostr(255));
end;
end;
// -----------------------------------------------------------------------------
procedure NFKPLANET_UpdatePlayersCount;
var Command : TNFKPLANET_CMD;
begin
if not mainform.Lobby.active then exit;
Command._cmd := 'S';
FillChar(Command._data,0,sizeof(Command._data));
mainform.Lobby.Socket.SendBuf (Command, sizeof(command));
// addmessage('NFKPLANET: Get Players Count');
end;
procedure NFKPLANET_UpdateServerList;
var Command_ : TNFKPLANET_CMD;
begin
if MP_STEP = 4 then begin
LAN_BroadCast;
exit;
end;
if mainform.Lobby.Active= false then begin
addmessage('NFKPLANET: Cannot execute command.. Not connected');
exit;
end;
if BNET_LOBBY_STATUS <> 2 then exit;
BRefreshEnabled := false;
MP_Sessions.Clear; // new.
serverofs := 0;
Command_._cmd := 'G';
FillChar(Command_._data,0,sizeof(Command_._data));
mainform.Lobby.Socket.SendBuf (Command_, sizeof(TNFKPLANET_CMD));
addmessage('^3NFKPLANET: Requesting server list');
end;
procedure NFKPLANET_ShowNewsDeliveryScreen;
var i : byte;
begin
if (DrawWindow(BNET_AU_Caption,'OK',BNET_AU_PosX,BNET_AU_PosY,BNET_AU_WidthX,BNET_AU_WidthY,1)=true) and (mouseLeft) then begin
BNET_AUTOUPDATE := false;
mapcansel := 10;
playsound(SND_Menu2,0,0);
BNET_AU_ShowUpdateInfo := false;
if BNET_AU_CanPlayWithThisVersion = false then begin
ShowCriticalError('Latest version is required for playing at NFK PLANET','Your NFK version ('+VERSION+')'+' is outdated. Please','visit official website for latest update ('+BNET_AU_LIST[1]+').');
applyHcommand('disconnect');
BNET_AUTOUPDATE := true;
exit;
end;
NFKPLANET_UpdateServerList;
exit;
end;
for i := 2 to BNET_AU_LIST.count-1 do
if strpar(BNET_AU_LIST[i],0)='w' then
ParseColorText(strpar_next(BNET_AU_LIST[i],3), strtoint(strpar(BNET_AU_LIST[i],1)), strtoint(strpar(BNET_AU_LIST[i],2)), 1);
mapcansel := 4;
end;
function NFKPLANET_AutoUpdate() :boolean;
var CRC32 : cardinal;
begin
result := false;
BNET_AU_ShowUpdateInfo := false;
if not BNET_AUTOUPDATE then exit;
BNET_AUTOUPDATE := false;
try
MainForm.NMHTTP1.body := ROOTDIR+'\system\au.dat';
MainForm.NMHTTP1.Get(BNET_UPDATEURL);
except result:=false; exit; end;
BNET_AU_LIST.LoadFromFile(ROOTDIR+'\system\au.dat');
BNET_AU_LIST.SaveToFile(ROOTDIR+'\system\au.dat'); // prevent bug... CRC32
if BNET_AU_LIST.count < 2 then exit; // kinda bug?
if strpar(BNET_AU_LIST[0],0) <> 'IDNFK' then exit; // special header..
CRC32 := LOADMAPCRC32(MainForm.NMHTTP1.body);
// New File... Updating.
if BNET_LASTUPDATESRC <> CRC32 then begin
// addmessage('BNET_LASTUPDATESRC='+inttostr(BNET_LASTUPDATESRC)+' CRC32='+inttostr(CRC32));
// BNET_LASTUPDATESRC := CRC32;
BNET_AU_ShowUpdateInfo := true;
BNET_AU_PosX := strtoint(strpar(BNET_AU_LIST[0],1));
BNET_AU_PosY := strtoint(strpar(BNET_AU_LIST[0],2));
BNET_AU_WidthX := strtoint(strpar(BNET_AU_LIST[0],3));
BNET_AU_WidthY := strtoint(strpar(BNET_AU_LIST[0],4));
BNET_AU_Caption := strpar_next(BNET_AU_LIST[0],5);
if VERSION <> BNET_AU_LIST[1] then begin //version rejection
BNET_AU_CanPlayWithThisVersion := false;
BNET_AUTOUPDATE := true;
result := false;
exit;
end else begin
BNET_AU_CanPlayWithThisVersion := true;
// BNET_AUTOUPDATE := false;
end;
result := true;
end else // old news, but still version rejection...
result := false;
if VERSION <> BNET_AU_LIST[1] then begin
ShowCriticalError('Latest version is required for playing at NFK PLANET','Your NFK version is outdated. Please','visit official website for latest update.');
applyHcommand('disconnect');
BNET_AUTOUPDATE := true;
BNET_AU_CanPlayWithThisVersion := false;
exit;
end else BNET_AU_CanPlayWithThisVersion := true;
end;
procedure NFKPLANET_SortList(ID:Byte);
var ts, ts2 : TStringList;
i,find : word;
STR:string;
begin
if mapcansel>0 then exit;
if mouseLeft=true then playsound(snd_menu1,0,0);
mapcansel := 4;
if MP_Sessions.count < 2 then exit;
ts := TStringList.Create;
ts2 := TStringList.Create;
for i := 0 to MP_Sessions.count-1 do
ts.add( strpar_np(MP_Sessions[i],ID));
if ID=3 then // special case in PLAYERS column
ts.CustomSort(CUSTOMSORT_PL) else
if ID=7 then // special case in PING column
ts.CustomSort(CUSTOMSORT_PING) else
ts.sort;
// ts.savetofile(rootdir+'\sorted.txt');
for i := 0 to ts.count-1 do
for find := 0 to MP_Sessions.count - 1 do
if strpar_np(MP_Sessions[find],ID) = ts[i] then begin
str := '';
str := str + strpar_np(MP_Sessions[find],0) +#0;
str := str + strpar_np(MP_Sessions[find],1) +#0;
str := str + strpar_np(MP_Sessions[find],2) +#0;
str := str + strpar_np(MP_Sessions[find],3) +#0;
str := str + strpar_np(MP_Sessions[find],4) +#0;
str := str + strpar_np(MP_Sessions[find],5) +#0;
if strpar_np(MP_Sessions[find],6) <> '' then str := str + strpar_np(MP_Sessions[find],6) +#0;
if strpar_np(MP_Sessions[find],7) <> '' then str := str + strpar_np(MP_Sessions[find],7) +#0;
ts2.add(str);
MP_Sessions.Delete(find);
// MP_Sessions[find] := '';
break;
end;
MP_Sessions.clear;
MP_Sessions.Assign(Ts2);
// MP_Sessions.SaveToFile(ROOTDIR+'\MP_Sessions.txt');
ts.free; ts2.free;
MP_SessionIndex := 0;
serverofs := 0;
end;
// -----------------------------------------------------------------------------
{###############################################################################
################################################################################
###############################################################################}
end.