source: binary-improvements2/MapRendering/Web/API/GetAnimalsLocation.cs@ 382

Last change on this file since 382 was 382, checked in by alloc, 2 years ago

Switched to use SpaceWizards.HttpListener

File size: 1.5 KB
RevLine 
[325]1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.JSON;
[251]4using AllocsFixes.LiveData;
[382]5using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
6using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
[251]7
[325]8namespace 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}
Note: See TracBrowser for help on using the repository browser.