Buy: Difference between revisions

From Kolmafia
Jump to navigation Jump to search
imported>Bale
see also
add extra overloads
 
(8 intermediate revisions by 5 users not shown)
Line 11: Line 11:
return_type={{#var:return_type}}|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
return_also={{#var:return_also}}|
parameter1={{Param|int|qty}}|
parameter1={{Param|item|it}}|
parameter2={{Param|item|it}}|
parameter2={{Param|int|qty}}|
p1desc={{pspan|it}} is the item to purchase|
p2desc={{pspan|qty}} is the (optional) number to purchase|
}}|
}}|


Line 18: Line 20:
name={{#var:name}}|
name={{#var:name}}|
aggregate={{#var:aggregate}}|
aggregate={{#var:aggregate}}|
return_type=boolean|
return_type=int|
return_also={{#var:return_also}}|
return_also={{#var:return_also}}|
parameter1={{Param|coinmaster|master}}|
parameter1={{Param|item|it}}|
parameter2={{Param|int|qty}}|
parameter2={{Param|int|qty}}|
parameter3={{Param|item|it}}|
parameter3={{Param|int|price}}|
p1desc={{pspan|it}} is the item to purchase|
p2desc={{pspan|qty}} is the number to purchase|
p3desc={{pspan|price}} is the (optional) maximum price to spend per item|
}}|
}}|


function3={{Function|
function3={{Function|
name={{#var:name}}|
aggregate={{#var:aggregate}}|
return_type={{#var:return_type}}|
return_also={{#var:return_also}}|
parameter1={{Param|int|qty}}|
parameter2={{Param|item|it}}|
p1desc={{pspan|qty}} is the number to purchase|
p2desc={{pspan|it}} is the item to purchase|
}}|
function4={{Function|
name={{#var:name}}|
name={{#var:name}}|
aggregate={{#var:aggregate}}|
aggregate={{#var:aggregate}}|
Line 36: Line 52:
p2desc={{pspan|it}} is the item to purchase|
p2desc={{pspan|it}} is the item to purchase|
p3desc={{pspan|price}} is the (optional) maximum price to spend per item|
p3desc={{pspan|price}} is the (optional) maximum price to spend per item|
p4desc={{pspan|master}} is the coinmaster to purchase from|
}}|
}}|


function_description=Attempts to purchase {{pspan|qty}} amount of item {{pspan|it}}. If {{pspan|qty}} is less than 1, the function will return true without purchasing anything. If the optional maximum {{pspan|price}} is specified, it returns the integer amount of items purchased; otherwise it returns true if the purchase succeeded and false if it did not. Without the optional {{pspan|price}} parameter, first preference is given to NPC stores when attempting purchases, after which it will attempt to use the mall.</p>
function5={{Function|
name={{#var:name}}|
aggregate={{#var:aggregate}}|
return_type=boolean|
return_also={{#var:return_also}}|
parameter1={{Param|coinmaster|master}}|
parameter2={{Param|int|qty}}|
parameter3={{Param|item|it}}|
p1desc={{pspan|master}} is the coinmaster to purchase from|
p2desc={{pspan|qty}} is the number to purchase|
p3desc={{pspan|it}} is the item to purchase|
}}|
 
function_description=Attempts to purchase {{pspan|qty}} amount of item {{pspan|it}}. (Will not buy using meat from storage; use buy_using_storage() instead.) If {{pspan|qty}} is less than 1, the function will return true without purchasing anything. If the optional maximum {{pspan|price}} is specified, it returns the integer amount of items purchased; otherwise it returns true if the purchase succeeded and false if it did not. If a {{pspan|price}} less than 1 is passed, the function will always buy {{pspan|qty}}, regardless of price. Without the optional {{pspan|price}} parameter, first preference is given to NPC stores when attempting purchases, after which it will attempt to use the mall.</p>
 
<p>The versions that return a boolean value determine success depending on whether your inventory of the desired item went up by the amount you tried to buy.</p>


<p>If a {{pspan|coinmaster}} is listed, it will attempt to purchase from the coinmaster using the correct alternate currency. Otherwise it will be purchased from an NPC store or mall using meat.|
<p>If a {{pspan|coinmaster}} is listed, it will attempt to purchase from the coinmaster using the correct alternate currency. Otherwise it will be purchased from an NPC store or mall using meat.|
Line 56: Line 86:
<syntaxhighlight>
<syntaxhighlight>
boolean buy_coinmaster(int qty, item it) {
boolean buy_coinmaster(int qty, item it) {
   coinmaster master = to_coinmaster(it.seller);
   coinmaster master = it.seller;
   if(master == $coinmaster[none]) {
   if(master == $coinmaster[none]) {
       print("You do not need a coinmaster to purchase that", "red");
       print("You do not need a coinmaster to purchase that", "red");
Line 66: Line 96:
   }
   }
   int coins = master.available_tokens;
   int coins = master.available_tokens;
   int price = buy_price(master, it);
   int price = sell_price(master, it);
   if(price > coins) {
   if(price > coins) {
       print("You only have "+coins+" "+master.token+", but it costs "+price+" "+master.token, "red");
       print("You only have "+coins+" "+master.token+", but it costs "+price+" "+master.token, "red");
Line 76: Line 106:
}}|
}}|


see_also={{SeeAlso|sell|buy_price}}|
see_also={{SeeAlso|sell|buy_price|buy_using_storage}}|
cli_equiv=The CLI commands "buy" and "acquire" work similarly.|
cli_equiv=The CLI commands "buy" and "acquire" work similarly.|
}}
}}


[[Category:Item Management]]
[[Category:Item Management]]

Latest revision as of 07:12, 3 July 2021

Function Syntax

boolean buy(item it ,int qty )

  • it is the item to purchase
  • qty is the (optional) number to purchase

int buy(item it ,int qty ,int price )

  • it is the item to purchase
  • qty is the number to purchase
  • price is the (optional) maximum price to spend per item

boolean buy(int qty ,item it )

  • qty is the number to purchase
  • it is the item to purchase

int buy(int qty ,item it ,int price )

  • qty is the number to purchase
  • it is the item to purchase
  • price is the (optional) maximum price to spend per item

boolean buy(coinmaster master ,int qty ,item it )

  • master is the coinmaster to purchase from
  • qty is the number to purchase
  • it is the item to purchase

Attempts to purchase qty amount of item it. (Will not buy using meat from storage; use buy_using_storage() instead.) If qty is less than 1, the function will return true without purchasing anything. If the optional maximum price is specified, it returns the integer amount of items purchased; otherwise it returns true if the purchase succeeded and false if it did not. If a price less than 1 is passed, the function will always buy qty, regardless of price. Without the optional price parameter, first preference is given to NPC stores when attempting purchases, after which it will attempt to use the mall.

The versions that return a boolean value determine success depending on whether your inventory of the desired item went up by the amount you tried to buy.

If a coinmaster is listed, it will attempt to purchase from the coinmaster using the correct alternate currency. Otherwise it will be purchased from an NPC store or mall using meat.

Code Sample

Buy a broken skull.

buy(1 , $item[broken skull]);


This expands upon purchasing from a coinmaster to show many related functions.

boolean buy_coinmaster(int qty, item it) {
   coinmaster master = it.seller;
   if(master == $coinmaster[none]) {
      print("You do not need a coinmaster to purchase that", "red");
      return false;
   }
   if(!is_accessible(master)) {
      print(inaccessible_reason(master), "red");
      return false;
   }
   int coins = master.available_tokens;
   int price = sell_price(master, it);
   if(price > coins) {
      print("You only have "+coins+" "+master.token+", but it costs "+price+" "+master.token, "red");
      return false;
   }
   return buy(master, qty, it);
}

CLI Equivalent

The CLI commands "buy" and "acquire" work similarly.

See Also

sell() | buy_price() | buy_using_storage()