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

Last change on this file since 247 was 245, checked in by alloc, 9 years ago

Fixes

File size: 3.9 KB
Line 
1using AllocsFixes.JSON;
2using AllocsFixes.PersistentData;
3using System;
4using System.Collections.Generic;
5using System.Net;
6
7namespace AllocsFixes.NetConnections.Servers.Web.API
8{
9 public class GetPlayerInventory : WebAPI {
10 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
11 if (req.QueryString ["steamid"] == null) {
12 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
13 Web.SetResponseTextContent (resp, "No SteamID given");
14 return;
15 }
16
17 Player p = PersistentContainer.Instance.Players [req.QueryString ["steamid"], false];
18 if (p == null) {
19 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
20 Web.SetResponseTextContent (resp, "Invalid or unknown SteamID given");
21 return;
22 }
23
24 PersistentData.Inventory inv = p.Inventory;
25
26
27 JSONObject result = new JSONObject ();
28
29 JSONArray bag = new JSONArray ();
30 JSONArray belt = new JSONArray ();
31 JSONObject equipment = new JSONObject ();
32 result.Add ("playername", new JSONString (p.Name));
33 result.Add ("bag", bag);
34 result.Add ("belt", belt);
35 result.Add ("equipment", equipment);
36
37 for (int i = 0; i < inv.belt.Count; i++) {
38 JSONObject item = new JSONObject ();
39 if (inv.belt [i] != null) {
40 item.Add ("count", new JSONNumber (inv.belt [i].count));
41 item.Add ("name", new JSONString (inv.belt [i].itemName));
42 } else {
43 item.Add ("count", new JSONNumber (0));
44 item.Add ("name", new JSONString (string.Empty));
45 }
46 belt.Add (item);
47 }
48 for (int i = 0; i < inv.bag.Count; i++) {
49 JSONObject item = new JSONObject ();
50 if (inv.bag [i] != null) {
51 item.Add ("count", new JSONNumber (inv.bag [i].count));
52 item.Add ("name", new JSONString (inv.bag [i].itemName));
53 } else {
54 item.Add ("count", new JSONNumber (0));
55 item.Add ("name", new JSONString (string.Empty));
56 }
57 bag.Add (item);
58 }
59
60 AddEquipment (equipment, "head", inv.equipment, XMLData.Item.EnumEquipmentSlot.Head, NGuiInvGridEquipment.EnumClothingLayer.Middle);
61 AddEquipment (equipment, "eyes", inv.equipment, XMLData.Item.EnumEquipmentSlot.Eyes, NGuiInvGridEquipment.EnumClothingLayer.Middle);
62 AddEquipment (equipment, "face", inv.equipment, XMLData.Item.EnumEquipmentSlot.Face, NGuiInvGridEquipment.EnumClothingLayer.Middle);
63
64 AddEquipment (equipment, "armor", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Outer);
65 AddEquipment (equipment, "jacket", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Middle);
66 AddEquipment (equipment, "shirt", inv.equipment, XMLData.Item.EnumEquipmentSlot.Chest, NGuiInvGridEquipment.EnumClothingLayer.Inner);
67
68 AddEquipment (equipment, "legarmor", inv.equipment, XMLData.Item.EnumEquipmentSlot.Legs, NGuiInvGridEquipment.EnumClothingLayer.Outer);
69 AddEquipment (equipment, "pants", inv.equipment, XMLData.Item.EnumEquipmentSlot.Legs, NGuiInvGridEquipment.EnumClothingLayer.Inner);
70 AddEquipment (equipment, "boots", inv.equipment, XMLData.Item.EnumEquipmentSlot.Feet, NGuiInvGridEquipment.EnumClothingLayer.Inner);
71
72 AddEquipment (equipment, "gloves", inv.equipment, XMLData.Item.EnumEquipmentSlot.Hands, NGuiInvGridEquipment.EnumClothingLayer.Inner);
73 AddEquipment (equipment, "backpack", inv.equipment, XMLData.Item.EnumEquipmentSlot.Back, NGuiInvGridEquipment.EnumClothingLayer.Outer);
74
75 WriteJSON (resp, result);
76 }
77
78 private void AddEquipment (JSONObject _eq, string _slotname, InvItem[] _items, XMLData.Item.EnumEquipmentSlot _slot, NGuiInvGridEquipment.EnumClothingLayer _layer) {
79 int index = (int)_slot + (int)_layer * (int)XMLData.Item.EnumEquipmentSlot.Count;
80 if (_items != null && _items [index] != null) {
81 _eq.Add (_slotname, new JSONString (_items [index].itemName));
82 } else {
83 _eq.Add (_slotname, new JSONBoolean (false));
84 }
85 }
86
87 }
88}
89
Note: See TracBrowser for help on using the repository browser.