Skip to content
This repository has been archived by the owner on Oct 2, 2018. It is now read-only.

Commit

Permalink
Making cards with ability require a target-selection (#115)
Browse files Browse the repository at this point in the history
* first, many errors

* removed log

* can drop

* removed '!== undefined'

* clearer

* couldn't remove this!

* some changes

* solved bug

* solved coloring problem

* passing clickedCard

* change for allowing drop

* one test

* some test

* wrong name

* cute space

* status message

* mocking status for test
  • Loading branch information
Masclins authored and kenan-rhoton committed Apr 24, 2018
1 parent 91a08ce commit e4582ed
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 28 deletions.
1 change: 1 addition & 0 deletions frontend/src/game/board/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module.exports = {
boardState.forEach(function (row, rownum) {
row["cards"].forEach(function (cardInRow) {
var newCard = builder.buildCard(templates.baseCard, cardInRow);
newCard.setAttribute("rownum", rownum);

if (cardInRow["owner"] === "me") {
fetchRow("#my-rows .game-row", rownum).appendChild(newCard);
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/game/board/builder.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"use strict";

var status = require("game/status.js");
var play = require("game/play/play.js");
const baseRow = document.getElementById("row-template")
.content.querySelector("div");
const myRows = document.getElementById("my-rows");
Expand Down Expand Up @@ -40,6 +42,14 @@ module.exports = {
newCard.classList.remove("col-1");
newCard.classList.add("col-2");
newCard.innerHTML = cardData["power"];
newCard.addEventListener('click', function() {
if (status.clickedCard &&
status.clickedCard.hasAttribute("row-played") &&
!status.clickedCard.hasAttribute("target")) {
status.clickedCard.setAttribute("target", 1);
play.playCard(status.clickedCard);
}
});

return newCard;
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/game/hand.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ module.exports = {

handState.forEach(function (cardInHand, index) {
var newCard = templates.baseCard.cloneNode(true);
newCard.innerHTML = cardInHand["power"]
newCard.innerHTML += showAddPower(cardInHand["add-power"])
newCard.innerHTML = cardInHand["power"];
newCard.setAttribute("add-power", cardInHand["add-power"]);
newCard.innerHTML += showAddPower(cardInHand["add-power"]);
newCard.setAttribute("index", index);
newCard.addEventListener('click', function(){play.clickCard(newCard)});

Expand Down
14 changes: 9 additions & 5 deletions frontend/src/game/play/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@ module.exports = {
if (status.clickedCard !== undefined) {
event.preventDefault();

play.playCard(event.target.getAttribute("rownum"),
status.clickedCard.getAttribute("index"));
var rownum = event.target.getAttribute("rownum");
status.clickedCard.setAttribute("row-played", rownum);
play.playCard(status.clickedCard);
}
},
allowDrop(event) {
event.preventDefault();
},
dropOnRow(event) {
event.preventDefault();
const rownum = event.target.getAttribute("rownum");
const cardindex = event.dataTransfer.getData("handIndex");
var card = document.querySelector('.card[index="'+
event.dataTransfer.getData("handIndex")+
'"]');
card.setAttribute("row-played",
event.target.getAttribute("rownum"));

status.onGetStatus(function(status) {
if (status === config.messages["play"]) {
play.playCard(rownum, cardindex);
play.playCard(card);
}
})
}
Expand Down
40 changes: 23 additions & 17 deletions frontend/src/game/play/play.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@ const config = require('config/config.js');
const backend = config.servers["backend"];

module.exports = {
playCard(rownum, cardindex, target) {
const playData = {
index: cardindex,
row: rownum,
target: target,
};
document.querySelector('.card[index="'+cardindex+'"]').style.background = "yellow";
playCard(card) {
if (!card.hasAttribute("target") &&
card.getAttribute("add-power") !== "undefined") {
card.style.background = "yellow";
status.clickedCard = card;
document.getElementById("game-status").innerHTML = "Select a target";
} else {
const playData = {
index: card.getAttribute("index"),
row: card.getAttribute("row-played"),
};
card.style.background = "green";

fetch(
`http://${backend}/games/${params.gameID}/player/${params.playerID}`,
{
method: 'POST',
body: JSON.stringify(playData),
headers: { 'Content-Type': 'application/json' }
}
)
.then( () => status.setStatus() )
.catch(error => console.log(error))
fetch(
`http://${backend}/games/${params.gameID}/player/${params.playerID}`,
{
method: 'POST',
body: JSON.stringify(playData),
headers: { 'Content-Type': 'application/json' }
}
)
.then( () => status.setStatus() )
.catch(error => console.log(error))
}
}
}
8 changes: 4 additions & 4 deletions frontend/tests/game/hand.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,18 @@ test('Set hand obeys the state', () => {
document.querySelectorAll('.hand .card')[0].innerHTML
).toBe("3");

// Cards are replaced
var sampleHand = [{power: 5}, {power: 1}];
// Cards show ability
var sampleHand = [{power: 5, "add-power": -3}, {power: 1, "add-power": 7}];
hand.setHand(sampleHand);
expect(
document.querySelectorAll('.hand .card').length
).toBe(2);
expect(
document.querySelectorAll('.hand .card')[0].innerHTML
).toBe("5");
).toBe("5 (-3)");
expect(
document.querySelectorAll('.hand .card')[1].innerHTML
).toBe("1");
).toBe("1 (+7)");

// Cards are deleted
var sampleHand = [];
Expand Down
22 changes: 22 additions & 0 deletions frontend/tests/game/play.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use strict";

const play = require('game/play/play.js');

test('Playing cards changes background as intended', () => {
var card = document.createElement("div");
var mockedStatus = document.createElement("div");
mockedStatus.setAttribute("id", "game-status");
document.body.appendChild(mockedStatus);

card.setAttribute("add-power", "undefined");
play.playCard(card);
expect(card.style.background).toBe("green");

card.setAttribute("add-power", 1);
play.playCard(card);
expect(card.style.background).toBe("yellow");

card.setAttribute("target", 1);
play.playCard(card);
expect(card.style.background).toBe("green");
});

0 comments on commit e4582ed

Please sign in to comment.