[325] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Net;
|
---|
[253] | 3 | using AllocsFixes.JSON;
|
---|
| 4 | using AllocsFixes.PersistentData;
|
---|
[454] | 5 | using Webserver;
|
---|
| 6 | using Webserver.WebAPI;
|
---|
[253] | 7 |
|
---|
[454] | 8 | namespace AllocsFixes.WebAPIs {
|
---|
| 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);
|
---|
[253] | 13 | return;
|
---|
| 14 | }
|
---|
| 15 |
|
---|
[454] | 16 | string userIdString = _context.Request.QueryString ["userid"];
|
---|
[369] | 17 | if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) {
|
---|
[454] | 18 | WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest);
|
---|
[369] | 19 | return;
|
---|
| 20 | }
|
---|
[348] | 21 |
|
---|
[446] | 22 | Player p = PersistentContainer.Instance.Players.GetByUserId (userId);
|
---|
[253] | 23 | if (p == null) {
|
---|
[454] | 24 | WebUtils.WriteText (_context.Response, "Unknown user id given", HttpStatusCode.NotFound);
|
---|
[253] | 25 | return;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
[454] | 28 | GetInventoryArguments (_context, out bool showIconColor, out bool showIconName);
|
---|
[253] | 29 |
|
---|
[446] | 30 | JSONObject result = DoPlayer (p, showIconColor, showIconName);
|
---|
[253] | 31 |
|
---|
[454] | 32 | LegacyApiHelper.WriteJSON (_context.Response, result);
|
---|
[348] | 33 | }
|
---|
| 34 |
|
---|
[454] | 35 | internal static void GetInventoryArguments (RequestContext _context, out bool _showIconColor, out bool _showIconName) {
|
---|
| 36 | if (_context.Request.QueryString ["showiconcolor"] == null || !bool.TryParse (_context.Request.QueryString ["showiconcolor"], out _showIconColor)) {
|
---|
[348] | 37 | _showIconColor = true;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
[454] | 40 | if (_context.Request.QueryString ["showiconname"] == null || !bool.TryParse (_context.Request.QueryString ["showiconname"], out _showIconName)) {
|
---|
[348] | 41 | _showIconName = true;
|
---|
| 42 | }
|
---|
| 43 | }
|
---|
| 44 |
|
---|
[446] | 45 | internal static JSONObject DoPlayer (Player _player, bool _showIconColor, bool _showIconName) {
|
---|
[348] | 46 | PersistentData.Inventory inv = _player.Inventory;
|
---|
| 47 |
|
---|
[253] | 48 | JSONObject result = new JSONObject ();
|
---|
| 49 |
|
---|
| 50 | JSONArray bag = new JSONArray ();
|
---|
| 51 | JSONArray belt = new JSONArray ();
|
---|
| 52 | JSONObject equipment = new JSONObject ();
|
---|
[446] | 53 | result.Add ("userid", new JSONString (_player.PlatformId.CombinedString));
|
---|
| 54 | result.Add ("crossplatformid", new JSONString (_player.CrossPlatformId?.CombinedString ?? ""));
|
---|
[348] | 55 | result.Add ("entityid", new JSONNumber (_player.EntityID));
|
---|
| 56 | result.Add ("playername", new JSONString (_player.Name));
|
---|
[253] | 57 | result.Add ("bag", bag);
|
---|
| 58 | result.Add ("belt", belt);
|
---|
| 59 | result.Add ("equipment", equipment);
|
---|
| 60 |
|
---|
[348] | 61 | DoInventory (belt, inv.belt, _showIconColor, _showIconName);
|
---|
| 62 | DoInventory (bag, inv.bag, _showIconColor, _showIconName);
|
---|
[253] | 63 |
|
---|
[348] | 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);
|
---|
[253] | 67 |
|
---|
[348] | 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);
|
---|
[253] | 71 |
|
---|
[348] | 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);
|
---|
[253] | 75 |
|
---|
[348] | 76 | AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
|
---|
[253] | 77 |
|
---|
[348] | 78 | return result;
|
---|
[253] | 79 | }
|
---|
| 80 |
|
---|
[348] | 81 | private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
|
---|
[253] | 82 | for (int i = 0; i < _inv.Count; i++) {
|
---|
[348] | 83 | _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
|
---|
[253] | 84 | }
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[348] | 87 | private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
|
---|
[253] | 88 | int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
|
---|
| 89 |
|
---|
| 90 | for (int i = 0; i < slotindices.Length; i++) {
|
---|
[369] | 91 | if (_items? [slotindices [i]] != null) {
|
---|
[253] | 92 | InvItem item = _items [slotindices [i]];
|
---|
[348] | 93 | _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
|
---|
[253] | 94 | return;
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | _eq.Add (_slotname, new JSONNull ());
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[348] | 101 | private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
|
---|
[326] | 102 | if (_item == null) {
|
---|
| 103 | return new JSONNull ();
|
---|
| 104 | }
|
---|
[325] | 105 |
|
---|
[326] | 106 | JSONObject jsonItem = new JSONObject ();
|
---|
| 107 | jsonItem.Add ("count", new JSONNumber (_item.count));
|
---|
| 108 | jsonItem.Add ("name", new JSONString (_item.itemName));
|
---|
[348] | 109 |
|
---|
| 110 | if (_showIconName) {
|
---|
| 111 | jsonItem.Add ("icon", new JSONString (_item.icon));
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | if (_showIconColor) {
|
---|
| 115 | jsonItem.Add ("iconcolor", new JSONString (_item.iconcolor));
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[326] | 118 | jsonItem.Add ("quality", new JSONNumber (_item.quality));
|
---|
| 119 | if (_item.quality >= 0) {
|
---|
| 120 | jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
|
---|
[253] | 121 | }
|
---|
[325] | 122 |
|
---|
[326] | 123 | return jsonItem;
|
---|
| 124 |
|
---|
[253] | 125 | }
|
---|
| 126 | }
|
---|
[325] | 127 | }
|
---|