Custom Calculators and Private Islands

Dan Parry
Dan Parry
Jul 28, 2017 · 2 min read

Years ago I wanted a private island. Most people don’t set their goals this high. Before you dismiss my ambition as being entirely unrealistic, this post takes place in the realm of Torn, a text based multiplayer role playing game. Don’t be alarmed by the themes of criminal activity. It’s a world much more focused on bringing people together. What makes it most exciting to play is the tremendous depth and near endless problems to solve and optimize.

Players start off living in a shack and can purchase better options along the way. The top tier property available is a private island. It’s not enough to have the basic model — you want it upgraded.

The price data is out there. Every property and upgrade cost is listed in the wiki. Upgrade considerations have been explored in prose. Unfortunately the data only comes in static formats that are tedious to work with.

Instead of manually adding and conditionally applying discounts, I want it to be easier to evaluate costs of different combinations of upgrades.

Incompatible combinations make for ugly spreadsheets.

I want a dynamic calculator rather than a static display of information. Instead of sloppily referencing different combinations, I want to select my options.

Using the property data from the wiki, I can store all the relevant values inside Objects.

// All possible upgrades
const modifications = {
hotTub: {cost: 17000, happy: 25},
sauna: {cost: 12000, happy: 50},
...
privateYacht: {cost: 895000000, happy: 500}
}
// This object can hold data on other properties if we add them
const properties = {
privateIsland: {cost: 500000000, happy: 2000}
}

With the data in place, I can now use form elements to decide which information to put to use. Every checkbox option can be tracked in state.

// Checkboxes are all the same
<input name='privateYacht'
type='checkbox'
checked={this.state.privateYacht}
onChange={this.handleCheckboxChange} />
// State is readily and easily updated
handleCheckboxChange(event) {
this.setState({
[event.target.name]: event.target.checked
})
}

State will be able to see which property modifications are selected. Now we can write a function to add the total cost of property and upgrades.

// App will render the result. Can also be adjusted for Happy
totalCost() {
let total = properties.privateIsland.cost
let options = this.state
Object.keys(modifications).forEach(function(key) {
if (options[key]){
total += modifications[key]["cost"]
}
})
return total
}

Now every option can be toggled and a cost will be calculated immediately!

Dan Parry

Written by

Dan Parry

Web Developer, pursuing excellence at every opportunity. Here to share and search for knowledge.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade