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

Last change on this file since 296 was 253, checked in by alloc, 9 years ago

Fixes 6_8_10

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