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

Last change on this file since 246 was 245, checked in by alloc, 9 years ago

Fixes

File size: 2.6 KB
Line 
1using AllocsFixes.JSON;
2using AllocsFixes.PersistentData;
3using System;
4using System.Collections.Generic;
5using System.Net;
6
7namespace 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 steamid = string.Empty;
14
15 if (req.QueryString ["steamid"] != null) {
16 long tempLong;
17 steamid = req.QueryString ["steamid"];
18 if (steamid.Length != 17 || !long.TryParse (steamid, out tempLong)) {
19 resp.StatusCode = (int)HttpStatusCode.BadRequest;
20 Web.SetResponseTextContent (resp, "Invalid SteamID given");
21 return;
22 }
23 }
24
25 JSONObject result = new JSONObject ();
26 result.Add ("claimsize", new JSONNumber (GamePrefs.GetInt (EnumGamePrefs.LandClaimSize)));
27
28 JSONArray claimOwners = new JSONArray ();
29 result.Add ("claimowners", claimOwners);
30
31 Dictionary<Vector3i, PersistentPlayerData> d = GameManager.Instance.GetPersistentPlayerList ().m_lpBlockMap;
32 if (d != null) {
33 World w = GameManager.Instance.World;
34 Dictionary<PersistentPlayerData, List<Vector3i>> owners = new Dictionary<PersistentPlayerData, List<Vector3i>> ();
35 foreach (KeyValuePair<Vector3i, PersistentPlayerData> kvp in d) {
36 if (steamid.Length == 0 || kvp.Value.PlayerId.Equals (steamid)) {
37 if (!owners.ContainsKey (kvp.Value)) {
38 owners.Add (kvp.Value, new List<Vector3i> ());
39 }
40 owners [kvp.Value].Add (kvp.Key);
41 }
42 }
43
44 foreach (KeyValuePair<PersistentPlayerData, List<Vector3i>> kvp in owners) {
45 if (steamid.Length == 0 || kvp.Key.PlayerId.Equals (steamid)) {
46 string curID = kvp.Key.PlayerId;
47 bool isActive = w.IsLandProtectionValidForPlayer (kvp.Key);
48
49 JSONObject owner = new JSONObject ();
50 claimOwners.Add (owner);
51
52 owner.Add ("steamid", new JSONString (curID));
53 owner.Add ("claimactive", new JSONBoolean (isActive));
54
55 if (PersistentContainer.Instance.Players [curID, false] != null) {
56 owner.Add ("playername", new JSONString (PersistentContainer.Instance.Players [curID, false].Name));
57 } else {
58 owner.Add ("playername", new JSONNull ());
59 }
60
61 JSONArray claims = new JSONArray ();
62 owner.Add ("claims", claims);
63
64 foreach (Vector3i v in kvp.Value) {
65 JSONObject claim = new JSONObject ();
66 claim.Add ("x", new JSONNumber (v.x));
67 claim.Add ("y", new JSONNumber (v.y));
68 claim.Add ("z", new JSONNumber (v.z));
69
70 claims.Add (claim);
71 }
72 }
73 }
74 }
75
76 WriteJSON (resp, result);
77 }
78 }
79}
80
Note: See TracBrowser for help on using the repository browser.