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

Last change on this file since 419 was 369, checked in by alloc, 3 years ago

Preparations for A20 release
Changes usage of "SteamID" to "UserID" in console commands
Also changes a bunch of the WebAPI stuff to show / use UserIDs

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