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

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

Fixes 6_8_10

File size: 3.2 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
11 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
12 if (req.QueryString ["steamid"] == null) {
13 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
14 Web.SetResponseTextContent (resp, "No SteamID given");
15 return;
16 }
17
18 Player p = PersistentContainer.Instance.Players [req.QueryString ["steamid"], false];
19 if (p == null) {
20 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
21 Web.SetResponseTextContent (resp, "Invalid or unknown SteamID given");
22 return;
23 }
24
25 PersistentData.Inventory inv = p.Inventory;
26
27
28 JSONObject result = new JSONObject ();
29
30 JSONArray bag = new JSONArray ();
31 JSONArray belt = new JSONArray ();
32 JSONObject equipment = new JSONObject ();
33 result.Add ("playername", new JSONString (p.Name));
34 result.Add ("bag", bag);
35 result.Add ("belt", belt);
36 result.Add ("equipment", equipment);
37
38 DoInventory (belt, inv.belt);
39 DoInventory (bag, inv.bag);
40
41 AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear);
42 AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear);
43 AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face);
44
45 AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor);
46 AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket);
47 AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt);
48
49 AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor);
50 AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs);
51 AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet);
52
53 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands);
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, EquipmentSlots _slot) {
65 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
66
67 for (int i = 0; i < slotindices.Length; i++) {
68 if (_items != null && _items [slotindices [i]] != null) {
69 InvItem item = _items [slotindices [i]];
70 _eq.Add (_slotname, GetJsonForItem (item));
71 return;
72 }
73 }
74
75 _eq.Add (_slotname, new JSONNull ());
76 }
77
78 private JSONNode GetJsonForItem (InvItem _item) {
79 if (_item != null) {
80 JSONObject jsonItem = new JSONObject ();
81 jsonItem.Add ("count", new JSONNumber (_item.count));
82 jsonItem.Add ("name", new JSONString (_item.itemName));
83 jsonItem.Add ("icon", new JSONString (_item.icon));
84 jsonItem.Add ("iconcolor", new JSONString (_item.iconcolor));
85 jsonItem.Add ("quality", new JSONNumber (_item.quality));
86 if (_item.quality >= 0) {
87 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
88 }
89 return jsonItem;
90 } else {
91 return new JSONNull ();
92 }
93 }
94
95 }
96}
97
Note: See TracBrowser for help on using the repository browser.