- Timestamp:
- Jan 27, 2023, 7:28:00 PM (22 months ago)
- Location:
- binary-improvements2/WebServer/src/WebAPI/APIs
- Files:
-
- 1 added
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements2/WebServer/src/WebAPI/APIs/GetPlayerInventory.cs
r401 r402 1 using System.Collections.Generic; 2 using System.Net; 3 using AllocsFixes.JSON; 4 using AllocsFixes.PersistentData; 5 using JetBrains.Annotations; 6 using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 7 8 namespace Webserver.WebAPI { 9 [UsedImplicitly] 10 public class GetPlayerInventory : AbsWebAPI { 11 public override void HandleRequest (RequestContext _context) { 12 if (_context.Request.QueryString ["userid"] == null) { 13 WebUtils.WriteText (_context.Response, "No user id given", HttpStatusCode.BadRequest); 14 return; 15 } 16 17 string userIdString = _context.Request.QueryString ["userid"]; 18 if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) { 19 WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest); 20 return; 21 } 22 23 Player p = PersistentContainer.Instance.Players [userId, false]; 24 if (p == null) { 25 WebUtils.WriteText (_context.Response, "Unknown user id given", HttpStatusCode.NotFound); 26 return; 27 } 28 29 GetInventoryArguments (_context.Request, out bool showIconColor, out bool showIconName); 30 31 JsonObject result = DoPlayer (userIdString, p, showIconColor, showIconName); 32 33 WebUtils.WriteJson (_context.Response, result); 34 } 35 36 internal static void GetInventoryArguments (HttpListenerRequest _req, out bool _showIconColor, out bool _showIconName) { 37 if (_req.QueryString ["showiconcolor"] == null || !bool.TryParse (_req.QueryString ["showiconcolor"], out _showIconColor)) { 38 _showIconColor = true; 39 } 40 41 if (_req.QueryString ["showiconname"] == null || !bool.TryParse (_req.QueryString ["showiconname"], out _showIconName)) { 42 _showIconName = true; 43 } 44 } 45 46 internal static JsonObject DoPlayer (string _steamId, Player _player, bool _showIconColor, bool _showIconName) { 47 AllocsFixes.PersistentData.Inventory inv = _player.Inventory; 48 49 JsonObject result = new JsonObject (); 50 51 JsonArray bag = new JsonArray (); 52 JsonArray belt = new JsonArray (); 53 JsonObject equipment = new JsonObject (); 54 result.Add ("userid", new JsonString (_steamId)); 55 result.Add ("entityid", new JsonNumber (_player.EntityID)); 56 result.Add ("playername", new JsonString (_player.Name)); 57 result.Add ("bag", bag); 58 result.Add ("belt", belt); 59 result.Add ("equipment", equipment); 60 61 DoInventory (belt, inv.belt, _showIconColor, _showIconName); 62 DoInventory (bag, inv.bag, _showIconColor, _showIconName); 63 64 AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear, _showIconColor, _showIconName); 65 AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear, _showIconColor, _showIconName); 66 AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face, _showIconColor, _showIconName); 67 68 AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor, _showIconColor, _showIconName); 69 AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket, _showIconColor, _showIconName); 70 AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt, _showIconColor, _showIconName); 71 72 AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor, _showIconColor, _showIconName); 73 AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs, _showIconColor, _showIconName); 74 AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet, _showIconColor, _showIconName); 75 76 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName); 77 78 return result; 79 } 80 81 private static void DoInventory (JsonArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) { 82 for (int i = 0; i < _inv.Count; i++) { 83 _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName)); 84 } 85 } 86 87 private static void AddEquipment (JsonObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) { 88 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot); 89 90 for (int i = 0; i < slotindices.Length; i++) { 91 if (_items? [slotindices [i]] == null) { 92 continue; 93 } 94 95 InvItem item = _items [slotindices [i]]; 96 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName)); 97 return; 98 } 99 100 _eq.Add (_slotname, new JsonNull ()); 101 } 102 103 private static JsonNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) { 104 if (_item == null) { 105 return new JsonNull (); 106 } 107 108 JsonObject jsonItem = new JsonObject (); 109 jsonItem.Add ("count", new JsonNumber (_item.count)); 110 jsonItem.Add ("name", new JsonString (_item.itemName)); 111 112 if (_showIconName) { 113 jsonItem.Add ("icon", new JsonString (_item.icon)); 114 } 115 116 if (_showIconColor) { 117 jsonItem.Add ("iconcolor", new JsonString (_item.iconcolor)); 118 } 119 120 jsonItem.Add ("quality", new JsonNumber (_item.quality)); 121 if (_item.quality >= 0) { 122 jsonItem.Add ("qualitycolor", new JsonString (QualityInfo.GetQualityColorHex (_item.quality))); 123 } 124 125 return jsonItem; 126 127 } 128 } 129 } 1 // using System.Collections.Generic; 2 // using System.Net; 3 // using AllocsFixes.PersistentData; 4 // using JetBrains.Annotations; 5 // using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest; 6 // 7 // namespace Webserver.WebAPI.APIs { 8 // [UsedImplicitly] 9 // public class GetPlayerInventory : AbsWebAPI { 10 // public override void HandleRequest (RequestContext _context) { 11 // if (_context.Request.QueryString ["userid"] == null) { 12 // WebUtils.WriteText (_context.Response, "No user id given", HttpStatusCode.BadRequest); 13 // return; 14 // } 15 // 16 // string userIdString = _context.Request.QueryString ["userid"]; 17 // if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) { 18 // WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest); 19 // return; 20 // } 21 // 22 // Player p = PersistentContainer.Instance.Players [userId, false]; 23 // if (p == null) { 24 // WebUtils.WriteText (_context.Response, "Unknown user id given", HttpStatusCode.NotFound); 25 // return; 26 // } 27 // 28 // GetInventoryArguments (_context.Request, out bool showIconColor, out bool showIconName); 29 // 30 // JsonObject result = DoPlayer (userIdString, p, showIconColor, showIconName); 31 // 32 // WebUtils.WriteJson (_context.Response, result); 33 // } 34 // 35 // internal static void GetInventoryArguments (HttpListenerRequest _req, out bool _showIconColor, out bool _showIconName) { 36 // if (_req.QueryString ["showiconcolor"] == null || !bool.TryParse (_req.QueryString ["showiconcolor"], out _showIconColor)) { 37 // _showIconColor = true; 38 // } 39 // 40 // if (_req.QueryString ["showiconname"] == null || !bool.TryParse (_req.QueryString ["showiconname"], out _showIconName)) { 41 // _showIconName = true; 42 // } 43 // } 44 // 45 // internal static JsonObject DoPlayer (string _steamId, Player _player, bool _showIconColor, bool _showIconName) { 46 // AllocsFixes.PersistentData.Inventory inv = _player.Inventory; 47 // 48 // JsonObject result = new JsonObject (); 49 // 50 // JsonArray bag = new JsonArray (); 51 // JsonArray belt = new JsonArray (); 52 // JsonObject equipment = new JsonObject (); 53 // result.Add ("userid", new JsonString (_steamId)); 54 // result.Add ("entityid", new JsonNumber (_player.EntityID)); 55 // result.Add ("playername", new JsonString (_player.Name)); 56 // result.Add ("bag", bag); 57 // result.Add ("belt", belt); 58 // result.Add ("equipment", equipment); 59 // 60 // DoInventory (belt, inv.belt, _showIconColor, _showIconName); 61 // DoInventory (bag, inv.bag, _showIconColor, _showIconName); 62 // 63 // AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear, _showIconColor, _showIconName); 64 // AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear, _showIconColor, _showIconName); 65 // AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face, _showIconColor, _showIconName); 66 // 67 // AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor, _showIconColor, _showIconName); 68 // AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket, _showIconColor, _showIconName); 69 // AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt, _showIconColor, _showIconName); 70 // 71 // AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor, _showIconColor, _showIconName); 72 // AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs, _showIconColor, _showIconName); 73 // AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet, _showIconColor, _showIconName); 74 // 75 // AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName); 76 // 77 // return result; 78 // } 79 // 80 // private static void DoInventory (JsonArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) { 81 // for (int i = 0; i < _inv.Count; i++) { 82 // _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName)); 83 // } 84 // } 85 // 86 // private static void AddEquipment (JsonObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) { 87 // int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot); 88 // 89 // for (int i = 0; i < slotindices.Length; i++) { 90 // if (_items? [slotindices [i]] == null) { 91 // continue; 92 // } 93 // 94 // InvItem item = _items [slotindices [i]]; 95 // _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName)); 96 // return; 97 // } 98 // 99 // _eq.Add (_slotname, new JsonNull ()); 100 // } 101 // 102 // private static JsonNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) { 103 // if (_item == null) { 104 // return new JsonNull (); 105 // } 106 // 107 // JsonObject jsonItem = new JsonObject (); 108 // jsonItem.Add ("count", new JsonNumber (_item.count)); 109 // jsonItem.Add ("name", new JsonString (_item.itemName)); 110 // 111 // if (_showIconName) { 112 // jsonItem.Add ("icon", new JsonString (_item.icon)); 113 // } 114 // 115 // if (_showIconColor) { 116 // jsonItem.Add ("iconcolor", new JsonString (_item.iconcolor)); 117 // } 118 // 119 // jsonItem.Add ("quality", new JsonNumber (_item.quality)); 120 // if (_item.quality >= 0) { 121 // jsonItem.Add ("qualitycolor", new JsonString (QualityInfo.GetQualityColorHex (_item.quality))); 122 // } 123 // 124 // return jsonItem; 125 // 126 // } 127 // } 128 // }
Note:
See TracChangeset
for help on using the changeset viewer.