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

Last change on this file since 446 was 446, checked in by alloc, 17 months ago

24_27_41

File size: 2.7 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
5
6namespace AllocsFixes.NetConnections.Servers.Web.API {
7 public class GetLandClaims : WebAPI {
8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
9 int _permissionLevel) {
10 PlatformUserIdentifierAbs requestedUserId = null;
11 if (_req.QueryString ["userid"] != null) {
12 if (!PlatformUserIdentifierAbs.TryFromCombinedString (_req.QueryString ["userid"], out requestedUserId)) {
13 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
14 Web.SetResponseTextContent (_resp, "Invalid user id given");
15 return;
16 }
17 }
18
19 // default user, cheap way to avoid 'null reference exception'
20 PlatformUserIdentifierAbs userId = _user?.UserId;
21
22 bool bViewAll = WebConnection.CanViewAllClaims (_permissionLevel);
23
24 JSONObject result = new JSONObject ();
25 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> (nameof(EnumGamePrefs.LandClaimSize)))));
26
27 JSONArray claimOwners = new JSONArray ();
28 result.Add ("claimowners", claimOwners);
29
30 LandClaimList.OwnerFilter[] ownerFilters = null;
31 if (requestedUserId != null || !bViewAll) {
32 if (requestedUserId != null && !bViewAll) {
33 ownerFilters = new[] {
34 LandClaimList.UserIdFilter (userId),
35 LandClaimList.UserIdFilter (requestedUserId)
36 };
37 } else if (!bViewAll) {
38 ownerFilters = new[] {LandClaimList.UserIdFilter (userId)};
39 } else {
40 ownerFilters = new[] {LandClaimList.UserIdFilter (requestedUserId)};
41 }
42 }
43
44 LandClaimList.PositionFilter[] posFilters = null;
45
46 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters);
47
48 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
49 JSONObject owner = new JSONObject ();
50 claimOwners.Add (owner);
51
52 owner.Add ("steamid", new JSONString (kvp.Key.PlatformId.CombinedString));
53 owner.Add ("crossplatformid", new JSONString (kvp.Key.CrossPlatformId?.CombinedString ?? ""));
54 owner.Add ("claimactive", new JSONBoolean (kvp.Key.LandProtectionActive));
55
56 if (kvp.Key.Name.Length > 0) {
57 owner.Add ("playername", new JSONString (kvp.Key.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 kvp.Value) {
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 WriteJSON (_resp, result);
76 }
77 }
78}
Note: See TracBrowser for help on using the repository browser.