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

Last change on this file since 457 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
RevLine 
[325]1using System.Collections.Generic;
2using System.Net;
[253]3using AllocsFixes.PersistentData;
[455]4using JetBrains.Annotations;
5using Utf8Json;
[454]6using Webserver;
7using Webserver.Permissions;
8using Webserver.WebAPI;
[253]9
[454]10namespace AllocsFixes.WebAPIs {
[455]11 [UsedImplicitly]
[454]12 public class GetLandClaims : AbsWebAPI {
[455]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
[454]22 public override void HandleRequest (RequestContext _context) {
[369]23 PlatformUserIdentifierAbs requestedUserId = null;
[454]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);
[253]27 return;
28 }
29 }
30
31 // default user, cheap way to avoid 'null reference exception'
[454]32 PlatformUserIdentifierAbs userId = _context.Connection?.UserId;
[253]33
[454]34 bool bViewAll = PermissionUtils.CanViewAllClaims (_context.PermissionLevel);
[325]35
[253]36 LandClaimList.OwnerFilter[] ownerFilters = null;
[369]37 if (requestedUserId != null || !bViewAll) {
38 if (requestedUserId != null && !bViewAll) {
[325]39 ownerFilters = new[] {
[369]40 LandClaimList.UserIdFilter (userId),
[455]41 LandClaimList.UserIdFilter (requestedUserId),
[253]42 };
43 } else if (!bViewAll) {
[369]44 ownerFilters = new[] {LandClaimList.UserIdFilter (userId)};
[253]45 } else {
[369]46 ownerFilters = new[] {LandClaimList.UserIdFilter (requestedUserId)};
[253]47 }
48 }
[325]49
[253]50 LandClaimList.PositionFilter[] posFilters = null;
51
[325]52 Dictionary<Player, List<Vector3i>> claims = LandClaimList.GetLandClaims (ownerFilters, posFilters);
[253]53
[455]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
[325]64 foreach (KeyValuePair<Player, List<Vector3i>> kvp in claims) {
[455]65 if (!first) {
66 writer.WriteValueSeparator ();
67 }
[253]68
[455]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);
[369]81 if (kvp.Key.Name.Length > 0) {
[455]82 writer.WriteString (kvp.Key.Name);
[369]83 } else {
[455]84 writer.WriteNull ();
[369]85 }
[455]86
87 writer.WriteRaw (jsonKeyClaims);
88 writer.WriteBeginArray ();
[253]89
[455]90 bool first2 = true;
[253]91
[369]92 foreach (Vector3i v in kvp.Value) {
[455]93 if (!first2) {
94 writer.WriteValueSeparator ();
95 }
[253]96
[455]97 first2 = false;
98
99 JsonCommons.WriteVector3I (ref writer, v);
[369]100 }
[455]101
102 writer.WriteEndArray ();
103
104 writer.WriteEndObject ();
[253]105 }
106
[455]107 writer.WriteEndArray ();
108
109 writer.WriteEndObject ();
110 WebUtils.WriteJsonData (_context.Response, ref writer);
[253]111 }
112 }
[325]113}
Note: See TracBrowser for help on using the repository browser.