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