source: binary-improvements2/MapRendering/Web/API/GetPlayersLocation.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: 2.0 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.JSON;
3using AllocsFixes.PersistentData;
4
5namespace AllocsFixes.NetConnections.Servers.Web.API {
6 public class GetPlayersLocation : AbsWebAPI {
7 public override void HandleRequest (RequestContext _context) {
8 AdminTools admTools = GameManager.Instance.adminTools;
9 PlatformUserIdentifierAbs userId = _context.Connection?.UserId;
10
11 bool listOffline = false;
12 if (_context.Request.QueryString ["offline"] != null) {
13 bool.TryParse (_context.Request.QueryString ["offline"], out listOffline);
14 }
15
16 bool bViewAll = WebConnection.CanViewAllPlayers (_context.PermissionLevel);
17
18 JSONArray playersJsResult = new JSONArray ();
19
20 Players playersList = PersistentContainer.Instance.Players;
21
22 foreach (KeyValuePair<PlatformUserIdentifierAbs, Player> kvp in playersList.Dict) {
23 if (admTools != null) {
24 if (admTools.IsBanned (kvp.Key, out _, out _)) {
25 continue;
26 }
27 }
28
29 Player p = kvp.Value;
30
31 if (listOffline || p.IsOnline) {
32 if (bViewAll || p.PlatformId.Equals (userId)) {
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));
37
38 JSONObject pJson = new JSONObject ();
39 pJson.Add ("steamid", new JSONString (kvp.Key.CombinedString));
40
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);
46
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
51 playersJsResult.Add (pJson);
52 }
53 }
54 }
55
56 WebUtils.WriteJson (_context.Response, playersJsResult);
57 }
58 }
59}
Note: See TracBrowser for help on using the repository browser.