| 1 | using AllocsFixes.LiveData;
|
|---|
| 2 | using JetBrains.Annotations;
|
|---|
| 3 | using Utf8Json;
|
|---|
| 4 | using Webserver;
|
|---|
| 5 | using Webserver.WebAPI;
|
|---|
| 6 |
|
|---|
| 7 | namespace 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 | }
|
|---|