Commit d04446b1 authored by Jyrki's avatar Jyrki 🐾

you are now able to buy things

parent c4777d89
......@@ -211,12 +211,7 @@ whtim = window.setInterval wheatTimer, 100
$(document).ready -> updateCounters()
setPriceLabels = (type) ->
selectorBuy = "span#market-price-#{type}-buy"
selectorSell = "span#market-price-#{type}-sell"
selectorValue = "span[name=market-#{type}-val]"
selectorBuyButton = "button[name=market-buy][data-type=#{type}]"
getPrice = (type) ->
switch type
when "wheat"
buyPrice = window.gameVars.prices.buy.wheat
......@@ -231,6 +226,16 @@ setPriceLabels = (type) ->
buyPrice = window.gameVars.prices.buy.cattleGrid
sellPrice = window.gameVars.prices.sell.cattleGrid
[ buyPrice, sellPrice ]
setPriceLabels = (type) ->
selectorBuy = "span#market-price-#{type}-buy"
selectorSell = "span#market-price-#{type}-sell"
selectorValue = "span[name=market-#{type}-val]"
selectorBuyButton = "button[name=market-buy][data-type=#{type}]"
[buyPrice, sellPrice] = getPrice type
buyPrice *= Number($(selectorValue).html())
sellPrice *= Number($(selectorValue).html())
......@@ -246,11 +251,39 @@ setPriceLabels = (type) ->
return true
marketSliderChange = ->
self = $ this
selector = "span[name=market-#{self[0].dataset.type}-val]"
$(selector).html self.val()
setPriceLabels(self[0].dataset.type)
return true
self = $ this
selector = "span[name=market-#{self[0].dataset.type}-val]"
$(selector).html self.val()
setPriceLabels(self[0].dataset.type)
return true
marketBuyItem = ->
self = $ this
selector = "input[name=market-slider][data-type=#{self[0].dataset.type}]"
amount = Number($(selector).val()) # thanks a lot, jQuery!
[buyPrice, sellPrice] = getPrice self[0].dataset.type
buyPrice *= amount
window.gameVars.gold -= buyPrice
switch self[0].dataset.type
when "wheat"
window.gameVars.wheat.count += amount
window.gameVars.wheat.bought += amount
when "livestock"
window.gameVars.liveStock.count += amount
window.gameVars.liveStock.bought += amount
when "fields"
window.gameVars.fields.count += amount
window.gameVars.fields.bought += amount
when "cattle"
window.gameVars.cattleGrid.count += amount
window.gameVars.cattleGrid.bought += amount
updateCounters()
$(document).on "change", "input[name=market-slider]", marketSliderChange
$(document).on "mousemove", "input[name=market-slider]", marketSliderChange
$(document).on "click", "button[name=market-buy]", marketBuyItem
......@@ -123,7 +123,7 @@
</div>
<div class="col-md-6">
<p class="market-price text-center text-danger">...for <span id="market-price-wheat-sell">{n}</span> gold.</p>
<button class="btn btn-market btn-block btn-danger">Sell <span name="market-wheat-val">{n}</span> Wheat</button>
<button class="btn btn-market btn-block btn-danger" name="market-sell" data-type="wheat">Sell <span name="market-wheat-val">{n}</span> Wheat</button>
</div>
</div>
<input type="range" name="market-slider" data-type="wheat">
......@@ -142,7 +142,7 @@
</div>
<div class="col-md-6">
<p class="market-price text-center text-danger">...for <span id="market-price-livestock-sell">{n}</span> gold.</p>
<button class="btn btn-market btn-block btn-danger">Sell <span name="market-livestock-val">{n}</span> Livestock</button>
<button class="btn btn-market btn-block btn-danger" name="market-sell" data-type="livestock">Sell <span name="market-livestock-val">{n}</span> Livestock</button>
</div>
</div>
<input type="range" name="market-slider" data-type="livestock">
......@@ -161,7 +161,7 @@
</div>
<div class="col-md-6">
<p class="market-price text-center text-danger">...for <span id="market-price-fields-sell">{n}</span> gold.</p>
<button class="btn btn-market btn-block btn-danger">Sell <span name="market-fields-val">{n}</span> Fields</button>
<button class="btn btn-market btn-block btn-danger" name="market-sell" data-type="fields">Sell <span name="market-fields-val">{n}</span> Fields</button>
</div>
</div>
<input type="range" name="market-slider" data-type="fields">
......@@ -180,7 +180,7 @@
</div>
<div class="col-md-6">
<p class="market-price text-center text-danger">...for <span id="market-price-cattle-sell">{n}</span> gold.</p>
<button class="btn btn-market btn-block btn-danger">Sell <span name="market-cattle-val">{n}</span> Cattle Grids</button>
<button class="btn btn-market btn-block btn-danger" name="market-sell" data-type="cattle">Sell <span name="market-cattle-val">{n}</span> Cattle Grids</button>
</div>
</div>
<input type="range" name="market-slider" data-type="cattle">
......
// Generated by CoffeeScript 1.7.1
(function() {
var eventTimer, evtim, fieldTimer, fitim, initItem, marketSliderChange, setPriceLabels, updateCounters, warTimer, wheatTimer, whtim;
var eventTimer, evtim, fieldTimer, fitim, getPrice, initItem, marketBuyItem, marketSliderChange, setPriceLabels, updateCounters, warTimer, wheatTimer, whtim;
initItem = function(defaultCount, consumable) {
var retobj;
......@@ -231,12 +231,8 @@
return updateCounters();
});
setPriceLabels = function(type) {
var buyPrice, selectorBuy, selectorBuyButton, selectorSell, selectorValue, sellPrice;
selectorBuy = "span#market-price-" + type + "-buy";
selectorSell = "span#market-price-" + type + "-sell";
selectorValue = "span[name=market-" + type + "-val]";
selectorBuyButton = "button[name=market-buy][data-type=" + type + "]";
getPrice = function(type) {
var buyPrice, sellPrice;
switch (type) {
case "wheat":
buyPrice = window.gameVars.prices.buy.wheat;
......@@ -254,6 +250,16 @@
buyPrice = window.gameVars.prices.buy.cattleGrid;
sellPrice = window.gameVars.prices.sell.cattleGrid;
}
return [buyPrice, sellPrice];
};
setPriceLabels = function(type) {
var buyPrice, selectorBuy, selectorBuyButton, selectorSell, selectorValue, sellPrice, _ref;
selectorBuy = "span#market-price-" + type + "-buy";
selectorSell = "span#market-price-" + type + "-sell";
selectorValue = "span[name=market-" + type + "-val]";
selectorBuyButton = "button[name=market-buy][data-type=" + type + "]";
_ref = getPrice(type), buyPrice = _ref[0], sellPrice = _ref[1];
buyPrice *= Number($(selectorValue).html());
sellPrice *= Number($(selectorValue).html());
if (buyPrice > window.gameVars.gold) {
......@@ -275,8 +281,38 @@
return true;
};
marketBuyItem = function() {
var amount, buyPrice, selector, self, sellPrice, _ref;
self = $(this);
selector = "input[name=market-slider][data-type=" + self[0].dataset.type + "]";
amount = Number($(selector).val());
_ref = getPrice(self[0].dataset.type), buyPrice = _ref[0], sellPrice = _ref[1];
buyPrice *= amount;
window.gameVars.gold -= buyPrice;
switch (self[0].dataset.type) {
case "wheat":
window.gameVars.wheat.count += amount;
window.gameVars.wheat.bought += amount;
break;
case "livestock":
window.gameVars.liveStock.count += amount;
window.gameVars.liveStock.bought += amount;
break;
case "fields":
window.gameVars.fields.count += amount;
window.gameVars.fields.bought += amount;
break;
case "cattle":
window.gameVars.cattleGrid.count += amount;
window.gameVars.cattleGrid.bought += amount;
}
return updateCounters();
};
$(document).on("change", "input[name=market-slider]", marketSliderChange);
$(document).on("mousemove", "input[name=market-slider]", marketSliderChange);
$(document).on("click", "button[name=market-buy]", marketBuyItem);
}).call(this);
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment