Changeset 455 for binary-improvements/MapRendering/API/GetPlayersOnline.cs
- Timestamp:
- Jul 31, 2023, 4:06:13 PM (16 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/MapRendering/API/GetPlayersOnline.cs
r454 r455 1 1 using System.Collections.Generic; 2 using AllocsFixes.JSON;3 2 using AllocsFixes.PersistentData; 3 using JetBrains.Annotations; 4 using Utf8Json; 4 5 using Webserver; 5 6 using Webserver.WebAPI; 6 7 7 8 namespace AllocsFixes.WebAPIs { 9 [UsedImplicitly] 8 10 public class GetPlayersOnline : AbsWebAPI { 11 private static readonly byte[] jsonKeySteamId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("steamid"); 12 private static readonly byte[] jsonKeyCrossPlatformId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("crossplatformid"); 13 private static readonly byte[] jsonKeyEntityId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("entityid"); 14 private static readonly byte[] jsonKeyIp = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("ip"); 15 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name"); 16 private static readonly byte[] jsonKeyOnline = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("online"); 17 private static readonly byte[] jsonKeyPosition = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("position"); 18 19 private static readonly byte[] jsonKeyLevel = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("level"); 20 private static readonly byte[] jsonKeyHealth = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("health"); 21 private static readonly byte[] jsonKeyStamina = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("stamina"); 22 private static readonly byte[] jsonKeyZombieKills = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("zombiekills"); 23 private static readonly byte[] jsonKeyPlayerKills = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("playerkills"); 24 private static readonly byte[] jsonKeyPlayerDeaths = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("playerdeaths"); 25 private static readonly byte[] jsonKeyScore = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("score"); 26 27 private static readonly byte[] jsonKeyTotalPlaytime = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("totalplaytime"); 28 private static readonly byte[] jsonKeyLastOnline = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lastonline"); 29 private static readonly byte[] jsonKeyPing = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("ping"); 30 9 31 public override void HandleRequest (RequestContext _context) { 10 JSONArray players = new JSONArray (); 32 JsonWriter writer = new JsonWriter (); 33 34 writer.WriteBeginArray (); 35 36 bool first = true; 11 37 12 38 World w = GameManager.Instance.World; … … 15 41 Player player = PersistentContainer.Instance.Players.GetByInternalId (ci.InternalId); 16 42 17 JSONObject pos = new JSONObject (); 18 pos.Add ("x", new JSONNumber ((int) current.Value.GetPosition ().x)); 19 pos.Add ("y", new JSONNumber ((int) current.Value.GetPosition ().y)); 20 pos.Add ("z", new JSONNumber ((int) current.Value.GetPosition ().z)); 43 if (!first) { 44 writer.WriteValueSeparator (); 45 } 21 46 22 JSONObject p = new JSONObject (); 23 p.Add ("steamid", new JSONString (ci.PlatformId.CombinedString)); 24 p.Add ("crossplatformid", new JSONString (ci.CrossplatformId?.CombinedString ?? "")); 25 p.Add ("entityid", new JSONNumber (ci.entityId)); 26 p.Add ("ip", new JSONString (ci.ip)); 27 p.Add ("name", new JSONString (current.Value.EntityName)); 28 p.Add ("online", new JSONBoolean (true)); 29 p.Add ("position", pos); 47 first = false; 48 49 writer.WriteRaw (jsonKeySteamId); 50 writer.WriteString (ci.PlatformId.CombinedString); 51 52 writer.WriteRaw (jsonKeyCrossPlatformId); 53 writer.WriteString (ci.CrossplatformId?.CombinedString ?? ""); 54 55 writer.WriteRaw (jsonKeyEntityId); 56 writer.WriteInt32 (ci.entityId); 57 58 writer.WriteRaw (jsonKeyIp); 59 writer.WriteString (ci.ip); 60 61 writer.WriteRaw (jsonKeyName); 62 writer.WriteString (current.Value.EntityName); 63 64 writer.WriteRaw (jsonKeyOnline); 65 writer.WriteBoolean (true); 66 67 writer.WriteRaw (jsonKeyPosition); 68 JsonCommons.WriteVector3I (ref writer, new Vector3i (current.Value.GetPosition ())); 69 70 writer.WriteRaw (jsonKeyLevel); 71 writer.WriteSingle (player?.Level ?? -1); 72 73 writer.WriteRaw (jsonKeyHealth); 74 writer.WriteInt32 (current.Value.Health); 75 76 writer.WriteRaw (jsonKeyStamina); 77 writer.WriteSingle (current.Value.Stamina); 78 79 writer.WriteRaw (jsonKeyZombieKills); 80 writer.WriteInt32 (current.Value.KilledZombies); 81 82 writer.WriteRaw (jsonKeyPlayerKills); 83 writer.WriteInt32 (current.Value.KilledPlayers); 84 85 writer.WriteRaw (jsonKeyPlayerDeaths); 86 writer.WriteInt32 (current.Value.Died); 87 88 writer.WriteRaw (jsonKeyScore); 89 writer.WriteInt32 (current.Value.Score); 30 90 31 p.Add ("level", new JSONNumber (player?.Level ?? -1)); 32 p.Add ("health", new JSONNumber (current.Value.Health)); 33 p.Add ("stamina", new JSONNumber (current.Value.Stamina)); 34 p.Add ("zombiekills", new JSONNumber (current.Value.KilledZombies)); 35 p.Add ("playerkills", new JSONNumber (current.Value.KilledPlayers)); 36 p.Add ("playerdeaths", new JSONNumber (current.Value.Died)); 37 p.Add ("score", new JSONNumber (current.Value.Score)); 91 writer.WriteRaw (jsonKeyTotalPlaytime); 92 writer.WriteInt64 (player?.TotalPlayTime ?? -1); 93 94 writer.WriteRaw (jsonKeyLastOnline); 95 writer.WriteString (player != null ? player.LastOnline.ToString ("s") : string.Empty); 38 96 39 p.Add ("totalplaytime", new JSONNumber (player?.TotalPlayTime ?? -1)); 40 p.Add ("lastonline", new JSONString (player != null ? player.LastOnline.ToString ("s") : string.Empty)); 41 p.Add ("ping", new JSONNumber (ci.ping)); 42 43 players.Add (p); 97 writer.WriteRaw (jsonKeyPing); 98 writer.WriteInt32 (ci.ping); 99 100 writer.WriteEndObject (); 44 101 } 45 46 LegacyApiHelper.WriteJSON (_context.Response, players); 102 103 writer.WriteEndArray (); 104 105 WebUtils.WriteJsonData (_context.Response, ref writer); 47 106 } 48 107 }
Note:
See TracChangeset
for help on using the changeset viewer.