source: binary-improvements/MapRendering/API/GetWebUIUpdates.cs@ 458

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

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 1.9 KB
Line 
1using AllocsFixes.LiveData;
2using JetBrains.Annotations;
3using Utf8Json;
4using Webserver;
5using Webserver.WebAPI;
6
7namespace AllocsFixes.WebAPIs {
8 [UsedImplicitly]
9 public class GetWebUIUpdates : AbsWebAPI {
10 private static readonly byte[] jsonKeyGameTime = JsonWriter.GetEncodedPropertyNameWithBeginObject ("gametime");
11
12 private static readonly byte[] jsonKeyPlayers = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("players");
13 private static readonly byte[] jsonKeyHostiles = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("hostiles");
14 private static readonly byte[] jsonKeyAnimals = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("animals");
15 private static readonly byte[] jsonKeyNewLogs = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("newlogs");
16
17 public override void HandleRequest (RequestContext _context) {
18 int latestLine;
19 if (_context.Request.QueryString ["latestLine"] == null ||
20 !int.TryParse (_context.Request.QueryString ["latestLine"], out latestLine)) {
21 latestLine = 0;
22 }
23
24 JsonWriter writer = new JsonWriter ();
25
26 writer.WriteRaw (jsonKeyGameTime);
27 (int days, int hours, int minutes) = GameUtils.WorldTimeToElements (GameManager.Instance.World.worldTime);
28 JsonCommons.WriteGameTimeObject (ref writer, days, hours, minutes);
29
30 writer.WriteRaw (jsonKeyPlayers);
31 writer.WriteInt32 (GameManager.Instance.World.Players.Count);
32
33 writer.WriteRaw (jsonKeyHostiles);
34 writer.WriteInt32 (Hostiles.Instance.GetCount ());
35
36 writer.WriteRaw (jsonKeyAnimals);
37 writer.WriteInt32 (Animals.Instance.GetCount ());
38
39 writer.WriteRaw (jsonKeyNewLogs);
40 writer.WriteInt32 (LogBuffer.Instance.LatestLine - latestLine);
41
42 writer.WriteEndObject ();
43
44 WebUtils.WriteJsonData (_context.Response, ref writer);
45 }
46
47 public override int DefaultPermissionLevel () {
48 return 2000;
49 }
50 }
51}
Note: See TracBrowser for help on using the repository browser.