1 | using System.Collections.Generic;
|
---|
2 | using System.Net;
|
---|
3 | using AllocsFixes.JSON;
|
---|
4 | using AllocsFixes.PersistentData;
|
---|
5 | using JetBrains.Annotations;
|
---|
6 | using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
|
---|
7 |
|
---|
8 | namespace Webserver.WebAPI {
|
---|
9 | [UsedImplicitly]
|
---|
10 | public class GetPlayerInventory : AbsWebAPI {
|
---|
11 | public override void HandleRequest (RequestContext _context) {
|
---|
12 | if (_context.Request.QueryString ["userid"] == null) {
|
---|
13 | WebUtils.WriteText (_context.Response, "No user id given", HttpStatusCode.BadRequest);
|
---|
14 | return;
|
---|
15 | }
|
---|
16 |
|
---|
17 | string userIdString = _context.Request.QueryString ["userid"];
|
---|
18 | if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) {
|
---|
19 | WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest);
|
---|
20 | return;
|
---|
21 | }
|
---|
22 |
|
---|
23 | Player p = PersistentContainer.Instance.Players [userId, false];
|
---|
24 | if (p == null) {
|
---|
25 | WebUtils.WriteText (_context.Response, "Unknown user id given", HttpStatusCode.NotFound);
|
---|
26 | return;
|
---|
27 | }
|
---|
28 |
|
---|
29 | GetInventoryArguments (_context.Request, out bool showIconColor, out bool showIconName);
|
---|
30 |
|
---|
31 | JsonObject result = DoPlayer (userIdString, p, showIconColor, showIconName);
|
---|
32 |
|
---|
33 | WebUtils.WriteJson (_context.Response, result);
|
---|
34 | }
|
---|
35 |
|
---|
36 | internal static void GetInventoryArguments (HttpListenerRequest _req, out bool _showIconColor, out bool _showIconName) {
|
---|
37 | if (_req.QueryString ["showiconcolor"] == null || !bool.TryParse (_req.QueryString ["showiconcolor"], out _showIconColor)) {
|
---|
38 | _showIconColor = true;
|
---|
39 | }
|
---|
40 |
|
---|
41 | if (_req.QueryString ["showiconname"] == null || !bool.TryParse (_req.QueryString ["showiconname"], out _showIconName)) {
|
---|
42 | _showIconName = true;
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | internal static JsonObject DoPlayer (string _steamId, Player _player, bool _showIconColor, bool _showIconName) {
|
---|
47 | AllocsFixes.PersistentData.Inventory inv = _player.Inventory;
|
---|
48 |
|
---|
49 | JsonObject result = new JsonObject ();
|
---|
50 |
|
---|
51 | JsonArray bag = new JsonArray ();
|
---|
52 | JsonArray belt = new JsonArray ();
|
---|
53 | JsonObject equipment = new JsonObject ();
|
---|
54 | result.Add ("userid", new JsonString (_steamId));
|
---|
55 | result.Add ("entityid", new JsonNumber (_player.EntityID));
|
---|
56 | result.Add ("playername", new JsonString (_player.Name));
|
---|
57 | result.Add ("bag", bag);
|
---|
58 | result.Add ("belt", belt);
|
---|
59 | result.Add ("equipment", equipment);
|
---|
60 |
|
---|
61 | DoInventory (belt, inv.belt, _showIconColor, _showIconName);
|
---|
62 | DoInventory (bag, inv.bag, _showIconColor, _showIconName);
|
---|
63 |
|
---|
64 | AddEquipment (equipment, "head", inv.equipment, EquipmentSlots.Headgear, _showIconColor, _showIconName);
|
---|
65 | AddEquipment (equipment, "eyes", inv.equipment, EquipmentSlots.Eyewear, _showIconColor, _showIconName);
|
---|
66 | AddEquipment (equipment, "face", inv.equipment, EquipmentSlots.Face, _showIconColor, _showIconName);
|
---|
67 |
|
---|
68 | AddEquipment (equipment, "armor", inv.equipment, EquipmentSlots.ChestArmor, _showIconColor, _showIconName);
|
---|
69 | AddEquipment (equipment, "jacket", inv.equipment, EquipmentSlots.Jacket, _showIconColor, _showIconName);
|
---|
70 | AddEquipment (equipment, "shirt", inv.equipment, EquipmentSlots.Shirt, _showIconColor, _showIconName);
|
---|
71 |
|
---|
72 | AddEquipment (equipment, "legarmor", inv.equipment, EquipmentSlots.LegArmor, _showIconColor, _showIconName);
|
---|
73 | AddEquipment (equipment, "pants", inv.equipment, EquipmentSlots.Legs, _showIconColor, _showIconName);
|
---|
74 | AddEquipment (equipment, "boots", inv.equipment, EquipmentSlots.Feet, _showIconColor, _showIconName);
|
---|
75 |
|
---|
76 | AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
|
---|
77 |
|
---|
78 | return result;
|
---|
79 | }
|
---|
80 |
|
---|
81 | private static void DoInventory (JsonArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
|
---|
82 | for (int i = 0; i < _inv.Count; i++) {
|
---|
83 | _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
|
---|
84 | }
|
---|
85 | }
|
---|
86 |
|
---|
87 | private static void AddEquipment (JsonObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
|
---|
88 | int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
|
---|
89 |
|
---|
90 | for (int i = 0; i < slotindices.Length; i++) {
|
---|
91 | if (_items? [slotindices [i]] == null) {
|
---|
92 | continue;
|
---|
93 | }
|
---|
94 |
|
---|
95 | InvItem item = _items [slotindices [i]];
|
---|
96 | _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
|
---|
97 | return;
|
---|
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 | }
|
---|