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

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

Fixes

File size: 3.9 KB
RevLine 
[230]1using AllocsFixes.JSON;
2using AllocsFixes.PersistentData;
3using System;
4using System.Collections.Generic;
5using System.Net;
6
7namespace AllocsFixes.NetConnections.Servers.Web.API
8{
[245]9 public class GetPlayerInventory : WebAPI {
10 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, WebConnection user, int permissionLevel) {
[230]11 if (req.QueryString ["steamid"] == null) {
12 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
13 Web.SetResponseTextContent (resp, "No SteamID given");
14 return;
15 }
16
[233]17 Player p = PersistentContainer.Instance.Players [req.QueryString ["steamid"], false];
[230]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 ();
[245]31 JSONObject equipment = new JSONObject ();
32 result.Add ("playername", new JSONString (p.Name));
[230]33 result.Add ("bag", bag);
34 result.Add ("belt", belt);
[245]35 result.Add ("equipment", equipment);
[230]36
37 for (int i = 0; i < inv.belt.Count; i++) {
[245]38 JSONObject item = new JSONObject ();
[230]39 if (inv.belt [i] != null) {
[245]40 item.Add ("count", new JSONNumber (inv.belt [i].count));
41 item.Add ("name", new JSONString (inv.belt [i].itemName));
[230]42 } else {
[245]43 item.Add ("count", new JSONNumber (0));
44 item.Add ("name", new JSONString (string.Empty));
[230]45 }
[245]46 belt.Add (item);
[230]47 }
48 for (int i = 0; i < inv.bag.Count; i++) {
[245]49 JSONObject item = new JSONObject ();
[230]50 if (inv.bag [i] != null) {
[245]51 item.Add ("count", new JSONNumber (inv.bag [i].count));
52 item.Add ("name", new JSONString (inv.bag [i].itemName));
[230]53 } else {
[245]54 item.Add ("count", new JSONNumber (0));
55 item.Add ("name", new JSONString (string.Empty));
[230]56 }
[245]57 bag.Add (item);
[230]58 }
59
[245]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
[230]75 WriteJSON (resp, result);
76 }
[245]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
[230]87 }
88}
89
Note: See TracBrowser for help on using the repository browser.