How i can calculate total ectoplasm on account? C# — Guild Wars 2 Forums

How i can calculate total ectoplasm on account? C#

Any solution how to check inventory of all characters?

        var connect = new WebClient() { Encoding = Encoding.UTF8 }.DownloadString(url);
        var getid = Getid.FromJson(connect);
        foreach (string i in getid)
        {
          #######
        }

Comments

  • Mighty Cole.7849Mighty Cole.7849 Member ✭✭✭
    edited August 8, 2019

    Code:
    https://gist.github.com/GorillaNuggets/e72f08c587c11b1a4a9c00527bc9d1e6

    This code is split between two functions. 1) Looks for items in all characters inventories. 2) Looks for items in the material storage area.
    P.S. For some reason, the forums isn't redirecting to my url. You might have to copy and paste it in to your browser.

  • Mighty Cole.7849Mighty Cole.7849 Member ✭✭✭
    edited August 8, 2019

    Also, make sure when setting up your API key, that you don't forget to include inventories and characters :)

  • Ty, very helpfull and how u avoid the problem with uncoding FR and DE symbols in name (Magè)?

  • Mighty Cole.7849Mighty Cole.7849 Member ✭✭✭

    Using UTF8 on your WebClient should avoid any decoding problems. If you do not specify an encoder, the program will use your default. This can cause problems with the letters in your character names.

    private static readonly WebClient HttpClient = new WebClient {Encoding = Encoding.UTF8};

  • Mighty Cole.7849Mighty Cole.7849 Member ✭✭✭

    I created a new character named just to test the code. It worked fine. :)

  • Morozzko.2136Morozzko.2136 Member ✭✭
    edited August 13, 2019

    Thank you! its work properly, and how i can do same for bank?

    System.InvalidOperationException: "Cannot access child value on Newtonsoft.Json.Linq.JValue."

    from

          private static int CheckBank(int itemId)
             {
                  var url = @"https://api.guildwars2.com/v2/account/bank?access_token=" + ApiKey;
                  var json = HttpClient.DownloadString(url);
                  var bank = JArray.Parse(json);
                  var itemCount = 0;
    
            foreach (var slot in bank)
            {
                if (int.Parse(slot["id"].ToString()) == itemId)
                {
                    itemCount += int.Parse(slot["count"].ToString());
                }
            }
    
            return itemCount;
    

    and how i can find same in eqiupment slot, for example check, if player have 81007 (druid stone) or 81908 (aurora) make += 1

  • I think that's because you're running in to null in your json data. Try this:

            private static int CheckBank(int itemId)
            {
                var url = @"https://api.guildwars2.com/v2/account/bank?access_token=" + ApiKey;
                var json = HttpClient.DownloadString(url);
                var bank = JArray.Parse(json);
    
                return bank           
                .Where(slot => slot.HasValues && (int)slot["id"] == itemId)
                .Sum(slot => (int)slot["count"]);
            }
    
  • As for the equipment question, just follow my first example.

    https://api.guildwars2.com/v2/characters?ids=all&access_token=

    Instead of using character["bags"] use character["equipment"]

  • Mighty Cole.7849Mighty Cole.7849 Member ✭✭✭
    edited August 13, 2019

    Also, if you're not familiar with Language Integrated Query (LINQ), give this a read:
    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/
    It's a powerful query tool that can be very helpful when sifting through data.

  • Mighty Cole.7849Mighty Cole.7849 Member ✭✭✭
    edited August 13, 2019

    See how I used LINQ to find each object within this JSON

    You can do the same to find your equipment :)

  • @Mighty Cole.7849 said:
    As for the equipment question, just follow my first example.

    https://api.guildwars2.com/v2/characters?ids=all&access_token=

    Instead of using character["bags"] use character["equipment"]

    thanks a lot, and instead

    from item in bag["bag"] to from item in bag["slot"]

  • hmm, seems harder, it return 0, how i can check if id 81908 in "Accessory1" or "Accessory2"

  • Are you looking for something like this?

    https://gist.github.com/GorillaNuggets/db013c0f09c7281761c19bdfe7999cd9

    It will go through each of your characters and assign a true or false if Accessory1 or Accessory2 has item number 81908 in it.

  • If you want to do a individual character, you can try something like this:

    https://gist.github.com/GorillaNuggets/763a3506eecaf0f4b5fec550cefae998

  • Morozzko.2136Morozzko.2136 Member ✭✭
    edited August 15, 2019

    @Mighty Cole.7849 said:
    Are you looking for something like this?

    https://gist.github.com/GorillaNuggets/db013c0f09c7281761c19bdfe7999cd9

    It will go through each of your characters and assign a true or false if Accessory1 or Accessory2 has item number 81908 in it.

    yeah, i make this from your code, now will try to do "quest helper" for legendarys

        static int CheckCharactersEquipment(int itemId)
        {
    
    
            var url = $"https://api.guildwars2.com/v2/characters?ids=all&&access_token=" + ApiKey;
            var json = HttpClient.DownloadString(url);
            var characters = JArray.Parse(json);
            var itemCount = 0;
            foreach (var character in characters)
            {
    
    
    
                if (CheckAccessory(character, "Accessory2", itemId) == true || CheckAccessory(character, "Accessory1", itemId) == true)
    
                {
                    itemCount += 1;
                }
    
    
    
    
            }
            return itemCount;
    
        }
        private static bool CheckAccessory(JToken character, string slot, int itemId)
        {
            var accessory = from equipment in character["equipment"]
                            where equipment.HasValues
                            where (string)equipment["slot"] == slot
                            select (int)equipment["id"];
    
            var isInAccessory = false;
    
            foreach (var item in accessory)
            {
                if (item == itemId)
                {
                    isInAccessory = true;
                }
            }
    
            return isInAccessory;
        }
    
  • Morozzko.2136Morozzko.2136 Member ✭✭
    edited August 15, 2019

    {

            var itemCount = 0;
    
            itemCount += CheckCharactersInventory(itemId);
            itemCount += CheckMaterialStorage(itemId);
            itemCount += CheckBank(itemId);
            itemCount += CheckCharactersEquipment(itemId);
    
            return itemCount;
        }
    

    and full count like this, thank you very much

  • You're Welcome :)
    If there is anything I can help you with, just let me know.

    Also, if you have a lot of code and want to share it on the forums, I'd recommend using something like

    https://pastebin.com/

    For example:

    https://pastebin.com/G4SVsq0m

    It's easier for people to read and creates less clutter on the forum. Plus, you can use it for free. :D

©2010–2018 ArenaNet, LLC. All rights reserved. Guild Wars, Guild Wars 2, Heart of Thorns, Guild Wars 2: Path of Fire, ArenaNet, NCSOFT, the Interlocking NC Logo, and all associated logos and designs are trademarks or registered trademarks of NCSOFT Corporation. All other trademarks are the property of their respective owners.