using AllocsFixes.JSON; using AllocsFixes.PersistentData; using System; using System.Collections.Generic; using System.Net; namespace AllocsFixes.NetConnections.Servers.Web.API { public class GetPlayerInventory : WebAPI { public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) { if (req.QueryString ["steamid"] == null) { resp.StatusCode = (int)HttpStatusCode.InternalServerError; Web.SetResponseTextContent (resp, "No SteamID given"); return; } Player p = PersistentContainer.Instance.Players [req.QueryString ["steamid"], false]; if (p == null) { resp.StatusCode = (int)HttpStatusCode.InternalServerError; Web.SetResponseTextContent (resp, "Invalid or unknown SteamID given"); return; } PersistentData.Inventory inv = p.Inventory; JSONObject result = new JSONObject (); JSONArray bag = new JSONArray (); JSONArray belt = new JSONArray (); JSONObject equipment = new JSONObject (); result.Add ("playername", new JSONString (p.Name)); result.Add ("bag", bag); result.Add ("belt", belt); result.Add ("equipment", equipment); DoInventory (belt, inv.belt); DoInventory (bag, inv.bag); AddEquipment (equipment, "head", inv.equipment, XMLData.Item.EnumEquipmentSlot.Head, NGuiInvGridEquipment.EnumClothingLayer.Middle); AddEquipment (equipment, "eyes", inv.equipment, XMLData.Item.EnumEquipmentSlot.Eyes, NGuiInvGridEquipment.EnumClothingLayer.Middle); AddEquipment (equipment, "face", inv.equipment, XMLData.Item.EnumEquipmentSlot.Face, NGuiInvGridEquipment.EnumClothingLayer.Middle); AddEquipment (equipment, "armor", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Outer); AddEquipment (equipment, "jacket", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Middle); AddEquipment (equipment, "shirt", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Inner); AddEquipment (equipment, "legarmor", inv.equipment, XMLData.Item.EnumEquipmentSlot.Legs, NGuiInvGridEquipment.EnumClothingLayer.Outer); AddEquipment (equipment, "pants", inv.equipment, XMLData.Item.EnumEquipmentSlot.Legs, NGuiInvGridEquipment.EnumClothingLayer.Inner); AddEquipment (equipment, "boots", inv.equipment, XMLData.Item.EnumEquipmentSlot.Feet, NGuiInvGridEquipment.EnumClothingLayer.Inner); AddEquipment (equipment, "gloves", inv.equipment, XMLData.Item.EnumEquipmentSlot.Hands, NGuiInvGridEquipment.EnumClothingLayer.Inner); AddEquipment (equipment, "backpack", inv.equipment, XMLData.Item.EnumEquipmentSlot.Back, NGuiInvGridEquipment.EnumClothingLayer.Outer); WriteJSON (resp, result); } private void DoInventory (JSONArray _jsonRes, List _inv) { for (int i = 0; i < _inv.Count; i++) { _jsonRes.Add (GetJsonForItem (_inv [i])); } } private void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, XMLData.Item.EnumEquipmentSlot _slot, NGuiInvGridEquipment.EnumClothingLayer _layer) { int index = (int)_slot + (int)_layer * (int)XMLData.Item.EnumEquipmentSlot.Count; if (_items != null) { _eq.Add (_slotname, GetJsonForItem (_items [index])); } else { _eq.Add (_slotname, new JSONNull ()); } } private JSONNode GetJsonForItem (InvItem _item) { if (_item != null) { JSONObject jsonItem = new JSONObject (); jsonItem.Add ("count", new JSONNumber (_item.count)); jsonItem.Add ("name", new JSONString (_item.itemName)); jsonItem.Add ("quality", new JSONNumber (_item.quality)); if (_item.quality >= 0) { jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality))); } return jsonItem; } else { return new JSONNull (); } } } }