|
|
|
@ -0,0 +1,144 @@
@@ -0,0 +1,144 @@
|
|
|
|
|
const ITEM_IRON_ORE = 1; |
|
|
|
|
const ITEM_IRON_BAR = 2; |
|
|
|
|
const ITEM_COAL = 3; |
|
|
|
|
|
|
|
|
|
class Factory { |
|
|
|
|
static get price() { |
|
|
|
|
return 400; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
constructor() { |
|
|
|
|
this.running = false; |
|
|
|
|
this.input = [ |
|
|
|
|
{ |
|
|
|
|
id: ITEM_IRON_ORE, |
|
|
|
|
amount: 30 |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
id: ITEM_COAL, |
|
|
|
|
amount: 10 |
|
|
|
|
} |
|
|
|
|
]; |
|
|
|
|
this.inventory = []; |
|
|
|
|
this.inventoryLimit = 24; |
|
|
|
|
this.name = ''; |
|
|
|
|
this.rate = 1; |
|
|
|
|
this.interval = null; |
|
|
|
|
this.itemToGenerate = { |
|
|
|
|
id: ITEM_IRON_BAR, |
|
|
|
|
name: 'Iron bar', |
|
|
|
|
recipe: { |
|
|
|
|
requires: [ |
|
|
|
|
{ |
|
|
|
|
id: ITEM_IRON_ORE, |
|
|
|
|
name: 'Iron ore', |
|
|
|
|
amount: 3 |
|
|
|
|
}, |
|
|
|
|
{ |
|
|
|
|
id: ITEM_COAL, |
|
|
|
|
name: 'Coal', |
|
|
|
|
amount: 1 |
|
|
|
|
} |
|
|
|
|
], |
|
|
|
|
output: 1, |
|
|
|
|
secondsToProduce: 5 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
start() { |
|
|
|
|
if (!this.itemToGenerate) return false; |
|
|
|
|
if (this.isInventoryFull()) return false; |
|
|
|
|
|
|
|
|
|
this.interval = setInterval(() => { |
|
|
|
|
this.produce(); |
|
|
|
|
}, this.itemToGenerate.recipe.secondsToProduce * (1000 / this.rate)); |
|
|
|
|
|
|
|
|
|
this.running = true; |
|
|
|
|
console.info('▶️ Production has started'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
stop() { |
|
|
|
|
clearInterval(this.interval); |
|
|
|
|
this.interval = null; |
|
|
|
|
this.running = false; |
|
|
|
|
console.info('🛑 Production has stopped'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
produce() { |
|
|
|
|
if (this.isInventoryFull()) { |
|
|
|
|
this.stop(); |
|
|
|
|
console.warn('⚠️ Inventory is full - Production stopped'); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let productionIssueOccured = false; |
|
|
|
|
|
|
|
|
|
this.itemToGenerate.recipe.requires.forEach((requiredItem) => { |
|
|
|
|
let inputItem = this.input.find(item => item.id === requiredItem.id); |
|
|
|
|
if (!inputItem || inputItem.amount <= 0 || requiredItem.amount > inputItem.amount) { |
|
|
|
|
console.warn('⚠️ Not enough '+requiredItem.name+' to produce '+this.itemToGenerate.name+' - requires '+requiredItem.amount+', got '+inputItem.amount); |
|
|
|
|
productionIssueOccured = true; |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.input.find(item => item.id === requiredItem.id).amount -= requiredItem.amount; |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
if (productionIssueOccured) { |
|
|
|
|
this.stop(); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.inventory.push(this.itemToGenerate); |
|
|
|
|
console.info('Produced 1 '+this.itemToGenerate.name+' ('+this.getItemCountInInventory(this.itemToGenerate.id)+')'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
getItemCountInInventory(id) { |
|
|
|
|
return this.inventory.filter(item => item.id === id).length; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
isInventoryFull() { |
|
|
|
|
return this.inventory.length >= this.inventoryLimit; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class Cluster { |
|
|
|
|
constructor() { |
|
|
|
|
this.inventory = []; |
|
|
|
|
this.factories = []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
startAll() { |
|
|
|
|
this.factories.forEach((factory) => { |
|
|
|
|
factory.start(); |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
stopAll() { |
|
|
|
|
this.factories.forEach((factory) => { |
|
|
|
|
factory.stop(); |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
class Game { |
|
|
|
|
constructor() { |
|
|
|
|
this.money = 1_000; |
|
|
|
|
this.cluster = new Cluster(); |
|
|
|
|
this.inventory = []; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
buyFactory() { |
|
|
|
|
if (this.money >= Factory.price) { |
|
|
|
|
this.cluster.factories.push(new Factory()); |
|
|
|
|
this.money -= Factory.price; |
|
|
|
|
} else { |
|
|
|
|
console.warn('💰 Not enough money to buy a factory'); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let game = new Game(); |
|
|
|
|
game.buyFactory(); |
|
|
|
|
game.cluster.startAll(); |