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

Last change on this file since 327 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

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 string requestedSteamID = string.Empty;
11
12 if (req.QueryString ["steamid"] != null) {
13 ulong lViewersSteamID;
14 requestedSteamID = req.QueryString ["steamid"];
15 if (requestedSteamID.Length != 17 || !ulong.TryParse (requestedSteamID, out lViewersSteamID)) {
16 resp.StatusCode = (int) HttpStatusCode.BadRequest;
17 Web.SetResponseTextContent (resp, "Invalid SteamID given");
18 return;
19 }
20 }
21
22 // default user, cheap way to avoid 'null reference exception'
23 user = user ?? new WebConnection ("", "", 0L);
24
25 bool bViewAll = WebConnection.CanViewAllClaims (permissionLevel);
26
27 JSONObject result = new JSONObject ();
28 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumGamePrefs.LandClaimSize)));
29
30 JSONArray claimOwners = new JSONArray ();
31 result.Add ("claimowners", claimOwners);
32
33 LandClaimList.OwnerFilter[] ownerFilters = null;
34 if (!string.IsNullOrEmpty (requestedSteamID) || !bViewAll) {
35 if (!string.IsNullOrEmpty (requestedSteamID) && !bViewAll) {
36 ownerFilters = new[] {
37 LandClaimList.SteamIdFilter (user.SteamID.ToString ()),
38 LandClaimList.SteamIdFilter (requestedSteamID)
39 };
40 } else if (!bViewAll) {
41 ownerFilters = new[] {LandClaimList.SteamIdFilter (user.SteamID.ToString ())};
42 } else {
43 ownerFilters = new[] {LandClaimList.SteamIdFilter (requestedSteamID)};
44 }
45 }
46
47 LandClaimList.PositionFilter[] posFilters = null;
48
49 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters);
50
51 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
52// try {
53 JSONObject owner = new JSONObject ();
54 claimOwners.Add (owner);
55
56 owner.Add ("steamid", new JSONString (kvp.Key.SteamID));
57 owner.Add ("claimactive", new JSONBoolean (kvp.Key.LandProtectionActive));
58
59 if (kvp.Key.Name.Length > 0) {
60 owner.Add ("playername", new JSONString (kvp.Key.Name));
61 } else {
62 owner.Add ("playername", new JSONNull ());
63 }
64
65 JSONArray claimsJson = new JSONArray ();
66 owner.Add ("claims", claimsJson);
67
68 foreach (Vector3i v in kvp.Value) {
69 JSONObject claim = new JSONObject ();
70 claim.Add ("x", new JSONNumber (v.x));
71 claim.Add ("y", new JSONNumber (v.y));
72 claim.Add ("z", new JSONNumber (v.z));
73
74 claimsJson.Add (claim);
75 }
76// } catch {
77// }
78 }
79
80 WriteJSON (resp, result);
81 }
82 }
83}
Note: See TracBrowser for help on using the repository browser.