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

Last change on this file since 450 was 444, checked in by alloc, 17 months ago

Added more details to API map/config
Updated web files to 0.7.0

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