source: binary-improvements2/MapRendering/Web/API/GetPlayerInventory.cs@ 382

Last change on this file since 382 was 382, checked in by alloc, 2 years ago

Switched to use SpaceWizards.HttpListener

File size: 5.1 KB
RevLine 
[325]1using System.Collections.Generic;
2using System.Net;
[253]3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
[382]5using HttpListenerRequest = SpaceWizards.HttpListener.HttpListenerRequest;
6using HttpListenerResponse = SpaceWizards.HttpListener.HttpListenerResponse;
[253]7
[325]8namespace AllocsFixes.NetConnections.Servers.Web.API {
[253]9 public class GetPlayerInventory : WebAPI {
[382]10 public override void HandleRequest (HttpListenerRequest _req, HttpListenerResponse _resp,
11 WebConnection _user, int _permissionLevel) {
[369]12 if (_req.QueryString ["userid"] == null) {
[348]13 _resp.StatusCode = (int) HttpStatusCode.BadRequest;
[369]14 Web.SetResponseTextContent (_resp, "No user id given");
[253]15 return;
16 }
17
[369]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 }
[348]24
[369]25 Player p = PersistentContainer.Instance.Players [userId, false];
[253]26 if (p == null) {
[348]27 _resp.StatusCode = (int) HttpStatusCode.NotFound;
[369]28 Web.SetResponseTextContent (_resp, "Unknown user id given");
[253]29 return;
30 }
31
[369]32 GetInventoryArguments (_req, out bool showIconColor, out bool showIconName);
[253]33
[369]34 JSONObject result = DoPlayer (userIdString, p, showIconColor, showIconName);
[253]35
[348]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
[253]52 JSONObject result = new JSONObject ();
53
54 JSONArray bag = new JSONArray ();
55 JSONArray belt = new JSONArray ();
56 JSONObject equipment = new JSONObject ();
[369]57 result.Add ("userid", new JSONString (_steamId));
[348]58 result.Add ("entityid", new JSONNumber (_player.EntityID));
59 result.Add ("playername", new JSONString (_player.Name));
[253]60 result.Add ("bag", bag);
61 result.Add ("belt", belt);
62 result.Add ("equipment", equipment);
63
[348]64 DoInventory (belt, inv.belt, _showIconColor, _showIconName);
65 DoInventory (bag, inv.bag, _showIconColor, _showIconName);
[253]66
[348]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);
[253]70
[348]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);
[253]74
[348]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);
[253]78
[348]79 AddEquipment (equipment, "gloves", inv.equipment, EquipmentSlots.Hands, _showIconColor, _showIconName);
[253]80
[348]81 return result;
[253]82 }
83
[348]84 private static void DoInventory (JSONArray _jsonRes, List<InvItem> _inv, bool _showIconColor, bool _showIconName) {
[253]85 for (int i = 0; i < _inv.Count; i++) {
[348]86 _jsonRes.Add (GetJsonForItem (_inv [i], _showIconColor, _showIconName));
[253]87 }
88 }
89
[348]90 private static void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, EquipmentSlots _slot, bool _showIconColor, bool _showIconName) {
[253]91 int[] slotindices = XUiM_PlayerEquipment.GetSlotIndicesByEquipmentSlot (_slot);
92
93 for (int i = 0; i < slotindices.Length; i++) {
[369]94 if (_items? [slotindices [i]] != null) {
[253]95 InvItem item = _items [slotindices [i]];
[348]96 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
[253]97 return;
98 }
99 }
100
101 _eq.Add (_slotname, new JSONNull ());
102 }
103
[348]104 private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
[326]105 if (_item == null) {
106 return new JSONNull ();
107 }
[325]108
[326]109 JSONObject jsonItem = new JSONObject ();
110 jsonItem.Add ("count", new JSONNumber (_item.count));
111 jsonItem.Add ("name", new JSONString (_item.itemName));
[348]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
[326]121 jsonItem.Add ("quality", new JSONNumber (_item.quality));
122 if (_item.quality >= 0) {
123 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
[253]124 }
[325]125
[326]126 return jsonItem;
127
[253]128 }
129 }
[325]130}
Note: See TracBrowser for help on using the repository browser.