source: binary-improvements/MapRendering/API/GetPlayersOnline.cs@ 457

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

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 4.5 KB
RevLine 
[325]1using System.Collections.Generic;
[233]2using AllocsFixes.PersistentData;
[455]3using JetBrains.Annotations;
4using Utf8Json;
[454]5using Webserver;
6using Webserver.WebAPI;
[230]7
[454]8namespace AllocsFixes.WebAPIs {
[455]9 [UsedImplicitly]
[454]10 public class GetPlayersOnline : AbsWebAPI {
[455]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
[454]31 public override void HandleRequest (RequestContext _context) {
[455]32 JsonWriter writer = new JsonWriter ();
33
34 writer.WriteBeginArray ();
35
36 bool first = true;
[230]37
38 World w = GameManager.Instance.World;
39 foreach (KeyValuePair<int, EntityPlayer> current in w.Players.dict) {
[324]40 ClientInfo ci = ConnectionManager.Instance.Clients.ForEntityId (current.Key);
[446]41 Player player = PersistentContainer.Instance.Players.GetByInternalId (ci.InternalId);
[230]42
[455]43 if (!first) {
44 writer.WriteValueSeparator ();
45 }
[230]46
[455]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);
[230]90
[455]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);
[233]96
[455]97 writer.WriteRaw (jsonKeyPing);
98 writer.WriteInt32 (ci.ping);
99
100 writer.WriteEndObject ();
[230]101 }
[455]102
103 writer.WriteEndArray ();
104
105 WebUtils.WriteJsonData (_context.Response, ref writer);
[230]106 }
107 }
[325]108}
Note: See TracBrowser for help on using the repository browser.