Thursday, September 30, 2021

Gem appraiser

I wanted to write a blog post today in celebration of Blackmoor week and Dave Arneson day tomorrow. Then I saw this draft of a blog post and thought, why not just finish it to get myself into gear before writing the actual post. There's not that much to say in this post anyway.

Ages ago I wrote a blog post detailing how to embed small scripts of code written in java script into a blog post. Last time i showed how to do one that could roll on a random table. I thought that would be the most useful since so many people publish random table constant on their blogs. With a few simple steps each of those posts could be interactive.

Since last time I have found out that you don't even need to host the code anywhere else that directly in the text field here on blogger. The code from the example below can be coped and pasted directly in between the lines of text and it will produce the program embedded into the post.

The program is a simple calculator of gem prices, that could easily be expanded upon. I thought I would make it as simple as possible so that most people would understand what the code does. The idea of the program is to be able to put a price on complex items. Instead of a simple table, the DM could set up parameters, such as quality, color, size, nature etc. and their influence on the price of the gem. For example the size might multiply the value of the gem exponentially. They might even put in a factor for the place in the world or a random factor representing luck. When this has been set up, the DM would always be able to price any gem just by inputting its details.

 


I think this is a great idea since objects of wildly varying prices can be put into the world, and their value will not be up to DM fiat, but instead will be a constant of the world. The DM doesn't have to produce a list of prices of all combinations since every combination can be appraised and will always give the same answer. 

Type of gem

Number of gems

Size of gem oz.

Press appraise to get a price

The code for the above program is the following. Try to go to HTML view while editing a blog post and pasting it in (though please note that java script will not work when the post is rendered as a preview).

<p style="color:grey">Type of gem <select id="gem_type" value="diamond"><option value="1">Diamond</option><option value="pearl">Pearl</option></select>
<p style="color:grey">Number of gems <input id="gem_no" type="number" value="1" step="1" /></p>
<p style="color:grey">Size of gem <input id="gem_size" type="number" value="1.2" step="0.1" /> oz.</p>
<button onclick="appraise()">Appraise</button>
<p style="color:grey"><span id="gem_result">Press appraise to get a price</span></p>

<script>
function appraise() {
	var gemTypeSelection = document.getElementById("gem_type")
	var gem_type = gemTypeSelection.options[gemTypeSelection.value].text;
  	var gem_no = document.getElementById("gem_no").value;
	var gem_size = document.getElementById("gem_size").value;
  	var price = Math.round(gem_no * gem_size * 277.78).toLocaleString('en');
	document.getElementById("gem_result").innerHTML = "The " + gem_type + " is worth " + price + " gold pieces";
}
</script>

Addendum: I just realized that you need to enable "Interpret Typed HTML" In the blogger settings. See the last post on programming JavaScript for instructions on how to enable the setting.

No comments:

Post a Comment