source: binary-improvements/MapRendering/API/GetLandClaims.cs@ 454

Last change on this file since 454 was 454, checked in by alloc, 16 months ago

24_29_43
Switched over to vanilla Web infrastructure

File size: 2.7 KB
RevLine 
[325]1using System.Collections.Generic;
2using System.Net;
[253]3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
[454]5using Webserver;
6using Webserver.Permissions;
7using Webserver.WebAPI;
[253]8
[454]9namespace AllocsFixes.WebAPIs {
10 public class GetLandClaims : AbsWebAPI {
11 public override void HandleRequest (RequestContext _context) {
[369]12 PlatformUserIdentifierAbs requestedUserId = null;
[454]13 if (_context.Request.QueryString ["userid"] != null) {
14 if (!PlatformUserIdentifierAbs.TryFromCombinedString (_context.Request.QueryString ["userid"], out requestedUserId)) {
15 WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest);
[253]16 return;
17 }
18 }
19
20 // default user, cheap way to avoid 'null reference exception'
[454]21 PlatformUserIdentifierAbs userId = _context.Connection?.UserId;
[253]22
[454]23 bool bViewAll = PermissionUtils.CanViewAllClaims (_context.PermissionLevel);
[325]24
[253]25 JSONObject result = new JSONObject ();
[420]26 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> (nameof(EnumGamePrefs.LandClaimSize)))));
[253]27
28 JSONArray claimOwners = new JSONArray ();
29 result.Add ("claimowners", claimOwners);
30
31 LandClaimList.OwnerFilter[] ownerFilters = null;
[369]32 if (requestedUserId != null || !bViewAll) {
33 if (requestedUserId != null && !bViewAll) {
[325]34 ownerFilters = new[] {
[369]35 LandClaimList.UserIdFilter (userId),
36 LandClaimList.UserIdFilter (requestedUserId)
[253]37 };
38 } else if (!bViewAll) {
[369]39 ownerFilters = new[] {LandClaimList.UserIdFilter (userId)};
[253]40 } else {
[369]41 ownerFilters = new[] {LandClaimList.UserIdFilter (requestedUserId)};
[253]42 }
43 }
[325]44
[253]45 LandClaimList.PositionFilter[] posFilters = null;
46
[325]47 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters);
[253]48
[325]49 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
[369]50 JSONObject owner = new JSONObject ();
51 claimOwners.Add (owner);
[253]52
[448]53 owner.Add ("steamid", new JSONString (kvp.Key.PlatformId?.CombinedString ?? ""));
[446]54 owner.Add ("crossplatformid", new JSONString (kvp.Key.CrossPlatformId?.CombinedString ?? ""));
[369]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
[454]76 LegacyApiHelper.WriteJSON (_context.Response, result);
[253]77 }
78 }
[325]79}
Note: See TracBrowser for help on using the repository browser.