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

Last change on this file since 338 was 332, checked in by alloc, 6 years ago

*Latest optimizations

File size: 2.7 KB
RevLine 
[325]1using System.Collections.Generic;
2using System.Net;
[253]3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
5
[325]6namespace AllocsFixes.NetConnections.Servers.Web.API {
[253]7 public class GetLandClaims : WebAPI {
[325]8 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
9 int permissionLevel) {
[253]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)) {
[325]16 resp.StatusCode = (int) HttpStatusCode.BadRequest;
[253]17 Web.SetResponseTextContent (resp, "Invalid SteamID given");
18 return;
19 }
20 }
21
22 // default user, cheap way to avoid 'null reference exception'
[332]23 user = user ?? new WebConnection ("", IPAddress.None, 0L);
[253]24
25 bool bViewAll = WebConnection.CanViewAllClaims (permissionLevel);
[325]26
[253]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) {
[325]36 ownerFilters = new[] {
[253]37 LandClaimList.SteamIdFilter (user.SteamID.ToString ()),
38 LandClaimList.SteamIdFilter (requestedSteamID)
39 };
40 } else if (!bViewAll) {
[325]41 ownerFilters = new[] {LandClaimList.SteamIdFilter (user.SteamID.ToString ())};
[253]42 } else {
[325]43 ownerFilters = new[] {LandClaimList.SteamIdFilter (requestedSteamID)};
[253]44 }
45 }
[325]46
[253]47 LandClaimList.PositionFilter[] posFilters = null;
48
[325]49 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters);
[253]50
[325]51 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
[326]52// try {
[253]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 }
[326]76// } catch {
77// }
[253]78 }
79
80 WriteJSON (resp, result);
81 }
82 }
[325]83}
Note: See TracBrowser for help on using the repository browser.