Add trading and other stuff

This commit is contained in:
pjht 2018-10-06 11:34:12 -05:00
parent 741ef70b37
commit da3f493897
2 changed files with 72 additions and 24 deletions

View File

@ -23,6 +23,9 @@
<li class="nav-item">
<a class="nav-link" id="link_research" onclick="set_tab('research')">Research</a>
</li>
<li class="nav-item">
<a class="nav-link" id="link_trading" onclick="set_tab('trading')">Trading</a>
</li>
<li class="nav-item">
<a class="nav-link" id="link_options" onclick="set_tab('options')">Options</a>
</li>
@ -61,6 +64,8 @@
<p>Researches:</p>
<div id="researches"></div>
</div>
<div id="tab_trading">
</div>
<div id="tab_options">
<button onclick="reset()">Reset</button>
</div>

91
main.js
View File

@ -7,7 +7,6 @@ var numResources=0;
var buildings={};
var unlockedBuildings=[];
var has_shelter=false;
var research_enabled=false;
var researches={};
var unlockedResearch=[];
var research_points=0;
@ -41,7 +40,6 @@ var all_buildings={
metal:15
},
build_effects:{
research_enabled:true,
max_research_points:150
}
},
@ -52,6 +50,12 @@ var all_buildings={
build_effects: {
max_resources:50
}
},
"trading post":{
ings:{
wood:100,
metal:50
}
}
};
var worker_resource={
@ -59,25 +63,35 @@ var worker_resource={
miner:"metal",
scientist:"research_points"
};
var worker_rate=0.5;
var worker_rate=0.1;
var all_researches={
"Faster Workers":{
maxLevel:3,
"Faster workers":{
maxLevel:6,
cost:100,
effects:{
worker_rate:0.5
worker_rate:0.2
}
}
};
var multiplier=1.17;
var trading_rates={
wood:1,
metal:10
};
function tenthRound(number) {
return Math.round(number*10)/10;
}
function updateShown() {
var has_resources=Object.keys(resources).length>0;
var has_lab=Object.keys(buildings).includes("lab");
var has_tpost=Object.keys(buildings).includes("trading post");
$(".shelter_required").toggle(has_shelter);
$("#link_population").toggle(has_shelter);
$(".resources_required").toggle(has_resources);
$("#link_buildings").toggle(has_resources);
$("#link_research").toggle(research_enabled);
$(".research_required").toggle(research_enabled);
$("#link_research").toggle(has_lab);
$(".research_required").toggle(has_lab);
$("#link_trading").toggle(has_tpost);
}
function incResource(name,amount=1) {
if (name=="research_points") {
@ -89,10 +103,11 @@ function incResource(name,amount=1) {
research_points-=amount;
return;
}
research_points=tenthRound(research_points);
updateResearchPointInfo();
updateResearchButtons();
} else {
if (numResources>maxResources) {
if (numResources>=maxResources) {
return;
}
if (resources[name]) {
@ -105,21 +120,25 @@ function incResource(name,amount=1) {
decResource(name,amount);
return;
}
resources[name]=tenthRound(resources[name]);
numResources=tenthRound(numResources);
updateResourceInfo();
updateCraftButtons();
updateTradingButtons();
}
updateShown();
}
function decResource(name,amount) {
function decResource(name,amount=1) {
if (resources[name]<amount) {
return;
}
numResources-=amount;
resources[name]-=amount;
resources[name]=tenthRound(resources[name]);
numResources=tenthRound(numResources);
updateResourceInfo();
updateCraftButtons();
updateTradingButtons();
}
function incBuilding(name) {
if (buildings[name]) {
@ -135,12 +154,8 @@ function incBuilding(name) {
updatePopulationInfo();
updateWorkerInfo();
}
if (effect=="research_enabled") {
research_enabled=effects[effect];
}
if (effect=="max_resources") {
maxResources+=effects[effect];
}
if (effect=="max_research_points") {
max_research_points+=effects[effect];
@ -178,8 +193,8 @@ function craft(name) {
decResource(ing,amount);
}
updateResourceInfo();
updateCraftButtons();
incBuilding(name);
updateCraftButtons();
}
}
function capitalizeFirst(str) {
@ -203,6 +218,9 @@ function updateBuildingInfo() {
function updateCraftButtons() {
$("#craft_buttons").html("");
for (var name in all_buildings) {
if (name=="trading post" && buildings["trading post"]>0) {
continue;
}
var ings=buildingCost(name);
var show=true;
var disabled=false;
@ -239,9 +257,9 @@ function updateCraftButtons() {
i++;
}
if (disabled) {
$("#craft_buttons").append("<button disabled onclick=craft(\""+name+"\")>Craft a "+btext+"</button><br>");
$("#craft_buttons").append("<button disabled onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>");
} else {
$("#craft_buttons").append("<button onclick=craft(\""+name+"\")>Craft a "+btext+"</button><br>");
$("#craft_buttons").append("<button onclick=\"craft('"+name+"')\">Craft a "+btext+"</button><br>");
}
}
}
@ -327,7 +345,7 @@ function fire(type) {
function autoInc() {
for (var worker in workers) {
worker_amount=workers[worker];
amount=worker_rate*worker_amount;
var amount=worker_rate*worker_amount;
if (amount>0) {
if (worker=="scientist") {
var usedMetal=Math.ceil(worker_amount*0.4);
@ -353,7 +371,6 @@ function save() {
buildings:buildings,
unlockedBuildings:unlockedBuildings,
has_shelter:has_shelter,
research_enabled:research_enabled,
researches:researches,
unlockedResearch:unlockedResearch,
research_points:research_points,
@ -376,7 +393,6 @@ function load() {
buildings=gamestate.buildings;
unlockedBuildings=gamestate.unlockedBuildings;
has_shelter=gamestate.has_shelter;
research_enabled=gamestate.research_enabled;
researches=gamestate.researches;
unlockedResearch=gamestate.unlockedResearch;
research_points=gamestate.research_points;
@ -401,7 +417,6 @@ function init() {
buildings={};
unlockedBuildings=[];
has_shelter=false;
research_enabled=false;
researches={};
unlockedResearch=[];
research_points=0;
@ -421,8 +436,8 @@ function init() {
updateResearchPointInfo();
updateResearchButtons();
updateResearchInfo();
applyResearches();
updateTradingButtons();
}
function set_tab(tab) {
if ($("#link_"+tab).is(":visible")) {
@ -450,6 +465,34 @@ function updateResearchInfo() {
$("#researches").append("<p>"+name+": Level "+researches[name]+"</p>");
}
}
function canSell(name,amount=1) {
var goldGotten=trading_rates[name]*amount;
var spacesNeeded=goldGotten-amount;
return maxResources-numResources>=spacesNeeded;
}
function buy(name,amount=1) {
decResource("gold",trading_rates[name]*amount);
incResource(name,amount);
}
function sell(name,amount=1) {
decResource(name,amount);
incResource("gold",trading_rates[name]*amount);
}
function updateTradingButtons() {
$("#tab_trading").html("");
for (var name in trading_rates) {
var rate=trading_rates[name];
$("#tab_trading").append("<p>"+capitalizeFirst(name)+": "+rate+" gold"+"</p>");
$("#tab_trading").append("<button id=sell1 onclick=\"sell('"+name+"')\">Sell 1</button>&nbsp");
$("#tab_trading").append("<button id=buy1 onclick=\"buy('"+name+"')\">Buy 1</button><br>");
$("#tab_trading").append("<button id=sell10 onclick=\"sell('"+name+"',10)\">Sell 10</button>&nbsp");
$("#tab_trading").append("<button id=buy10 onclick=\"buy('"+name+"',10)\">Buy 10</button>");
$("#tab_trading #sell1").prop("disabled",!canSell(name)).removeAttr("id");
$("#tab_trading #buy1").prop("disabled",resources["gold"]<trading_rates[name]).removeAttr("id");
$("#tab_trading #sell10").prop("disabled",!canSell(name,10)).removeAttr("id");
$("#tab_trading #buy10").prop("disabled",resources["gold"]<trading_rates[name]*10).removeAttr("id");
}
}
$(document).ready(function() {
init();
$(window).on("unload",function(e){