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