source: binary-improvements2/WebServer/src/WebAPI/GetPlayersOnline.cs@ 391

Last change on this file since 391 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 1.9 KB
RevLine 
[391]1using System.Collections.Generic;
2using AllocsFixes.JSON;
3using AllocsFixes.PersistentData;
4using JetBrains.Annotations;
5
6namespace Webserver.WebAPI {
7 [UsedImplicitly]
8 public class GetPlayersOnline : AbsWebAPI {
9 public override void HandleRequest (RequestContext _context) {
10 JsonArray players = new JsonArray ();
11
12 World w = GameManager.Instance.World;
13 foreach ((int entityId, EntityPlayer entityPlayer) in w.Players.dict) {
14 ClientInfo ci = ConnectionManager.Instance.Clients.ForEntityId (entityId);
15 Player player = PersistentContainer.Instance.Players [ci.InternalId, false];
16
17 JsonObject pos = new JsonObject ();
18 pos.Add ("x", new JsonNumber ((int) entityPlayer.GetPosition ().x));
19 pos.Add ("y", new JsonNumber ((int) entityPlayer.GetPosition ().y));
20 pos.Add ("z", new JsonNumber ((int) entityPlayer.GetPosition ().z));
21
22 JsonObject p = new JsonObject ();
23 p.Add ("steamid", new JsonString (ci.PlatformId.CombinedString));
24 p.Add ("entityid", new JsonNumber (ci.entityId));
25 p.Add ("ip", new JsonString (ci.ip));
26 p.Add ("name", new JsonString (entityPlayer.EntityName));
27 p.Add ("online", new JsonBoolean (true));
28 p.Add ("position", pos);
29
30 p.Add ("level", new JsonNumber (player?.Level ?? -1));
31 p.Add ("health", new JsonNumber (entityPlayer.Health));
32 p.Add ("stamina", new JsonNumber (entityPlayer.Stamina));
33 p.Add ("zombiekills", new JsonNumber (entityPlayer.KilledZombies));
34 p.Add ("playerkills", new JsonNumber (entityPlayer.KilledPlayers));
35 p.Add ("playerdeaths", new JsonNumber (entityPlayer.Died));
36 p.Add ("score", new JsonNumber (entityPlayer.Score));
37
38 p.Add ("totalplaytime", new JsonNumber (player?.TotalPlayTime ?? -1));
39 p.Add ("lastonline", new JsonString (player != null ? player.LastOnline.ToString ("s") : string.Empty));
40 p.Add ("ping", new JsonNumber (ci.ping));
41
42 players.Add (p);
43 }
44
45 WebUtils.WriteJson (_context.Response, players);
46 }
47 }
48}
Note: See TracBrowser for help on using the repository browser.