source: binary-improvements/MapRendering/Web/API/GetPlayersLocation.cs @ 326

Last change on this file since 326 was 326, checked in by alloc, 5 years ago

More cleanup, allocation improvements

File size: 2.0 KB
Line 
1using System.Net;
2using AllocsFixes.JSON;
3using AllocsFixes.PersistentData;
4
5namespace AllocsFixes.NetConnections.Servers.Web.API {
6        public class GetPlayersLocation : WebAPI {
7                public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
8                        int permissionLevel) {
9                        AdminTools admTools = GameManager.Instance.adminTools;
10                        user = user ?? new WebConnection ("", "", 0L);
11
12                        bool listOffline = false;
13                        if (req.QueryString ["offline"] != null) {
14                                bool.TryParse (req.QueryString ["offline"], out listOffline);
15                        }
16
17                        bool bViewAll = WebConnection.CanViewAllPlayers (permissionLevel);
18
19                        JSONArray playersJsResult = new JSONArray ();
20
21                        Players playersList = PersistentContainer.Instance.Players;
22
23                        foreach (string sid in playersList.SteamIDs) {
24                                if (admTools != null) {
25                                        if (admTools.IsBanned (sid)) {
26                                                continue;
27                                        }
28                                }
29
30                                Player p = playersList [sid, false];
31
32                                if (listOffline || p.IsOnline) {
33                                        ulong player_steam_ID;
34                                        if (!ulong.TryParse (sid, out player_steam_ID)) {
35                                                player_steam_ID = 0L;
36                                        }
37
38                                        if (player_steam_ID == user.SteamID || bViewAll) {
39                                                JSONObject pos = new JSONObject ();
40                                                pos.Add ("x", new JSONNumber (p.LastPosition.x));
41                                                pos.Add ("y", new JSONNumber (p.LastPosition.y));
42                                                pos.Add ("z", new JSONNumber (p.LastPosition.z));
43
44                                                JSONObject pJson = new JSONObject ();
45                                                pJson.Add ("steamid", new JSONString (sid));
46
47                                                //                                      pJson.Add("entityid", new JSONNumber (p.EntityID));
48                                                //                    pJson.Add("ip", new JSONString (p.IP));
49                                                pJson.Add ("name", new JSONString (p.Name));
50                                                pJson.Add ("online", new JSONBoolean (p.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                        }
61
62                        WriteJSON (resp, playersJsResult);
63                }
64        }
65}
Note: See TracBrowser for help on using the repository browser.