-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerPollerJson6720.dpr
105 lines (89 loc) · 3.31 KB
/
ServerPollerJson6720.dpr
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
program ServerPollerJson6720;
{$APPTYPE CONSOLE}
uses
SysUtils,
StrUtils,
Vcl.Forms,
windows,
Classes,
Math,
KM_Defaults,
KM_Settings,
KM_ServerQuery,
KM_NetworkTypes,
KM_utils,
DBXJSON;
const
GameStateTextIDs: array [TMPGameState] of string = ('mgsNone', 'mgsLobby', 'mgsLoading', 'mgsGame');
PlayerTypeTextIDs: array [TNetPlayerType] of string = ('nptHuman', 'nptComputer', 'nptClosed');
MasterServerAddress = 'http://kam.hodgman.id.au/';
TIMEOUT=6000;
var
fServerQuery: TKMServerQuery;
I, J, TIME, PlayerCount, RoomCount: Integer;
R: TKMRoomInfo;
S: TKMServerInfo;
DisplayName, ShortName: string;
JSONObject, RoomObject, PlayerObject : TJSONObject;
RoomsArray,PlayersArray: TJSONArray;
begin
fServerQuery := TKMServerQuery.Create(MasterServerAddress);
fServerQuery.RefreshList;
TIME := GetTickCount;
while (GetTickCount - TIME) < TIMEOUT do
begin
Application.ProcessMessages;
CheckSynchronize;
fserverquery.UpdateStateIdle;
//writeln('start ' + inttostr(GetTickCount - TIME));
//writeln('Rcount='+inttostr(fServerQuery.Rooms.Count));
// writeln('ActiveCount='+inttostr(fServerQuery.Servers.Count));
//writeln('TIME='+inttostr(TIME));
sleep(50);
if fServerQuery.GetQueriesIsDone then break;
end;
JSONObject := TJSONObject.Create;
RoomsArray := TJSONArray.Create();
RoomCount :=0;
for I := 0 to fServerQuery.Rooms.Count - 1 do
begin
R := fServerQuery.Rooms[I];
S := fServerQuery.Servers[R.ServerIndex];
ShortName := StripColor(S.Name + ' #' + IntToStr(R.RoomID + 1));
if R.GameInfo.PlayerCount=0 then continue;
inc(RoomCount);
RoomObject:=TJSONObject.Create;
RoomObject.AddPair('ServerName',ShortName);
RoomObject.AddPair('Description',R.GameInfo.Description);
RoomObject.AddPair('Password', boolToStr(R.GameInfo.PasswordLocked,true));
RoomObject.AddPair('Map',R.GameInfo.Map);
RoomObject.AddPair('GameTime',TimeToStr(R.GameInfo.GameTime));
RoomObject.AddPair('GameState',GameStateTextIDs[R.GameInfo.GameState]);
//RoomObject.AddPair('PlayerCount',TJSONNumber.Create(R.GameInfo.PlayerCount));
PlayersArray := TJSONArray.Create();
PlayerCount := 0;
for J := 1 to 10 do
begin
if (R.GameInfo.Players[J].Connected) and (PlayerTypeTextIDs[R.GameInfo.Players[J].PlayerType] <> 'nptClosed') then
begin
inc(PlayerCount);
PlayerObject := TJSONObject.Create;
if (PlayerTypeTextIDs[R.GameInfo.Players[J].PlayerType] = 'nptComputer') then
PlayerObject.AddPair('Name','Computer')
else
PlayerObject.AddPair('Name',R.GameInfo.Players[J].Name);
PlayerObject.AddPair('Color',RightStr(inttohex(R.GameInfo.Players[J].Color,8),6));
PlayerObject.AddPair('PlayerType',PlayerTypeTextIDs[R.GameInfo.Players[J].PlayerType]);
PlayersArray.AddElement(PlayerObject);
end;
end;
RoomObject.AddPair('PlayerCount',TJSONNumber.Create(PlayerCount));
RoomObject.AddPair('Players',PlayersArray);
RoomsArray.AddElement(RoomObject);
end;
JSONObject.AddPair('RoomsCount',TJSONNumber.Create(RoomCount));
JSONObject.AddPair('Rooms',RoomsArray);
writeln(JSONObject.ToString);
JSONObject.Destroy;
fserverquery.Destroy;
end.