Skip to content

Commit

Permalink
[#33] Swapnil/Mustakim - resolved bugs in manage market real estate.
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnillothe committed Feb 26, 2019
1 parent 93b55c1 commit b10130f
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 120 deletions.
2 changes: 1 addition & 1 deletion public/pages/board.html
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ <h3 id="player_liabilities">Liabilities</h3>
<div id="manage-debt-form" class="manage-debt-form">
<div id="manage-debt-form-msg-Box"></div>
</div>
<div id="real-estate-div" class="real-estate-div">
<div id="market-card-div" class="market-card-div">
</div>
<div id="low-balance" class="popup-background">
<div class="top-popup">
Expand Down
25 changes: 12 additions & 13 deletions public/scripts/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const getBoard = function() {
};

const getEntriesHtml = function(entry) {
const {time, type, amount, currentBalance, event} = entry;
const symbols = {debit: "-", credit: "+"};
const { time, type, amount, currentBalance, event } = entry;
const symbols = { debit: "-", credit: "+" };
const currentTime = formatTime(new Date(time));
const entryDiv = createElement("div");
entryDiv.className = "entry";
Expand All @@ -25,7 +25,7 @@ const getEntriesHtml = function(entry) {
};

const setCashLedger = function(player) {
const {entries} = player;
const { entries } = player;
const entriesDiv = getElementById("cash-ledger-entries");
const entriesHtml = entries.map(getEntriesHtml);
appendChildren(entriesDiv, entriesHtml);
Expand Down Expand Up @@ -111,7 +111,7 @@ const doCharity = function() {
const acceptCharity = function() {
fetch("/isabletodocharity")
.then(res => res.json())
.then(({isAble}) => {
.then(({ isAble }) => {
if (isAble) doCharity();
});
};
Expand Down Expand Up @@ -207,11 +207,11 @@ const rollDice = function(numberOfDice) {
};
fetch("/rolldice", {
method: "POST",
headers: {"Content-Type": "application/json"},
body: JSON.stringify({numberOfDice})
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ numberOfDice })
})
.then(res => res.json())
.then(({diceValues, spaceType}) => {
.then(({ diceValues, spaceType }) => {
showDice(diceValues);
spacesHandlers[spaceType] && spacesHandlers[spaceType]();
});
Expand All @@ -227,7 +227,7 @@ const rollOneDice = function() {
const rollDie = function() {
fetch("/hascharity")
.then(res => res.json())
.then(({hasCharityTurns}) => {
.then(({ hasCharityTurns }) => {
if (!hasCharityTurns) return rollOneDice();
showOverlay("num_of_dice");
openOverlay("num_of_dice");
Expand All @@ -246,7 +246,7 @@ const processBankruptcy = function(currentPlayer) {
};

const polling = function(game) {
let {players, requester, currentPlayer} = game;
let { players, requester, currentPlayer } = game;
if (game.activeCard) {
showCard(game.activeCard, game.isMyTurn, requester);
}
Expand All @@ -265,7 +265,6 @@ const polling = function(game) {
};

const showCard = function(card, isMyTurn, player) {
if (isSameCard(card.data.title)) return;
const bigDealactions = [acceptBigDeal, declineBigDeal];

const cardHandlers = {
Expand Down Expand Up @@ -315,7 +314,7 @@ const updateGamePiece = function(player) {
newSpace.appendChild(gamePiece);
};

const createActivity = function({playerName, msg, time}) {
const createActivity = function({ playerName, msg, time }) {
const activity = createElement("div");
const activityPara = createElement("p");
activity.classList.add("activity");
Expand All @@ -337,8 +336,8 @@ const updateActivityLog = function(activityLog) {
};

const getPlayerData = function(playersData) {
const {playerName} = parseCookie();
const playerData = playersData.filter(({name}) => name == playerName)[0];
const { playerName } = parseCookie();
const playerData = playersData.filter(({ name }) => name == playerName)[0];
return playerData;
};

Expand Down
55 changes: 35 additions & 20 deletions public/scripts/market.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,48 @@ const getCommon = function(list1, list2) {
}, []);
};

const showMarketCard = function(card, player) {
const { title, message, relatedRealEstates } = card.data;
const cardDiv = getElementById("card");
showOverlay("card");
cardDiv.classList = [];
cardDiv.classList.add("plain-card");
cardDiv.classList.add("market-card");
const titleDiv = createTextDiv(title);
titleDiv.classList.add("card-title");
const messageDiv = createTextDiv(message);
messageDiv.classList.add("card-message");
appendChildren(cardDiv, [titleDiv, messageDiv]);
const handleRealEstate = function(player, { relatedRealEstates }) {
const playerRealEstates = player.liabilities.realEstate;
const commonEstates = getCommon(relatedRealEstates, playerRealEstates);
const cardDiv = getElementById("card");
const displayCommonEstates = displayEstates.bind(null, commonEstates);

if (commonEstates.length > 0 && !player.isTurnComplete) {
const sellButton = createPopupButton(
"Sell",
displayEstates.bind(null, commonEstates)
);
const sellButton = createPopupButton("Sell", displayCommonEstates);
const cancelButton = createPopupButton("Cancel", completeTurn);
appendChildren(cardDiv, [titleDiv, messageDiv, sellButton, cancelButton]);
cardDiv.appendChild(cancelButton);
cardDiv.appendChild(sellButton);
}
};

const handleGoldCoin = function() {};
const handleMarketCard = function(player, card) {
const { relatedTo } = card;
const marketHandlers = {
realEstate: handleRealEstate,
goldCoin: handleGoldCoin
};

marketHandlers[relatedTo](player, card);
};

const showMarketCard = function(card, player) {
const { title, message } = card.data;
const cardDiv = getElementById("card");
const titleDiv = createTextDiv(title);
const messageDiv = createTextDiv(message);
cardDiv.className = "plain-card market-card";
titleDiv.className = "card-title";
messageDiv.className = "card-message";
appendChildren(cardDiv, [titleDiv, messageDiv]);

handleMarketCard(player, card.data);
showOverlay("card");
};

const completeTurn = function() {
hideOverlay("card");
hideOverlay("real-estate-div");
hideOverlay("market-card-div");
fetch("/completeturn");
};

Expand Down Expand Up @@ -78,9 +93,9 @@ const showCommonEstates = function() {
const displayEstates = function(commonEstates) {
if (commonEstates.length == 0) return completeTurn();
hideOverlay("card");
const estateDiv = getElementById("real-estate-div");
const estateDiv = getElementById("market-card-div");
const estateTable = createTableHtml(commonEstates);
const doneButton = createPopupButton("Done", completeTurn);
appendChildren(estateDiv, [estateTable, doneButton]);
showOverlay("real-estate-div");
showOverlay("market-card-div");
};
2 changes: 1 addition & 1 deletion public/stylesheets/board.css
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ button:hover {
border-radius: 10px;
}

.real-estate-div {
.market-card-div {
height: 250px;
width: 300px;

Expand Down
63 changes: 32 additions & 31 deletions src/gameHandlers.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const {UNABLE_TO_DO_CHARITY_MSG} = require("./constant");
const { UNABLE_TO_DO_CHARITY_MSG } = require("./constant");

const startGame = function(req, res) {
req.game.startGame();
res.end();
};

const getGame = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const game = req.game;
const {currentPlayer} = game;
const { currentPlayer } = game;
if (currentPlayer.isDownSized()) {
game.skipTurn();
}
Expand All @@ -18,23 +18,23 @@ const getGame = function(req, res) {
};

const getPlayersFinancialStatement = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const requiredPlayer = req.game.getPlayerByName(playerName);
res.send(JSON.stringify(requiredPlayer));
};

const rollDice = function(req, res) {
const {numberOfDice} = req.body;
const {playerName} = req.cookies;
const { numberOfDice } = req.body;
const { playerName } = req.cookies;
const game = req.game;
const {currentPlayer} = game;
const { currentPlayer } = game;
if (currentPlayer.isDownSized()) {
game.skipTurn();
res.json({diceValues: [null]});
res.json({ diceValues: [null] });
return;
}
if (!game.isCurrentPlayer(playerName) || currentPlayer.rolledDice) {
res.json({diceValues: [null]});
res.json({ diceValues: [null] });
return;
}
const currentSpaceDetails = req.game.rollDice(numberOfDice);
Expand All @@ -45,7 +45,7 @@ const acceptCharity = function(req, res) {
req.game.acceptCharity();
const ledgerBalance = req.game.currentPlayer.getLedgerBalance();
req.game.nextPlayer();
res.send(JSON.stringify({ledgerBalance}));
res.send(JSON.stringify({ ledgerBalance }));
};

const declineCharity = function(req, res) {
Expand All @@ -65,7 +65,7 @@ const selectBigDeal = function(req, res) {
};

const grantLoan = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const loanAmount = +req.body.amount;
const game = req.game;
game.grantLoan(playerName, loanAmount);
Expand All @@ -74,7 +74,7 @@ const grantLoan = function(req, res) {
};

const payDebt = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const debtDetails = req.body;
const game = req.game;
game.payDebt(playerName, debtDetails);
Expand All @@ -83,7 +83,7 @@ const payDebt = function(req, res) {
};

const provideLiabilities = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const game = req.game;
const player = game.getPlayerByName(playerName);
res.send(JSON.stringify(player));
Expand All @@ -92,11 +92,11 @@ const provideLiabilities = function(req, res) {
const isAbleToDoCharity = function(req, res) {
const isAble = req.game.currentPlayer.isAbleToDoCharity();
if (!isAble) req.game.currentPlayer.setNotification(UNABLE_TO_DO_CHARITY_MSG);
res.send(JSON.stringify({isAble}));
res.send(JSON.stringify({ isAble }));
};

const acceptSmallDeal = function(req, res) {
const {activeCard} = req.game;
const { activeCard } = req.game;
let isSuccessful = true;
if (activeCard.data.relatedTo == "realEstate") {
isSuccessful = req.game.currentPlayer.addRealEstate(activeCard.data);
Expand All @@ -119,9 +119,9 @@ const rejectSmallDeal = function(req, res) {
};

const acceptBigDeal = function(req, res) {
const {activeCard} = req.game;
const { activeCard } = req.game;
const isSuccessful = req.game.currentPlayer.addRealEstate(activeCard.data);
if (!isSuccessful) return res.send({isSuccessful});
if (!isSuccessful) return res.send({ isSuccessful });
let requestedPlayer = req.cookies["playerName"];
req.game.addActivity(`${requestedPlayer} has accepted the deal`);
req.game.nextPlayer();
Expand All @@ -136,61 +136,62 @@ const rejectBigDeal = function(req, res) {
};

const hasCharity = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const game = req.game;
if (!game.isCurrentPlayer(playerName)) {
res.send(JSON.stringify({hasCharityTurns: false}));
res.send(JSON.stringify({ hasCharityTurns: false }));
return;
}
const hasCharityTurns = req.game.hasCharityTurns();
res.send(JSON.stringify({hasCharityTurns}));
res.send(JSON.stringify({ hasCharityTurns }));
};

const isSharePresent = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const hasShares = req.game.hasShares(playerName);
res.json({hasShares});
res.json({ hasShares });
};

const buyShares = function(req, res) {
let {numberOfShares} = req.body;
let { numberOfShares } = req.body;
const isCapable = req.game.isPlayerCapableToBuy(numberOfShares);
if (isCapable) {
req.game.buyShares(numberOfShares);
}
res.json({isCapable});
res.json({ isCapable });
};

const sellShares = function(req, res) {
const {playerName} = req.cookies;
let {numberOfShares} = req.body;
const { playerName } = req.cookies;
let { numberOfShares } = req.body;
const isCapable = req.game.isPlayerCapableToSell(playerName, numberOfShares);
if (isCapable) {
req.game.sellShares(playerName, numberOfShares);
}
res.json({isCapable});
res.json({ isCapable });
};

const completeTurn = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const player = req.game.getPlayerByName(playerName);
player.completeTurn();
req.game.nextPlayer();
res.end();
};

const sellEstate = function(req, res) {
const estate = req.body;
const game = req.game;
const marketCard = game.activeCard;
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const player = req.game.getPlayerByName(playerName);
const profit = player.sellEstate(estate, marketCard);
game.addActivity(`sold realEstate for $${profit} `, playerName);
game.addActivity(` sold Real Estate for $${profit} `, playerName);
res.end();
};

const provideCommonEstates = function(req, res) {
const {playerName} = req.cookies;
const { playerName } = req.cookies;
const commonEstates = req.game.getCommonEstates(playerName);
res.send(JSON.stringify(commonEstates));
};
Expand Down
6 changes: 3 additions & 3 deletions src/model/financialStatement.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {add, hasIntersection} = require("../utils/utils.js");
const { add, hasIntersection, isEqual } = require("../utils/utils.js");
const CashLedger = require("./cashLedger");
const getIncome = (value, content) => value + content.cashflow;

Expand Down Expand Up @@ -133,10 +133,10 @@ class FinancialStatement extends CashLedger {
const profit = this.calculateProfit(estate, marketCard);
this.addToLedgerBalance(profit);
this.liabilities.realEstate = this.liabilities.realEstate.filter(
realEstate => JSON.stringify(realEstate) !== JSON.stringify(estate)
realEstate => !isEqual(realEstate, estate)
);

this.addCreditEvent(profit, "sold Real Estate");
this.addCreditEvent(profit, " sold Real Estate");
return profit;
}

Expand Down
Loading

0 comments on commit b10130f

Please sign in to comment.