1 | using AllocsFixes.JSON;
|
---|
2 | using AllocsFixes.PersistentData;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Net;
|
---|
6 |
|
---|
7 | namespace AllocsFixes.NetConnections.Servers.Web.API
|
---|
8 | {
|
---|
9 | public class GetLandClaims : WebAPI
|
---|
10 | {
|
---|
11 | public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel)
|
---|
12 | {
|
---|
13 | string ViewersSteamID = string.Empty;
|
---|
14 | ulong lViewersSteamID = 0L;
|
---|
15 |
|
---|
16 | if (req.QueryString ["steamid"] != null) {
|
---|
17 | ViewersSteamID = req.QueryString ["steamid"];
|
---|
18 | if (ViewersSteamID.Length != 17 || !ulong.TryParse (ViewersSteamID, out lViewersSteamID)) {
|
---|
19 | resp.StatusCode = (int)HttpStatusCode.BadRequest;
|
---|
20 | Web.SetResponseTextContent (resp, "Invalid SteamID given");
|
---|
21 | return;
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | // default user, cheap way to avoid 'null reference exception'
|
---|
26 | try { user = user ?? new WebConnection ("", "", 0L); } catch { }
|
---|
27 |
|
---|
28 | bool bViewAll = false; try { bViewAll = user.CanViewAllClaims (permissionLevel); } catch { }
|
---|
29 |
|
---|
30 | JSONObject result = new JSONObject ();
|
---|
31 | result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumGamePrefs.LandClaimSize)));
|
---|
32 |
|
---|
33 | JSONArray claimOwners = new JSONArray ();
|
---|
34 | result.Add ("claimowners", claimOwners);
|
---|
35 |
|
---|
36 | Dictionary<Vector3i, PersistentPlayerData> d = GameManager.Instance.GetPersistentPlayerList ().m_lpBlockMap;
|
---|
37 | if (d != null) {
|
---|
38 | World w = GameManager.Instance.World;
|
---|
39 | Dictionary<PersistentPlayerData, List<Vector3i>> owners = new Dictionary<PersistentPlayerData, List<Vector3i>> ();
|
---|
40 |
|
---|
41 | // Add all owners to this temporary list regardless of permissions
|
---|
42 | foreach (KeyValuePair<Vector3i, PersistentPlayerData> kvp in d) {
|
---|
43 | if (kvp.Value.PlayerId.Equals (ViewersSteamID)) {
|
---|
44 | if (!owners.ContainsKey (kvp.Value)) {
|
---|
45 | owners.Add (kvp.Value, new List<Vector3i> ());
|
---|
46 | }
|
---|
47 | owners [kvp.Value].Add (kvp.Key);
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | // Loop through all claim owners...
|
---|
52 | foreach (KeyValuePair<PersistentPlayerData, List<Vector3i>> kvp in owners) {
|
---|
53 | try
|
---|
54 | {
|
---|
55 | // ... but only show us claims that are from the current web user or if the current web user can see all claims regardless of ownership
|
---|
56 | if (kvp.Key.PlayerId.Equals (ViewersSteamID) || bViewAll)
|
---|
57 | {
|
---|
58 | string currentSteamID = kvp.Key.PlayerId;
|
---|
59 | bool isActive = w.IsLandProtectionValidForPlayer (kvp.Key);
|
---|
60 |
|
---|
61 | JSONObject owner = new JSONObject ();
|
---|
62 | claimOwners.Add (owner);
|
---|
63 |
|
---|
64 | owner.Add("steamid", new JSONString (currentSteamID));
|
---|
65 | owner.Add("claimactive", new JSONBoolean (isActive));
|
---|
66 |
|
---|
67 | if (PersistentContainer.Instance.Players [currentSteamID, false] != null) {
|
---|
68 | owner.Add("playername", new JSONString (PersistentContainer.Instance.Players [currentSteamID, false].Name));
|
---|
69 | } else {
|
---|
70 | owner.Add("playername", new JSONNull ());
|
---|
71 | }
|
---|
72 |
|
---|
73 | JSONArray claims = new JSONArray ();
|
---|
74 | owner.Add ("claims", claims);
|
---|
75 |
|
---|
76 | foreach (Vector3i v in kvp.Value) {
|
---|
77 | JSONObject claim = new JSONObject ();
|
---|
78 | claim.Add ("x", new JSONNumber (v.x));
|
---|
79 | claim.Add ("y", new JSONNumber (v.y));
|
---|
80 | claim.Add ("z", new JSONNumber (v.z));
|
---|
81 |
|
---|
82 | claims.Add (claim);
|
---|
83 | }
|
---|
84 | }
|
---|
85 | }
|
---|
86 | catch { }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | WriteJSON (resp, result);
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|