Changeset 402 for binary-improvements2/WebServer/src/WebAPI/APIs/Hostile.cs
- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- Location:
- binary-improvements2/WebServer/src/WebAPI/APIs
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/WebAPI/APIs/Hostile.cs
r401 r402 1 1 using System.Collections.Generic; 2 using AllocsFixes.JSON;3 using AllocsFixes.LiveData;4 2 using JetBrains.Annotations; 3 using Utf8Json; 4 using Webserver.LiveData; 5 5 6 namespace Webserver.WebAPI {6 namespace Webserver.WebAPI.APIs { 7 7 [UsedImplicitly] 8 internal class GetHostileLocation : AbsWebAPI{9 private readonly List<EntityEnemy> en emies = new List<EntityEnemy> ();8 internal class Hostile : AbsRestApi { 9 private readonly List<EntityEnemy> entities = new List<EntityEnemy> (); 10 10 11 public override void HandleRequest (RequestContext _context) { 12 JsonArray hostilesJsResult = new JsonArray (); 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"); 13 14 14 Hostiles.Instance.Get (enemies); 15 for (int i = 0; i < enemies.Count; i++) { 16 EntityEnemy entity = enemies [i]; 17 Vector3i position = new Vector3i (entity.GetPosition ()); 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); 18 38 19 JsonObject jsonPOS = new JsonObject (); 20 jsonPOS.Add ("x", new JsonNumber (position.x)); 21 jsonPOS.Add ("y", new JsonNumber (position.y)); 22 jsonPOS.Add ("z", new JsonNumber (position.z)); 23 24 JsonObject pJson = new JsonObject (); 25 pJson.Add ("id", new JsonNumber (entity.entityId)); 26 27 if (!string.IsNullOrEmpty (entity.EntityName)) { 28 pJson.Add ("name", new JsonString (entity.EntityName)); 29 } else { 30 pJson.Add ("name", new JsonString ("enemy class #" + entity.entityClass)); 39 writer.WriteEndObject (); 31 40 } 32 33 pJson.Add ("position", jsonPOS);34 35 hostilesJsResult.Add (pJson);36 41 } 37 38 WebUtils.WriteJson (_context.Response, hostilesJsResult); 42 43 writer.WriteEndArray (); 44 SendEnvelopedResult (_context, ref writer); 39 45 } 40 46 }
Note:
See TracChangeset
for help on using the changeset viewer.