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