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

Last change on this file since 330 was 326, checked in by alloc, 6 years ago

More cleanup, allocation improvements

File size: 3.2 KB
RevLine 
[325]1using System.Collections.Generic;
2using System.Net;
[253]3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
5
[325]6namespace AllocsFixes.NetConnections.Servers.Web.API {
[253]7 public class GetPlayerInventory : WebAPI {
[325]8 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
9 int permissionLevel) {
[253]10 if (req.QueryString ["steamid"] == null) {
[325]11 resp.StatusCode = (int) HttpStatusCode.InternalServerError;
[253]12 Web.SetResponseTextContent (resp, "No SteamID given");
13 return;
14 }
15
16 Player p = PersistentContainer.Instance.Players [req.QueryString ["steamid"], false];
17 if (p == null) {
[325]18 resp.StatusCode = (int) HttpStatusCode.InternalServerError;
[253]19 Web.SetResponseTextContent (resp, "Invalid or unknown SteamID given");
20 return;
21 }
22
23 PersistentData.Inventory inv = p.Inventory;
24
25
26 JSONObject result = new JSONObject ();
27
28 JSONArray bag = new JSONArray ();
29 JSONArray belt = new JSONArray ();
30 JSONObject equipment = new JSONObject ();
31 result.Add ("playername", new JSONString (p.Name));
32 result.Add ("bag", bag);
33 result.Add ("belt", belt);
34 result.Add ("equipment", equipment);
35
36 DoInventory (belt, inv.belt);
37 DoInventory (bag, inv.bag);
38
39 AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear);
40 AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear);
41 AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face);
42
43 AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor);
44 AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket);
45 AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt);
46
47 AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor);
48 AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs);
49 AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet);
50
51 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands);
52
53 WriteJSON (resp, result);
54 }
55
[321]56 internal static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv) {
[253]57 for (int i = 0; i < _inv.Count; i++) {
58 _jsonRes.Add (GetJsonForItem (_inv [i]));
59 }
60 }
61
[321]62 internal static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot) {
[253]63 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
64
65 for (int i = 0; i < slotindices.Length; i++) {
66 if (_items != null && _items [slotindices [i]] != null) {
67 InvItem item = _items [slotindices [i]];
68 _eq.Add (_slotname, GetJsonForItem (item));
69 return;
70 }
71 }
72
73 _eq.Add (_slotname, new JSONNull ());
74 }
75
[321]76 internal static JSONNode GetJsonForItem (InvItem _item) {
[326]77 if (_item == null) {
78 return new JSONNull ();
79 }
[325]80
[326]81 JSONObject jsonItem = new JSONObject ();
82 jsonItem.Add ("count", new JSONNumber (_item.count));
83 jsonItem.Add ("name", new JSONString (_item.itemName));
84 jsonItem.Add ("icon", new JSONString (_item.icon));
85 jsonItem.Add ("iconcolor", new JSONString (_item.iconcolor));
86 jsonItem.Add ("quality", new JSONNumber (_item.quality));
87 if (_item.quality >= 0) {
88 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
[253]89 }
[325]90
[326]91 return jsonItem;
92
[253]93 }
94 }
[325]95}
Note: See TracBrowser for help on using the repository browser.