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

91
main.js
View File

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