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

Last change on this file since 475 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
RevLine 
[332]1using System.Collections.Generic;
[253]2using AllocsFixes.PersistentData;
[455]3using JetBrains.Annotations;
4using Utf8Json;
[454]5using Webserver;
6using Webserver.Permissions;
7using Webserver.WebAPI;
[253]8
[454]9namespace AllocsFixes.WebAPIs {
[455]10 [UsedImplicitly]
[454]11 public class GetPlayersLocation : AbsWebAPI {
[455]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
[454]18 public override void HandleRequest (RequestContext _context) {
[325]19 AdminTools admTools = GameManager.Instance.adminTools;
[454]20 PlatformUserIdentifierAbs userId = _context.Connection?.UserId;
[253]21
[315]22 bool listOffline = false;
[454]23 if (_context.Request.QueryString ["offline"] != null) {
24 bool.TryParse (_context.Request.QueryString ["offline"], out listOffline);
[315]25 }
26
[454]27 bool bViewAll = PermissionUtils.CanViewAllPlayers (_context.PermissionLevel);
[253]28
[455]29 JsonWriter writer = new JsonWriter ();
30
31 writer.WriteBeginArray ();
32
33 bool first = true;
[253]34
35 Players playersList = PersistentContainer.Instance.Players;
36
[369]37 foreach (KeyValuePair<PlatformUserIdentifierAbs, Player> kvp in playersList.Dict) {
[325]38 if (admTools != null) {
[420]39 if (admTools.Blacklist.IsBanned (kvp.Key, out _, out _)) {
[325]40 continue;
41 }
42 }
[253]43
[332]44 Player p = kvp.Value;
[253]45
[315]46 if (listOffline || p.IsOnline) {
[446]47 if (bViewAll || p.InternalId.Equals (userId)) {
[253]48
[455]49 if (!first) {
50 writer.WriteValueSeparator ();
51 }
[253]52
[455]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 ();
[325]71 }
[315]72 }
[325]73 }
[455]74
75 writer.WriteEndArray ();
76
77 WebUtils.WriteJsonData (_context.Response, ref writer);
[253]78 }
79 }
[325]80}
Note: See TracBrowser for help on using the repository browser.