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
Line 
1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
5using Webserver;
6using Webserver.Permissions;
7using Webserver.WebAPI;
8
9namespace AllocsFixes.WebAPIs {
10 public class GetLandClaims : AbsWebAPI {
11 public override void HandleRequest (RequestContext _context) {
12 PlatformUserIdentifierAbs requestedUserId = null;
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);
16 return;
17 }
18 }
19
20 // default user, cheap way to avoid 'null reference exception'
21 PlatformUserIdentifierAbs userId = _context.Connection?.UserId;
22
23 bool bViewAll = PermissionUtils.CanViewAllClaims (_context.PermissionLevel);
24
25 JSONObject result = new JSONObject ();
26 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> (nameof(EnumGamePrefs.LandClaimSize)))));
27
28 JSONArray claimOwners = new JSONArray ();
29 result.Add ("claimowners", claimOwners);
30
31 LandClaimList.OwnerFilter[] ownerFilters = null;
32 if (requestedUserId != null || !bViewAll) {
33 if (requestedUserId != null && !bViewAll) {
34 ownerFilters = new[] {
35 LandClaimList.UserIdFilter (userId),
36 LandClaimList.UserIdFilter (requestedUserId)
37 };
38 } else if (!bViewAll) {
39 ownerFilters = new[] {LandClaimList.UserIdFilter (userId)};
40 } else {
41 ownerFilters = new[] {LandClaimList.UserIdFilter (requestedUserId)};
42 }
43 }
44
45 LandClaimList.PositionFilter[] posFilters = null;
46
47 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters);
48
49 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
50 JSONObject owner = new JSONObject ();
51 claimOwners.Add (owner);
52
53 owner.Add ("steamid", new JSONString (kvp.Key.PlatformId?.CombinedString ?? ""));
54 owner.Add ("crossplatformid", new JSONString (kvp.Key.CrossPlatformId?.CombinedString ?? ""));
55 owner.Add ("claimactive", new JSONBoolean (kvp.Key.LandProtectionActive));
56
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 }
62
63 JSONArray claimsJson = new JSONArray ();
64 owner.Add ("claims", claimsJson);
65
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));
71
72 claimsJson.Add (claim);
73 }
74 }
75
76 LegacyApiHelper.WriteJSON (_context.Response, result);
77 }
78 }
79}
Note: See TracBrowser for help on using the repository browser.