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