source: binary-improvements/MapRendering/Web/API/GetPlayerInventory.cs@ 250

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

Fixes 5_7_9

File size: 3.8 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 GetPlayerInventory : WebAPI {
10 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
11 if (req.QueryString ["steamid"] == null) {
12 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
13 Web.SetResponseTextContent (resp, "No SteamID given");
14 return;
15 }
16
17 Player p = PersistentContainer.Instance.Players [req.QueryString ["steamid"], false];
18 if (p == null) {
19 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
20 Web.SetResponseTextContent (resp, "Invalid or unknown SteamID given");
21 return;
22 }
23
24 PersistentData.Inventory inv = p.Inventory;
25
26
27 JSONObject result = new JSONObject ();
28
29 JSONArray bag = new JSONArray ();
30 JSONArray belt = new JSONArray ();
31 JSONObject equipment = new JSONObject ();
32 result.Add ("playername", new JSONString (p.Name));
33 result.Add ("bag", bag);
34 result.Add ("belt", belt);
35 result.Add ("equipment", equipment);
36
37 DoInventory (belt, inv.belt);
38 DoInventory (bag, inv.bag);
39
40 AddEquipment (equipment, "head", inv.equipment, XMLData.Item.EnumEquipmentSlot.Head, NGuiInvGridEquipment.EnumClothingLayer.Middle);
41 AddEquipment (equipment, "eyes", inv.equipment, XMLData.Item.EnumEquipmentSlot.Eyes, NGuiInvGridEquipment.EnumClothingLayer.Middle);
42 AddEquipment (equipment, "face", inv.equipment, XMLData.Item.EnumEquipmentSlot.Face, NGuiInvGridEquipment.EnumClothingLayer.Middle);
43
44 AddEquipment (equipment, "armor", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Outer);
45 AddEquipment (equipment, "jacket", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Middle);
46 AddEquipment (equipment, "shirt", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Inner);
47
48 AddEquipment (equipment, "legarmor", inv.equipment, XMLData.Item.EnumEquipmentSlot.Legs, NGuiInvGridEquipment.EnumClothingLayer.Outer);
49 AddEquipment (equipment, "pants", inv.equipment, XMLData.Item.EnumEquipmentSlot.Legs, NGuiInvGridEquipment.EnumClothingLayer.Inner);
50 AddEquipment (equipment, "boots", inv.equipment, XMLData.Item.EnumEquipmentSlot.Feet, NGuiInvGridEquipment.EnumClothingLayer.Inner);
51
52 AddEquipment (equipment, "gloves", inv.equipment, XMLData.Item.EnumEquipmentSlot.Hands, NGuiInvGridEquipment.EnumClothingLayer.Inner);
53 AddEquipment (equipment, "backpack", inv.equipment, XMLData.Item.EnumEquipmentSlot.Back, NGuiInvGridEquipment.EnumClothingLayer.Outer);
54
55 WriteJSON (resp, result);
56 }
57
58 private void DoInventory (JSONArray _jsonRes, List<InvItem> _inv) {
59 for (int i = 0; i < _inv.Count; i++) {
60 _jsonRes.Add (GetJsonForItem (_inv [i]));
61 }
62 }
63
64 private void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, XMLData.Item.EnumEquipmentSlot _slot, NGuiInvGridEquipment.EnumClothingLayer _layer) {
65 int index = (int)_slot + (int)_layer * (int)XMLData.Item.EnumEquipmentSlot.Count;
66 if (_items != null) {
67 _eq.Add (_slotname, GetJsonForItem (_items [index]));
68 } else {
69 _eq.Add (_slotname, new JSONNull ());
70 }
71 }
72
73 private JSONNode GetJsonForItem (InvItem _item) {
74 if (_item != null) {
75 JSONObject jsonItem = new JSONObject ();
76 jsonItem.Add ("count", new JSONNumber (_item.count));
77 jsonItem.Add ("name", new JSONString (_item.itemName));
78 jsonItem.Add ("quality", new JSONNumber (_item.quality));
79 if (_item.quality >= 0) {
80 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
81 }
82 return jsonItem;
83 } else {
84 return new JSONNull ();
85 }
86 }
87
88 }
89}
90
Note: See TracBrowser for help on using the repository browser.