Browse Source

Refactor

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

1
.idea/inspectionProfiles/Project_Default.xml

@ -6,6 +6,7 @@ @@ -6,6 +6,7 @@
<language minSize="52" name="JavaScript" />
</Languages>
</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">
<option name="processCode" value="true" />
<option name="processLiterals" value="true" />

4
index.html

@ -41,8 +41,8 @@ @@ -41,8 +41,8 @@
<div class="card-body" id="collapseQuest">
<div v-if="currentQuest">
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)">
<img :title="resource.name" class="resource-icon" :src="resource.icon"> {{ currentQuest[resource.name.toLowerCase()] }}
<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="resources[resource].name" class="resource-icon" :src="resources[resource].icon"> {{ currentQuest[resource] }}
</span>
<br/>
<br/>

289
js/app.js

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

Loading…
Cancel
Save