[325] | 1 | using System.Collections.Generic;
|
---|
| 2 | using System.Net;
|
---|
[253] | 3 | using AllocsFixes.PersistentData;
|
---|
[455] | 4 | using JetBrains.Annotations;
|
---|
| 5 | using Utf8Json;
|
---|
[454] | 6 | using Webserver;
|
---|
| 7 | using Webserver.WebAPI;
|
---|
[253] | 8 |
|
---|
[454] | 9 | namespace AllocsFixes.WebAPIs {
|
---|
[455] | 10 | [UsedImplicitly]
|
---|
[454] | 11 | public class GetPlayerInventory : AbsWebAPI {
|
---|
| 12 | public override void HandleRequest (RequestContext _context) {
|
---|
| 13 | if (_context.Request.QueryString ["userid"] == null) {
|
---|
| 14 | WebUtils.WriteText (_context.Response, "No user id given", HttpStatusCode.BadRequest);
|
---|
[253] | 15 | return;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
[454] | 18 | string userIdString = _context.Request.QueryString ["userid"];
|
---|
[369] | 19 | if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) {
|
---|
[454] | 20 | WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest);
|
---|
[369] | 21 | return;
|
---|
| 22 | }
|
---|
[348] | 23 |
|
---|
[446] | 24 | Player p = PersistentContainer.Instance.Players.GetByUserId (userId);
|
---|
[253] | 25 | if (p == null) {
|
---|
[454] | 26 | WebUtils.WriteText (_context.Response, "Unknown user id given", HttpStatusCode.NotFound);
|
---|
[253] | 27 | return;
|
---|
| 28 | }
|
---|
| 29 |
|
---|
[454] | 30 | GetInventoryArguments (_context, out bool showIconColor, out bool showIconName);
|
---|
[253] | 31 |
|
---|
[455] | 32 | JsonWriter writer = new JsonWriter ();
|
---|
| 33 | DoPlayer (ref writer, p, showIconColor, showIconName);
|
---|
| 34 | WebUtils.WriteJsonData (_context.Response, ref writer);
|
---|
[348] | 35 | }
|
---|
| 36 |
|
---|
[454] | 37 | internal static void GetInventoryArguments (RequestContext _context, out bool _showIconColor, out bool _showIconName) {
|
---|
| 38 | if (_context.Request.QueryString ["showiconcolor"] == null || !bool.TryParse (_context.Request.QueryString ["showiconcolor"], out _showIconColor)) {
|
---|
[348] | 39 | _showIconColor = true;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
[454] | 42 | if (_context.Request.QueryString ["showiconname"] == null || !bool.TryParse (_context.Request.QueryString ["showiconname"], out _showIconName)) {
|
---|
[348] | 43 | _showIconName = true;
|
---|
| 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
[455] | 47 | private static readonly byte[] jsonKeyUserId = JsonWriter.GetEncodedPropertyNameWithBeginObject ("userid");
|
---|
| 48 | private static readonly byte[] jsonKeyCrossPlatformId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("crossplatformid");
|
---|
| 49 | private static readonly byte[] jsonKeyEntityId = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("entityid");
|
---|
| 50 | private static readonly byte[] jsonKeyPlayerName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("playername");
|
---|
| 51 | private static readonly byte[] jsonKeyBag = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("bag");
|
---|
| 52 | private static readonly byte[] jsonKeyBelt = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("belt");
|
---|
| 53 | private static readonly byte[] jsonKeyEquipment = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("equipment");
|
---|
| 54 |
|
---|
| 55 | internal static void DoPlayer (ref JsonWriter _writer, Player _player, bool _showIconColor, bool _showIconName) {
|
---|
[348] | 56 | PersistentData.Inventory inv = _player.Inventory;
|
---|
[455] | 57 |
|
---|
| 58 | _writer.WriteRaw (jsonKeyUserId);
|
---|
| 59 | _writer.WriteString (_player.PlatformId.CombinedString);
|
---|
| 60 |
|
---|
| 61 | _writer.WriteRaw (jsonKeyCrossPlatformId);
|
---|
| 62 | _writer.WriteString (_player.CrossPlatformId?.CombinedString ?? "");
|
---|
| 63 |
|
---|
| 64 | _writer.WriteRaw (jsonKeyEntityId);
|
---|
| 65 | _writer.WriteInt32 (_player.EntityID);
|
---|
| 66 |
|
---|
| 67 | _writer.WriteRaw (jsonKeyPlayerName);
|
---|
| 68 | _writer.WriteString (_player.Name);
|
---|
| 69 |
|
---|
| 70 | _writer.WriteRaw (jsonKeyBag);
|
---|
| 71 | DoInventory (ref _writer, inv.bag, _showIconColor, _showIconName);
|
---|
| 72 |
|
---|
| 73 | _writer.WriteRaw (jsonKeyBelt);
|
---|
| 74 | DoInventory (ref _writer, inv.belt, _showIconColor, _showIconName);
|
---|
| 75 |
|
---|
| 76 | _writer.WriteRaw (jsonKeyEquipment);
|
---|
| 77 | DoEquipment (ref _writer, inv.equipment, _showIconColor, _showIconName);
|
---|
| 78 |
|
---|
| 79 | _writer.WriteEndObject ();
|
---|
| 80 | }
|
---|
[348] | 81 |
|
---|
[455] | 82 | private static void DoInventory (ref JsonWriter _writer, IReadOnlyList<InvItem> _inv, bool _showIconColor, bool _showIconName) {
|
---|
| 83 | _writer.WriteBeginArray ();
|
---|
| 84 | for (int i = 0; i < _inv.Count; i++) {
|
---|
| 85 | if (i > 0) {
|
---|
| 86 | _writer.WriteValueSeparator ();
|
---|
| 87 | }
|
---|
| 88 | GetJsonForItem (ref _writer, _inv [i], _showIconColor, _showIconName);
|
---|
| 89 | }
|
---|
| 90 | _writer.WriteEndArray ();
|
---|
| 91 | }
|
---|
[253] | 92 |
|
---|
[455] | 93 | private static void DoEquipment (ref JsonWriter _writer, IReadOnlyList<InvItem> _equ, bool _showIconColor, bool _showIconName) {
|
---|
| 94 | _writer.WriteBeginObject ();
|
---|
| 95 |
|
---|
| 96 | AddEquipment (ref _writer, "head", _equ, EquipmentSlots.Headgear, _showIconColor, _showIconName);
|
---|
| 97 | _writer.WriteValueSeparator ();
|
---|
| 98 | AddEquipment (ref _writer, "eyes", _equ, EquipmentSlots.Eyewear, _showIconColor, _showIconName);
|
---|
| 99 | _writer.WriteValueSeparator ();
|
---|
| 100 | AddEquipment (ref _writer, "face", _equ, EquipmentSlots.Face, _showIconColor, _showIconName);
|
---|
| 101 | _writer.WriteValueSeparator ();
|
---|
[253] | 102 |
|
---|
[455] | 103 | AddEquipment (ref _writer, "armor", _equ, EquipmentSlots.ChestArmor, _showIconColor, _showIconName);
|
---|
| 104 | _writer.WriteValueSeparator ();
|
---|
| 105 | AddEquipment (ref _writer, "jacket", _equ, EquipmentSlots.Jacket, _showIconColor, _showIconName);
|
---|
| 106 | _writer.WriteValueSeparator ();
|
---|
| 107 | AddEquipment (ref _writer, "shirt", _equ, EquipmentSlots.Shirt, _showIconColor, _showIconName);
|
---|
| 108 | _writer.WriteValueSeparator ();
|
---|
[253] | 109 |
|
---|
[455] | 110 | AddEquipment (ref _writer, "legarmor", _equ, EquipmentSlots.LegArmor, _showIconColor, _showIconName);
|
---|
| 111 | _writer.WriteValueSeparator ();
|
---|
| 112 | AddEquipment (ref _writer, "pants", _equ, EquipmentSlots.Legs, _showIconColor, _showIconName);
|
---|
| 113 | _writer.WriteValueSeparator ();
|
---|
| 114 | AddEquipment (ref _writer, "boots", _equ, EquipmentSlots.Feet, _showIconColor, _showIconName);
|
---|
| 115 | _writer.WriteValueSeparator ();
|
---|
[253] | 116 |
|
---|
[455] | 117 | AddEquipment (ref _writer, "gloves", _equ, EquipmentSlots.Hands, _showIconColor, _showIconName);
|
---|
| 118 |
|
---|
| 119 | _writer.WriteEndObject ();
|
---|
[253] | 120 | }
|
---|
| 121 |
|
---|
[455] | 122 | private static void AddEquipment (ref JsonWriter _writer, string _slotname, IReadOnlyList<InvItem> _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
|
---|
| 123 | _writer.WritePropertyName (_slotname);
|
---|
[253] | 124 |
|
---|
| 125 | int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
|
---|
| 126 |
|
---|
| 127 | for (int i = 0; i < slotindices.Length; i++) {
|
---|
[369] | 128 | if (_items? [slotindices [i]] != null) {
|
---|
[253] | 129 | InvItem item = _items [slotindices [i]];
|
---|
[455] | 130 |
|
---|
| 131 | GetJsonForItem (ref _writer, item, _showIconColor, _showIconName);
|
---|
[253] | 132 | return;
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 |
|
---|
[455] | 136 | // Slot not found / empty
|
---|
| 137 | _writer.WriteNull ();
|
---|
[253] | 138 | }
|
---|
| 139 |
|
---|
[455] | 140 | private static readonly byte[] jsonKeyCount = JsonWriter.GetEncodedPropertyNameWithBeginObject ("count");
|
---|
| 141 | private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name");
|
---|
| 142 | private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon");
|
---|
| 143 | private static readonly byte[] jsonKeyIconColor = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("iconcolor");
|
---|
| 144 | private static readonly byte[] jsonKeyQuality = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("quality");
|
---|
| 145 | private static readonly byte[] jsonKeyQualityColor = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("qualitycolor");
|
---|
| 146 |
|
---|
| 147 | private static void GetJsonForItem (ref JsonWriter _writer, InvItem _item, bool _showIconColor, bool _showIconName) {
|
---|
[326] | 148 | if (_item == null) {
|
---|
[455] | 149 | _writer.WriteNull ();
|
---|
| 150 | return;
|
---|
[326] | 151 | }
|
---|
[348] | 152 |
|
---|
[455] | 153 | _writer.WriteRaw (jsonKeyCount);
|
---|
| 154 | _writer.WriteInt32 (_item.count);
|
---|
| 155 |
|
---|
| 156 | _writer.WriteRaw (jsonKeyName);
|
---|
| 157 | _writer.WriteString (_item.itemName);
|
---|
| 158 |
|
---|
[348] | 159 | if (_showIconName) {
|
---|
[455] | 160 | _writer.WriteRaw (jsonKeyIcon);
|
---|
| 161 | _writer.WriteString (_item.icon);
|
---|
[348] | 162 | }
|
---|
| 163 |
|
---|
| 164 | if (_showIconColor) {
|
---|
[455] | 165 | _writer.WriteRaw (jsonKeyIconColor);
|
---|
| 166 | _writer.WriteString (_item.iconcolor);
|
---|
[348] | 167 | }
|
---|
| 168 |
|
---|
[455] | 169 | _writer.WriteRaw (jsonKeyQuality);
|
---|
| 170 | _writer.WriteInt32 (_item.quality);
|
---|
| 171 |
|
---|
[326] | 172 | if (_item.quality >= 0) {
|
---|
[455] | 173 | _writer.WriteRaw (jsonKeyQualityColor);
|
---|
| 174 | _writer.WriteString (QualityInfo.GetQualityColorHex (_item.quality));
|
---|
[253] | 175 | }
|
---|
[455] | 176 |
|
---|
| 177 | _writer.WriteEndObject ();
|
---|
[253] | 178 | }
|
---|
| 179 | }
|
---|
[325] | 180 | }
|
---|