Changeset 325 for binary-improvements/MapRendering/Web/API/GetPlayerList.cs
- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/Web/API/GetPlayerList.cs
r324 r325 1 using AllocsFixes.JSON;2 using AllocsFixes.PersistentData;3 1 using System; 4 2 using System.Collections.Generic; … … 6 4 using System.Net; 7 5 using System.Text.RegularExpressions; 8 9 namespace AllocsFixes.NetConnections.Servers.Web.API 10 { 11 public class GetPlayerList : WebAPI 12 { 13 private static Regex numberFilterMatcher = new Regex (@"^(>=|=>|>|<=|=<|<|==|=)?\s*([0-9]+(\.[0-9]*)?)$"); 14 private enum NumberMatchType { 15 Equal, 16 Greater, 17 GreaterEqual, 18 Lesser, 19 LesserEqual 20 } 21 22 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) 23 { 24 AdminTools admTools = GameManager.Instance.adminTools; 25 user = user ?? new WebConnection ("", "", 0L); 26 27 bool bViewAll = WebConnection.CanViewAllPlayers (permissionLevel); 6 using AllocsFixes.JSON; 7 using AllocsFixes.PersistentData; 8 9 namespace AllocsFixes.NetConnections.Servers.Web.API { 10 public class GetPlayerList : WebAPI { 11 private static readonly Regex numberFilterMatcher = 12 new Regex (@"^(>=|=>|>|<=|=<|<|==|=)?\s*([0-9]+(\.[0-9]*)?)$"); 13 14 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, 15 int permissionLevel) { 16 AdminTools admTools = GameManager.Instance.adminTools; 17 user = user ?? new WebConnection ("", "", 0L); 18 19 bool bViewAll = WebConnection.CanViewAllPlayers (permissionLevel); 28 20 29 21 // 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) … … 46 38 47 39 foreach (string sid in playersList.SteamIDs) { 48 Player p = playersList [sid, false]; 49 50 ulong player_steam_ID = 0L; 51 if (!ulong.TryParse (sid, out player_steam_ID)) 52 player_steam_ID = 0L; 53 54 if ((player_steam_ID == user.SteamID) || bViewAll) { 55 JSONObject pos = new JSONObject (); 56 pos.Add("x", new JSONNumber (p.LastPosition.x)); 57 pos.Add("y", new JSONNumber (p.LastPosition.y)); 58 pos.Add("z", new JSONNumber (p.LastPosition.z)); 59 60 JSONObject pJson = new JSONObject (); 61 pJson.Add("steamid", new JSONString (sid)); 62 pJson.Add("entityid", new JSONNumber (p.EntityID)); 63 pJson.Add("ip", new JSONString (p.IP)); 64 pJson.Add("name", new JSONString (p.Name)); 65 pJson.Add("online", new JSONBoolean (p.IsOnline)); 66 pJson.Add("position", pos); 40 Player p = playersList [sid, false]; 41 42 ulong player_steam_ID = 0L; 43 if (!ulong.TryParse (sid, out player_steam_ID)) { 44 player_steam_ID = 0L; 45 } 46 47 if (player_steam_ID == user.SteamID || bViewAll) { 48 JSONObject pos = new JSONObject (); 49 pos.Add ("x", new JSONNumber (p.LastPosition.x)); 50 pos.Add ("y", new JSONNumber (p.LastPosition.y)); 51 pos.Add ("z", new JSONNumber (p.LastPosition.z)); 52 53 JSONObject pJson = new JSONObject (); 54 pJson.Add ("steamid", new JSONString (sid)); 55 pJson.Add ("entityid", new JSONNumber (p.EntityID)); 56 pJson.Add ("ip", new JSONString (p.IP)); 57 pJson.Add ("name", new JSONString (p.Name)); 58 pJson.Add ("online", new JSONBoolean (p.IsOnline)); 59 pJson.Add ("position", pos); 67 60 68 61 pJson.Add ("totalplaytime", new JSONNumber (p.TotalPlayTime)); 69 pJson.Add ("lastonline", new JSONString (p.LastOnline.ToUniversalTime ().ToString ("yyyy-MM-ddTHH:mm:ssZ"))); 62 pJson.Add ("lastonline", 63 new JSONString (p.LastOnline.ToUniversalTime ().ToString ("yyyy-MM-ddTHH:mm:ssZ"))); 70 64 pJson.Add ("ping", new JSONNumber (p.IsOnline ? p.ClientInfo.ping : -1)); 71 65 … … 76 70 banned = new JSONBoolean (false); 77 71 } 72 78 73 pJson.Add ("banned", banned); 79 74 80 75 playerList.Add (pJson); 81 82 76 } 77 } 83 78 84 79 IEnumerable<JSONObject> list = playerList; … … 124 119 } 125 120 126 private IEnumerable<JSONObject> ExecuteFilter (IEnumerable<JSONObject> _list, string _filterCol, string _filterVal) { 121 private IEnumerable<JSONObject> ExecuteFilter (IEnumerable<JSONObject> _list, string _filterCol, 122 string _filterVal) { 127 123 if (_list.Count () == 0) { 128 124 return _list; … … 131 127 if (_list.First ().ContainsKey (_filterCol)) { 132 128 Type colType = _list.First () [_filterCol].GetType (); 133 if (colType == typeof (JSONNumber)) {129 if (colType == typeof (JSONNumber)) { 134 130 return ExecuteNumberFilter (_list, _filterCol, _filterVal); 135 } else if (colType == typeof(JSONBoolean)) { 131 } 132 133 if (colType == typeof (JSONBoolean)) { 136 134 bool value = _filterVal.Trim ().ToLower () == "true"; 137 135 return _list.Where (line => (line [_filterCol] as JSONBoolean).GetBool () == value); 138 } else if (colType == typeof(JSONString)) { 136 } 137 138 if (colType == typeof (JSONString)) { 139 139 // regex-match whole ^string$, replace * by .*, ? by .?, + by .+ 140 140 _filterVal = _filterVal.Replace ("*", ".*").Replace ("?", ".?").Replace ("+", ".+"); 141 141 _filterVal = "^" + _filterVal + "$"; 142 142 143 //Log.Out ("GetPlayerList: Filter on String with Regex '" + _filterVal + "'"); 143 144 Regex matcher = new Regex (_filterVal, RegexOptions.IgnoreCase); … … 145 146 } 146 147 } 148 147 149 return _list; 148 150 } 149 151 150 152 151 private IEnumerable<JSONObject> ExecuteNumberFilter (IEnumerable<JSONObject> _list, string _filterCol, string _filterVal) { 153 private IEnumerable<JSONObject> ExecuteNumberFilter (IEnumerable<JSONObject> _list, string _filterCol, 154 string _filterVal) { 152 155 // allow value (exact match), =, ==, >=, >, <=, < 153 156 Match filterMatch = numberFilterMatcher.Match (_filterVal); … … 157 160 double epsilon = value / 100000; 158 161 switch (filterMatch.Groups [1].Value) { 159 case "": 160 case "=": 161 case "==": 162 matchType = NumberMatchType.Equal; 163 break; 164 case ">": 165 matchType = NumberMatchType.Greater; 166 break; 167 case ">=": 168 case "=>": 169 matchType = NumberMatchType.GreaterEqual; 170 break; 171 case "<": 172 matchType = NumberMatchType.Lesser; 173 break; 174 case "<=": 175 case "=<": 176 matchType = NumberMatchType.LesserEqual; 177 break; 178 default: 179 matchType = NumberMatchType.Equal; 180 break; 181 } 182 return _list.Where (delegate(JSONObject line) { 162 case "": 163 case "=": 164 case "==": 165 matchType = NumberMatchType.Equal; 166 break; 167 case ">": 168 matchType = NumberMatchType.Greater; 169 break; 170 case ">=": 171 case "=>": 172 matchType = NumberMatchType.GreaterEqual; 173 break; 174 case "<": 175 matchType = NumberMatchType.Lesser; 176 break; 177 case "<=": 178 case "=<": 179 matchType = NumberMatchType.LesserEqual; 180 break; 181 default: 182 matchType = NumberMatchType.Equal; 183 break; 184 } 185 186 return _list.Where (delegate (JSONObject line) { 183 187 double objVal = (line [_filterCol] as JSONNumber).GetDouble (); 184 188 switch (matchType) { 185 case NumberMatchType.Greater:186 return objVal > value;187 case NumberMatchType.GreaterEqual:188 return objVal >= value;189 case NumberMatchType.Lesser:190 return objVal < value;191 case NumberMatchType.LesserEqual:192 return objVal <= value;193 case NumberMatchType.Equal:194 default:195 return NearlyEqual (objVal, value, epsilon);189 case NumberMatchType.Greater: 190 return objVal > value; 191 case NumberMatchType.GreaterEqual: 192 return objVal >= value; 193 case NumberMatchType.Lesser: 194 return objVal < value; 195 case NumberMatchType.LesserEqual: 196 return objVal <= value; 197 case NumberMatchType.Equal: 198 default: 199 return NearlyEqual (objVal, value, epsilon); 196 200 } 197 201 }); 198 } else {199 Log.Out ("GetPlayerList: ignoring invalid filter for number-column '{0}': '{1}'", _filterCol, _filterVal); 200 }202 } 203 204 Log.Out ("GetPlayerList: ignoring invalid filter for number-column '{0}': '{1}'", _filterCol, _filterVal); 201 205 return _list; 202 206 } … … 210 214 if (_list.First ().ContainsKey (_sortCol)) { 211 215 Type colType = _list.First () [_sortCol].GetType (); 212 if (colType == typeof (JSONNumber)) {216 if (colType == typeof (JSONNumber)) { 213 217 if (_ascending) { 214 218 return _list.OrderBy (line => (line [_sortCol] as JSONNumber).GetDouble ()); 215 } else { 216 return _list.OrderByDescending (line => (line [_sortCol] as JSONNumber).GetDouble ()); 217 } 218 } else if (colType == typeof(JSONBoolean)) { 219 } 220 221 return _list.OrderByDescending (line => (line [_sortCol] as JSONNumber).GetDouble ()); 222 } 223 224 if (colType == typeof (JSONBoolean)) { 219 225 if (_ascending) { 220 226 return _list.OrderBy (line => (line [_sortCol] as JSONBoolean).GetBool ()); 221 } else { 222 return _list.OrderByDescending (line => (line [_sortCol] as JSONBoolean).GetBool ()); 223 } 224 } else { 225 if (_ascending) { 226 return _list.OrderBy (line => line [_sortCol].ToString ()); 227 } else { 228 return _list.OrderByDescending (line => line [_sortCol].ToString ()); 229 } 230 } 231 } 227 } 228 229 return _list.OrderByDescending (line => (line [_sortCol] as JSONBoolean).GetBool ()); 230 } 231 232 if (_ascending) { 233 return _list.OrderBy (line => line [_sortCol].ToString ()); 234 } 235 236 return _list.OrderByDescending (line => line [_sortCol].ToString ()); 237 } 238 232 239 return _list; 233 240 } 234 241 235 242 236 private bool NearlyEqual(double a, double b, double epsilon) 237 { 238 double absA = Math.Abs(a); 239 double absB = Math.Abs(b); 240 double diff = Math.Abs(a - b); 241 242 if (a == b) 243 { // shortcut, handles infinities 243 private bool NearlyEqual (double a, double b, double epsilon) { 244 double absA = Math.Abs (a); 245 double absB = Math.Abs (b); 246 double diff = Math.Abs (a - b); 247 248 if (a == b) { 244 249 return true; 245 } 246 else if (a == 0 || b == 0 || diff < Double.Epsilon) 247 { 248 // a or b is zero or both are extremely close to it 249 // relative error is less meaningful here 250 } 251 252 if (a == 0 || b == 0 || diff < double.Epsilon) { 250 253 return diff < epsilon; 251 254 } 252 else 253 { // use relative error 254 return diff / (absA + absB) < epsilon; 255 } 256 } 257 255 256 return diff / (absA + absB) < epsilon; 257 } 258 259 private enum NumberMatchType { 260 Equal, 261 Greater, 262 GreaterEqual, 263 Lesser, 264 LesserEqual 265 } 258 266 } 259 267 } 260
Note:
See TracChangeset
for help on using the changeset viewer.