source: TFP-WebServer/WebServer/src/WebAPI/APIs/ServerState/ServerStats.cs@ 483

Last change on this file since 483 was 437, checked in by alloc, 18 months ago

Added GameStats and GamePrefs APIs
Fixed ServerInfo API JSON output

File size: 1.5 KB
Line 
1using JetBrains.Annotations;
2using Utf8Json;
3using Webserver.LiveData;
4using Webserver.Permissions;
5
6namespace Webserver.WebAPI.APIs.ServerState {
7 [UsedImplicitly]
8 public class ServerStats : 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
14
15 protected override void HandleRestGet (RequestContext _context) {
16 PrepareEnvelopedResult (out JsonWriter writer);
17
18 writer.WriteRaw (jsonKeyGameTime);
19 (int days, int hours, int minutes) = GameUtils.WorldTimeToElements (GameManager.Instance.World.worldTime);
20 JsonCommons.WriteGameTimeObject (ref writer, days, hours, minutes);
21
22 writer.WriteRaw (jsonKeyPlayers);
23 writer.WriteInt32 (GameManager.Instance.World.Players.Count);
24
25 writer.WriteRaw (jsonKeyHostiles);
26 writer.WriteInt32 (Hostiles.Instance.GetCount ());
27
28 writer.WriteRaw (jsonKeyAnimals);
29 writer.WriteInt32 (Animals.Instance.GetCount ());
30
31 writer.WriteEndObject ();
32
33 SendEnvelopedResult (_context, ref writer);
34 }
35
36 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest;
37 }
38}
Note: See TracBrowser for help on using the repository browser.