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

Last change on this file since 425 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
Line 
1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
5
6namespace AllocsFixes.NetConnections.Servers.Web.API {
7 public class GetPlayerInventory : WebAPI {
8 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp, WebConnection _user,
9 int _permissionLevel) {
10 if (_req.QueryString ["userid"] == null) {
11 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
12 Web.SetResponseTextContent (_resp, "No user id given");
13 return;
14 }
15
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 }
22
23 Player p = PersistentContainer.Instance.Players [userId, false];
24 if (p == null) {
25 _resp.StatusCode = (int) HttpStatusCode.NotFound;
26 Web.SetResponseTextContent (_resp, "Unknown user id given");
27 return;
28 }
29
30 GetInventoryArguments (_req, out bool showIconColor, out bool showIconName);
31
32 JSONObject result = DoPlayer (userIdString, p, showIconColor, showIconName);
33
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
50 JSONObject result = new JSONObject ();
51
52 JSONArray bag = new JSONArray ();
53 JSONArray belt = new JSONArray ();
54 JSONObject equipment = new JSONObject ();
55 result.Add ("userid", new JSONString (_steamId));
56 result.Add ("entityid", new JSONNumber (_player.EntityID));
57 result.Add ("playername", new JSONString (_player.Name));
58 result.Add ("bag", bag);
59 result.Add ("belt", belt);
60 result.Add ("equipment", equipment);
61
62 DoInventory (belt, inv.belt, _showIconColor, _showIconName);
63 DoInventory (bag, inv.bag, _showIconColor, _showIconName);
64
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);
68
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);
72
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);
76
77 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
78
79 return result;
80 }
81
82 private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
83 for (int i = 0; i < _inv.Count; i++) {
84 _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
85 }
86 }
87
88 private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
89 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
90
91 for (int i = 0; i < slotindices.Length; i++) {
92 if (_items? [slotindices [i]] != null) {
93 InvItem item = _items [slotindices [i]];
94 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
95 return;
96 }
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.