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

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

27_32_48

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