source: binary-improvements2/MapRendering/Web/API/GetLandClaims.cs@ 390

Last change on this file since 390 was 387, checked in by alloc, 2 years ago

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

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