using System.Collections.Generic; using System.Net; using AllocsFixes.JSON; using AllocsFixes.PersistentData; using Webserver; using Webserver.Permissions; using Webserver.WebAPI; namespace AllocsFixes.WebAPIs { public class GetLandClaims : AbsWebAPI { public override void HandleRequest (RequestContext _context) { PlatformUserIdentifierAbs requestedUserId = null; if (_context.Request.QueryString ["userid"] != null) { if (!PlatformUserIdentifierAbs.TryFromCombinedString (_context.Request.QueryString ["userid"], out requestedUserId)) { WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest); return; } } // default user, cheap way to avoid 'null reference exception' PlatformUserIdentifierAbs userId = _context.Connection?.UserId; bool bViewAll = PermissionUtils.CanViewAllClaims (_context.PermissionLevel); JSONObject result = new JSONObject (); result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumUtils.Parse (nameof(EnumGamePrefs.LandClaimSize))))); JSONArray claimOwners = new JSONArray (); result.Add ("claimowners", claimOwners); LandClaimList.OwnerFilter[] ownerFilters = null; if (requestedUserId != null || !bViewAll) { if (requestedUserId != null && !bViewAll) { ownerFilters = new[] { LandClaimList.UserIdFilter (userId), LandClaimList.UserIdFilter (requestedUserId) }; } else if (!bViewAll) { ownerFilters = new[] {LandClaimList.UserIdFilter (userId)}; } else { ownerFilters = new[] {LandClaimList.UserIdFilter (requestedUserId)}; } } LandClaimList.PositionFilter[] posFilters = null; Dictionary> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters); foreach (KeyValuePair> kvp in claims) { JSONObject owner = new JSONObject (); claimOwners.Add (owner); owner.Add ("steamid", new JSONString (kvp.Key.PlatformId?.CombinedString ?? "")); owner.Add ("crossplatformid", new JSONString (kvp.Key.CrossPlatformId?.CombinedString ?? "")); owner.Add ("claimactive", new JSONBoolean (kvp.Key.LandProtectionActive)); if (kvp.Key.Name.Length > 0) { owner.Add ("playername", new JSONString (kvp.Key.Name)); } else { owner.Add ("playername", new JSONNull ()); } JSONArray claimsJson = new JSONArray (); owner.Add ("claims", claimsJson); foreach (Vector3i v in kvp.Value) { JSONObject claim = new JSONObject (); claim.Add ("x", new JSONNumber (v.x)); claim.Add ("y", new JSONNumber (v.y)); claim.Add ("z", new JSONNumber (v.z)); claimsJson.Add (claim); } } LegacyApiHelper.WriteJSON (_context.Response, result); } } }