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

Last change on this file since 455 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
RevLine 
[251]1using AllocsFixes.LiveData;
[455]2using JetBrains.Annotations;
3using Utf8Json;
[454]4using Webserver;
5using Webserver.WebAPI;
[251]6
[454]7namespace AllocsFixes.WebAPIs {
[455]8 [UsedImplicitly]
[454]9 public class GetWebUIUpdates : AbsWebAPI {
[455]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
[454]17 public override void HandleRequest (RequestContext _context) {
[251]18 int latestLine;
[454]19 if (_context.Request.QueryString ["latestLine"] == null ||
20 !int.TryParse (_context.Request.QueryString ["latestLine"], out latestLine)) {
[251]21 latestLine = 0;
22 }
23
[455]24 JsonWriter writer = new JsonWriter ();
[251]25
[455]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);
[251]45 }
[279]46
47 public override int DefaultPermissionLevel () {
48 return 2000;
49 }
[251]50 }
[325]51}
Note: See TracBrowser for help on using the repository browser.