source: binary-improvements2/WebServer/src/WebAPI/GetLandClaims.cs@ 398

Last change on this file since 398 was 391, checked in by alloc, 2 years ago

Major refactoring/cleanup

File size: 2.6 KB
RevLine 
[391]1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes;
4using AllocsFixes.JSON;
5using AllocsFixes.PersistentData;
6using JetBrains.Annotations;
7
8namespace Webserver.WebAPI {
9 [UsedImplicitly]
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 = WebConnection.CanViewAllClaims (_context.PermissionLevel);
24
25 JsonObject result = new JsonObject ();
26 result.Add ("claimsize", new JsonNumber (GamePrefs.GetInt (EnumUtils.Parse<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 ((Player player, List<Vector3i> claimPositions) in claims) {
50 JsonObject owner = new JsonObject ();
51 claimOwners.Add (owner);
52
53 owner.Add ("steamid", new JsonString (player.PlatformId.CombinedString));
54 owner.Add ("claimactive", new JsonBoolean (player.LandProtectionActive));
55
56 if (player.Name.Length > 0) {
57 owner.Add ("playername", new JsonString (player.Name));
58 } else {
59 owner.Add ("playername", new JsonNull ());
60 }
61
62 JsonArray claimsJson = new JsonArray ();
63 owner.Add ("claims", claimsJson);
64
65 foreach (Vector3i v in claimPositions) {
66 JsonObject claim = new JsonObject ();
67 claim.Add ("x", new JsonNumber (v.x));
68 claim.Add ("y", new JsonNumber (v.y));
69 claim.Add ("z", new JsonNumber (v.z));
70
71 claimsJson.Add (claim);
72 }
73 }
74
75 WebUtils.WriteJson (_context.Response, result);
76 }
77 }
78}
Note: See TracBrowser for help on using the repository browser.