Browse Source

Refactor

master
Nero Ignis 4 years ago
parent
commit
588fcdb8db
  1. 1
      .idea/inspectionProfiles/Project_Default.xml
  2. 4
      index.html
  3. 281
      js/app.js

1
.idea/inspectionProfiles/Project_Default.xml

@ -6,6 +6,7 @@
<language minSize="52" name="JavaScript" /> <language minSize="52" name="JavaScript" />
</Languages> </Languages>
</inspection_tool> </inspection_tool>
<inspection_tool class="JSUnresolvedFunction" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
<inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false"> <inspection_tool class="SpellCheckingInspection" enabled="false" level="TYPO" enabled_by_default="false">
<option name="processCode" value="true" /> <option name="processCode" value="true" />
<option name="processLiterals" value="true" /> <option name="processLiterals" value="true" />

4
index.html

@ -41,8 +41,8 @@
<div class="card-body" id="collapseQuest"> <div class="card-body" id="collapseQuest">
<div v-if="currentQuest"> <div v-if="currentQuest">
Items needed: <br/> Items needed: <br/>
<span v-for="resource in resources" v-if="currentQuest[resource.name.toLowerCase()] > 0" :style="'color: ' + getColorForQuestResource(currentQuest[resource.name.toLowerCase()], resource.amount)"> <span v-for="resource in Object.keys(resources)" v-if="resources[resource].unlocked && currentQuest[resource] > 0" :style="'color: ' + getColorForQuestResource(currentQuest[resource], resources[resource].amount)">
<img :title="resource.name" class="resource-icon" :src="resource.icon"> {{ currentQuest[resource.name.toLowerCase()] }} <img :title="resources[resource].name" class="resource-icon" :src="resources[resource].icon"> {{ currentQuest[resource] }}
</span> </span>
<br/> <br/>
<br/> <br/>

281
js/app.js

@ -65,8 +65,8 @@ let game = new Vue({
} }
}, },
storageNames: null, storageNames: null,
buildings: [ buildings: {
{ bank: {
name: 'Bank', name: 'Bank',
resource: 'gold', resource: 'gold',
icon: 'medieval_largeCastle', icon: 'medieval_largeCastle',
@ -81,7 +81,7 @@ let game = new Vue({
pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000], pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000],
intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120], intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120],
}, },
{ lumberjack: {
name: 'Lumberjack', name: 'Lumberjack',
resource: 'wood', resource: 'wood',
icon: 'medieval_lumber', icon: 'medieval_lumber',
@ -96,7 +96,7 @@ let game = new Vue({
pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000], pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000],
intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120], intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120],
}, },
{ carpenter: {
name: 'Carpenter', name: 'Carpenter',
resource: 'planks', resource: 'planks',
icon: 'medieval_lumber', icon: 'medieval_lumber',
@ -115,7 +115,7 @@ let game = new Vue({
pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000], pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000],
intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120], intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120],
}, },
{ quarry: {
name: 'Quarry', name: 'Quarry',
resource: 'stone', resource: 'stone',
icon: 'medieval_mine', icon: 'medieval_mine',
@ -130,7 +130,7 @@ let game = new Vue({
pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000], pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000],
intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120], intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120],
}, },
{ coalmine: {
name: 'Coal Mine', name: 'Coal Mine',
resource: 'coal', resource: 'coal',
icon: 'medieval_mine', icon: 'medieval_mine',
@ -145,7 +145,7 @@ let game = new Vue({
pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000], pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000],
intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120], intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120],
}, },
// { // blacksmith: {
// name: 'Blacksmith', // name: 'Blacksmith',
// resource: 'iron', // resource: 'iron',
// icon: 'medieval_blacksmith', // icon: 'medieval_blacksmith',
@ -165,7 +165,7 @@ let game = new Vue({
// pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000], // pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000],
// intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120], // intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120],
// }, // },
{ farm: {
name: 'Farm', name: 'Farm',
resource: 'corn', resource: 'corn',
icon: 'medieval_farm', icon: 'medieval_farm',
@ -180,7 +180,7 @@ let game = new Vue({
pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000], pricePerLevel: [500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000],
intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120], intervalPerLevel: [15, 25, 30, 45, 60, 90, 90, 120, 120, 120],
}, },
], },
currentQuest: null, currentQuest: null,
loadedIntervals: [] loadedIntervals: []
}, },
@ -232,20 +232,21 @@ let game = new Vue({
}, },
loadBuildingsFromStorage() { loadBuildingsFromStorage() {
let game = this;
let savedBuildings = JSON.parse(localStorage.getItem(this.storageNames.buildings)); let savedBuildings = JSON.parse(localStorage.getItem(this.storageNames.buildings));
if (savedBuildings) { if (savedBuildings) {
savedBuildings.forEach((building) => { Object.keys(savedBuildings).forEach((building) => {
if (building.isOwned) { if (savedBuildings[building].isOwned) {
building.loader = 0; savedBuildings[building].loader = 0;
} }
if (building.hasMissingResources) { if (savedBuildings[building].hasMissingResources) {
building.loader = 100; savedBuildings[building].loader = 100;
} }
if (building.level > building.maxLevel) { if (savedBuildings[building].level > savedBuildings[building].maxLevel) {
building.level = building.maxLevel; savedBuildings[building].level = savedBuildings[building].maxLevel;
} }
}); });
@ -267,90 +268,60 @@ let game = new Vue({
}, },
// Resource-Management // Resource-Management
// add resources
add(amount = 0, resource = 'gold') { add(amount = 0, resource = 'gold') {
switch (resource) { let resourceName = this.resources[resource].name.toLowerCase();
case 'wood':
this.sendResourceMessage(amount, 'wood');
console.log(this.resources.wood.amount, amount);
this.resources.wood.amount += amount;
this.resources.wood.unlocked = true;
break;
case 'planks':
this.sendResourceMessage(amount, 'planks');
this.resources.planks.amount += amount;
this.resources.planks.unlocked = true;
break;
case 'stone':
this.sendResourceMessage(amount, 'stone');
this.resources.stone.amount += amount;
this.resources.stone.unlocked = true;
break;
case 'bricks':
this.sendResourceMessage(amount, 'bricks');
this.resources.bricks.amount += amount;
this.resources.bricks.unlocked = true;
break;
case 'coal':
this.sendResourceMessage(amount, 'coal');
this.resources.coal.amount += amount;
this.resources.coal.unlocked = true;
break;
case 'iron':
this.sendResourceMessage(amount, 'iron');
this.resources.iron.amount += amount;
this.resources.iron.unlocked = true;
break;
case 'corn':
this.sendResourceMessage(amount, 'corn');
this.resources.corn.amount += amount;
this.resources.corn.unlocked = true;
break;
case 'gold':
this.sendResourceMessage(amount, 'gold');
this.resources.gold.amount += amount;
this.resources.gold.unlocked = true;
break;
}
this.resources[resourceName].amount += amount;
this.resources[resourceName].unlocked = true;
this.sendResourceMessage(amount, resourceName);
this.$forceUpdate(); this.$forceUpdate();
this.saveResourcesToStorage(); this.saveResourcesToStorage();
}, },
sub(amount = 0, resource = false) { // decrease resource
this.resources.gold.amount -= amount; sub(amount = 0, resource = 'gold') {
this.resources[resource].amount -= amount;
return this.saveResourcesToStorage(); return this.saveResourcesToStorage();
}, },
// Buying & upgrading buildings + checks + interval-generators // Buying & upgrading buildings + checks + interval-generators
reloadBuildings() { reloadBuildings() {
let vue = this; let game = this;
this.buildings.forEach((building) => { Object.keys(this.buildings).forEach((building) => {
if (building.isOwned && !building.hasMissingResources) { if (game.buildings[building].isOwned && !game.buildings[building].hasMissingResources) {
building.price = building.pricePerLevel[building.level -1]; game.buildings[building].price = game.buildings[building].pricePerLevel[game.buildings[building].level - 1];
building.amount = building.amountPerLevel[building.level -1]; game.buildings[building].amount = game.buildings[building].amountPerLevel[game.buildings[building].level - 1];
building.interval = building.intervalPerLevel[building.level -1]; game.buildings[building].interval = game.buildings[building].intervalPerLevel[game.buildings[building].level - 1];
vue.initiateIntervals(building); game.initiateIntervals(game.buildings[building]);
} }
}); });
}, },
initiateIntervals(building) { initiateIntervals(building) {
building.intervalEarnID = setInterval(() => { building.intervalEarnID = setInterval(() => {
if (building.requires) { if (building.hasRequirements) {
if ( if (
game.buildingHasEnoughResourcesToStart(building) game.buildingHasEnoughResourcesToStart(building)
) { ) {
building.hasMissingResources = true;
game.sendWarning(building.name + ' has stopped production of ' + building.resource + ' due to missing resources.')
clearInterval(building.intervalLoadingID);
game.saveBuildingsToStorage();
} else {
building.hasMissingResources = false; building.hasMissingResources = false;
game.sendWarning(building.name + ' has resumed production of ' + building.resource + '.') building.loader = 0;
game.sendNotification(building.name + ' has resumed production of ' + building.resource + '.')
game.useRequiredResources(building); game.useRequiredResources(building);
game.add(building.amount, building.resource); game.add(building.amount, building.resource);
game.reloadSingleBuilding(building); game.reloadSingleBuilding(building);
game.saveBuildingsToStorage();
} else {
if (building.hasMissingResources === false) {
building.hasMissingResources = true;
game.sendWarning(building.name + ' has stopped production of ' + building.resource + ' due to missing resources.')
clearInterval(building.intervalLoadingID);
game.saveBuildingsToStorage();
}
} }
} else { } else {
game.add(building.amount, building.resource); game.add(building.amount, building.resource);
@ -434,15 +405,27 @@ let game = new Vue({
}, },
buildingHasEnoughResourcesToStart(building) { buildingHasEnoughResourcesToStart(building) {
let resourcesMissing = [];
let game = this;
if (building.requires) { if (building.requires) {
return ( Object.keys(building.requires).forEach((resource) => {
building.requires.wood > this.resources.wood.amount || if (building.requires[resource] > this.resources[resource].amount) {
building.requires.stone > this.resources.stone.amount || resourcesMissing.push(resource);
building.requires.iron > this.resources.iron.amount || }
building.requires.bricks > this.resources.bricks.amount || });
building.requires.coal > this.resources.coal.amount ||
building.requires.corn > this.resources.corn.amount if (resourcesMissing.length) {
resourcesMissing.forEach((resource) => {
game.sendWarning(
'Not enough ' + resource + ' to produce ' + building.resource + '!'
); );
});
return false;
} else {
return true;
}
} else { } else {
return true; return true;
} }
@ -450,109 +433,83 @@ let game = new Vue({
// Quests // Quests
generateQuestWithRandomItems() { generateQuestWithRandomItems() {
// defines the rates of possible amounts of resources for quests let game = this;
let possibleAmountsForQuest = [ let quest = [];
0, 0, 0, 0, 5, 10, 15, 20, 25, 30, 35, 5, 10, 15, 20, 25, 30, 35, 50
];
let randomWood = 0;
let randomStone = 0;
let randomIron = 0;
let randomBricks = 0;
let randomCorn = 0;
let randomCoal = 0;
let randomPlanks = 0;
let rewardSum = 0; let rewardSum = 0;
let resources = Object.keys(game.resources);
if (this.resources.wood.unlocked > 0) { resources.forEach((resource) => {
randomWood = this.getRandomElementForQuestResource(possibleAmountsForQuest); if (game.resources[resource].unlocked === true && resource !== 'gold') {
rewardSum += (randomWood * this.resources.wood.worth); let amount = game.getRandomAmountForQuestResource();
}
if (this.resources.stone.unlocked > 0) {
randomStone = this.getRandomElementForQuestResource(possibleAmountsForQuest);
rewardSum += (randomStone * this.resources.stone.worth);
}
if (this.resources.iron.unlocked > 0) {
randomIron = this.getRandomElementForQuestResource(possibleAmountsForQuest);
rewardSum += (randomIron * this.resources.iron.worth);
}
if (this.resources.bricks.unlocked > 0) {
randomBricks = this.getRandomElementForQuestResource(possibleAmountsForQuest);
rewardSum += (randomBricks * this.resources.bricks.worth);
}
if (this.resources.corn.unlocked > 0) {
randomCorn = this.getRandomElementForQuestResource(possibleAmountsForQuest);
rewardSum += (randomCorn * this.resources.corn.worth);
}
if (this.resources.coal.unlocked > 0) { if (amount) {
randomCoal = this.getRandomElementForQuestResource(possibleAmountsForQuest); quest[resource] = amount;
rewardSum += (randomCoal * this.resources.coal.worth); rewardSum += (quest[resource] * game.resources[resource].worth);
} }
if (this.resources.planks.unlocked > 0) {
randomPlanks = this.getRandomElementForQuestResource(possibleAmountsForQuest);
rewardSum += (randomPlanks * this.resources.planks.worth);
} }
});
if (rewardSum > 0) { if (rewardSum > 0) {
this.currentQuest = { quest['reward'] = rewardSum;
wood: randomWood, game.currentQuest = quest;
planks: randomPlanks, game.$forceUpdate();
stone: randomStone,
coal: randomCoal,
iron: randomIron,
bricks: randomBricks,
corn: randomCorn,
reward: rewardSum
};
} else { } else {
this.currentQuest = null; game.currentQuest = null;
this.sendInfo('There are no quests available. Please try again.') game.sendInfo('There are no quests available. Please try again.')
} }
localStorage.setItem(this.storageNames.currentQuest, JSON.stringify(this.currentQuest)); localStorage.setItem(this.storageNames.currentQuest, JSON.stringify(this.currentQuest));
return this.currentQuest; return this.currentQuest;
}, },
getRandomElementForQuestResource(possibleValues) { getRandomAmountForQuestResource() {
return possibleValues[Math.floor(Math.random() * possibleValues.length)]; // defines the rates of possible amounts of resources for quests
let possibleAmountsForQuest = [
5, 10, 15, 20, 25, 30, 35, 50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 550, 600
];
return possibleAmountsForQuest[Math.floor(Math.random() * possibleAmountsForQuest.length)];
}, },
isQuestRedeemable() { isQuestRedeemable() {
let enoughWood = this.resources.wood.amount >= this.currentQuest.wood; let game = this;
let enoughStone = this.resources.stone.amount >= this.currentQuest.stone; let resourcesMissing = [];
let enoughIron = this.resources.iron.amount >= this.currentQuest.iron; let atLeastOneResourceIsMissing = false;
let enoughBricks = this.resources.bricks.amount >= this.currentQuest.bricks;
let enoughCorn = this.resources.corn.amount >= this.currentQuest.corn; Object.keys(game.currentQuest).forEach((resource) => {
let enoughCoal = this.resources.coal.amount >= this.currentQuest.coal; if (resource !== 'reward' && game.resources[resource].amount < game.currentQuest[resource]) {
resourcesMissing.push(resource);
if (enoughWood && enoughStone && enoughIron && enoughBricks && enoughCorn && enoughCoal) { atLeastOneResourceIsMissing = true;
return true; }
} else { });
if (atLeastOneResourceIsMissing) {
resourcesMissing.forEach((resource) => {
game.sendWarning(
'Not enough ' + resource + ' to end quest.'
);
});
return false; return false;
} else {
return true;
} }
}, },
redeemReward() { redeemReward() {
if (this.isQuestRedeemable()) { let game = this;
this.resources.gold.amount += this.currentQuest.reward;
this.resources.wood.amount -= this.currentQuest.wood; if (game.isQuestRedeemable()) {
this.resources.stone.amount -= this.currentQuest.stone; Object.keys(game.currentQuest).forEach((resource) => {
this.resources.iron.amount -= this.currentQuest.iron; if (resource !== 'reward') {
this.resources.bricks.amount -= this.currentQuest.bricks; game.resources[resource].amount -= game.currentQuest[resource];
this.resources.corn.amount -= this.currentQuest.corn; }
this.resources.coal.amount -= this.currentQuest.coal; });
this.sendRewardMessage(this.currentQuest.reward); game.add(game.currentQuest['reward']);
this.generateQuestWithRandomItems(); game.sendRewardMessage(game.currentQuest['reward']);
game.generateQuestWithRandomItems();
} else { } else {
this.sendWarning('Not enough resources to redeem reward.');
return false; return false;
} }
}, },

Loading…
Cancel
Save