source: binary-improvements2/MapRendering/Web/API/GetLandClaims.cs@ 384

Last change on this file since 384 was 382, checked in by alloc, 2 years ago

Switched to use SpaceWizards.HttpListener

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