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

Last change on this file since 358 was 348, checked in by alloc, 6 years ago

Fixed #156

File size: 4.7 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
5
6namespace AllocsFixes.NetConnections.Servers.Web.API {
7 public class GetPlayerInventory : WebAPI {
8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
9 int _permissionLevel) {
10 if (_req.QueryString ["steamid"] == null) {
11 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
12 Web.SetResponseTextContent (_resp, "No SteamID given");
13 return;
14 }
15
16 string steamId = _req.QueryString ["steamid"];
17
18 Player p = PersistentContainer.Instance.Players [steamId, false];
19 if (p == null) {
20 _resp.StatusCode = (int) HttpStatusCode.NotFound;
21 Web.SetResponseTextContent (_resp, "Invalid or unknown SteamID given");
22 return;
23 }
24
25 bool showIconColor, showIconName;
26 GetInventoryArguments (_req, out showIconColor, out showIconName);
27
28 JSONObject result = DoPlayer (steamId, p, showIconColor, showIconName);
29
30 WriteJSON (_resp, result);
31 }
32
33 internal static void GetInventoryArguments (HttpListenerRequest _req, out bool _showIconColor, out bool _showIconName) {
34 if (_req.QueryString ["showiconcolor"] == null || !bool.TryParse (_req.QueryString ["showiconcolor"], out _showIconColor)) {
35 _showIconColor = true;
36 }
37
38 if (_req.QueryString ["showiconname"] == null || !bool.TryParse (_req.QueryString ["showiconname"], out _showIconName)) {
39 _showIconName = true;
40 }
41 }
42
43 internal static JSONObject DoPlayer (string _steamId, Player _player, bool _showIconColor, bool _showIconName) {
44 PersistentData.Inventory inv = _player.Inventory;
45
46 JSONObject result = new JSONObject ();
47
48 JSONArray bag = new JSONArray ();
49 JSONArray belt = new JSONArray ();
50 JSONObject equipment = new JSONObject ();
51 result.Add ("steamid", new JSONString (_steamId));
52 result.Add ("entityid", new JSONNumber (_player.EntityID));
53 result.Add ("playername", new JSONString (_player.Name));
54 result.Add ("bag", bag);
55 result.Add ("belt", belt);
56 result.Add ("equipment", equipment);
57
58 DoInventory (belt, inv.belt, _showIconColor, _showIconName);
59 DoInventory (bag, inv.bag, _showIconColor, _showIconName);
60
61 AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear, _showIconColor, _showIconName);
62 AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear, _showIconColor, _showIconName);
63 AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face, _showIconColor, _showIconName);
64
65 AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor, _showIconColor, _showIconName);
66 AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket, _showIconColor, _showIconName);
67 AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt, _showIconColor, _showIconName);
68
69 AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor, _showIconColor, _showIconName);
70 AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs, _showIconColor, _showIconName);
71 AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet, _showIconColor, _showIconName);
72
73 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
74
75 return result;
76 }
77
78 private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
79 for (int i = 0; i < _inv.Count; i++) {
80 _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
81 }
82 }
83
84 private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
85 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
86
87 for (int i = 0; i < slotindices.Length; i++) {
88 if (_items != null && _items [slotindices [i]] != null) {
89 InvItem item = _items [slotindices [i]];
90 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
91 return;
92 }
93 }
94
95 _eq.Add (_slotname, new JSONNull ());
96 }
97
98 private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
99 if (_item == null) {
100 return new JSONNull ();
101 }
102
103 JSONObject jsonItem = new JSONObject ();
104 jsonItem.Add ("count", new JSONNumber (_item.count));
105 jsonItem.Add ("name", new JSONString (_item.itemName));
106
107 if (_showIconName) {
108 jsonItem.Add ("icon", new JSONString (_item.icon));
109 }
110
111 if (_showIconColor) {
112 jsonItem.Add ("iconcolor", new JSONString (_item.iconcolor));
113 }
114
115 jsonItem.Add ("quality", new JSONNumber (_item.quality));
116 if (_item.quality >= 0) {
117 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
118 }
119
120 return jsonItem;
121
122 }
123 }
124}
Note: See TracBrowser for help on using the repository browser.