Skip to content

Instantly share code, notes, and snippets.

@GorillaNuggets
Last active August 9, 2019 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GorillaNuggets/e72f08c587c11b1a4a9c00527bc9d1e6 to your computer and use it in GitHub Desktop.
Save GorillaNuggets/e72f08c587c11b1a4a9c00527bc9d1e6 to your computer and use it in GitHub Desktop.
using System;
using System.Linq;
using System.Net;
using System.Text;
using Newtonsoft.Json.Linq;
namespace ConsoleApp
{
internal static class Program
{
private const string ApiKey = "YOUR API KEY";
private static readonly WebClient HttpClient = new WebClient {Encoding = Encoding.UTF8};
private static void Main(string[] args)
{
const int itemId = 19721; // Pile of Ectoplasm Id number
var itemCount = 0;
itemCount += CheckCharactersInventory(itemId);
itemCount += CheckMaterialStorage(itemId);
Console.WriteLine(itemCount);
Console.ReadLine();
}
private static int CheckCharactersInventory(int itemId)
{
var url = $"https://api.guildwars2.com/v2/characters?ids=all&access_token={ApiKey}";
// Find and count itemId in all character's inventories:
var json = HttpClient.DownloadString(url);
var characters = JArray.Parse(json);
return characters.Sum(character => (
from bag in character["bags"]
where bag.HasValues
from item in bag["inventory"]
where item.HasValues
where (int) item["id"] == itemId
select (int) item["count"]
).Sum()
);
}
private static int CheckMaterialStorage(int itemId)
{
var url = $"https://api.guildwars2.com/v2/account/materials?access_token={ApiKey}";
// Find and count itemId in your material storage:
var json = HttpClient.DownloadString(url);
var materials = JArray.Parse(json);
return materials
.Where(material => (int) material["id"] == itemId)
.Sum(material => (int) material["count"]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment