source: binary-improvements/MapRendering/Web/API/GetPlayerList.cs@ 412

Last change on this file since 412 was 369, checked in by alloc, 3 years ago

Preparations for A20 release
Changes usage of "SteamID" to "UserID" in console commands
Also changes a bunch of the WebAPI stuff to show / use UserIDs

File size: 8.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Net;
5using System.Text.RegularExpressions;
6using AllocsFixes.JSON;
7using AllocsFixes.PersistentData;
8
9namespace 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#if ENABLE_PROFILER
15 private static readonly UnityEngine.Profiling.CustomSampler jsonSerializeSampler = UnityEngine.Profiling.CustomSampler.Create ("JSON_Build");
16#endif
17
18 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
19 int _permissionLevel) {
20 AdminTools admTools = GameManager.Instance.adminTools;
21 PlatformUserIdentifierAbs userId = _user?.UserId;
22
23 bool bViewAll = WebConnection.CanViewAllPlayers (_permissionLevel);
24
25 // 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)
26
27 int rowsPerPage = 25;
28 if (_req.QueryString ["rowsperpage"] != null) {
29 int.TryParse (_req.QueryString ["rowsperpage"], out rowsPerPage);
30 }
31
32 int page = 0;
33 if (_req.QueryString ["page"] != null) {
34 int.TryParse (_req.QueryString ["page"], out page);
35 }
36
37 int firstEntry = page * rowsPerPage;
38
39 Players playersList = PersistentContainer.Instance.Players;
40
41
42 List<JSONObject> playerList = new List<JSONObject> ();
43
44#if ENABLE_PROFILER
45 jsonSerializeSampler.Begin ();
46#endif
47
48 foreach (KeyValuePair<PlatformUserIdentifierAbs, Player> kvp in playersList.Dict) {
49 Player p = kvp.Value;
50
51 if (bViewAll || p.PlatformId.Equals (userId)) {
52 JSONObject pos = new JSONObject ();
53 pos.Add ("x", new JSONNumber (p.LastPosition.x));
54 pos.Add ("y", new JSONNumber (p.LastPosition.y));
55 pos.Add ("z", new JSONNumber (p.LastPosition.z));
56
57 JSONObject pJson = new JSONObject ();
58 pJson.Add ("steamid", new JSONString (kvp.Key.CombinedString));
59 pJson.Add ("entityid", new JSONNumber (p.EntityID));
60 pJson.Add ("ip", new JSONString (p.IP));
61 pJson.Add ("name", new JSONString (p.Name));
62 pJson.Add ("online", new JSONBoolean (p.IsOnline));
63 pJson.Add ("position", pos);
64
65 pJson.Add ("totalplaytime", new JSONNumber (p.TotalPlayTime));
66 pJson.Add ("lastonline",
67 new JSONString (p.LastOnline.ToUniversalTime ().ToString ("yyyy-MM-ddTHH:mm:ssZ")));
68 pJson.Add ("ping", new JSONNumber (p.IsOnline ? p.ClientInfo.ping : -1));
69
70 JSONBoolean banned = admTools != null ? new JSONBoolean (admTools.IsBanned (kvp.Key, out _, out _)) : new JSONBoolean (false);
71
72 pJson.Add ("banned", banned);
73
74 playerList.Add (pJson);
75 }
76 }
77
78#if ENABLE_PROFILER
79 jsonSerializeSampler.End ();
80#endif
81
82 IEnumerable<JSONObject> list = playerList;
83
84 foreach (string key in _req.QueryString.AllKeys) {
85 if (!string.IsNullOrEmpty (key) && key.StartsWith ("filter[")) {
86 string filterCol = key.Substring (key.IndexOf ('[') + 1);
87 filterCol = filterCol.Substring (0, filterCol.Length - 1);
88 string filterVal = _req.QueryString.Get (key).Trim ();
89
90 list = ExecuteFilter (list, filterCol, filterVal);
91 }
92 }
93
94 int totalAfterFilter = list.Count ();
95
96 foreach (string key in _req.QueryString.AllKeys) {
97 if (!string.IsNullOrEmpty (key) && key.StartsWith ("sort[")) {
98 string sortCol = key.Substring (key.IndexOf ('[') + 1);
99 sortCol = sortCol.Substring (0, sortCol.Length - 1);
100 string sortVal = _req.QueryString.Get (key);
101
102 list = ExecuteSort (list, sortCol, sortVal == "0");
103 }
104 }
105
106 list = list.Skip (firstEntry);
107 list = list.Take (rowsPerPage);
108
109
110 JSONArray playersJsResult = new JSONArray ();
111 foreach (JSONObject jsO in list) {
112 playersJsResult.Add (jsO);
113 }
114
115 JSONObject result = new JSONObject ();
116 result.Add ("total", new JSONNumber (totalAfterFilter));
117 result.Add ("totalUnfiltered", new JSONNumber (playerList.Count));
118 result.Add ("firstResult", new JSONNumber (firstEntry));
119 result.Add ("players", playersJsResult);
120
121 WriteJSON (_resp, result);
122 }
123
124 private IEnumerable<JSONObject> ExecuteFilter (IEnumerable<JSONObject> _list, string _filterCol,
125 string _filterVal) {
126 if (!_list.Any()) {
127 return _list;
128 }
129
130 if (_list.First ().ContainsKey (_filterCol)) {
131 Type colType = _list.First () [_filterCol].GetType ();
132 if (colType == typeof (JSONNumber)) {
133 return ExecuteNumberFilter (_list, _filterCol, _filterVal);
134 }
135
136 if (colType == typeof (JSONBoolean)) {
137 bool value = StringParsers.ParseBool (_filterVal);
138 return _list.Where (_line => ((JSONBoolean) _line [_filterCol]).GetBool () == value);
139 }
140
141 if (colType == typeof (JSONString)) {
142 // regex-match whole ^string$, replace * by .*, ? by .?, + by .+
143 _filterVal = _filterVal.Replace ("*", ".*").Replace ("?", ".?").Replace ("+", ".+");
144 _filterVal = "^" + _filterVal + "$";
145
146 //Log.Out ("GetPlayerList: Filter on String with Regex '" + _filterVal + "'");
147 Regex matcher = new Regex (_filterVal, RegexOptions.IgnoreCase);
148 return _list.Where (_line => matcher.IsMatch (((JSONString) _line [_filterCol]).GetString ()));
149 }
150 }
151
152 return _list;
153 }
154
155
156 private IEnumerable<JSONObject> ExecuteNumberFilter (IEnumerable<JSONObject> _list, string _filterCol,
157 string _filterVal) {
158 // allow value (exact match), =, ==, >=, >, <=, <
159 Match filterMatch = numberFilterMatcher.Match (_filterVal);
160 if (filterMatch.Success) {
161 double value = StringParsers.ParseDouble (filterMatch.Groups [2].Value);
162 NumberMatchType matchType;
163 double epsilon = value / 100000;
164 switch (filterMatch.Groups [1].Value) {
165 case "":
166 case "=":
167 case "==":
168 matchType = NumberMatchType.Equal;
169 break;
170 case ">":
171 matchType = NumberMatchType.Greater;
172 break;
173 case ">=":
174 case "=>":
175 matchType = NumberMatchType.GreaterEqual;
176 break;
177 case "<":
178 matchType = NumberMatchType.Lesser;
179 break;
180 case "<=":
181 case "=<":
182 matchType = NumberMatchType.LesserEqual;
183 break;
184 default:
185 matchType = NumberMatchType.Equal;
186 break;
187 }
188
189 return _list.Where (delegate (JSONObject _line) {
190 double objVal = ((JSONNumber) _line [_filterCol]).GetDouble ();
191 switch (matchType) {
192 case NumberMatchType.Greater:
193 return objVal > value;
194 case NumberMatchType.GreaterEqual:
195 return objVal >= value;
196 case NumberMatchType.Lesser:
197 return objVal < value;
198 case NumberMatchType.LesserEqual:
199 return objVal <= value;
200 case NumberMatchType.Equal:
201 default:
202 return NearlyEqual (objVal, value, epsilon);
203 }
204 });
205 }
206
207 Log.Out ("GetPlayerList: ignoring invalid filter for number-column '{0}': '{1}'", _filterCol, _filterVal);
208 return _list;
209 }
210
211
212 private IEnumerable<JSONObject> ExecuteSort (IEnumerable<JSONObject> _list, string _sortCol, bool _ascending) {
213 if (_list.Count () == 0) {
214 return _list;
215 }
216
217 if (_list.First ().ContainsKey (_sortCol)) {
218 Type colType = _list.First () [_sortCol].GetType ();
219 if (colType == typeof (JSONNumber)) {
220 if (_ascending) {
221 return _list.OrderBy (_line => ((JSONNumber) _line [_sortCol]).GetDouble ());
222 }
223
224 return _list.OrderByDescending (_line => ((JSONNumber) _line [_sortCol]).GetDouble ());
225 }
226
227 if (colType == typeof (JSONBoolean)) {
228 if (_ascending) {
229 return _list.OrderBy (_line => ((JSONBoolean) _line [_sortCol]).GetBool ());
230 }
231
232 return _list.OrderByDescending (_line => ((JSONBoolean) _line [_sortCol]).GetBool ());
233 }
234
235 if (_ascending) {
236 return _list.OrderBy (_line => _line [_sortCol].ToString ());
237 }
238
239 return _list.OrderByDescending (_line => _line [_sortCol].ToString ());
240 }
241
242 return _list;
243 }
244
245
246 private bool NearlyEqual (double _a, double _b, double _epsilon) {
247 double absA = Math.Abs (_a);
248 double absB = Math.Abs (_b);
249 double diff = Math.Abs (_a - _b);
250
251 if (_a == _b) {
252 return true;
253 }
254
255 if (_a == 0 || _b == 0 || diff < double.Epsilon) {
256 return diff < _epsilon;
257 }
258
259 return diff / (absA + absB) < _epsilon;
260 }
261
262 private enum NumberMatchType {
263 Equal,
264 Greater,
265 GreaterEqual,
266 Lesser,
267 LesserEqual
268 }
269 }
270}
Note: See TracBrowser for help on using the repository browser.