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

Last change on this file since 361 was 351, checked in by alloc, 6 years ago

Fixed game version compatibility of GamePrefs
Code style cleanup (mostly argument names)

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 {
[351]8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
9 int _permissionLevel) {
[253]10 string requestedSteamID = string.Empty;
11
[351]12 if (_req.QueryString ["steamid"] != null) {
[253]13 ulong lViewersSteamID;
[351]14 requestedSteamID = _req.QueryString ["steamid"];
[253]15 if (requestedSteamID.Length != 17 || !ulong.TryParse (requestedSteamID, out lViewersSteamID)) {
[351]16 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
17 Web.SetResponseTextContent (_resp, "Invalid SteamID given");
[253]18 return;
19 }
20 }
21
22 // default user, cheap way to avoid 'null reference exception'
[351]23 _user = _user ?? new WebConnection ("", IPAddress.None, 0L);
[253]24
[351]25 bool bViewAll = WebConnection.CanViewAllClaims (_permissionLevel);
[325]26
[253]27 JSONObject result = new JSONObject ();
[351]28 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> ("LandClaimSize"))));
[253]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[] {
[351]37 LandClaimList.SteamIdFilter (_user.SteamID.ToString ()),
[253]38 LandClaimList.SteamIdFilter (requestedSteamID)
39 };
40 } else if (!bViewAll) {
[351]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
[351]80 WriteJSON (_resp, result);
[253]81 }
82 }
[325]83}
Note: See TracBrowser for help on using the repository browser.