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

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

24_27_41

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