source: binary-improvements2/WebServer/src/WebAPI/APIs/WebUiUpdates.cs@ 415

Last change on this file since 415 was 408, checked in by alloc, 21 months ago

Few pieces of code cleanup

File size: 2.4 KB
RevLine 
[391]1using JetBrains.Annotations;
[402]2using Utf8Json;
3using Webserver.LiveData;
[391]4
[402]5namespace Webserver.WebAPI.APIs {
[391]6 [UsedImplicitly]
[402]7 public class WebUiUpdates : AbsRestApi {
8 private static readonly byte[] jsonKeyGameTime = JsonWriter.GetEncodedPropertyNameWithBeginObject ("gameTime");
9 private static readonly byte[] jsonKeyPlayers = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("players");
10 private static readonly byte[] jsonKeyHostiles = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("hostiles");
11 private static readonly byte[] jsonKeyAnimals = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("animals");
12 private static readonly byte[] jsonKeyNewLogs = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("newLogs");
13
14 private static readonly byte[] jsonKeyDays = JsonWriter.GetEncodedPropertyNameWithBeginObject ("days");
15 private static readonly byte[] jsonKeyHours = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("hours");
16 private static readonly byte[] jsonKeyMinutes = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("minutes");
17
18
19 protected override void HandleRestGet (RequestContext _context) {
[391]20 if (_context.Request.QueryString ["latestLine"] == null ||
21 !int.TryParse (_context.Request.QueryString ["latestLine"], out int latestLine)) {
22 latestLine = 0;
23 }
[402]24
25 PrepareEnvelopedResult (out JsonWriter writer);
26
27 writer.WriteRaw (jsonKeyGameTime);
[391]28
[402]29 (int days, int hours, int minutes) = GameUtils.WorldTimeToElements (GameManager.Instance.World.worldTime);
30
31 writer.WriteRaw (jsonKeyDays);
32 writer.WriteInt32 (days);
33
34 writer.WriteRaw (jsonKeyHours);
35 writer.WriteInt32 (hours);
36
37 writer.WriteRaw (jsonKeyMinutes);
38 writer.WriteInt32 (minutes);
39
40 writer.WriteEndObject ();
[391]41
[402]42 writer.WriteRaw (jsonKeyPlayers);
43 writer.WriteInt32 (GameManager.Instance.World.Players.Count);
44
45 writer.WriteRaw (jsonKeyHostiles);
46 writer.WriteInt32 (Hostiles.Instance.GetCount ());
47
48 writer.WriteRaw (jsonKeyAnimals);
49 writer.WriteInt32 (Animals.Instance.GetCount ());
50
51 writer.WriteRaw (jsonKeyNewLogs);
52 writer.WriteInt32 (LogBuffer.Instance.LatestLine - latestLine);
[391]53
[402]54 writer.WriteEndObject ();
[391]55
[402]56 SendEnvelopedResult (_context, ref writer);
[391]57 }
58
[408]59 public override int DefaultPermissionLevel () => 2000;
[391]60 }
61}
Note: See TracBrowser for help on using the repository browser.