Skip to content

Commit

Permalink
Many changes, all related to building.
Browse files Browse the repository at this point in the history
ajax.js:
    Change promptSettlement to promptVertex
    Retool promptNewSettlement so that it gives correct options
    Refactor myself a promptSetupSettlement to do setup settlements

gamelogic.js:
    Make insertSettlement take a compressed vertex
    Create a getBadDistanceRule
    Remove unnecessary if-statements in remove.*Resources()
  • Loading branch information
hashbrowncipher committed Dec 19, 2011
1 parent 8fd4508 commit 7940588
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 74 deletions.
51 changes: 29 additions & 22 deletions client/js/ajax.js
Expand Up @@ -25,8 +25,7 @@ var req_handlers = {
"req_robber" : do_robber
}

//TODO: Chance this to promptVertex
function promptSettlement(accept) {
function promptVertex(accept) {
var dfd = $.Deferred();

accept.forEach(function(i) {
Expand All @@ -41,14 +40,31 @@ function promptSettlement(accept) {
return dfd.promise();
}

function promptSetupSettlement() {
var accept = getValidDistanceRule();
return promptVertex(accept);
}

function promptNewSettlement() {
var o = getValidSettlementPlaces();
var a = [];
for(var i in o) {
a.push(o[i]);
}
var accept = [];
var distanceRule = getBadDistanceRule();
var settlements = gameboard.settlements;
gameboard.roads[userID].forEach(function(r) {
if(
r.user == userID &&
!(r.vertex2 in distanceRule)
) {
accept.push(r.vertex2);
}
if(
r.user == userID &&
!(r.vertex1 in distanceRule)
) {
accept.push(r.vertex1);
}
});

return promptSettlement(a);
return promptVertex(accept);
}

//if p is passed, allow only roads from position p
Expand Down Expand Up @@ -144,7 +160,7 @@ function handle_resources_gained(log_entry) {
function do_setup(log_entry) {
var settlement;

promptNewSettlement().done(gotSettlement)
promptSetupSettlement().done(gotSettlement)

function gotSettlement(p) {
settlement = p;
Expand Down Expand Up @@ -182,7 +198,7 @@ function handle_settlement_built(log_entry) {

sendToTicker(name(log_entry.user) + " built a settlement!");
// TODO: register the settlement build in our global gamestate model
insertSettlement(log_entry.user, decompress(log_entry.vertex));
insertSettlement(log_entry.user, log_entry.vertex);
drawSettlement(log_entry.vertex, gameboard.users[log_entry.user].color);
}

Expand Down Expand Up @@ -229,35 +245,26 @@ function do_robber(log_entry) {

function do_turn(log_entry) {
function send_update_new_settlement(p) {
insertSettlement(userID, decompress(p));
insertSettlement(userID, p);
//drawSettlement(p);
$.get(HOSTNAME + "/build_settlement", {"vertex" : p, "game" : gameID});

// keep giving the option to build, if we can.
if (hasRoadResources() || hasSettlementResources()) {
do_build();
}
do_build();
}

function send_update_new_road(p) {
insertRoad(userID, p.vertex1, p.vertex2);
$.get(HOSTNAME + "/build_road", {"vertex1" : p.vertex1, "vertex2" : p.vertex2, "game" : gameID});
do_build();
}

function do_build() {
var built = false;

if(hasRoadResources()) {
console.log("We can build a road!");
promptRoad().then(send_update_new_road);
built = true;
}
if(hasSettlementResources()) {
console.log("We can build a Settlement!");
promptNewSettlement().then(send_update_new_settlement);
built = true;
}
return built;
}

do_build();
Expand Down
82 changes: 30 additions & 52 deletions client/js/gamelogic.js
Expand Up @@ -3,18 +3,14 @@
// entries in `settlements` read as follows:
// with key of vertex, value is type of
// { settlement: CITY | SETTLEMENT, user:userID }
function insertSettlement(user, uvertex) {
// make sure we're valid and a settlement doesn't exist
// at that vertex yet.
var vertex = compress(uvertex);
if (isvalid(uvertex) && !gameboard.settlements[vertex]) {
gameboard.settlements[vertex] =
{
"settlement" : SETTLEMENT,
"user" : user
};
}
drawSettlement(vertex);
function insertSettlement(user, p, type) {
gameboard.settlements[p] =
{
"settlement" : type,
"user" : user
};

drawSettlement(p, gameboard.users[userID].color);
}

function hex_adjacent(p) {
Expand All @@ -32,24 +28,6 @@ function hex_adjacent(p) {
return adjacent;
}

function insertCity(user, vertex) {
// make sure we're valid and a settlement does exist
// at that vertex yet.
if (isvalid(vertex) && gameboard.settlements[vertex]
&& gameboard.settlements[vertex].settlement == SETTLEMENT
&& gameboard.settlements[vertex].user == user) {
if (hasCityResources()) {
// gameboard.settlements[vertex] =
// {"settlement" : CITY,
// "user" : userID};

if (user == userID) {
removeCityResources();
}
}
}
}

function insertRoad(user, vertex1, vertex2) {
if(!gameboard.roads[user]) {
gameboard.roads[user] = [];
Expand All @@ -66,7 +44,6 @@ function insertRoad(user, vertex1, vertex2) {
drawRoad(road);
}


function hasCityResources() {
if (gameboard.cards.ore > 3 && gameboard.cards.grain > 2) {
return true;
Expand All @@ -81,20 +58,6 @@ function hasRoadResources() {
else return false;
}

function removeRoadResources() {
if (hasRoadResources()) {
gameboard.cards.brick--;
gameboard.cards.lumber--;
}
}

function removeCityResources() {
if (hasCityResources()) {
gameboard.cards.ore -= 3;
gameboard.cards.grain -= 2;
}
}

function hasSettlementResources() {
if (gameboard.cards.brick
&& gameboard.cards.lumber
Expand All @@ -105,16 +68,24 @@ function hasSettlementResources() {
else return false;
}

function removeRoadResources() {
gameboard.cards.brick--;
gameboard.cards.lumber--;
}

function removeCityResources() {
gameboard.cards.ore -= 3;
gameboard.cards.grain -= 2;
}

function removeSettlementResources() {
if (hasSettlementResources()) {
gameboard.cards.brick--;
gameboard.cards.lumber--;
gameboard.cards.wool--;
gameboard.cards.grain--;
}
gameboard.cards.brick--;
gameboard.cards.lumber--;
gameboard.cards.wool--;
gameboard.cards.grain--;
}

function getValidSettlementPlaces() {
function getBadDistanceRule() {
var excluded = {};

for (var vertex in gameboard.settlements) {
Expand All @@ -127,6 +98,13 @@ function getValidSettlementPlaces() {
});
}

return excluded;
}


function getValidDistanceRule() {
var excluded = getBadDistanceRule();

return VERTICES.filter(function(vertex) {
return !(vertex in excluded)
});
Expand Down

0 comments on commit 7940588

Please sign in to comment.