source: binary-improvements2/MapRendering/Web/API/GetLandClaims.cs@ 386

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

Switched to use SpaceWizards.HttpListener

File size: 2.7 KB
RevLine 
[325]1using System.Collections.Generic;
2using System.Net;
[253]3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
[382]5using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
6using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
[253]7
[325]8namespace AllocsFixes.NetConnections.Servers.Web.API {
[253]9 public class GetLandClaims : WebAPI {
[351]10 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
11 int _permissionLevel) {
[369]12 PlatformUserIdentifierAbs requestedUserId = null;
13 if (_req.QueryString ["userid"] != null) {
14 if (!PlatformUserIdentifierAbs.TryFromCombinedString (_req.QueryString ["userid"], out requestedUserId)) {
[351]15 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
[369]16 Web.SetResponseTextContent (_resp, "Invalid user id given");
[253]17 return;
18 }
19 }
20
21 // default user, cheap way to avoid 'null reference exception'
[369]22 PlatformUserIdentifierAbs userId = _user?.UserId;
[253]23
[351]24 bool bViewAll = WebConnection.CanViewAllClaims (_permissionLevel);
[325]25
[253]26 JSONObject result = new JSONObject ();
[351]27 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> ("LandClaimSize"))));
[253]28
29 JSONArray claimOwners = new JSONArray ();
30 result.Add ("claimowners", claimOwners);
31
32 LandClaimList.OwnerFilter[] ownerFilters = null;
[369]33 if (requestedUserId != null || !bViewAll) {
34 if (requestedUserId != null && !bViewAll) {
[325]35 ownerFilters = new[] {
[369]36 LandClaimList.UserIdFilter (userId),
37 LandClaimList.UserIdFilter (requestedUserId)
[253]38 };
39 } else if (!bViewAll) {
[369]40 ownerFilters = new[] {LandClaimList.UserIdFilter (userId)};
[253]41 } else {
[369]42 ownerFilters = new[] {LandClaimList.UserIdFilter (requestedUserId)};
[253]43 }
44 }
[325]45
[253]46 LandClaimList.PositionFilter[] posFilters = null;
47
[325]48 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters);
[253]49
[325]50 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
[369]51 JSONObject owner = new JSONObject ();
52 claimOwners.Add (owner);
[253]53
[369]54 owner.Add ("steamid", new JSONString (kvp.Key.PlatformId.CombinedString));
55 owner.Add ("claimactive", new JSONBoolean (kvp.Key.LandProtectionActive));
[253]56
[369]57 if (kvp.Key.Name.Length > 0) {
58 owner.Add ("playername", new JSONString (kvp.Key.Name));
59 } else {
60 owner.Add ("playername", new JSONNull ());
61 }
[253]62
[369]63 JSONArray claimsJson = new JSONArray ();
64 owner.Add ("claims", claimsJson);
[253]65
[369]66 foreach (Vector3i v in kvp.Value) {
67 JSONObject claim = new JSONObject ();
68 claim.Add ("x", new JSONNumber (v.x));
69 claim.Add ("y", new JSONNumber (v.y));
70 claim.Add ("z", new JSONNumber (v.z));
[253]71
[369]72 claimsJson.Add (claim);
73 }
[253]74 }
75
[351]76 WriteJSON (_resp, result);
[253]77 }
78 }
[325]79}
Note: See TracBrowser for help on using the repository browser.