Changeset 348


Ignore:
Timestamp:
Jan 19, 2019, 5:13:47 PM (6 years ago)
Author:
alloc
Message:

Fixed #156

Location:
binary-improvements/MapRendering/Web/API
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • binary-improvements/MapRendering/Web/API/GetPlayerInventories.cs

    r332 r348  
    66namespace AllocsFixes.NetConnections.Servers.Web.API {
    77        public class GetPlayerInventories : WebAPI {
    8                 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user,
    9                         int permissionLevel) {
     8                public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
     9                        int _permissionLevel) {
     10
     11                        bool showIconColor, showIconName;
     12                        GetPlayerInventory.GetInventoryArguments (_req, out showIconColor, out showIconName);
     13
    1014                        JSONArray AllInventoriesResult = new JSONArray ();
    1115
     
    1822
    1923                                if (p.IsOnline) {
    20                                         PersistentData.Inventory inv = p.Inventory;
    21 
    22                                         JSONObject result = new JSONObject ();
    23                                         JSONArray bag = new JSONArray ();
    24                                         JSONArray belt = new JSONArray ();
    25                                         JSONObject equipment = new JSONObject ();
    26                                         result.Add ("steamid", new JSONString (kvp.Key));
    27                                         result.Add ("entityid", new JSONNumber (p.EntityID));
    28                                         result.Add ("playername", new JSONString (p.Name));
    29                                         result.Add ("bag", bag);
    30                                         result.Add ("belt", belt);
    31                                         result.Add ("equipment", equipment);
    32 
    33                                         GetPlayerInventory.DoInventory (belt, inv.belt);
    34                                         GetPlayerInventory.DoInventory (bag, inv.bag);
    35 
    36                                         GetPlayerInventory.AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear);
    37                                         GetPlayerInventory.AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear);
    38                                         GetPlayerInventory.AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face);
    39 
    40                                         GetPlayerInventory.AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor);
    41                                         GetPlayerInventory.AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket);
    42                                         GetPlayerInventory.AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt);
    43 
    44                                         GetPlayerInventory.AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor);
    45                                         GetPlayerInventory.AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs);
    46                                         GetPlayerInventory.AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet);
    47 
    48                                         GetPlayerInventory.AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands);
    49 
    50                                         AllInventoriesResult.Add (result);
     24                                        AllInventoriesResult.Add (GetPlayerInventory.DoPlayer (kvp.Key, p, showIconName, showIconName));
    5125                                }
    5226                        }
    5327
    54                         WriteJSON (resp, AllInventoriesResult);
     28                        WriteJSON (_resp, AllInventoriesResult);
    5529                }
    5630        }
  • binary-improvements/MapRendering/Web/API/GetPlayerInventory.cs

    r326 r348  
    66namespace AllocsFixes.NetConnections.Servers.Web.API {
    77        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.InternalServerError;
    12                                 Web.SetResponseTextContent (resp, "No SteamID given");
     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");
    1313                                return;
    1414                        }
    1515
    16                         Player p = PersistentContainer.Instance.Players [req.QueryString ["steamid"], false];
     16                        string steamId = _req.QueryString ["steamid"];
     17
     18                        Player p = PersistentContainer.Instance.Players [steamId, false];
    1719                        if (p == null) {
    18                                 resp.StatusCode = (int) HttpStatusCode.InternalServerError;
    19                                 Web.SetResponseTextContent (resp, "Invalid or unknown SteamID given");
     20                                _resp.StatusCode = (int) HttpStatusCode.NotFound;
     21                                Web.SetResponseTextContent (_resp, "Invalid or unknown SteamID given");
    2022                                return;
    2123                        }
    2224
    23                         PersistentData.Inventory inv = p.Inventory;
     25                        bool showIconColor, showIconName;
     26                        GetInventoryArguments (_req, out showIconColor, out showIconName);
    2427
     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;
    2545
    2646                        JSONObject result = new JSONObject ();
     
    2949                        JSONArray belt = new JSONArray ();
    3050                        JSONObject equipment = new JSONObject ();
    31                         result.Add ("playername", new JSONString (p.Name));
     51                        result.Add ("steamid", new JSONString (_steamId));
     52                        result.Add ("entityid", new JSONNumber (_player.EntityID));
     53                        result.Add ("playername", new JSONString (_player.Name));
    3254                        result.Add ("bag", bag);
    3355                        result.Add ("belt", belt);
    3456                        result.Add ("equipment", equipment);
    3557
    36                         DoInventory (belt, inv.belt);
    37                         DoInventory (bag, inv.bag);
     58                        DoInventory (belt, inv.belt, _showIconColor, _showIconName);
     59                        DoInventory (bag, inv.bag, _showIconColor, _showIconName);
    3860
    39                         AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear);
    40                         AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear);
    41                         AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face);
     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);
    4264
    43                         AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor);
    44                         AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket);
    45                         AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt);
     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);
    4668
    47                         AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor);
    48                         AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs);
    49                         AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet);
     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);
    5072
    51                         AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands);
     73                        AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
    5274
    53                         WriteJSON (resp, result);
     75                        return result;
    5476                }
    5577
    56                 internal static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv) {
     78                private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
    5779                        for (int i = 0; i < _inv.Count; i++) {
    58                                 _jsonRes.Add (GetJsonForItem (_inv [i]));
     80                                _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
    5981                        }
    6082                }
    6183
    62                 internal static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot) {
     84                private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
    6385                        int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
    6486
     
    6688                                if (_items != null && _items [slotindices [i]] != null) {
    6789                                        InvItem item = _items [slotindices [i]];
    68                                         _eq.Add (_slotname, GetJsonForItem (item));
     90                                        _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
    6991                                        return;
    7092                                }
     
    7496                }
    7597
    76                 internal static JSONNode GetJsonForItem (InvItem _item) {
     98                private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
    7799                        if (_item == null) {
    78100                                return new JSONNull ();
     
    82104                        jsonItem.Add ("count", new JSONNumber (_item.count));
    83105                        jsonItem.Add ("name", new JSONString (_item.itemName));
    84                         jsonItem.Add ("icon", new JSONString (_item.icon));
    85                         jsonItem.Add ("iconcolor", new JSONString (_item.iconcolor));
     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
    86115                        jsonItem.Add ("quality", new JSONNumber (_item.quality));
    87116                        if (_item.quality >= 0) {
Note: See TracChangeset for help on using the changeset viewer.