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

Last change on this file since 455 was 455, checked in by alloc, 16 months ago

25_30_44

  • Got rid (mostly) of custom JSON serialization
  • Some code cleanup
File size: 3.8 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.PersistentData;
4using JetBrains.Annotations;
5using Utf8Json;
6using Webserver;
7using Webserver.Permissions;
8using Webserver.WebAPI;
9
10namespace AllocsFixes.WebAPIs {
11 [UsedImplicitly]
12 public class GetLandClaims : AbsWebAPI {
13 private static readonly byte[] jsonKeyClaimSize = JsonWriter.GetEncodedPropertyNameWithBeginObject ("claimsize");
14 private static readonly byte[] jsonKeyClaimOwners = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("claimowners");
15
16 private static readonly byte[] jsonKeySteamId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("steamid");
17 private static readonly byte[] jsonKeyCrossPlatformId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("crossplatformid");
18 private static readonly byte[] jsonKeyClaimActive = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("claimactive");
19 private static readonly byte[] jsonKeyPlayerName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("playername");
20 private static readonly byte[] jsonKeyClaims = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("claims");
21
22 public override void HandleRequest (RequestContext _context) {
23 PlatformUserIdentifierAbs requestedUserId = null;
24 if (_context.Request.QueryString ["userid"] != null) {
25 if (!PlatformUserIdentifierAbs.TryFromCombinedString (_context.Request.QueryString ["userid"], out requestedUserId)) {
26 WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest);
27 return;
28 }
29 }
30
31 // default user, cheap way to avoid 'null reference exception'
32 PlatformUserIdentifierAbs userId = _context.Connection?.UserId;
33
34 bool bViewAll = PermissionUtils.CanViewAllClaims (_context.PermissionLevel);
35
36 LandClaimList.OwnerFilter[] ownerFilters = null;
37 if (requestedUserId != null || !bViewAll) {
38 if (requestedUserId != null && !bViewAll) {
39 ownerFilters = new[] {
40 LandClaimList.UserIdFilter (userId),
41 LandClaimList.UserIdFilter (requestedUserId),
42 };
43 } else if (!bViewAll) {
44 ownerFilters = new[] {LandClaimList.UserIdFilter (userId)};
45 } else {
46 ownerFilters = new[] {LandClaimList.UserIdFilter (requestedUserId)};
47 }
48 }
49
50 LandClaimList.PositionFilter[] posFilters = null;
51
52 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters);
53
54 JsonWriter writer = new JsonWriter ();
55
56 writer.WriteRaw (jsonKeyClaimSize);
57 writer.WriteInt32 (GamePrefs.GetInt (EnumUtils.Parse<EnumGamePrefs> (nameof(EnumGamePrefs.LandClaimSize))));
58
59 writer.WriteRaw (jsonKeyClaimOwners);
60 writer.WriteBeginArray ();
61
62 bool first = true;
63
64 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
65 if (!first) {
66 writer.WriteValueSeparator ();
67 }
68
69 first = false;
70
71 writer.WriteRaw (jsonKeySteamId);
72 writer.WriteString (kvp.Key.PlatformId?.CombinedString ?? "");
73
74 writer.WriteRaw (jsonKeyCrossPlatformId);
75 writer.WriteString (kvp.Key.CrossPlatformId?.CombinedString ?? "");
76
77 writer.WriteRaw (jsonKeyClaimActive);
78 writer.WriteBoolean (kvp.Key.LandProtectionActive);
79
80 writer.WriteRaw (jsonKeyPlayerName);
81 if (kvp.Key.Name.Length > 0) {
82 writer.WriteString (kvp.Key.Name);
83 } else {
84 writer.WriteNull ();
85 }
86
87 writer.WriteRaw (jsonKeyClaims);
88 writer.WriteBeginArray ();
89
90 bool first2 = true;
91
92 foreach (Vector3i v in kvp.Value) {
93 if (!first2) {
94 writer.WriteValueSeparator ();
95 }
96
97 first2 = false;
98
99 JsonCommons.WriteVector3I (ref writer, v);
100 }
101
102 writer.WriteEndArray ();
103
104 writer.WriteEndObject ();
105 }
106
107 writer.WriteEndArray ();
108
109 writer.WriteEndObject ();
110 WebUtils.WriteJsonData (_context.Response, ref writer);
111 }
112 }
113}
Note: See TracBrowser for help on using the repository browser.