source: binary-improvements2/MapRendering/Web/API/GetPlayersLocation.cs@ 389

Last change on this file since 389 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: 2.0 KB
RevLine 
[332]1using System.Collections.Generic;
[253]2using AllocsFixes.JSON;
3using AllocsFixes.PersistentData;
4
[325]5namespace AllocsFixes.NetConnections.Servers.Web.API {
[387]6 public class GetPlayersLocation : AbsWebAPI {
7 public override void HandleRequest (RequestContext _context) {
[325]8 AdminTools admTools = GameManager.Instance.adminTools;
[387]9 PlatformUserIdentifierAbs userId = _context.Connection?.UserId;
[253]10
[315]11 bool listOffline = false;
[387]12 if (_context.Request.QueryString ["offline"] != null) {
13 bool.TryParse (_context.Request.QueryString ["offline"], out listOffline);
[315]14 }
15
[387]16 bool bViewAll = WebConnection.CanViewAllPlayers (_context.PermissionLevel);
[253]17
[325]18 JSONArray playersJsResult = new JSONArray ();
[253]19
20 Players playersList = PersistentContainer.Instance.Players;
21
[369]22 foreach (KeyValuePair<PlatformUserIdentifierAbs, Player> kvp in playersList.Dict) {
[325]23 if (admTools != null) {
[369]24 if (admTools.IsBanned (kvp.Key, out _, out _)) {
[325]25 continue;
26 }
27 }
[253]28
[332]29 Player p = kvp.Value;
[253]30
[315]31 if (listOffline || p.IsOnline) {
[369]32 if (bViewAll || p.PlatformId.Equals (userId)) {
[325]33 JSONObject pos = new JSONObject ();
34 pos.Add ("x", new JSONNumber (p.LastPosition.x));
35 pos.Add ("y", new JSONNumber (p.LastPosition.y));
36 pos.Add ("z", new JSONNumber (p.LastPosition.z));
[253]37
[325]38 JSONObject pJson = new JSONObject ();
[369]39 pJson.Add ("steamid", new JSONString (kvp.Key.CombinedString));
[253]40
[325]41 // pJson.Add("entityid", new JSONNumber (p.EntityID));
42 // pJson.Add("ip", new JSONString (p.IP));
43 pJson.Add ("name", new JSONString (p.Name));
44 pJson.Add ("online", new JSONBoolean (p.IsOnline));
45 pJson.Add ("position", pos);
[268]46
[325]47 // pJson.Add ("totalplaytime", new JSONNumber (p.TotalPlayTime));
48 // pJson.Add ("lastonline", new JSONString (p.LastOnline.ToString ("s")));
49 // pJson.Add ("ping", new JSONNumber (p.IsOnline ? p.ClientInfo.ping : -1));
50
[315]51 playersJsResult.Add (pJson);
[325]52 }
[315]53 }
[325]54 }
[253]55
[387]56 WebUtils.WriteJson (_context.Response, playersJsResult);
[253]57 }
58 }
[325]59}
Note: See TracBrowser for help on using the repository browser.