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

Last change on this file since 446 was 446, checked in by alloc, 17 months ago

24_27_41

File size: 5.0 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.GetByUserId (userId);
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 (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 (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 (_player.PlatformId.CombinedString));
56 result.Add ("crossplatformid", new JSONString (_player.CrossPlatformId?.CombinedString ?? ""));
57 result.Add ("entityid", new JSONNumber (_player.EntityID));
58 result.Add ("playername", new JSONString (_player.Name));
59 result.Add ("bag", bag);
60 result.Add ("belt", belt);
61 result.Add ("equipment", equipment);
62
63 DoInventory (belt, inv.belt, _showIconColor, _showIconName);
64 DoInventory (bag, inv.bag, _showIconColor, _showIconName);
65
66 AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear, _showIconColor, _showIconName);
67 AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear, _showIconColor, _showIconName);
68 AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face, _showIconColor, _showIconName);
69
70 AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor, _showIconColor, _showIconName);
71 AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket, _showIconColor, _showIconName);
72 AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt, _showIconColor, _showIconName);
73
74 AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor, _showIconColor, _showIconName);
75 AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs, _showIconColor, _showIconName);
76 AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet, _showIconColor, _showIconName);
77
78 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
79
80 return result;
81 }
82
83 private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
84 for (int i = 0; i < _inv.Count; i++) {
85 _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
86 }
87 }
88
89 private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
90 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
91
92 for (int i = 0; i < slotindices.Length; i++) {
93 if (_items? [slotindices [i]] != null) {
94 InvItem item = _items [slotindices [i]];
95 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
96 return;
97 }
98 }
99
100 _eq.Add (_slotname, new JSONNull ());
101 }
102
103 private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
104 if (_item == null) {
105 return new JSONNull ();
106 }
107
108 JSONObject jsonItem = new JSONObject ();
109 jsonItem.Add ("count", new JSONNumber (_item.count));
110 jsonItem.Add ("name", new JSONString (_item.itemName));
111
112 if (_showIconName) {
113 jsonItem.Add ("icon", new JSONString (_item.icon));
114 }
115
116 if (_showIconColor) {
117 jsonItem.Add ("iconcolor", new JSONString (_item.iconcolor));
118 }
119
120 jsonItem.Add ("quality", new JSONNumber (_item.quality));
121 if (_item.quality >= 0) {
122 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
123 }
124
125 return jsonItem;
126
127 }
128 }
129}
Note: See TracBrowser for help on using the repository browser.