source: TFP-WebServer/WebServer/src/WebAPI/APIs/ServerState/GameStats.cs@ 453

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

Added GameStats and GamePrefs APIs
Fixed ServerInfo API JSON output

File size: 1.9 KB
Line 
1using JetBrains.Annotations;
2using Utf8Json;
3
4namespace Webserver.WebAPI.APIs.ServerState {
5 [UsedImplicitly]
6 public class GameStats : AbsRestApi {
7 private static readonly byte[] keyType = JsonWriter.GetEncodedPropertyNameWithBeginObject ("type");
8 private static readonly byte[] keyValue = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("value");
9
10 private int largestBuffer;
11
12 protected override void HandleRestGet (RequestContext _context) {
13 PrepareEnvelopedResult (out JsonWriter writer);
14
15 writer.EnsureCapacity (largestBuffer);
16 writer.WriteBeginObject ();
17
18 bool first = true;
19 foreach (EnumGameStats stat in EnumUtils.Values<EnumGameStats> ()) {
20 string name = stat.ToStringCached ();
21 global::GameStats.EnumType? type = global::GameStats.GetStatType (stat);
22
23 if (!first) {
24 writer.WriteValueSeparator ();
25 }
26
27 first = false;
28
29 writer.WritePropertyName (name);
30
31 writer.WriteRaw (keyType);
32 writer.WriteString (type.HasValue ? type.Value.ToStringCached () : "noType");
33
34 writer.WriteRaw (keyValue);
35 switch (type) {
36 case global::GameStats.EnumType.Int:
37 writer.WriteInt32 (global::GameStats.GetInt (stat));
38 break;
39 case global::GameStats.EnumType.Float:
40 writer.WriteSingle (global::GameStats.GetFloat (stat));
41 break;
42 case global::GameStats.EnumType.String:
43 writer.WriteString (global::GameStats.GetString (stat));
44 break;
45 case global::GameStats.EnumType.Bool:
46 writer.WriteBoolean (global::GameStats.GetBool (stat));
47 break;
48 default:
49 writer.WriteNull ();
50 break;
51 }
52
53 writer.WriteEndObject ();
54 }
55
56 writer.WriteEndObject ();
57
58 int bufferContentSize = writer.CurrentOffset + 128;
59 if (bufferContentSize > largestBuffer) {
60 largestBuffer = bufferContentSize;
61 }
62
63 SendEnvelopedResult (_context, ref writer);
64 }
65 }
66}
Note: See TracBrowser for help on using the repository browser.