source: binary-improvements/MapRendering/API/GetPlayerInventory.cs@ 490

Last change on this file since 490 was 488, checked in by alloc, 5 months ago

27_32_48

  • V1.0 compatibility
File size: 6.5 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.PersistentData;
4using JetBrains.Annotations;
5using Utf8Json;
6using Webserver;
7using Webserver.WebAPI;
8
9namespace 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.Head, _showIconColor, _showIconName);
97 _writer.WriteValueSeparator ();
98
99 AddEquipment (ref _writer, "armor", _equ, EquipmentSlots.Chest, _showIconColor, _showIconName);
100 _writer.WriteValueSeparator ();
101
102 AddEquipment (ref _writer, "boots", _equ, EquipmentSlots.Feet, _showIconColor, _showIconName);
103 _writer.WriteValueSeparator ();
104
105 AddEquipment (ref _writer, "gloves", _equ, EquipmentSlots.Hands, _showIconColor, _showIconName);
106
107 _writer.WriteEndObject ();
108 }
109
110 private static void AddEquipment (ref JsonWriter _writer, string _slotname, IReadOnlyList<InvItem> _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
111 _writer.WritePropertyName (_slotname);
112
113 int slotindex = (int)_slot;
114
115 if (_items == null || _items[slotindex] == null) {
116 // Slot not found / empty
117 _writer.WriteNull ();
118 return;
119 }
120
121 InvItem item = _items [slotindex];
122 GetJsonForItem (ref _writer, item, _showIconColor, _showIconName);
123 }
124
125 private static readonly byte[] jsonKeyCount = JsonWriter.GetEncodedPropertyNameWithBeginObject ("count");
126 private static readonly byte[] jsonKeyName = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("name");
127 private static readonly byte[] jsonKeyIcon = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("icon");
128 private static readonly byte[] jsonKeyIconColor = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("iconcolor");
129 private static readonly byte[] jsonKeyQuality = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("quality");
130 private static readonly byte[] jsonKeyQualityColor = JsonWriter.GetEncodedPropertyNameWithPrefixValueSeparator ("qualitycolor");
131
132 private static void GetJsonForItem (ref JsonWriter _writer, InvItem _item, bool _showIconColor, bool _showIconName) {
133 if (_item == null) {
134 _writer.WriteNull ();
135 return;
136 }
137
138 _writer.WriteRaw (jsonKeyCount);
139 _writer.WriteInt32 (_item.count);
140
141 _writer.WriteRaw (jsonKeyName);
142 _writer.WriteString (_item.itemName);
143
144 if (_showIconName) {
145 _writer.WriteRaw (jsonKeyIcon);
146 _writer.WriteString (_item.icon);
147 }
148
149 if (_showIconColor) {
150 _writer.WriteRaw (jsonKeyIconColor);
151 _writer.WriteString (_item.iconcolor);
152 }
153
154 _writer.WriteRaw (jsonKeyQuality);
155 _writer.WriteInt32 (_item.quality);
156
157 if (_item.quality >= 0) {
158 _writer.WriteRaw (jsonKeyQualityColor);
159 _writer.WriteString (QualityInfo.GetQualityColorHex (_item.quality));
160 }
161
162 _writer.WriteEndObject ();
163 }
164 }
165}
Note: See TracBrowser for help on using the repository browser.