source: binary-improvements/7dtd-server-fixes/src/NetConnections/Servers/Web/API/GetPlayerInventory.cs@ 215

Last change on this file since 215 was 168, checked in by alloc, 10 years ago

fixes

File size: 1.8 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 {
11 public override void HandleRequest (HttpListenerRequest req, HttpListenerResponse resp, HttpListenerBasicIdentity user)
12 {
13 if (req.QueryString ["steamid"] == null) {
14 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
15 Web.SetResponseTextContent (resp, "No SteamID given");
16 return;
17 }
18
19 Player p = PersistentContainer.Instance.Players [req.QueryString ["steamid"]];
20 if (p == null) {
21 resp.StatusCode = (int)HttpStatusCode.InternalServerError;
22 Web.SetResponseTextContent (resp, "Invalid or unknown SteamID given");
23 return;
24 }
25
26 PersistentData.Inventory inv = p.Inventory;
27
28
29 JSONObject result = new JSONObject ();
30
31 JSONArray bag = new JSONArray ();
32 JSONArray belt = new JSONArray ();
33 result.Add ("bag", bag);
34 result.Add ("belt", belt);
35
36 for (int i = 0; i < inv.belt.Count; i++) {
37 JSONObject item = new JSONObject();
38 if (inv.belt [i] != null) {
39 item.Add ("count", new JSONNumber(inv.belt[i].count));
40 item.Add ("name", new JSONString(inv.belt[i].itemName));
41 } else {
42 item.Add ("count", new JSONNumber(0));
43 item.Add ("name", new JSONString(string.Empty));
44 }
45 belt.Add(item);
46 }
47 for (int i = 0; i < inv.bag.Count; i++) {
48 JSONObject item = new JSONObject();
49 if (inv.bag [i] != null) {
50 item.Add ("count", new JSONNumber(inv.bag[i].count));
51 item.Add ("name", new JSONString(inv.bag[i].itemName));
52 } else {
53 item.Add ("count", new JSONNumber(0));
54 item.Add ("name", new JSONString(string.Empty));
55 }
56 bag.Add(item);
57 }
58
59 WriteJSON (resp, result);
60 }
61 }
62}
63
Note: See TracBrowser for help on using the repository browser.