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