source: TFP-WebServer/WebServer/src/WebAPI/APIs/WorldState/Player.cs@ 477

Last change on this file since 477 was 463, checked in by alloc, 15 months ago

21.1.16.0 release
Completed OpenAPI specs
Add support to path handlers to register OpenAPI specs
Fixed ItemIconHandler throwing error when requested path contains no dot

File size: 8.0 KB
RevLine 
[425]1using System;
2using System.Net;
3using JetBrains.Annotations;
4using Utf8Json;
5using Webserver.Permissions;
6
[434]7namespace Webserver.WebAPI.APIs.WorldState {
[425]8 [UsedImplicitly]
9 public class Player : AbsRestApi {
10 private static readonly byte[] jsonPlayersKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("players");
11
12 protected override void HandleRestGet (RequestContext _context) {
13 string id = _context.RequestPath;
14 int permissionLevel = _context.PermissionLevel;
15
16 bool bViewAll = PermissionUtils.CanViewAllPlayers (permissionLevel);
17 PlatformUserIdentifierAbs nativeUserId = _context.Connection?.UserId;
18
19 // string qs;
20 // if ((qs = _context.Request.QueryString ["count"]) == null || !int.TryParse (qs, out int count)) {
21 // count = 50;
22 // }
23
24 PrepareEnvelopedResult (out JsonWriter writer);
25
26 writer.WriteRaw (jsonPlayersKey);
27 writer.WriteBeginArray ();
28
29 ClientInfo ci;
30 int written = 0;
31
32 if (string.IsNullOrEmpty (id)) {
33 for (int i = 0; i < ConnectionManager.Instance.Clients.List.Count; i++) {
34 ClientInfo clientInfo = ConnectionManager.Instance.Clients.List [i];
35
36 writePlayerJson (ref writer, ref written, clientInfo.PlatformId, bViewAll, nativeUserId);
37 }
38 } else if (int.TryParse (id, out int entityId) && (ci = ConnectionManager.Instance.Clients.ForEntityId (entityId)) != null) {
39 // TODO: Allow finding offline players, also for search other than by EntityId
40 writePlayerJson (ref writer, ref written, ci.PlatformId, bViewAll, nativeUserId);
41 } else {
42 writer.WriteEndArray ();
43 writer.WriteEndObject ();
44 SendEnvelopedResult (_context, ref writer, HttpStatusCode.NotFound);
45 return;
46 }
47
48 writer.WriteEndArray ();
49 writer.WriteEndObject ();
50
51 SendEnvelopedResult (_context, ref writer);
52 }
53
54
55#region JSON keys for player result
56
57 private static readonly byte[] jsonEntityIdKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("entityId");
58 private static readonly byte[] jsonNameKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name");
59 private static readonly byte[] jsonPlatformIdKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("platformId");
60 private static readonly byte[] jsonCrossplatformIdKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("crossplatformId");
61 private static readonly byte[] jsonTotalPlayTimeKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("totalPlayTimeSeconds");
62 private static readonly byte[] jsonLastOnlineKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("lastOnline");
63 private static readonly byte[] jsonOnlineKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("online");
64 private static readonly byte[] jsonIpKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("ip");
65 private static readonly byte[] jsonPingKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("ping");
66 private static readonly byte[] jsonPositionKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("position");
67 private static readonly byte[] jsonLevelKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("level");
68 private static readonly byte[] jsonHealthKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("health");
69 private static readonly byte[] jsonStaminaKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("stamina");
70 private static readonly byte[] jsonScoreKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("score");
71 private static readonly byte[] jsonDeathsKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("deaths");
72
73 private static readonly byte[] jsonKillsKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("kills");
74 private static readonly byte[] jsonKillsZombiesKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("zombies");
75 private static readonly byte[] jsonKillsPlayersKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("players");
76
77 private static readonly byte[] jsonBannedKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("banned");
78 private static readonly byte[] jsonBanActiveKey = JsonWriter.GetEncodedPropertyNameWithBeginObject ("banActive");
79 private static readonly byte[] jsonBanReasonKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("reason");
80 private static readonly byte[] jsonBanUntilKey = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("until");
81
82#endregion
83
84
85 private void writePlayerJson (ref JsonWriter _writer, ref int _written, PlatformUserIdentifierAbs _nativeUserId,
86 bool _allowViewAll, PlatformUserIdentifierAbs _requesterNativeUserId) {
87
88 if (!_allowViewAll && (_requesterNativeUserId == null || !_requesterNativeUserId.Equals (_nativeUserId) )) {
89 return;
90 }
91
92 ClientInfo ci = ConnectionManager.Instance.Clients.ForUserId (_nativeUserId);
93 if (ci == null) {
[459]94 Log.Warning ($"[Web] Player.GET: ClientInfo null");
[425]95 return;
96 }
97
98 int entityId = ci.entityId;
99 GameManager.Instance.World.Players.dict.TryGetValue (entityId, out EntityPlayer entity);
100
101 if (entity == null) {
[459]102 Log.Warning ($"[Web] Player.GET: EntityPlayer null");
[425]103 return;
104 }
105
106 if (_written > 0) {
107 _writer.WriteValueSeparator ();
108 }
109 _written++;
110
111 bool online = true; // TODO
112
113 _writer.WriteRaw (jsonEntityIdKey);
114 _writer.WriteInt32 (entityId);
115
116 _writer.WriteRaw (jsonNameKey);
117 _writer.WriteString (ci.playerName);
118
119 _writer.WriteRaw (jsonPlatformIdKey);
120 JsonCommons.WritePlatformUserIdentifier (ref _writer, _nativeUserId);
121
122 _writer.WriteRaw (jsonCrossplatformIdKey);
123 JsonCommons.WritePlatformUserIdentifier (ref _writer, ci.CrossplatformId);
124
125 _writer.WriteRaw (jsonTotalPlayTimeKey);
126 //_writer.WriteLong (player.TotalPlayTime); TODO
127 _writer.WriteNull ();
128
129 _writer.WriteRaw (jsonLastOnlineKey);
130 //JsonCommons.WriteDateTime (ref _writer, player.LastOnline); TODO
131 _writer.WriteNull ();
132
133 _writer.WriteRaw (jsonOnlineKey);
134 _writer.WriteBoolean (online);
135
136 _writer.WriteRaw (jsonIpKey);
137 if (online) {
138 _writer.WriteString (ci.ip);
139 // TODO: Possibly show last used IP?
140 } else {
141 _writer.WriteNull ();
142 }
143
144 _writer.WriteRaw (jsonPingKey);
145 if (online) {
146 _writer.WriteInt32 (ci.ping);
147 } else {
148 _writer.WriteNull ();
149 }
150
151 _writer.WriteRaw (jsonPositionKey);
152 if (online) {
[444]153 JsonCommons.WriteVector3 (ref _writer, entity.GetPosition ());
[425]154 // TODO: Possibly show last position?
155 } else {
156 _writer.WriteNull ();
157 }
158
159 _writer.WriteRaw (jsonLevelKey);
160 _writer.WriteNull (); // TODO
161
162 _writer.WriteRaw (jsonHealthKey);
163 _writer.WriteInt32 (entity.Health);
164
165 _writer.WriteRaw (jsonStaminaKey);
166 _writer.WriteSingle (entity.Stamina);
167
168 _writer.WriteRaw (jsonScoreKey);
169 _writer.WriteInt32 (entity.Score);
170
171 _writer.WriteRaw (jsonDeathsKey);
172 _writer.WriteInt32 (entity.Died);
173
174
175 _writer.WriteRaw (jsonKillsKey);
176
177 _writer.WriteRaw (jsonKillsZombiesKey);
178 _writer.WriteInt32 (entity.KilledZombies);
179
180 _writer.WriteRaw (jsonKillsPlayersKey);
181 _writer.WriteInt32 (entity.KilledPlayers);
182
183 _writer.WriteEndObject (); // End of jsonKillsKey
184
185
186 _writer.WriteRaw (jsonBannedKey);
187
188 bool banned = GameManager.Instance.adminTools.Blacklist.IsBanned (_nativeUserId, out DateTime bannedUntil, out string banReason);
189 if (!banned && ci.CrossplatformId != null) {
190 banned = GameManager.Instance.adminTools.Blacklist.IsBanned (ci.CrossplatformId, out bannedUntil, out banReason);
191 }
192
193 _writer.WriteRaw (jsonBanActiveKey);
194 _writer.WriteBoolean (banned);
195
196 _writer.WriteRaw (jsonBanReasonKey);
197 if (banned) {
198 _writer.WriteString (banReason);
199 } else {
200 _writer.WriteNull ();
201 }
202
203 _writer.WriteRaw (jsonBanUntilKey);
204 if (banned) {
205 JsonCommons.WriteDateTime (ref _writer, bannedUntil);
206 } else {
207 _writer.WriteNull ();
208 }
209
210 _writer.WriteEndObject (); // End of jsonBannedKeys
211
212
213 _writer.WriteEndObject (); // End of jsonEntityIdKey
214 }
215
[426]216 public override int DefaultPermissionLevel () => AdminWebModules.PermissionLevelGuest;
[425]217 }
218}
Note: See TracBrowser for help on using the repository browser.