source: binary-improvements/MapRendering/API/GetPlayersLocation.cs@ 455

Last change on this file since 455 was 455, checked in by alloc, 16 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 2.5 KB
Line 
1using System.Collections.Generic;
2using AllocsFixes.PersistentData;
3using JetBrains.Annotations;
4using Utf8Json;
5using Webserver;
6using Webserver.Permissions;
7using Webserver.WebAPI;
8
9namespace AllocsFixes.WebAPIs {
10 [UsedImplicitly]
11 public class GetPlayersLocation : AbsWebAPI {
12 private static readonly byte[] jsonKeySteamId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("steamid");
13 private static readonly byte[] jsonKeyCrossPlatformId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("crossplatformid");
14 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name");
15 private static readonly byte[] jsonKeyOnline = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("online");
16 private static readonly byte[] jsonKeyPosition = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("position");
17
18 public override void HandleRequest (RequestContext _context) {
19 AdminTools admTools = GameManager.Instance.adminTools;
20 PlatformUserIdentifierAbs userId = _context.Connection?.UserId;
21
22 bool listOffline = false;
23 if (_context.Request.QueryString ["offline"] != null) {
24 bool.TryParse (_context.Request.QueryString ["offline"], out listOffline);
25 }
26
27 bool bViewAll = PermissionUtils.CanViewAllPlayers (_context.PermissionLevel);
28
29 JsonWriter writer = new JsonWriter ();
30
31 writer.WriteBeginArray ();
32
33 bool first = true;
34
35 Players playersList = PersistentContainer.Instance.Players;
36
37 foreach (KeyValuePair<PlatformUserIdentifierAbs, Player> kvp in playersList.Dict) {
38 if (admTools != null) {
39 if (admTools.Blacklist.IsBanned (kvp.Key, out _, out _)) {
40 continue;
41 }
42 }
43
44 Player p = kvp.Value;
45
46 if (listOffline || p.IsOnline) {
47 if (bViewAll || p.InternalId.Equals (userId)) {
48
49 if (!first) {
50 writer.WriteValueSeparator ();
51 }
52
53 first = false;
54
55 writer.WriteRaw (jsonKeySteamId);
56 writer.WriteString (kvp.Value.PlatformId.CombinedString);
57
58 writer.WriteRaw (jsonKeyCrossPlatformId);
59 writer.WriteString (kvp.Value.CrossPlatformId?.CombinedString ?? "");
60
61 writer.WriteRaw (jsonKeyName);
62 writer.WriteString (p.Name);
63
64 writer.WriteRaw (jsonKeyOnline);
65 writer.WriteBoolean (p.IsOnline);
66
67 writer.WriteRaw (jsonKeyPosition);
68 JsonCommons.WriteVector3I (ref writer, p.LastPosition);
69
70 writer.WriteEndObject ();
71 }
72 }
73 }
74
75 writer.WriteEndArray ();
76
77 WebUtils.WriteJsonData (_context.Response, ref writer);
78 }
79 }
80}
Note: See TracBrowser for help on using the repository browser.