1 | using System.Collections.Generic;
|
---|
2 | using System.Net;
|
---|
3 | using AllocsFixes.PersistentData;
|
---|
4 | using JetBrains.Annotations;
|
---|
5 | using Utf8Json;
|
---|
6 | using Webserver;
|
---|
7 | using Webserver.WebAPI;
|
---|
8 |
|
---|
9 | namespace AllocsFixes.WebAPIs {
|
---|
10 | [UsedImplicitly]
|
---|
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);
|
---|
15 | return;
|
---|
16 | }
|
---|
17 |
|
---|
18 | string userIdString = _context.Request.QueryString ["userid"];
|
---|
19 | if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) {
|
---|
20 | WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest);
|
---|
21 | return;
|
---|
22 | }
|
---|
23 |
|
---|
24 | Player p = PersistentContainer.Instance.Players.GetByUserId (userId);
|
---|
25 | if (p == null) {
|
---|
26 | WebUtils.WriteText (_context.Response, "Unknown user id given", HttpStatusCode.NotFound);
|
---|
27 | return;
|
---|
28 | }
|
---|
29 |
|
---|
30 | GetInventoryArguments (_context, out bool showIconColor, out bool showIconName);
|
---|
31 |
|
---|
32 | JsonWriter writer = new JsonWriter ();
|
---|
33 | DoPlayer (ref writer, p, showIconColor, showIconName);
|
---|
34 | WebUtils.WriteJsonData (_context.Response, ref writer);
|
---|
35 | }
|
---|
36 |
|
---|
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)) {
|
---|
39 | _showIconColor = true;
|
---|
40 | }
|
---|
41 |
|
---|
42 | if (_context.Request.QueryString ["showiconname"] == null || !bool.TryParse (_context.Request.QueryString ["showiconname"], out _showIconName)) {
|
---|
43 | _showIconName = true;
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
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) {
|
---|
56 | PersistentData.Inventory inv = _player.Inventory;
|
---|
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 | }
|
---|
81 |
|
---|
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 | }
|
---|
92 |
|
---|
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 ();
|
---|
102 |
|
---|
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 ();
|
---|
109 |
|
---|
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 ();
|
---|
116 |
|
---|
117 | AddEquipment (ref _writer, "gloves", _equ, EquipmentSlots.Hands, _showIconColor, _showIconName);
|
---|
118 |
|
---|
119 | _writer.WriteEndObject ();
|
---|
120 | }
|
---|
121 |
|
---|
122 | private static void AddEquipment (ref JsonWriter _writer, string _slotname, IReadOnlyList<InvItem> _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
|
---|
123 | _writer.WritePropertyName (_slotname);
|
---|
124 |
|
---|
125 | int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
|
---|
126 |
|
---|
127 | for (int i = 0; i < slotindices.Length; i++) {
|
---|
128 | if (_items? [slotindices [i]] != null) {
|
---|
129 | InvItem item = _items [slotindices [i]];
|
---|
130 |
|
---|
131 | GetJsonForItem (ref _writer, item, _showIconColor, _showIconName);
|
---|
132 | return;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | // Slot not found / empty
|
---|
137 | _writer.WriteNull ();
|
---|
138 | }
|
---|
139 |
|
---|
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) {
|
---|
148 | if (_item == null) {
|
---|
149 | _writer.WriteNull ();
|
---|
150 | return;
|
---|
151 | }
|
---|
152 |
|
---|
153 | _writer.WriteRaw (jsonKeyCount);
|
---|
154 | _writer.WriteInt32 (_item.count);
|
---|
155 |
|
---|
156 | _writer.WriteRaw (jsonKeyName);
|
---|
157 | _writer.WriteString (_item.itemName);
|
---|
158 |
|
---|
159 | if (_showIconName) {
|
---|
160 | _writer.WriteRaw (jsonKeyIcon);
|
---|
161 | _writer.WriteString (_item.icon);
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (_showIconColor) {
|
---|
165 | _writer.WriteRaw (jsonKeyIconColor);
|
---|
166 | _writer.WriteString (_item.iconcolor);
|
---|
167 | }
|
---|
168 |
|
---|
169 | _writer.WriteRaw (jsonKeyQuality);
|
---|
170 | _writer.WriteInt32 (_item.quality);
|
---|
171 |
|
---|
172 | if (_item.quality >= 0) {
|
---|
173 | _writer.WriteRaw (jsonKeyQualityColor);
|
---|
174 | _writer.WriteString (QualityInfo.GetQualityColorHex (_item.quality));
|
---|
175 | }
|
---|
176 |
|
---|
177 | _writer.WriteEndObject ();
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|