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