Difference between pages "Template:CodeSample" and "Historical price"

From Kolmafia
(Difference between pages)
Jump to navigation Jump to search
 
 
Line 1: Line 1:
<includeonly>{{#if: {{{title|}}}|<h2>{{{title}}}</h2>}}{{#if: {{{description|}}}|<p>{{{description}}}</p>}}
+
<onlyinclude>{{{{{format|Function2}}}
{{{code}}}{{#if: {{{moreinfo|}}}|{{{moreinfo}}}}}</includeonly>
+
|name=historical_price
<noinclude>
+
|function1.return_type=int
<p>Uses the named parameters ''title'', ''description'' and ''code'' to display a code block & its information.</p>
+
|function1.description=Returns the most recently seen mall price of an item without making a server request.
<p>Note that only ''code'' is required; the ''title'' and ''description'' are optional.</p>
+
|function1.param1=shop_for
 +
|function1.param1.type=item
 +
|function1.param1.description=Item to retrieve the price of
 +
|description=
 +
<p>This obeys the same restrictions as {{f|mall_price}}. Unlike {{f|mall_price}}, this function will never hit the server.</p>
 +
|code1={{CodeSample
 +
  |title=Code Samples
 +
  |description=Gives you an estimate of your total mall-worth.
 +
  |code=
 +
{{{!}} class="wikitable"
 +
! style="width: 50%" {{!}} ASH
 +
! style="width: 50%" {{!}} JavaScript
 +
{{!}}- style="vertical-align: top"
 +
{{!}}
 +
<syntaxhighlight lang="d" line highlight="8">
 +
cli_execute( "outfit save Backup" );
 +
outfit( "birthday suit" );
 +
int [ item ] inventory = get_inventory();
 +
outfit( "Backup" );
  
==Example==
+
int total;
{| class="wikitable"
+
foreach it in inventory {
! Wiki markup
+
   total += historical_price( it ) * inventory[ it ];
! Result
 
|-
 
| <pre>
 
{{CodeSample|
 
title=Sample Code|
 
description=Some basic sample code.|
 
code=
 
<syntaxhighlight lang="d" line>
 
int i = 1; //this line is useless!
 
if (to_int(my_id()) > 1) {
 
   print("Aw, I guess you aren't Jick.");
 
 
}
 
}
</syntaxhighlight>|
+
string amount = to_string( total, "%,d" );
moreinfo=
+
print( `The estimated total mall-worth of your inventory is {amount} meat.`, "blue" );
Here is how you show a return value:
+
</syntaxhighlight>
&lt;pre&gt;
+
{{!}}
"Aw, I guess you aren't Jick.");
+
<syntaxhighlight lang="js" line highlight="17">
&lt;/pre&gt;
+
const {
}}
+
  cliExecute,
</pre>
+
  getInventory,
|
+
  historicalPrice,
{{CodeSample|
+
  outfit,
title=Sample Code|
+
  print,
description=Some basic sample code.|
+
  toString: formatString,
code=
+
} = require("kolmafia");
<syntaxhighlight lang="d" line>
+
 
int i = 1; //this line is useless!
+
cliExecute("outfit save Backup");
if (to_int(my_id()) > 1) {
+
outfit("birthday suit");
  print("Aw, I guess you aren't Jick.");
+
const inventory = getInventory();
 +
outfit("Backup");
 +
 
 +
let total = 0;
 +
for (let itemName in inventory) {
 +
  total += historicalPrice(Item.get(itemName)) * inventory[itemName];
 
}
 
}
</syntaxhighlight>|
+
// Because all JavaScript numbers are floating-point, we must format it as such
moreinfo=
+
// and manually remove everything after the dot.
Here is how you show a return value:
+
const amount = formatString(total, "%,f").split(".")[0];
<pre>
+
print(
Aw, I guess you aren't Jick.
+
  "The estimated total mall-worth of your inventory is " + amount + " meat.",
</pre>
+
  "blue"
 +
);
 +
</syntaxhighlight>
 +
{{!}}}
 +
  |moreinfo=
 
}}
 
}}
|}
+
|see_also={{SeeAlso/Mall Prices}}
 
+
|cli_equiv=
==Read Me==
+
|more_info=
Please keep the following in mind when adding a code sample:
+
|special=
* Tabs display poorly in the <nowiki><syntaxhighlight></nowiki> tag. Please use 3 spaces for ASH, 2 spaces for JS.
+
|{{{1|}}}
** Yes, some people have other preferences; I'm not going to tell you how to write your own scripts
+
}}</onlyinclude>
** In order to make the wiki as easy-to-use as possible, it's important to keep to ONE standard, please
 
* Please make sure there is one code sample (the 1st) that is as short & to-the-point as is possible.
 
* Please make sure that testing the code sample won't be significantly negative to the user; these are designed as hands-on examples
 
** For instance, if you must autosell items in the sample, choose something cheap
 
** Do NOT include anything that could cause "permanent" damage!
 
* Please follow other conventions used on other code samples as much as possible
 
* Please avoid using cli_execute() if there is a built-in ash function
 
* Please note that incredibly long lines will break formatting on smaller screens; break things up if needed
 
* Do not modify a code sample purely to "add your own mark" to a page; if it illustrates the function, leave it be
 
 
 
[[Category:Basic Templates]]</noinclude>
 

Revision as of 14:56, 31 December 2020

Function Syntax

int historical_priceitem shop_for )

Returns the most recently seen mall price of an item without making a server request.
  • shop_for: Item to retrieve the price of

This obeys the same restrictions as mall_price(). Unlike mall_price(), this function will never hit the server.

Code Samples

Gives you an estimate of your total mall-worth.

ASH JavaScript
 1 cli_execute( "outfit save Backup" );
 2 outfit( "birthday suit" );
 3 int [ item ] inventory = get_inventory();
 4 outfit( "Backup" );
 5 
 6 int total;
 7 foreach it in inventory {
 8    total += historical_price( it ) * inventory[ it ];
 9 }
10 string amount = to_string( total, "%,d" );
11 print( `The estimated total mall-worth of your inventory is {amount} meat.`, "blue" );
 1 const {
 2   cliExecute,
 3   getInventory,
 4   historicalPrice,
 5   outfit,
 6   print,
 7   toString: formatString,
 8 } = require("kolmafia");
 9 
10 cliExecute("outfit save Backup");
11 outfit("birthday suit");
12 const inventory = getInventory();
13 outfit("Backup");
14 
15 let total = 0;
16 for (let itemName in inventory) {
17   total += historicalPrice(Item.get(itemName)) * inventory[itemName];
18 }
19 // Because all JavaScript numbers are floating-point, we must format it as such
20 // and manually remove everything after the dot.
21 const amount = formatString(total, "%,f").split(".")[0];
22 print(
23   "The estimated total mall-worth of your inventory is " + amount + " meat.",
24   "blue"
25 );

See Also

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