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

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