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

Last change on this file since 454 was 454, checked in by alloc, 16 months ago

24_29_43
Switched over to vanilla Web infrastructure

File size: 5.0 KB
Line 
1using System.Collections.Generic;
2using System.Net;
3using AllocsFixes.JSON;
4using AllocsFixes.PersistentData;
5using Webserver;
6using Webserver.WebAPI;
7
8namespace AllocsFixes.WebAPIs {
9 public class GetPlayerInventory : AbsWebAPI {
10 public override void HandleRequest (RequestContext _context) {
11 if (_context.Request.QueryString ["userid"] == null) {
12 WebUtils.WriteText (_context.Response, "No user id given", HttpStatusCode.BadRequest);
13 return;
14 }
15
16 string userIdString = _context.Request.QueryString ["userid"];
17 if (!PlatformUserIdentifierAbs.TryFromCombinedString (userIdString, out PlatformUserIdentifierAbs userId)) {
18 WebUtils.WriteText (_context.Response, "Invalid user id given", HttpStatusCode.BadRequest);
19 return;
20 }
21
22 Player p = PersistentContainer.Instance.Players.GetByUserId (userId);
23 if (p == null) {
24 WebUtils.WriteText (_context.Response, "Unknown user id given", HttpStatusCode.NotFound);
25 return;
26 }
27
28 GetInventoryArguments (_context, out bool showIconColor, out bool showIconName);
29
30 JSONObject result = DoPlayer (p, showIconColor, showIconName);
31
32 LegacyApiHelper.WriteJSON (_context.Response, result);
33 }
34
35 internal static void GetInventoryArguments (RequestContext _context, out bool _showIconColor, out bool _showIconName) {
36 if (_context.Request.QueryString ["showiconcolor"] == null || !bool.TryParse (_context.Request.QueryString ["showiconcolor"], out _showIconColor)) {
37 _showIconColor = true;
38 }
39
40 if (_context.Request.QueryString ["showiconname"] == null || !bool.TryParse (_context.Request.QueryString ["showiconname"], out _showIconName)) {
41 _showIconName = true;
42 }
43 }
44
45 internal static JSONObject DoPlayer (Player _player, bool _showIconColor, bool _showIconName) {
46 PersistentData.Inventory inv = _player.Inventory;
47
48 JSONObject result = new JSONObject ();
49
50 JSONArray bag = new JSONArray ();
51 JSONArray belt = new JSONArray ();
52 JSONObject equipment = new JSONObject ();
53 result.Add ("userid", new JSONString (_player.PlatformId.CombinedString));
54 result.Add ("crossplatformid", new JSONString (_player.CrossPlatformId?.CombinedString ?? ""));
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 InvItem item = _items [slotindices [i]];
93 _eq.Add (_slotname, GetJsonForItem (item, _showIconColor, _showIconName));
94 return;
95 }
96 }
97
98 _eq.Add (_slotname, new JSONNull ());
99 }
100
101 private static JSONNode GetJsonForItem (InvItem _item, bool _showIconColor, bool _showIconName) {
102 if (_item == null) {
103 return new JSONNull ();
104 }
105
106 JSONObject jsonItem = new JSONObject ();
107 jsonItem.Add ("count", new JSONNumber (_item.count));
108 jsonItem.Add ("name", new JSONString (_item.itemName));
109
110 if (_showIconName) {
111 jsonItem.Add ("icon", new JSONString (_item.icon));
112 }
113
114 if (_showIconColor) {
115 jsonItem.Add ("iconcolor", new JSONString (_item.iconcolor));
116 }
117
118 jsonItem.Add ("quality", new JSONNumber (_item.quality));
119 if (_item.quality >= 0) {
120 jsonItem.Add ("qualitycolor", new JSONString (QualityInfo.GetQualityColorHex (_item.quality)));
121 }
122
123 return jsonItem;
124
125 }
126 }
127}
Note: See TracBrowser for help on using the repository browser.