Difference between pages "Historical price" and "Historical age"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
 
(Convert to Template:Function2 format, use Template:SeeAlso/Mall Prices, add JS sample code)
 
Line 1: Line 1:
 
<onlyinclude>{{{{{format|Function2}}}
 
<onlyinclude>{{{{{format|Function2}}}
|name=historical_price
+
|name=historical_age
|function1.return_type=int
+
|function1.return_type=float
|function1.description=Returns the most recently seen mall price of an item without making a server request.
+
|function1.description=Returns the age of the price returned by {{f|historical_age}} in days.
 
|function1.param1=shop_for
 
|function1.param1=shop_for
 
|function1.param1.type=item
 
|function1.param1.type=item
|function1.param1.description=Item to retrieve the price of
+
|function1.param1.description=Item to check for
|description=
+
|description=The return value is in days. For example, a return value of 0.5 means that the price is 12 hours old.
<p>This obeys the same restrictions as {{f|mall_price}}. Unlike {{f|mall_price}}, this function will never hit the server.</p>
 
 
|code1={{CodeSample
 
|code1={{CodeSample
 
   |title=Code Samples
 
   |title=Code Samples
   |description=Gives you an estimate of your total mall-worth.
+
   |description=This function returns a price no more than 18 hours old by checking {{f|mall_price}} if {{f|historical_price}} is older than that.
 
   |code=
 
   |code=
 
{{{!}} class="wikitable"
 
{{{!}} class="wikitable"
Line 17: Line 16:
 
{{!}}- style="vertical-align: top"
 
{{!}}- style="vertical-align: top"
 
{{!}}
 
{{!}}
<syntaxhighlight lang="d" line highlight="8">
+
<syntaxhighlight lang="d" line highlight="2">
cli_execute( "outfit save Backup" );
+
int check_price( item shopfor ) {
outfit( "birthday suit" );
+
  if ( historical_age( shopfor ) > .75 )
int [ item ] inventory = get_inventory();
+
      return mall_price( shopfor );
outfit( "Backup" );
+
   return historical_price( shopfor );
 
 
int total;
 
foreach it in inventory {
 
   total += historical_price( it ) * inventory[ it ];
 
 
}
 
}
string amount = to_string( total, "%,d" );
 
print( `The estimated total mall-worth of your inventory is {amount} meat.`, "blue" );
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
{{!}}
 
{{!}}
<syntaxhighlight lang="js" line highlight="17">
+
<syntaxhighlight lang="js" line highlight="5">
const {
+
const { historicalAge, historicalPrice, mallPrice } = require("kolmafia");
  cliExecute,
 
  getInventory,
 
  historicalPrice,
 
  outfit,
 
  print,
 
  toString: formatString,
 
} = require("kolmafia");
 
 
 
cliExecute("outfit save Backup");
 
outfit("birthday suit");
 
const inventory = getInventory();
 
outfit("Backup");
 
  
let total = 0;
+
function checkPrice(shopfor) {
for (let itemName in inventory) {
+
   if (historicalAge(shopfor) > .75)
   total += historicalPrice(Item.get(itemName)) * inventory[itemName];
+
    return mallPrice(shopfor);
 +
  return historicalPrice(shopfor);
 
}
 
}
// Because all JavaScript numbers are floating-point, we must format it as such
 
// and manually remove everything after the dot.
 
const amount = formatString(total, "%,f").split(".")[0];
 
print(
 
  "The estimated total mall-worth of your inventory is " + amount + " meat.",
 
  "blue"
 
);
 
 
</syntaxhighlight>
 
</syntaxhighlight>
 
{{!}}}
 
{{!}}}
Line 67: Line 42:
 
|{{{1|}}}
 
|{{{1|}}}
 
}}</onlyinclude>
 
}}</onlyinclude>
 +
[[Category:Item Management]]

Revision as of 15:06, 31 December 2020

Function Syntax

float historical_ageitem shop_for )

Returns the age of the price returned by historical_age() in days.
  • shop_for: Item to check for

The return value is in days. For example, a return value of 0.5 means that the price is 12 hours old.

Code Samples

This function returns a price no more than 18 hours old by checking mall_price() if historical_price() is older than that.

ASH JavaScript
1 int check_price( item shopfor ) {
2    if ( historical_age( shopfor ) > .75 )
3       return mall_price( shopfor );
4    return historical_price( shopfor );
5 }
1 const { historicalAge, historicalPrice, mallPrice } = require("kolmafia");
2 
3 function checkPrice(shopfor) {
4   if (historicalAge(shopfor) > .75)
5     return mallPrice(shopfor);
6   return historicalPrice(shopfor);
7 }

See Also

historical_age() | historical_price() | mall_price() | mall_prices()