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

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

Fixed #156

File size: 4.7 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 {
[348]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");
[253]13 return;
14 }
15
[348]16 string steamId = _req.QueryString ["steamid"];
17
18 Player p = PersistentContainer.Instance.Players [steamId, false];
[253]19 if (p == null) {
[348]20 _resp.StatusCode = (int) HttpStatusCode.NotFound;
21 Web.SetResponseTextContent (_resp, "Invalid or unknown SteamID given");
[253]22 return;
23 }
24
[348]25 bool showIconColor, showIconName;
26 GetInventoryArguments (_req, out showIconColor, out showIconName);
[253]27
[348]28 JSONObject result = DoPlayer (steamId, p, showIconColor, showIconName);
[253]29
[348]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
[253]46 JSONObject result = new JSONObject ();
47
48 JSONArray bag = new JSONArray ();
49 JSONArray belt = new JSONArray ();
50 JSONObject equipment = new JSONObject ();
[348]51 result.Add ("steamid", new JSONString (_steamId));
52 result.Add ("entityid", new JSONNumber (_player.EntityID));
53 result.Add ("playername", new JSONString (_player.Name));
[253]54 result.Add ("bag", bag);
55 result.Add ("belt", belt);
56 result.Add ("equipment", equipment);
57
[348]58 DoInventory (belt, inv.belt, _showIconColor, _showIconName);
59 DoInventory (bag, inv.bag, _showIconColor, _showIconName);
[253]60
[348]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);
[253]64
[348]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);
[253]68
[348]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);
[253]72
[348]73 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
[253]74
[348]75 return result;
[253]76 }
77
[348]78 private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
[253]79 for (int i = 0; i < _inv.Count; i++) {
[348]80 _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
[253]81 }
82 }
83
[348]84 private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
[253]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]];
[348]90 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
[253]91 return;
92 }
93 }
94
95 _eq.Add (_slotname, new JSONNull ());
96 }
97
[348]98 private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
[326]99 if (_item == null) {
100 return new JSONNull ();
101 }
[325]102
[326]103 JSONObject jsonItem = new JSONObject ();
104 jsonItem.Add ("count", new JSONNumber (_item.count));
105 jsonItem.Add ("name", new JSONString (_item.itemName));
[348]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
[326]115 jsonItem.Add ("quality", new JSONNumber (_item.quality));
116 if (_item.quality >= 0) {
117 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
[253]118 }
[325]119
[326]120 return jsonItem;
121
[253]122 }
123 }
[325]124}
Note: See TracBrowser for help on using the repository browser.