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

Last change on this file since 402 was 369, checked in by alloc, 3 years ago

Preparations for A20 release
Changes usage of "SteamID" to "UserID" in console commands
Also changes a bunch of the WebAPI stuff to show / use UserIDs

File size: 4.9 KB
RevLine 
[325]1using System.Collections.Generic;
2using System.Net;
[253]3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
5
[325]6namespace AllocsFixes.NetConnections.Servers.Web.API {
[253]7 public class GetPlayerInventory : WebAPI {
[348]8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
9 int _permissionLevel) {
[369]10 if (_req.QueryString ["userid"] == null) {
[348]11 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
[369]12 Web.SetResponseTextContent (_resp, "No user id given");
[253]13 return;
14 }
15
[369]16 string userIdString = _req.QueryString ["userid"];
17 if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) {
18 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
19 Web.SetResponseTextContent (_resp, "Invalid user id given");
20 return;
21 }
[348]22
[369]23 Player p = PersistentContainer.Instance.Players [userId, false];
[253]24 if (p == null) {
[348]25 _resp.StatusCode = (int) HttpStatusCode.NotFound;
[369]26 Web.SetResponseTextContent (_resp, "Unknown user id given");
[253]27 return;
28 }
29
[369]30 GetInventoryArguments (_req, out bool showIconColor, out bool showIconName);
[253]31
[369]32 JSONObject result = DoPlayer (userIdString, p, showIconColor, showIconName);
[253]33
[348]34 WriteJSON (_resp, result);
35 }
36
37 internal static void GetInventoryArguments (HttpListenerRequest _req, out bool _showIconColor, out bool _showIconName) {
38 if (_req.QueryString ["showiconcolor"] == null || !bool.TryParse (_req.QueryString ["showiconcolor"], out _showIconColor)) {
39 _showIconColor = true;
40 }
41
42 if (_req.QueryString ["showiconname"] == null || !bool.TryParse (_req.QueryString ["showiconname"], out _showIconName)) {
43 _showIconName = true;
44 }
45 }
46
47 internal static JSONObject DoPlayer (string _steamId, Player _player, bool _showIconColor, bool _showIconName) {
48 PersistentData.Inventory inv = _player.Inventory;
49
[253]50 JSONObject result = new JSONObject ();
51
52 JSONArray bag = new JSONArray ();
53 JSONArray belt = new JSONArray ();
54 JSONObject equipment = new JSONObject ();
[369]55 result.Add ("userid", new JSONString (_steamId));
[348]56 result.Add ("entityid", new JSONNumber (_player.EntityID));
57 result.Add ("playername", new JSONString (_player.Name));
[253]58 result.Add ("bag", bag);
59 result.Add ("belt", belt);
60 result.Add ("equipment", equipment);
61
[348]62 DoInventory (belt, inv.belt, _showIconColor, _showIconName);
63 DoInventory (bag, inv.bag, _showIconColor, _showIconName);
[253]64
[348]65 AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear, _showIconColor, _showIconName);
66 AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear, _showIconColor, _showIconName);
67 AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face, _showIconColor, _showIconName);
[253]68
[348]69 AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor, _showIconColor, _showIconName);
70 AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket, _showIconColor, _showIconName);
71 AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt, _showIconColor, _showIconName);
[253]72
[348]73 AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor, _showIconColor, _showIconName);
74 AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs, _showIconColor, _showIconName);
75 AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet, _showIconColor, _showIconName);
[253]76
[348]77 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
[253]78
[348]79 return result;
[253]80 }
81
[348]82 private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
[253]83 for (int i = 0; i < _inv.Count; i++) {
[348]84 _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
[253]85 }
86 }
87
[348]88 private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
[253]89 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
90
91 for (int i = 0; i < slotindices.Length; i++) {
[369]92 if (_items? [slotindices [i]] != null) {
[253]93 InvItem item = _items [slotindices [i]];
[348]94 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
[253]95 return;
96 }
97 }
98
99 _eq.Add (_slotname, new JSONNull ());
100 }
101
[348]102 private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
[326]103 if (_item == null) {
104 return new JSONNull ();
105 }
[325]106
[326]107 JSONObject jsonItem = new JSONObject ();
108 jsonItem.Add ("count", new JSONNumber (_item.count));
109 jsonItem.Add ("name", new JSONString (_item.itemName));
[348]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
[326]119 jsonItem.Add ("quality", new JSONNumber (_item.quality));
120 if (_item.quality >= 0) {
121 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
[253]122 }
[325]123
[326]124 return jsonItem;
125
[253]126 }
127 }
[325]128}
Note: See TracBrowser for help on using the repository browser.