[325] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Net;
|
---|
| 3 | using AllocsFixes.JSON;
|
---|
[251] | 4 | using AllocsFixes.LiveData;
|
---|
[382] | 5 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
|
---|
| 6 | using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
|
---|
[251] | 7 |
|
---|
[325] | 8 | namespace AllocsFixes.NetConnections.Servers.Web.API {
|
---|
| 9 | internal class GetAnimalsLocation : WebAPI {
|
---|
| 10 | private readonly List<EntityAnimal> animals = new List<EntityAnimal> ();
|
---|
[306] | 11 |
|
---|
[351] | 12 | public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
|
---|
| 13 | int _permissionLevel) {
|
---|
[325] | 14 | JSONArray animalsJsResult = new JSONArray ();
|
---|
[251] | 15 |
|
---|
[313] | 16 | Animals.Instance.Get (animals);
|
---|
[325] | 17 | for (int i = 0; i < animals.Count; i++) {
|
---|
[306] | 18 | EntityAnimal entity = animals [i];
|
---|
[325] | 19 | Vector3i position = new Vector3i (entity.GetPosition ());
|
---|
[251] | 20 |
|
---|
[325] | 21 | JSONObject jsonPOS = new JSONObject ();
|
---|
| 22 | jsonPOS.Add ("x", new JSONNumber (position.x));
|
---|
| 23 | jsonPOS.Add ("y", new JSONNumber (position.y));
|
---|
| 24 | jsonPOS.Add ("z", new JSONNumber (position.z));
|
---|
[251] | 25 |
|
---|
[325] | 26 | JSONObject pJson = new JSONObject ();
|
---|
| 27 | pJson.Add ("id", new JSONNumber (entity.entityId));
|
---|
[251] | 28 |
|
---|
[325] | 29 | if (!string.IsNullOrEmpty (entity.EntityName)) {
|
---|
| 30 | pJson.Add ("name", new JSONString (entity.EntityName));
|
---|
| 31 | } else {
|
---|
| 32 | pJson.Add ("name", new JSONString ("animal class #" + entity.entityClass));
|
---|
| 33 | }
|
---|
[251] | 34 |
|
---|
[325] | 35 | pJson.Add ("position", jsonPOS);
|
---|
[251] | 36 |
|
---|
[325] | 37 | animalsJsResult.Add (pJson);
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[351] | 40 | WriteJSON (_resp, animalsJsResult);
|
---|
[325] | 41 | }
|
---|
| 42 | }
|
---|
| 43 | }
|
---|