source: binary-improvements2/WebServer/src/WebAPI/APIs/GetPlayerInventory.cs@ 414

Last change on this file since 414 was 402, checked in by alloc, 22 months ago
  • Major refactoring
  • Using Utf8Json for (de)serialization
  • Moving APIs to REST
  • Removing dependencies from WebServer and MapRenderer to ServerFixes
File size: 5.3 KB
RevLine 
[402]1// using System.Collections.Generic;
2// using System.Net;
3// using AllocsFixes.PersistentData;
4// using JetBrains.Annotations;
5// using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
6//
7// namespace Webserver.WebAPI.APIs {
8// [UsedImplicitly]
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);
13// return;
14// }
15//
16// string userIdString = _context.Request.QueryString ["userid"];
17// if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) {
18// WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest);
19// return;
20// }
21//
22// Player p = PersistentContainer.Instance.Players [userId, false];
23// if (p == null) {
24// WebUtils.WriteText (_context.Response, "Unknown user id given", HttpStatusCode.NotFound);
25// return;
26// }
27//
28// GetInventoryArguments (_context.Request, out bool showIconColor, out bool showIconName);
29//
30// JsonObject result = DoPlayer (userIdString, p, showIconColor, showIconName);
31//
32// WebUtils.WriteJson (_context.Response, result);
33// }
34//
35// internal static void GetInventoryArguments (HttpListenerRequest _req, out bool _showIconColor, out bool _showIconName) {
36// if (_req.QueryString ["showiconcolor"] == null || !bool.TryParse (_req.QueryString ["showiconcolor"], out _showIconColor)) {
37// _showIconColor = true;
38// }
39//
40// if (_req.QueryString ["showiconname"] == null || !bool.TryParse (_req.QueryString ["showiconname"], out _showIconName)) {
41// _showIconName = true;
42// }
43// }
44//
45// internal static JsonObject DoPlayer (string _steamId, Player _player, bool _showIconColor, bool _showIconName) {
46// AllocsFixes.PersistentData.Inventory inv = _player.Inventory;
47//
48// JsonObject result = new JsonObject ();
49//
50// JsonArray bag = new JsonArray ();
51// JsonArray belt = new JsonArray ();
52// JsonObject equipment = new JsonObject ();
53// result.Add ("userid", new JsonString (_steamId));
54// result.Add ("entityid", new JsonNumber (_player.EntityID));
55// result.Add ("playername", new JsonString (_player.Name));
56// result.Add ("bag", bag);
57// result.Add ("belt", belt);
58// result.Add ("equipment", equipment);
59//
60// DoInventory (belt, inv.belt, _showIconColor, _showIconName);
61// DoInventory (bag, inv.bag, _showIconColor, _showIconName);
62//
63// AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear, _showIconColor, _showIconName);
64// AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear, _showIconColor, _showIconName);
65// AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face, _showIconColor, _showIconName);
66//
67// AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor, _showIconColor, _showIconName);
68// AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket, _showIconColor, _showIconName);
69// AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt, _showIconColor, _showIconName);
70//
71// AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor, _showIconColor, _showIconName);
72// AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs, _showIconColor, _showIconName);
73// AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet, _showIconColor, _showIconName);
74//
75// AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
76//
77// return result;
78// }
79//
80// private static void DoInventory (JsonArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
81// for (int i = 0; i < _inv.Count; i++) {
82// _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
83// }
84// }
85//
86// private static void AddEquipment (JsonObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
87// int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
88//
89// for (int i = 0; i < slotindices.Length; i++) {
90// if (_items? [slotindices [i]] == null) {
91// continue;
92// }
93//
94// InvItem item = _items [slotindices [i]];
95// _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
96// return;
97// }
98//
99// _eq.Add (_slotname, new JsonNull ());
100// }
101//
102// private static JsonNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
103// if (_item == null) {
104// return new JsonNull ();
105// }
106//
107// JsonObject jsonItem = new JsonObject ();
108// jsonItem.Add ("count", new JsonNumber (_item.count));
109// jsonItem.Add ("name", new JsonString (_item.itemName));
110//
111// if (_showIconName) {
112// jsonItem.Add ("icon", new JsonString (_item.icon));
113// }
114//
115// if (_showIconColor) {
116// jsonItem.Add ("iconcolor", new JsonString (_item.iconcolor));
117// }
118//
119// jsonItem.Add ("quality", new JsonNumber (_item.quality));
120// if (_item.quality >= 0) {
121// jsonItem.Add ("qualitycolor", new JsonString (QualityInfo.GetQualityColorHex (_item.quality)));
122// }
123//
124// return jsonItem;
125//
126// }
127// }
128// }
Note: See TracBrowser for help on using the repository browser.