Do I have [item] ?

Darkness

New member
I wanted to write a little script to anwser the question: Do I have this item?
The idea is that it checks inventory, then closet, then hagnks, then clan stash and tells me if I have the item.

I wanted it to run from the gCLI and use the same fuzzy matcher that the gCLI uses, so I can also test what the gCLI is matching before issuing a command (and for ease of use).

Does the ash have access to the fuzzy matcher? I cant find the function.
 

Metraxis

Member
This is actually pretty simple to do.

Code:
void main(string need) {
  item target = string_to_item(need);
  if(item_amount(target) > 0) {
    print("Inventory");
    return;
  }
  if(closet_amount(target) > 0) {
    print("Closet");
    return;
  }
  if(storage_amount(target) > 0 && (can_interact() || pulls_remaining() > 0)) {
    print("Hagnk's");
    return;
  }
  if(stash_amount(target) > 0 && can_interact()) {
    print("Clan Stash");
    return;
  }
  if(museum_amount(target) > 0 && can_interact()) {
    print("Collection");
    return;
  }
  if(shop_amount(target) > 0 && can_interact()) {
    print("Your Mall Store");
    return;
  }
  print("None Available");
  return;
}

Edit: fixed code tags - and spelling and code. ;)
 

macman104

Member
Also, typing a command like
Code:
use? 1 kg saus
into the gCLI will just print the item that the gCLI fuzzy matches.
 

Darkness

New member
Thanks metraxis and macman.
This was very helpful.
I somehow had missed the ? command modifier.
I made a slightly different version that reports all the available items.
Its messy and maybe a bit too verbose but anyway....

Code:
void main(string need) {
  int x;
  int total;
  int SCtotal;
  string hcs1="";
  string hcs2="";
  string ps="";

  item target = string_to_item(need);
  print("Looking for "+item_to_string(target));
  x=item_amount(target);
  total=x;
  if(x > 0) {
    print("Inventory "+x);
  }

  x=closet_amount(target);
  total=total+x;
  SCtotal=total;
  if(x > 0) {
    print("Closet "+x);
  }

  if (!can_interact())  {
	hcs1="(";
	hcs2=")";
    if (pulls_remaining() > 0) {
      ps=" ("+pulls_remaining()+" pulls left"+")";
    }
  }

  x=storage_amount(target);
  total=total+x;
  if(x > 0 ) {
    print(hcs1+"Hagnk's"+hcs2+" "+x+ps);
  }

  x=stash_amount(target);
  total=total+x;
  if(x > 0 ) {
    print(hcs1+"Clan Stash"+hcs2+" "+x);
  }

  x=museum_amount(target);
  total=total+x;
  if(x > 0 ){
    print(hcs1+"Collection"+hcs2+" "+x);
  }
  x=shop_amount(target);
  total=total+x;
  if(x> 0 ) {
    print(hcs1+"Your Mall Store"+hcs2+" "+x);
  }

  if (can_interact()) {
    SCtotal=total;
  }

  if ( total < 1)  {
    print("None Available");
  }
  else {
    print(total+" total.");
  }
}

EDIT: How do I handle exceptions when passing invalid data to string_to_item() ?
Since it returns a string I cant use an if() to catch the failure.
 

holatuwol

Developer
Unlike monster conversions, KoLmafia prints a debug log whenever string_to_item() doesn't map to an actual item (monsters, KoLmafia can mess up on ... items, it can't). So, you can't actually trap failures. But, it's a harmless debug log.
Edit: Eh ... I guess parse exceptions during runtime are things people would like to trap as $whatever[none]. So, in the next release, these functions will return $whatever[none] if it can't figure out what the item should be, rather than generating debug logs.
 
Top