Changeset 455 for binary-improvements/MapRendering/API/GetPlayerList.cs
- Timestamp:
- Jul 31, 2023, 4:06:13 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/API/GetPlayerList.cs
r454 r455 2 2 using System.Collections.Generic; 3 3 using System.Linq; 4 using System.Text; 4 5 using System.Text.RegularExpressions; 5 using AllocsFixes.JSON;6 6 using AllocsFixes.PersistentData; 7 using JetBrains.Annotations; 7 8 using Webserver; 8 9 using Webserver.Permissions; … … 10 11 11 12 namespace AllocsFixes.WebAPIs { 13 [UsedImplicitly] 12 14 public class GetPlayerList : AbsWebAPI { 13 15 private static readonly Regex numberFilterMatcher = … … 121 123 result.Add ("players", playersJsResult); 122 124 123 LegacyApiHelper.WriteJSON (_context.Response, result); 124 } 125 126 private IEnumerable<JSONObject> ExecuteFilter (IEnumerable<JSONObject> _list, string _filterCol, 125 StringBuilder sb = new StringBuilder (); 126 result.ToString (sb); 127 WebUtils.WriteText (_context.Response, sb.ToString(), _mimeType: WebUtils.MimeJson); 128 } 129 130 private static IEnumerable<JSONObject> ExecuteFilter (IEnumerable<JSONObject> _list, string _filterCol, 127 131 string _filterVal) { 128 132 if (!_list.Any()) { … … 144 148 // regex-match whole ^string$, replace * by .*, ? by .?, + by .+ 145 149 _filterVal = _filterVal.Replace ("*", ".*").Replace ("?", ".?").Replace ("+", ".+"); 146 _filterVal = "^" + _filterVal + "$";150 _filterVal = $"^{_filterVal}$"; 147 151 148 152 //Log.Out ("GetPlayerList: Filter on String with Regex '" + _filterVal + "'"); … … 156 160 157 161 158 private IEnumerable<JSONObject> ExecuteNumberFilter (IEnumerable<JSONObject> _list, string _filterCol,162 private static IEnumerable<JSONObject> ExecuteNumberFilter (IEnumerable<JSONObject> _list, string _filterCol, 159 163 string _filterVal) { 160 164 // allow value (exact match), =, ==, >=, >, <=, < … … 207 211 } 208 212 209 Log.Out ( "GetPlayerList: ignoring invalid filter for number-column '{0}': '{1}'", _filterCol, _filterVal);213 Log.Out ($"GetPlayerList: ignoring invalid filter for number-column '{_filterCol}': '{_filterVal}'"); 210 214 return _list; 211 215 } 212 216 213 217 214 private IEnumerable<JSONObject> ExecuteSort (IEnumerable<JSONObject> _list, string _sortCol, bool _ascending) {218 private static IEnumerable<JSONObject> ExecuteSort (IEnumerable<JSONObject> _list, string _sortCol, bool _ascending) { 215 219 if (_list.Count () == 0) { 216 220 return _list; … … 246 250 247 251 248 private bool NearlyEqual (double _a, double _b, double _epsilon) {252 private static bool NearlyEqual (double _a, double _b, double _epsilon) { 249 253 double absA = Math.Abs (_a); 250 254 double absB = Math.Abs (_b); … … 267 271 GreaterEqual, 268 272 Lesser, 269 LesserEqual 270 } 273 LesserEqual, 274 } 275 276 277 #region JSON encoder 278 279 private abstract class JSONNode { 280 public abstract void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0); 281 282 public override string ToString () { 283 StringBuilder sb = new StringBuilder (); 284 ToString (sb); 285 return sb.ToString (); 286 } 287 } 288 289 private abstract class JSONValue : JSONNode { 290 } 291 292 private class JSONNull : JSONValue { 293 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 294 _stringBuilder.Append ("null"); 295 } 296 } 297 298 private class JSONBoolean : JSONValue { 299 private readonly bool value; 300 301 public JSONBoolean (bool _value) { 302 value = _value; 303 } 304 305 public bool GetBool () { 306 return value; 307 } 308 309 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 310 _stringBuilder.Append (value ? "true" : "false"); 311 } 312 } 313 314 private class JSONNumber : JSONValue { 315 private readonly double value; 316 317 public JSONNumber (double _value) { 318 value = _value; 319 } 320 321 public double GetDouble () { 322 return value; 323 } 324 325 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 326 _stringBuilder.Append (value.ToCultureInvariantString ()); 327 } 328 } 329 330 private class JSONString : JSONValue { 331 private readonly string value; 332 333 public JSONString (string _value) { 334 value = _value; 335 } 336 337 public string GetString () { 338 return value; 339 } 340 341 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 342 if (string.IsNullOrEmpty (value)) { 343 _stringBuilder.Append ("\"\""); 344 return; 345 } 346 347 int len = value.Length; 348 349 _stringBuilder.EnsureCapacity (_stringBuilder.Length + 2 * len); 350 351 _stringBuilder.Append ('"'); 352 353 foreach (char c in value) { 354 switch (c) { 355 case '\\': 356 case '"': 357 358 // case '/': 359 _stringBuilder.Append ('\\'); 360 _stringBuilder.Append (c); 361 break; 362 case '\b': 363 _stringBuilder.Append ("\\b"); 364 break; 365 case '\t': 366 _stringBuilder.Append ("\\t"); 367 break; 368 case '\n': 369 _stringBuilder.Append ("\\n"); 370 break; 371 case '\f': 372 _stringBuilder.Append ("\\f"); 373 break; 374 case '\r': 375 _stringBuilder.Append ("\\r"); 376 break; 377 default: 378 if (c < ' ') { 379 _stringBuilder.Append ("\\u"); 380 _stringBuilder.Append (((int) c).ToString ("X4")); 381 } else { 382 _stringBuilder.Append (c); 383 } 384 385 break; 386 } 387 } 388 389 _stringBuilder.Append ('"'); 390 } 391 392 } 393 394 private class JSONArray : JSONNode { 395 private readonly List<JSONNode> nodes = new List<JSONNode> (); 396 397 public void Add (JSONNode _node) { 398 nodes.Add (_node); 399 } 400 401 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 402 _stringBuilder.Append ("["); 403 if (_prettyPrint) { 404 _stringBuilder.Append ('\n'); 405 } 406 407 foreach (JSONNode n in nodes) { 408 if (_prettyPrint) { 409 _stringBuilder.Append (new string ('\t', _currentLevel + 1)); 410 } 411 412 n.ToString (_stringBuilder, _prettyPrint, _currentLevel + 1); 413 _stringBuilder.Append (","); 414 if (_prettyPrint) { 415 _stringBuilder.Append ('\n'); 416 } 417 } 418 419 if (nodes.Count > 0) { 420 _stringBuilder.Remove (_stringBuilder.Length - (_prettyPrint ? 2 : 1), 1); 421 } 422 423 if (_prettyPrint) { 424 _stringBuilder.Append (new string ('\t', _currentLevel)); 425 } 426 427 _stringBuilder.Append ("]"); 428 } 429 430 } 431 432 private class JSONObject : JSONNode { 433 private readonly Dictionary<string, JSONNode> nodes = new Dictionary<string, JSONNode> (); 434 435 public JSONNode this [string _name] => nodes [_name]; 436 437 public bool ContainsKey (string _name) { 438 return nodes.ContainsKey (_name); 439 } 440 441 public void Add (string _name, JSONNode _node) { 442 nodes.Add (_name, _node); 443 } 444 445 public override void ToString (StringBuilder _stringBuilder, bool _prettyPrint = false, int _currentLevel = 0) { 446 _stringBuilder.Append ("{"); 447 if (_prettyPrint) { 448 _stringBuilder.Append ('\n'); 449 } 450 451 foreach (KeyValuePair<string, JSONNode> kvp in nodes) { 452 if (_prettyPrint) { 453 _stringBuilder.Append (new string ('\t', _currentLevel + 1)); 454 } 455 456 _stringBuilder.Append ($"\"{kvp.Key}\":"); 457 if (_prettyPrint) { 458 _stringBuilder.Append (" "); 459 } 460 461 kvp.Value.ToString (_stringBuilder, _prettyPrint, _currentLevel + 1); 462 _stringBuilder.Append (","); 463 if (_prettyPrint) { 464 _stringBuilder.Append ('\n'); 465 } 466 } 467 468 if (nodes.Count > 0) { 469 _stringBuilder.Remove (_stringBuilder.Length - (_prettyPrint ? 2 : 1), 1); 470 } 471 472 if (_prettyPrint) { 473 _stringBuilder.Append (new string ('\t', _currentLevel)); 474 } 475 476 _stringBuilder.Append ("}"); 477 } 478 479 } 480 481 #endregion 482 271 483 } 272 484 }
Note:
See TracChangeset
for help on using the changeset viewer.