1 | using JetBrains.Annotations;
|
---|
2 | using Utf8Json;
|
---|
3 | using Webserver.LiveData;
|
---|
4 |
|
---|
5 | namespace Webserver.WebAPI.APIs {
|
---|
6 | [UsedImplicitly]
|
---|
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) {
|
---|
20 | if (_context.Request.QueryString ["latestLine"] == null ||
|
---|
21 | !int.TryParse (_context.Request.QueryString ["latestLine"], out int latestLine)) {
|
---|
22 | latestLine = 0;
|
---|
23 | }
|
---|
24 |
|
---|
25 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
26 |
|
---|
27 | writer.WriteRaw (jsonKeyGameTime);
|
---|
28 |
|
---|
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 ();
|
---|
41 |
|
---|
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);
|
---|
53 |
|
---|
54 | writer.WriteEndObject ();
|
---|
55 |
|
---|
56 | SendEnvelopedResult (_context, ref writer);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override int DefaultPermissionLevel () => 2000;
|
---|
60 | }
|
---|
61 | }
|
---|