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

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

Major refactoring/cleanup

File size: 2.0 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.JSON;
3using AllocsFixes.PersistentData;
4using JetBrains.Annotations;
5
6namespace Webserver.WebAPI {
7 [UsedImplicitly]
8 public class GetPlayersLocation : AbsWebAPI {
9 public override void HandleRequest (RequestContext _context) {
10 AdminTools admTools = GameManager.Instance.adminTools;
11 PlatformUserIdentifierAbs reqUserId = _context.Connection?.UserId;
12
13 bool listOffline = false;
14 if (_context.Request.QueryString ["offline"] != null) {
15 bool.TryParse (_context.Request.QueryString ["offline"], out listOffline);
16 }
17
18 bool bViewAll = WebConnection.CanViewAllPlayers (_context.PermissionLevel);
19
20 JsonArray playersJsResult = new JsonArray ();
21
22 Players playersList = PersistentContainer.Instance.Players;
23
24 foreach ((PlatformUserIdentifierAbs userId, Player player) in playersList.Dict) {
25 if (admTools != null) {
26 if (admTools.IsBanned (userId, out _, out _)) {
27 continue;
28 }
29 }
30
31 if (!listOffline && !player.IsOnline) {
32 continue;
33 }
34
35 if (!bViewAll && !player.PlatformId.Equals (reqUserId)) {
36 continue;
37 }
38
39 JsonObject pos = new JsonObject ();
40 pos.Add ("x", new JsonNumber (player.LastPosition.x));
41 pos.Add ("y", new JsonNumber (player.LastPosition.y));
42 pos.Add ("z", new JsonNumber (player.LastPosition.z));
43
44 JsonObject pJson = new JsonObject ();
45 pJson.Add ("steamid", new JsonString (userId.CombinedString));
46
47 // pJson.Add("entityid", new JSONNumber (p.EntityID));
48 // pJson.Add("ip", new JSONString (p.IP));
49 pJson.Add ("name", new JsonString (player.Name));
50 pJson.Add ("online", new JsonBoolean (player.IsOnline));
51 pJson.Add ("position", pos);
52
53 // pJson.Add ("totalplaytime", new JSONNumber (p.TotalPlayTime));
54 // pJson.Add ("lastonline", new JSONString (p.LastOnline.ToString ("s")));
55 // pJson.Add ("ping", new JSONNumber (p.IsOnline ? p.ClientInfo.ping : -1));
56
57 playersJsResult.Add (pJson);
58 }
59
60 WebUtils.WriteJson (_context.Response, playersJsResult);
61 }
62 }
63}
Note: See TracBrowser for help on using the repository browser.