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

Last change on this file since 402 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 2.4 KB
Line 
1using JetBrains.Annotations;
2using Utf8Json;
3using Webserver.LiveData;
4
5namespace 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 () {
60 return 2000;
61 }
62 }
63}
Note: See TracBrowser for help on using the repository browser.