source: binary-improvements2/MapRendering/Web/API/GetPlayersOnline.cs@ 387

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

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

File size: 1.9 KB
RevLine 
[325]1using System.Collections.Generic;
[230]2using AllocsFixes.JSON;
[233]3using AllocsFixes.PersistentData;
[230]4
[325]5namespace AllocsFixes.NetConnections.Servers.Web.API {
[387]6 public class GetPlayersOnline : AbsWebAPI {
7 public override void HandleRequest (RequestContext _context) {
[325]8 JSONArray players = new JSONArray ();
[230]9
10 World w = GameManager.Instance.World;
11 foreach (KeyValuePair<int, EntityPlayer> current in w.Players.dict) {
[324]12 ClientInfo ci = ConnectionManager.Instance.Clients.ForEntityId (current.Key);
[371]13 Player player = PersistentContainer.Instance.Players [ci.InternalId, false];
[230]14
[325]15 JSONObject pos = new JSONObject ();
16 pos.Add ("x", new JSONNumber ((int) current.Value.GetPosition ().x));
17 pos.Add ("y", new JSONNumber ((int) current.Value.GetPosition ().y));
18 pos.Add ("z", new JSONNumber ((int) current.Value.GetPosition ().z));
[230]19
[325]20 JSONObject p = new JSONObject ();
[369]21 p.Add ("steamid", new JSONString (ci.PlatformId.CombinedString));
[256]22 p.Add ("entityid", new JSONNumber (ci.entityId));
[309]23 p.Add ("ip", new JSONString (ci.ip));
[233]24 p.Add ("name", new JSONString (current.Value.EntityName));
25 p.Add ("online", new JSONBoolean (true));
26 p.Add ("position", pos);
[230]27
[369]28 p.Add ("level", new JSONNumber (player?.Level ?? -1));
[233]29 p.Add ("health", new JSONNumber (current.Value.Health));
30 p.Add ("stamina", new JSONNumber (current.Value.Stamina));
31 p.Add ("zombiekills", new JSONNumber (current.Value.KilledZombies));
32 p.Add ("playerkills", new JSONNumber (current.Value.KilledPlayers));
33 p.Add ("playerdeaths", new JSONNumber (current.Value.Died));
34 p.Add ("score", new JSONNumber (current.Value.Score));
35
[369]36 p.Add ("totalplaytime", new JSONNumber (player?.TotalPlayTime ?? -1));
[268]37 p.Add ("lastonline", new JSONString (player != null ? player.LastOnline.ToString ("s") : string.Empty));
[326]38 p.Add ("ping", new JSONNumber (ci.ping));
[268]39
[325]40 players.Add (p);
[230]41 }
42
[387]43 WebUtils.WriteJson (_context.Response, players);
[230]44 }
45 }
[325]46}
Note: See TracBrowser for help on using the repository browser.