[391] | 1 | using System.Collections.Generic;
|
---|
| 2 | using JetBrains.Annotations;
|
---|
[402] | 3 | using Utf8Json;
|
---|
| 4 | using Webserver.LiveData;
|
---|
[391] | 5 |
|
---|
[402] | 6 | namespace Webserver.WebAPI.APIs {
|
---|
[391] | 7 | [UsedImplicitly]
|
---|
[402] | 8 | internal class Hostile : AbsRestApi {
|
---|
| 9 | private readonly List<EntityEnemy> entities = new List<EntityEnemy> ();
|
---|
[391] | 10 |
|
---|
[402] | 11 | private static readonly byte[] jsonKeyId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("id");
|
---|
| 12 | private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithBeginObject ("name");
|
---|
| 13 | private static readonly byte[] jsonKeyPosition = JsonWriter.GetEncodedPropertyNameWithBeginObject ("position");
|
---|
[391] | 14 |
|
---|
[402] | 15 | protected override void HandleRestGet (RequestContext _context) {
|
---|
| 16 | PrepareEnvelopedResult (out JsonWriter writer);
|
---|
| 17 | writer.WriteBeginArray ();
|
---|
| 18 |
|
---|
| 19 | lock (entities) {
|
---|
| 20 | Hostiles.Instance.Get (entities);
|
---|
| 21 |
|
---|
| 22 | for (int i = 0; i < entities.Count; i++) {
|
---|
| 23 | if (i > 0) {
|
---|
| 24 | writer.WriteValueSeparator ();
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | EntityAlive entity = entities [i];
|
---|
| 28 | Vector3i position = new Vector3i (entity.GetPosition ());
|
---|
| 29 |
|
---|
| 30 | writer.WriteRaw (jsonKeyId);
|
---|
| 31 | writer.WriteInt32 (entity.entityId);
|
---|
| 32 |
|
---|
| 33 | writer.WriteRaw (jsonKeyName);
|
---|
| 34 | writer.WriteString (!string.IsNullOrEmpty (entity.EntityName) ? entity.EntityName : $"enemy class #{entity.entityClass}");
|
---|
| 35 |
|
---|
| 36 | writer.WriteRaw (jsonKeyPosition);
|
---|
| 37 | JsonCommons.WritePositionObject (writer, position);
|
---|
[391] | 38 |
|
---|
[402] | 39 | writer.WriteEndObject ();
|
---|
[391] | 40 | }
|
---|
| 41 | }
|
---|
[402] | 42 |
|
---|
| 43 | writer.WriteEndArray ();
|
---|
| 44 | SendEnvelopedResult (_context, ref writer);
|
---|
[391] | 45 | }
|
---|
| 46 | }
|
---|
[402] | 47 | }
|
---|