[325] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Net;
|
---|
[253] | 3 | using AllocsFixes.PersistentData;
|
---|
[455] | 4 | using JetBrains.Annotations;
|
---|
| 5 | using Utf8Json;
|
---|
[454] | 6 | using Webserver;
|
---|
| 7 | using Webserver.Permissions;
|
---|
| 8 | using Webserver.WebAPI;
|
---|
[253] | 9 |
|
---|
[454] | 10 | namespace 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 | }
|
---|