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
Line 
1using System.Collections.Generic;
2using AllocsFixes.JSON;
3using AllocsFixes.PersistentData;
4
5namespace AllocsFixes.NetConnections.Servers.Web.API {
6 public class GetPlayersOnline : AbsWebAPI {
7 public override void HandleRequest (RequestContext _context) {
8 JSONArray players = new JSONArray ();
9
10 World w = GameManager.Instance.World;
11 foreach (KeyValuePair<int, EntityPlayer> current in w.Players.dict) {
12 ClientInfo ci = ConnectionManager.Instance.Clients.ForEntityId (current.Key);
13 Player player = PersistentContainer.Instance.Players [ci.InternalId, false];
14
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));
19
20 JSONObject p = new JSONObject ();
21 p.Add ("steamid", new JSONString (ci.PlatformId.CombinedString));
22 p.Add ("entityid", new JSONNumber (ci.entityId));
23 p.Add ("ip", new JSONString (ci.ip));
24 p.Add ("name", new JSONString (current.Value.EntityName));
25 p.Add ("online", new JSONBoolean (true));
26 p.Add ("position", pos);
27
28 p.Add ("level", new JSONNumber (player?.Level ?? -1));
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
36 p.Add ("totalplaytime", new JSONNumber (player?.TotalPlayTime ?? -1));
37 p.Add ("lastonline", new JSONString (player != null ? player.LastOnline.ToString ("s") : string.Empty));
38 p.Add ("ping", new JSONNumber (ci.ping));
39
40 players.Add (p);
41 }
42
43 WebUtils.WriteJson (_context.Response, players);
44 }
45 }
46}
Note: See TracBrowser for help on using the repository browser.