Changeset 351 for binary-improvements/MapRendering/Web/API
- Timestamp:
- Jan 19, 2019, 6:12:21 PM (6 years ago)
- Location:
- binary-improvements/MapRendering/Web/API
- Files:
-
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/API/ExecuteConsoleCommand.cs
r347 r351 4 4 namespace AllocsFixes.NetConnections.Servers.Web.API { 5 5 public class ExecuteConsoleCommand : WebAPI { 6 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,7 int permissionLevel) {8 if (string.IsNullOrEmpty ( req.QueryString ["command"])) {9 resp.StatusCode = (int) HttpStatusCode.BadRequest;10 Web.SetResponseTextContent ( resp, "No command given");6 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 7 int _permissionLevel) { 8 if (string.IsNullOrEmpty (_req.QueryString ["command"])) { 9 _resp.StatusCode = (int) HttpStatusCode.BadRequest; 10 Web.SetResponseTextContent (_resp, "No command given"); 11 11 return; 12 12 } 13 13 14 14 WebCommandResult.ResultType responseType = 15 req.QueryString ["raw"] != null15 _req.QueryString ["raw"] != null 16 16 ? WebCommandResult.ResultType.Raw 17 : ( req.QueryString ["simple"] != null17 : (_req.QueryString ["simple"] != null 18 18 ? WebCommandResult.ResultType.ResultOnly 19 19 : WebCommandResult.ResultType.Full); 20 20 21 string commandline = req.QueryString ["command"];21 string commandline = _req.QueryString ["command"]; 22 22 string commandPart = commandline.Split (' ') [0]; 23 23 string argumentsPart = commandline.Substring (Math.Min (commandline.Length, commandPart.Length + 1)); … … 26 26 27 27 if (command == null) { 28 resp.StatusCode = (int) HttpStatusCode.NotFound;29 Web.SetResponseTextContent ( resp, "Unknown command");28 _resp.StatusCode = (int) HttpStatusCode.NotFound; 29 Web.SetResponseTextContent (_resp, "Unknown command"); 30 30 return; 31 31 } … … 34 34 GameManager.Instance.adminTools.GetAdminToolsCommandPermission (command.GetCommands ()); 35 35 36 if ( permissionLevel > atcp.PermissionLevel) {37 resp.StatusCode = (int) HttpStatusCode.Forbidden;38 Web.SetResponseTextContent ( resp, "You are not allowed to execute this command");36 if (_permissionLevel > atcp.PermissionLevel) { 37 _resp.StatusCode = (int) HttpStatusCode.Forbidden; 38 Web.SetResponseTextContent (_resp, "You are not allowed to execute this command"); 39 39 return; 40 40 } 41 41 42 resp.SendChunked = true;43 WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, resp);42 _resp.SendChunked = true; 43 WebCommandResult wcr = new WebCommandResult (commandPart, argumentsPart, responseType, _resp); 44 44 SdtdConsole.Instance.ExecuteAsync (commandline, wcr); 45 45 } -
binary-improvements/MapRendering/Web/API/GetAllowedCommands.cs
r325 r351 4 4 namespace AllocsFixes.NetConnections.Servers.Web.API { 5 5 public class GetAllowedCommands : WebAPI { 6 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,7 int permissionLevel) {6 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 7 int _permissionLevel) { 8 8 JSONObject result = new JSONObject (); 9 9 JSONArray entries = new JSONArray (); … … 11 11 AdminToolsCommandPermissions atcp = 12 12 GameManager.Instance.adminTools.GetAdminToolsCommandPermission (cc.GetCommands ()); 13 if ( permissionLevel <= atcp.PermissionLevel) {13 if (_permissionLevel <= atcp.PermissionLevel) { 14 14 string cmd = string.Empty; 15 15 foreach (string s in cc.GetCommands ()) { … … 29 29 result.Add ("commands", entries); 30 30 31 WriteJSON ( resp, result);31 WriteJSON (_resp, result); 32 32 } 33 33 -
binary-improvements/MapRendering/Web/API/GetAnimalsLocation.cs
r325 r351 8 8 private readonly List<EntityAnimal> animals = new List<EntityAnimal> (); 9 9 10 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,11 int permissionLevel) {10 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 11 int _permissionLevel) { 12 12 JSONArray animalsJsResult = new JSONArray (); 13 13 … … 36 36 } 37 37 38 WriteJSON ( resp, animalsJsResult);38 WriteJSON (_resp, animalsJsResult); 39 39 } 40 40 } -
binary-improvements/MapRendering/Web/API/GetHostileLocation.cs
r325 r351 8 8 private readonly List<EntityEnemy> enemies = new List<EntityEnemy> (); 9 9 10 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,11 int permissionLevel) {10 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 11 int _permissionLevel) { 12 12 JSONArray hostilesJsResult = new JSONArray (); 13 13 … … 36 36 } 37 37 38 WriteJSON ( resp, hostilesJsResult);38 WriteJSON (_resp, hostilesJsResult); 39 39 } 40 40 } -
binary-improvements/MapRendering/Web/API/GetLandClaims.cs
r332 r351 6 6 namespace AllocsFixes.NetConnections.Servers.Web.API { 7 7 public class GetLandClaims : WebAPI { 8 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,9 int permissionLevel) {8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 9 int _permissionLevel) { 10 10 string requestedSteamID = string.Empty; 11 11 12 if ( req.QueryString ["steamid"] != null) {12 if (_req.QueryString ["steamid"] != null) { 13 13 ulong lViewersSteamID; 14 requestedSteamID = req.QueryString ["steamid"];14 requestedSteamID = _req.QueryString ["steamid"]; 15 15 if (requestedSteamID.Length != 17 || !ulong.TryParse (requestedSteamID, out lViewersSteamID)) { 16 resp.StatusCode = (int) HttpStatusCode.BadRequest;17 Web.SetResponseTextContent ( resp, "Invalid SteamID given");16 _resp.StatusCode = (int) HttpStatusCode.BadRequest; 17 Web.SetResponseTextContent (_resp, "Invalid SteamID given"); 18 18 return; 19 19 } … … 21 21 22 22 // default user, cheap way to avoid 'null reference exception' 23 user =user ?? new WebConnection ("", IPAddress.None, 0L);23 _user = _user ?? new WebConnection ("", IPAddress.None, 0L); 24 24 25 bool bViewAll = WebConnection.CanViewAllClaims ( permissionLevel);25 bool bViewAll = WebConnection.CanViewAllClaims (_permissionLevel); 26 26 27 27 JSONObject result = new JSONObject (); 28 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (Enum GamePrefs.LandClaimSize)));28 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> ("LandClaimSize")))); 29 29 30 30 JSONArray claimOwners = new JSONArray (); … … 35 35 if (!string.IsNullOrEmpty (requestedSteamID) && !bViewAll) { 36 36 ownerFilters = new[] { 37 LandClaimList.SteamIdFilter ( user.SteamID.ToString ()),37 LandClaimList.SteamIdFilter (_user.SteamID.ToString ()), 38 38 LandClaimList.SteamIdFilter (requestedSteamID) 39 39 }; 40 40 } else if (!bViewAll) { 41 ownerFilters = new[] {LandClaimList.SteamIdFilter ( user.SteamID.ToString ())};41 ownerFilters = new[] {LandClaimList.SteamIdFilter (_user.SteamID.ToString ())}; 42 42 } else { 43 43 ownerFilters = new[] {LandClaimList.SteamIdFilter (requestedSteamID)}; … … 78 78 } 79 79 80 WriteJSON ( resp, result);80 WriteJSON (_resp, result); 81 81 } 82 82 } -
binary-improvements/MapRendering/Web/API/GetPlayerList.cs
r332 r351 17 17 #endif 18 18 19 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,20 int permissionLevel) {19 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 20 int _permissionLevel) { 21 21 AdminTools admTools = GameManager.Instance.adminTools; 22 user =user ?? new WebConnection ("", IPAddress.None, 0L);23 24 bool bViewAll = WebConnection.CanViewAllPlayers ( permissionLevel);22 _user = _user ?? new WebConnection ("", IPAddress.None, 0L); 23 24 bool bViewAll = WebConnection.CanViewAllPlayers (_permissionLevel); 25 25 26 26 // TODO: Sort (and filter?) prior to converting to JSON ... hard as how to get the correct column's data? (i.e. column name matches JSON object field names, not source data) 27 27 28 28 int rowsPerPage = 25; 29 if ( req.QueryString ["rowsperpage"] != null) {30 int.TryParse ( req.QueryString ["rowsperpage"], out rowsPerPage);29 if (_req.QueryString ["rowsperpage"] != null) { 30 int.TryParse (_req.QueryString ["rowsperpage"], out rowsPerPage); 31 31 } 32 32 33 33 int page = 0; 34 if ( req.QueryString ["page"] != null) {35 int.TryParse ( req.QueryString ["page"], out page);34 if (_req.QueryString ["page"] != null) { 35 int.TryParse (_req.QueryString ["page"], out page); 36 36 } 37 37 … … 55 55 } 56 56 57 if (player_steam_ID == user.SteamID || bViewAll) {57 if (player_steam_ID == _user.SteamID || bViewAll) { 58 58 JSONObject pos = new JSONObject (); 59 59 pos.Add ("x", new JSONNumber (p.LastPosition.x)); … … 93 93 IEnumerable<JSONObject> list = playerList; 94 94 95 foreach (string key in req.QueryString.AllKeys) {95 foreach (string key in _req.QueryString.AllKeys) { 96 96 if (!string.IsNullOrEmpty (key) && key.StartsWith ("filter[")) { 97 97 string filterCol = key.Substring (key.IndexOf ('[') + 1); 98 98 filterCol = filterCol.Substring (0, filterCol.Length - 1); 99 string filterVal = req.QueryString.Get (key).Trim ();99 string filterVal = _req.QueryString.Get (key).Trim (); 100 100 101 101 list = ExecuteFilter (list, filterCol, filterVal); … … 105 105 int totalAfterFilter = list.Count (); 106 106 107 foreach (string key in req.QueryString.AllKeys) {107 foreach (string key in _req.QueryString.AllKeys) { 108 108 if (!string.IsNullOrEmpty (key) && key.StartsWith ("sort[")) { 109 109 string sortCol = key.Substring (key.IndexOf ('[') + 1); 110 110 sortCol = sortCol.Substring (0, sortCol.Length - 1); 111 string sortVal = req.QueryString.Get (key);111 string sortVal = _req.QueryString.Get (key); 112 112 113 113 list = ExecuteSort (list, sortCol, sortVal == "0"); … … 130 130 result.Add ("players", playersJsResult); 131 131 132 WriteJSON ( resp, result);132 WriteJSON (_resp, result); 133 133 } 134 134 135 135 private IEnumerable<JSONObject> ExecuteFilter (IEnumerable<JSONObject> _list, string _filterCol, 136 136 string _filterVal) { 137 if ( _list.Count () == 0) {137 if (!_list.Any()) { 138 138 return _list; 139 139 } … … 147 147 if (colType == typeof (JSONBoolean)) { 148 148 bool value = StringParsers.ParseBool (_filterVal); 149 return _list.Where ( line => ((JSONBoolean)line [_filterCol]).GetBool () == value);149 return _list.Where (_line => ((JSONBoolean) _line [_filterCol]).GetBool () == value); 150 150 } 151 151 … … 157 157 //Log.Out ("GetPlayerList: Filter on String with Regex '" + _filterVal + "'"); 158 158 Regex matcher = new Regex (_filterVal, RegexOptions.IgnoreCase); 159 return _list.Where ( line => matcher.IsMatch (((JSONString)line [_filterCol]).GetString ()));159 return _list.Where (_line => matcher.IsMatch (((JSONString) _line [_filterCol]).GetString ())); 160 160 } 161 161 } … … 198 198 } 199 199 200 return _list.Where (delegate (JSONObject line) {201 double objVal = ((JSONNumber) line [_filterCol]).GetDouble ();200 return _list.Where (delegate (JSONObject _line) { 201 double objVal = ((JSONNumber) _line [_filterCol]).GetDouble (); 202 202 switch (matchType) { 203 203 case NumberMatchType.Greater: … … 230 230 if (colType == typeof (JSONNumber)) { 231 231 if (_ascending) { 232 return _list.OrderBy ( line => ((JSONNumber)line [_sortCol]).GetDouble ());233 } 234 235 return _list.OrderByDescending ( line => ((JSONNumber)line [_sortCol]).GetDouble ());232 return _list.OrderBy (_line => ((JSONNumber) _line [_sortCol]).GetDouble ()); 233 } 234 235 return _list.OrderByDescending (_line => ((JSONNumber) _line [_sortCol]).GetDouble ()); 236 236 } 237 237 238 238 if (colType == typeof (JSONBoolean)) { 239 239 if (_ascending) { 240 return _list.OrderBy ( line => ((JSONBoolean)line [_sortCol]).GetBool ());241 } 242 243 return _list.OrderByDescending ( line => ((JSONBoolean)line [_sortCol]).GetBool ());240 return _list.OrderBy (_line => ((JSONBoolean) _line [_sortCol]).GetBool ()); 241 } 242 243 return _list.OrderByDescending (_line => ((JSONBoolean) _line [_sortCol]).GetBool ()); 244 244 } 245 245 246 246 if (_ascending) { 247 return _list.OrderBy ( line =>line [_sortCol].ToString ());248 } 249 250 return _list.OrderByDescending ( line =>line [_sortCol].ToString ());247 return _list.OrderBy (_line => _line [_sortCol].ToString ()); 248 } 249 250 return _list.OrderByDescending (_line => _line [_sortCol].ToString ()); 251 251 } 252 252 … … 255 255 256 256 257 private bool NearlyEqual (double a, double b, doubleepsilon) {258 double absA = Math.Abs ( a);259 double absB = Math.Abs ( b);260 double diff = Math.Abs ( a -b);261 262 if ( a ==b) {257 private bool NearlyEqual (double _a, double _b, double _epsilon) { 258 double absA = Math.Abs (_a); 259 double absB = Math.Abs (_b); 260 double diff = Math.Abs (_a - _b); 261 262 if (_a == _b) { 263 263 return true; 264 264 } 265 265 266 if ( a == 0 ||b == 0 || diff < double.Epsilon) {267 return diff < epsilon;268 } 269 270 return diff / (absA + absB) < epsilon;266 if (_a == 0 || _b == 0 || diff < double.Epsilon) { 267 return diff < _epsilon; 268 } 269 270 return diff / (absA + absB) < _epsilon; 271 271 } 272 272 -
binary-improvements/MapRendering/Web/API/GetPlayersLocation.cs
r332 r351 6 6 namespace AllocsFixes.NetConnections.Servers.Web.API { 7 7 public class GetPlayersLocation : WebAPI { 8 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,9 int permissionLevel) {8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 9 int _permissionLevel) { 10 10 AdminTools admTools = GameManager.Instance.adminTools; 11 user =user ?? new WebConnection ("", IPAddress.None, 0L);11 _user = _user ?? new WebConnection ("", IPAddress.None, 0L); 12 12 13 13 bool listOffline = false; 14 if ( req.QueryString ["offline"] != null) {15 bool.TryParse ( req.QueryString ["offline"], out listOffline);14 if (_req.QueryString ["offline"] != null) { 15 bool.TryParse (_req.QueryString ["offline"], out listOffline); 16 16 } 17 17 18 bool bViewAll = WebConnection.CanViewAllPlayers ( permissionLevel);18 bool bViewAll = WebConnection.CanViewAllPlayers (_permissionLevel); 19 19 20 20 JSONArray playersJsResult = new JSONArray (); … … 37 37 } 38 38 39 if (player_steam_ID == user.SteamID || bViewAll) {39 if (player_steam_ID == _user.SteamID || bViewAll) { 40 40 JSONObject pos = new JSONObject (); 41 41 pos.Add ("x", new JSONNumber (p.LastPosition.x)); … … 61 61 } 62 62 63 WriteJSON ( resp, playersJsResult);63 WriteJSON (_resp, playersJsResult); 64 64 } 65 65 } -
binary-improvements/MapRendering/Web/API/GetPlayersOnline.cs
r326 r351 6 6 namespace AllocsFixes.NetConnections.Servers.Web.API { 7 7 public class GetPlayersOnline : WebAPI { 8 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,9 int permissionLevel) {8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 9 int _permissionLevel) { 10 10 JSONArray players = new JSONArray (); 11 11 … … 46 46 } 47 47 48 WriteJSON ( resp, players);48 WriteJSON (_resp, players); 49 49 } 50 50 } -
binary-improvements/MapRendering/Web/API/GetServerInfo.cs
r325 r351 5 5 namespace AllocsFixes.NetConnections.Servers.Web.API { 6 6 public class GetServerInfo : WebAPI { 7 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,8 int permissionLevel) {7 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 8 int _permissionLevel) { 9 9 JSONObject serverInfo = new JSONObject (); 10 10 … … 42 42 43 43 44 WriteJSON ( resp, serverInfo);44 WriteJSON (_resp, serverInfo); 45 45 } 46 46 } -
binary-improvements/MapRendering/Web/API/GetStats.cs
r325 r351 5 5 namespace AllocsFixes.NetConnections.Servers.Web.API { 6 6 public class GetStats : WebAPI { 7 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,8 int permissionLevel) {7 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 8 int _permissionLevel) { 9 9 JSONObject result = new JSONObject (); 10 10 … … 19 19 result.Add ("animals", new JSONNumber (Animals.Instance.GetCount ())); 20 20 21 WriteJSON ( resp, result);21 WriteJSON (_resp, result); 22 22 } 23 23 -
binary-improvements/MapRendering/Web/API/GetWebUIUpdates.cs
r325 r351 5 5 namespace AllocsFixes.NetConnections.Servers.Web.API { 6 6 public class GetWebUIUpdates : WebAPI { 7 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,8 int permissionLevel) {7 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 8 int _permissionLevel) { 9 9 int latestLine; 10 if ( req.QueryString ["latestLine"] == null ||11 !int.TryParse ( req.QueryString ["latestLine"], out latestLine)) {10 if (_req.QueryString ["latestLine"] == null || 11 !int.TryParse (_req.QueryString ["latestLine"], out latestLine)) { 12 12 latestLine = 0; 13 13 } … … 27 27 result.Add ("newlogs", new JSONNumber (LogBuffer.Instance.LatestLine - latestLine)); 28 28 29 WriteJSON ( resp, result);29 WriteJSON (_resp, result); 30 30 } 31 31 -
binary-improvements/MapRendering/Web/API/Null.cs
r325 r351 4 4 namespace AllocsFixes.NetConnections.Servers.Web.API { 5 5 public class Null : WebAPI { 6 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,7 int permissionLevel) {8 resp.ContentLength64 = 0;9 resp.ContentType = "text/plain";10 resp.ContentEncoding = Encoding.ASCII;11 resp.OutputStream.Write (new byte[] { }, 0, 0);6 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 7 int _permissionLevel) { 8 _resp.ContentLength64 = 0; 9 _resp.ContentType = "text/plain"; 10 _resp.ContentEncoding = Encoding.ASCII; 11 _resp.OutputStream.Write (new byte[] { }, 0, 0); 12 12 } 13 13 } -
binary-improvements/MapRendering/Web/API/WebAPI.cs
r332 r351 17 17 #endif 18 18 19 public static void WriteJSON (HttpListenerResponse resp, JSONNoderoot) {19 public static void WriteJSON (HttpListenerResponse _resp, JSONNode _root) { 20 20 #if ENABLE_PROFILER 21 21 jsonSerializeSampler.Begin (); 22 22 #endif 23 23 StringBuilder sb = new StringBuilder (); 24 root.ToString (sb);24 _root.ToString (sb); 25 25 #if ENABLE_PROFILER 26 26 jsonSerializeSampler.End (); … … 28 28 #endif 29 29 byte[] buf = Encoding.UTF8.GetBytes (sb.ToString ()); 30 resp.ContentLength64 = buf.Length;31 resp.ContentType = "application/json";32 resp.ContentEncoding = Encoding.UTF8;33 resp.OutputStream.Write (buf, 0, buf.Length);30 _resp.ContentLength64 = buf.Length; 31 _resp.ContentType = "application/json"; 32 _resp.ContentEncoding = Encoding.UTF8; 33 _resp.OutputStream.Write (buf, 0, buf.Length); 34 34 #if ENABLE_PROFILER 35 35 netWriteSampler.End (); … … 45 45 } 46 46 47 public abstract void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnectionuser,48 int permissionLevel);47 public abstract void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user, 48 int _permissionLevel); 49 49 50 50 public virtual int DefaultPermissionLevel () {
Note:
See TracChangeset
for help on using the changeset viewer.