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

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

Big refactoring in Web to pass around a Context instead of a bunch of individual arguments all the time

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