From eba6379b84c47ac60772b2ebc52b6c4aa047d4b4 Mon Sep 17 00:00:00 2001 From: Annapurna Pingale <44020027+anupingale@users.noreply.github.com> Date: Tue, 26 Feb 2019 09:44:17 +0530 Subject: [PATCH] [#31] Annapurna/Shubham - Extracted deal related functions into deal.js --- public/pages/board.html | 1 + public/scripts/board.js | 392 ++++----------------------------------- public/scripts/deals.js | 250 +++++++++++++++++++++++++ public/scripts/utils.js | 10 +- src/gameHandlers.js | 31 +--- test/gameHandlersTest.js | 33 +--- 6 files changed, 308 insertions(+), 409 deletions(-) create mode 100644 public/scripts/deals.js diff --git a/public/pages/board.html b/public/pages/board.html index 8380f67..37098bd 100644 --- a/public/pages/board.html +++ b/public/pages/board.html @@ -1,6 +1,7 @@ + diff --git a/public/scripts/board.js b/public/scripts/board.js index a9b76d4..4f6320e 100644 --- a/public/scripts/board.js +++ b/public/scripts/board.js @@ -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"; @@ -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); @@ -97,14 +97,6 @@ const createFinancialStatement = function() { }); }; -const createTextDiv = function(text) { - const textDiv = document.createElement("div"); - const textPara = document.createElement("p"); - textPara.innerText = text; - textDiv.appendChild(textPara); - return textDiv; -}; - const doCharity = function() { hideOverlay("askCharity"); closeOverlay("card-overlay"); @@ -119,7 +111,7 @@ const doCharity = function() { const acceptCharity = function() { fetch("/isabletodocharity") .then(res => res.json()) - .then(({ isAble }) => { + .then(({isAble}) => { if (isAble) doCharity(); }); }; @@ -130,132 +122,13 @@ const declineCharity = function() { fetch("/declineCharity"); }; -const acceptSmallDeal = function(event) { - let parent = event.target.parentElement; - fetch("/acceptSmallDeal") - .then(data => data.json()) - .then(({ isSuccessful }) => { - if (isSuccessful) { - parent.style.visibility = "hidden"; - return; - } - openOverlay("low-balance"); - }); -}; - -const declineSmallDeal = function() { - let parent = event.target.parentElement; - parent.style.visibility = "hidden"; - fetch("/declineSmallDeal"); -}; - -const acceptBigDeal = function(event) { - fetch("/acceptBigDeal") - .then(data => data.json()) - .then(({ isSuccessful }) => { - if (isSuccessful) { - let parent = event.target.parentElement; - parent.style.visibility = "hidden"; - return; - } - openOverlay("low-balance"); - }); -}; - -const declineBigDeal = function() { - let parent = event.target.parentElement; - parent.style.visibility = "hidden"; - fetch("/declineBigDeal"); -}; - -const createCardButtons = function(actions) { - const buttons = createElement("div"); - buttons.classList.add("buttons-div"); - const button1 = createButton( - "Accept", - "button_div", - "accept", - "button", - actions[0] - ); - - const button2 = createButton( - "decline", - "button_div", - "reject", - "button", - actions[1] - ); - appendChildren(buttons, [button1, button2]); - return buttons; -}; - -const getCardDiv = function(type) { - const cardDiv = getElementById("card"); - cardDiv.style.visibility = "visible"; - cardDiv.innerHTML = null; - cardDiv.classList = []; - cardDiv.classList.add("plain-card"); - cardDiv.classList.add(type); - return cardDiv; +const getCardTitle = function() { + let card = getElementById("card"); + return card.children[0] && card.children[0].children[0].innerText; }; -const createSharesSmallDeal = function(actions, card) { - const { title, message, symbol, historicTradingRange, currentPrice } = card; - const cardDiv = createCardDiv("smallDeal"); - const titleDiv = createTextDiv(title); - const messageDiv = createTextDiv(message); - const symbolDiv = createTextDiv(`Company Name : ${symbol}`); - const rangeDiv = createTextDiv(`Range : ${historicTradingRange}`); - const currentPriceDiv = createTextDiv(`current price : ${currentPrice}`); - const bottomDiv = createElement("div"); - bottomDiv.classList.add("card-bottom"); - const buttons = createCardButtons(actions); - appendChildren(bottomDiv, [symbolDiv, rangeDiv, currentPriceDiv]); - appendChildren(cardDiv, [titleDiv, messageDiv, bottomDiv, buttons]); -}; - -const createRealEstateDealCard = function(actions, card, isMyTurn) { - const { title, message, cost, mortgage, downPayment, cashflow } = card; - const cardDiv = createCardDiv("smallDeal"); - const titleDiv = createTextDiv(title); - const messageDiv = createTextDiv(message); - const mortgageDiv = createTextDiv(`Mortgage : ${mortgage}`); - const costDiv = createTextDiv(`Cost : ${cost}`); - const downPaymentDiv = createTextDiv(`Down Payment : ${downPayment}`); - const cashflowDiv = createTextDiv(`Cashflow ${cashflow}`); - const bottomDiv1 = createElement("div"); - const bottomDiv2 = createElement("div"); - bottomDiv1.classList.add("card-bottom"); - bottomDiv2.classList.add("card-bottom"); - appendChildren(bottomDiv1, [costDiv, cashflowDiv]); - appendChildren(bottomDiv2, [mortgageDiv, downPaymentDiv]); - appendChildren(cardDiv, [titleDiv, messageDiv, bottomDiv1, bottomDiv2]); - if (isMyTurn) cardDiv.appendChild(createCardButtons(actions)); -}; - -const createGoldSmallDeal = function(actions, card, isMyTurn) { - const { title, message, numberOfCoins, cost } = card; - const cardDiv = createCardDiv("smallDeal"); - const titleDiv = createTextDiv(title); - const messageDiv = createTextDiv(message); - const numberDiv = createTextDiv(`Coins : ${numberOfCoins}`); - const costDiv = createTextDiv(`Cost : ${cost}`); - const bottomDiv = createElement("div"); - bottomDiv.classList.add("card-bottom"); - appendChildren(bottomDiv, [numberDiv, costDiv]); - appendChildren(cardDiv, [titleDiv, messageDiv, bottomDiv]); - if (isMyTurn) cardDiv.appendChild(createCardButtons(actions)); -}; - -const showSmallDealCard = function(title, expenseAmount, type) { - const cardDiv = getCardDiv(type); - const titleDiv = createTextDiv(title); - titleDiv.classList.add("card-title"); - const expenseDiv = createTextDiv("Pay $ " + expenseAmount); - expenseDiv.classList.add("card-expense"); - cardDiv.appendChild(titleDiv); - cardDiv.appendChild(expenseDiv); +const isSameCard = function(cardTitle) { + return cardTitle == getCardTitle(); }; const showPlainCard = function(title, expenseAmount, type, msg) { @@ -273,197 +146,32 @@ const showPlainCard = function(title, expenseAmount, type, msg) { cardDiv.appendChild(expenseDiv); }; -const nothing = () => {}; - -const createCard = function(card) { - const {title, message, symbol, historicTradingRange, currentPrice} = card; - const cardDiv = getCardDiv("smallDeal"); - const titleDiv = createTextDiv(title); - const messageDiv = createTextDiv(message); - const symbolDiv = createTextDiv(`Company Name : ${symbol}`); - const rangeDiv = createTextDiv(`Range : ${historicTradingRange}`); - const currentPriceDiv = createTextDiv(`current price : ${currentPrice}`); - const bottomDiv = createElement("div"); - bottomDiv.classList.add("card-bottom"); - appendChildren(cardDiv, [titleDiv, messageDiv, bottomDiv]); - appendChildren(bottomDiv, [symbolDiv, rangeDiv, currentPriceDiv]); - return cardDiv; -}; - -const showNotEnoughBalance = function() { - alert(`you don't have enough balance`); -}; - -const showInvalidShareCount = function() { - alert(`you don't have enough shares`); -}; - -const buyShares = function() { - const numberOfShares = getElementById("share-count").value; - fetch("/buyshares", { - method: "POST", - headers: {"Content-Type": "application/json"}, - body: JSON.stringify({numberOfShares}) - }) - .then(res => res.json()) - .then(({isCapable}) => { - if (!isCapable) return showNotEnoughBalance(); - closeOverlay("share-card"); - closeOverlay("buttons-container"); - }); -}; - -const sellShares = function() { - const numberOfShares = getElementById("share-count").value; - fetch("/sellshares", { - method: "POST", - headers: {"Content-Type": "application/json"}, - body: JSON.stringify({numberOfShares}) - }) - .then(res => res.json()) - .then(({isCapable}) => { - if (!isCapable) return showInvalidShareCount(); - closeOverlay("share-card"); - closeOverlay("buttons-container"); - }); -}; - -const shareForm = function(func, id, value) { - const shareCard = getElementById("share-card"); - shareCard.style.display = "flex"; - const numberOfShares = createInput( - "shareCount", - "Enter No of shares", - "text", - "share-count" - ); - const submit = createButton(value, "", id, "", func); - appendChildren(shareCard, [numberOfShares, submit]); -}; - -const passDeal = function() { - closeOverlay("buttons-container"); -}; - -const createSellButton = function() { - return createButton( - "Sell", - "button_div", - "sell", - "button", - shareForm.bind(null, sellShares, "sell", "Sell") - ); -}; - -const createBuyButton = function() { - return createButton( - "Buy", - "button_div", - "buy", - "button", - shareForm.bind(null, buyShares, "buy", "Buy") - ); -}; - -const acceptSharesDeal = function(isAbleToSell) { - const buttonDiv = getElementById("buttons-container"); - const buy = createBuyButton(); - appendChildren(buttonDiv, [buy]); - if (isAbleToSell) { - const sell = createSellButton(); - buttonDiv.appendChild(sell); - } -}; - -const showBuyAndSellOptions = function(card) { - const cardDiv = createCard(card); - const buttons = createElement("div", "buttons-container"); - buttons.classList.add("buttons-div"); - const accept = createButton( - "Accept", - "button_div", - "accept", - "button", - acceptSharesDeal.bind(null, true) - ); - const decline = createButton( - "Decline", - "button_div", - "decline", - "button", - declineSmallDeal - ); - cardDiv.appendChild(buttons); - appendChildren(buttons, [accept, decline]); -}; - -const showBuyOption = function(card) { - const cardDiv = createCard(card); - const buttons = createElement("div", "buttons-container"); - buttons.classList.add("buttons-div"); - const accept = createButton( - "Accept", - "button_div", - "accept", - "button", - acceptSharesDeal.bind(null, false) - ); - const decline = createButton( - "Decline", - "button_div", - "decline", - "button", - declineSmallDeal - ); - cardDiv.appendChild(buttons); - appendChildren(buttons, [accept, decline]); -}; - -const showSellOption = function(card) { - const cardDiv = createCard(card); - const buttons = createElement("div", "buttons-container"); - buttons.classList.add("buttons-div"); - const sell = createSellButton(); - const pass = createButton("Pass", "button_div", "pass", "button", passDeal); - cardDiv.appendChild(buttons); - appendChildren(buttons, [sell, pass]); -}; - -const handleSharesSmallDeal = function(card, isMyTurn) { - fetch("/issharepresent") - .then(data => data.json()) - .then(({hasShares}) => { - if (hasShares && isMyTurn) return showBuyAndSellOptions(card.data); - if (hasShares) return showSellOption(card.data); - if (isMyTurn) return showBuyOption(card.data); - createCard(card.data); - }); -}; +const showCard = function(card, isMyTurn) { + if (isSameCard(card.data.title)) return; + const bigDealactions = [acceptBigDeal, declineBigDeal]; -const getSmallDealHandler = function(card, isMyTurn) { - const smallDealactions = [acceptSmallDeal, declineSmallDeal]; - let actions = smallDealactions; - if (card.data.relatedTo == "shares") { - return handleSharesSmallDeal(card, isMyTurn); - } - const dealCardTypes = { - goldCoins: createGoldSmallDeal.bind(null, actions), - realEstate: createRealEstateDealCard.bind(null, actions) + const cardHandlers = { + doodad: showPlainCard.bind( + null, + card.data.title, + card.data.expenseAmount, + "doodad-card" + ), + market: showPlainCard.bind( + null, + card.data.title, + card.data.cash, + "market-card" + ), + smallDeal: getSmallDealHandler(card, isMyTurn), + bigDeal: createRealEstateDealCard.bind( + null, + bigDealactions, + card.data, + isMyTurn + ) }; - if (card.dealDone) return nothing; - return ( - dealCardTypes[card.data.relatedTo] && - dealCardTypes[card.data.relatedTo].bind(null, card.data, isMyTurn) - ); -}; - -const getCardTitle = function() { - let card = getElementById("card"); - return card.children[0] && card.children[0].children[0].innerText; -}; - -const isSameCard = function(cardTitle) { - return cardTitle == getCardTitle(); + cardHandlers[card.type] && cardHandlers[card.type](); }; const handleCharity = function() { @@ -527,11 +235,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](); }); @@ -547,7 +255,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"); @@ -607,25 +315,6 @@ const showCard = function(card, isMyTurn, player) { cardHandlers[card.type] && cardHandlers[card.type](); }; -const getSmallDealHandler = function(card, isMyTurn) { - const shareDealactions = [acceptShareDeal, declineShareDeal]; - const smallDealactions = [acceptSmallDeal, declineSmallDeal]; - let actions = smallDealactions; - if (card.data.relatedTo == "shares") { - actions = shareDealactions; - } - const dealCardTypes = { - shares: createSharesSmallDeal.bind(null, actions), - goldCoins: createGoldSmallDeal.bind(null, actions), - realEstate: createRealEstateDealCard.bind(null, actions) - }; - if (card.dealDone) return nothing; - return ( - dealCardTypes[card.data.relatedTo] && - dealCardTypes[card.data.relatedTo].bind(null, card.data, isMyTurn) - ); -}; - const showNotification = function(notification) { const odlNotifDiv = document.getElementById("notification-div").children[0]; const oldNotification = odlNotifDiv && odlNotifDiv.children[0].innerText; @@ -646,7 +335,6 @@ const showNotification = function(notification) { }; const updateGamePiece = function(player) { - console.log(player); let gamePiece = document.getElementById("gamePiece" + player.turn); gamePiece.classList.add("visible"); let space = gamePiece.parentNode; @@ -655,7 +343,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"); @@ -677,8 +365,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; }; diff --git a/public/scripts/deals.js b/public/scripts/deals.js new file mode 100644 index 0000000..6f46ea5 --- /dev/null +++ b/public/scripts/deals.js @@ -0,0 +1,250 @@ +const getCardDiv = function(type) { + const cardDiv = getElementById("card"); + cardDiv.style.visibility = "visible"; + cardDiv.innerHTML = null; + cardDiv.classList = []; + cardDiv.classList.add("plain-card"); + cardDiv.classList.add(type); + return cardDiv; +}; + +const acceptSmallDeal = function(event) { + let parent = event.target.parentElement; + fetch("/acceptSmallDeal") + .then(data => data.json()) + .then(({isSuccessful}) => { + if (isSuccessful) { + parent.style.visibility = "hidden"; + return; + } + openOverlay("low-balance"); + }); +}; + +const declineSmallDeal = function() { + let parent = event.target.parentElement; + parent.style.visibility = "hidden"; + fetch("/declineSmallDeal"); +}; + +const acceptBigDeal = function(event) { + fetch("/acceptBigDeal") + .then(data => data.json()) + .then(({isSuccessful}) => { + if (isSuccessful) { + let parent = event.target.parentElement; + parent.style.visibility = "hidden"; + return; + } + openOverlay("low-balance"); + }); +}; + +const declineBigDeal = function() { + let parent = event.target.parentElement; + parent.style.visibility = "hidden"; + fetch("/declineBigDeal"); +}; + +const createAcceptButton = actions => + createButton("Accept", "button_div", "accept", "button", actions); + +const createDeclineButton = actions => + createButton("decline", "button_div", "reject", "button", actions); + +const createSellButton = function() { + return createButton( + "Sell", + "button_div", + "sell", + "button", + shareForm.bind(null, sellShares, "sell", "Sell") + ); +}; + +const createBuyButton = () => { + return createButton( + "Buy", + "button_div", + "buy", + "button", + shareForm.bind(null, buyShares, "buy", "Buy") + ); +}; + +const nothing = () => {}; + +const passDeal = () => closeOverlay("buttons-container"); + +const showNotEnoughBalance = function() { + alert(`you don't have enough balance`); +}; + +const showInvalidShareCount = function() { + alert(`you don't have enough shares`); +}; + +const createCardButtons = function(actions) { + const buttons = createElement("div"); + buttons.classList.add("buttons-div"); + const accept = createAcceptButton(actions[0]); + const decline = createDeclineButton(actions[1]); + appendChildren(buttons, [accept, decline]); + return buttons; +}; + +const createRealEstateDealCard = function(actions, card, isMyTurn) { + const {title, message, cost, mortgage, downPayment, cashflow} = card; + const cardDiv = getCardDiv("smallDeal"); + const titleDiv = createTextDiv(title); + const messageDiv = createTextDiv(message); + const mortgageDiv = createTextDiv(`Mortgage : ${mortgage}`); + const costDiv = createTextDiv(`Cost : ${cost}`); + const downPaymentDiv = createTextDiv(`Down Payment : ${downPayment}`); + const cashflowDiv = createTextDiv(`Cashflow ${cashflow}`); + const bottomDiv1 = createElement("div"); + const bottomDiv2 = createElement("div"); + bottomDiv1.classList.add("card-bottom"); + bottomDiv2.classList.add("card-bottom"); + appendChildren(bottomDiv1, [costDiv, cashflowDiv]); + appendChildren(bottomDiv2, [mortgageDiv, downPaymentDiv]); + appendChildren(cardDiv, [titleDiv, messageDiv, bottomDiv1, bottomDiv2]); + if (isMyTurn) cardDiv.appendChild(createCardButtons(actions)); +}; + +const createGoldSmallDeal = function(actions, card, isMyTurn) { + const {title, message, numberOfCoins, cost} = card; + const cardDiv = getCardDiv("smallDeal"); + const titleDiv = createTextDiv(title); + const messageDiv = createTextDiv(message); + const numberDiv = createTextDiv(`Coins : ${numberOfCoins}`); + const costDiv = createTextDiv(`Cost : ${cost}`); + const bottomDiv = createElement("div"); + bottomDiv.classList.add("card-bottom"); + appendChildren(bottomDiv, [numberDiv, costDiv]); + appendChildren(cardDiv, [titleDiv, messageDiv, bottomDiv]); + if (isMyTurn) cardDiv.appendChild(createCardButtons(actions)); +}; + +const createCard = function(card) { + const {title, message, symbol, historicTradingRange, currentPrice} = card; + const cardDiv = getCardDiv("smallDeal"); + const titleDiv = createTextDiv(title); + const messageDiv = createTextDiv(message); + const symbolDiv = createTextDiv(`Company Name : ${symbol}`); + const rangeDiv = createTextDiv(`Range : ${historicTradingRange}`); + const currentPriceDiv = createTextDiv(`current price : ${currentPrice}`); + const bottomDiv = createElement("div"); + bottomDiv.classList.add("card-bottom"); + appendChildren(cardDiv, [titleDiv, messageDiv, bottomDiv]); + appendChildren(bottomDiv, [symbolDiv, rangeDiv, currentPriceDiv]); + return cardDiv; +}; + +const buyShares = function() { + const numberOfShares = getElementById("share-count").value; + fetch("/buyshares", { + method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify({numberOfShares}) + }) + .then(res => res.json()) + .then(({isCapable}) => { + if (!isCapable) return showNotEnoughBalance(); + closeOverlay("share-card"); + closeOverlay("buttons-container"); + }); +}; + +const sellShares = function() { + const numberOfShares = getElementById("share-count").value; + fetch("/sellshares", { + method: "POST", + headers: {"Content-Type": "application/json"}, + body: JSON.stringify({numberOfShares}) + }) + .then(res => res.json()) + .then(({isCapable}) => { + if (!isCapable) return showInvalidShareCount(); + closeOverlay("share-card"); + closeOverlay("buttons-container"); + }); +}; + +const shareForm = function(func, id, value) { + const shareCard = getElementById("share-card"); + shareCard.style.display = "flex"; + const numberOfShares = createInput( + "shareCount", + "Enter No of shares", + "text", + "share-count" + ); + const submit = createButton(value, "", id, "", func); + appendChildren(shareCard, [numberOfShares, submit]); +}; + +const showOptions = function(isAbleToSell) { + const buttonDiv = getElementById("buttons-container"); + const buy = createBuyButton(); + appendChildren(buttonDiv, [buy]); + if (isAbleToSell) { + const sell = createSellButton(); + buttonDiv.appendChild(sell); + } +}; + +const showBuyAndSellOptions = function(card) { + const cardDiv = createCard(card); + const buttons = createElement("div", "buttons-container"); + buttons.classList.add("buttons-div"); + const accept = createAcceptButton(showOptions.bind(null, true)); + const decline = createDeclineButton(declineSmallDeal); + cardDiv.appendChild(buttons); + appendChildren(buttons, [accept, decline]); +}; + +const showBuyOption = function(card) { + const cardDiv = createCard(card); + const buttons = createElement("div", "buttons-container"); + buttons.classList.add("buttons-div"); + const accept = createAcceptButton(showOptions.bind(null, false)); + const decline = createDeclineButton(declineSmallDeal); + cardDiv.appendChild(buttons); + appendChildren(buttons, [accept, decline]); +}; + +const showSellOption = function(card) { + const cardDiv = createCard(card); + const buttons = createElement("div", "buttons-container"); + buttons.classList.add("buttons-div"); + const sell = createSellButton(); + const pass = createButton("Pass", "button_div", "pass", "button", passDeal); + cardDiv.appendChild(buttons); + appendChildren(buttons, [sell, pass]); +}; + +const handleSharesSmallDeal = function(card, isMyTurn) { + fetch("/issharepresent") + .then(data => data.json()) + .then(({hasShares}) => { + if (hasShares && isMyTurn) return showBuyAndSellOptions(card.data); + if (hasShares) return showSellOption(card.data); + if (isMyTurn) return showBuyOption(card.data); + createCard(card.data); + }); +}; + +const getSmallDealHandler = function(card, isMyTurn) { + const smallDealactions = [acceptSmallDeal, declineSmallDeal]; + const dealCardTypes = { + shares: handleSharesSmallDeal.bind(null, card, isMyTurn), + goldCoins: createGoldSmallDeal.bind(null, smallDealactions), + realEstate: createRealEstateDealCard.bind(null, smallDealactions) + }; + if (card.dealDone) return nothing; + return ( + dealCardTypes[card.data.relatedTo] && + dealCardTypes[card.data.relatedTo].bind(null, card.data, isMyTurn) + ); +}; diff --git a/public/scripts/utils.js b/public/scripts/utils.js index d07c9da..c9a2a79 100644 --- a/public/scripts/utils.js +++ b/public/scripts/utils.js @@ -55,7 +55,7 @@ const parseCookie = function() { keyValuePairs.forEach(keyValue => { const [key, value] = keyValue.split("="); parsedCookie[decodeURI(key)] = decodeURI(value); - }); + }); return parsedCookie; }; @@ -125,3 +125,11 @@ const showOverlay = function(id) { const element = getElementById(id); element.style.visibility = "visible"; }; + +const createTextDiv = function(text) { + const textDiv = document.createElement("div"); + const textPara = document.createElement("p"); + textPara.innerText = text; + textDiv.appendChild(textPara); + return textDiv; +}; diff --git a/src/gameHandlers.js b/src/gameHandlers.js index 1525a3e..a9f9c28 100644 --- a/src/gameHandlers.js +++ b/src/gameHandlers.js @@ -55,20 +55,12 @@ const declineCharity = function(req, res) { }; const selectSmallDeal = function(req, res) { - let {currentPlayer} = req.game; - if (currentPlayer.gotDeal) { - req.game.handleSmallDeal(); - currentPlayer.gotDeal = false; - } + req.game.handleSmallDeal(); res.end(); }; const selectBigDeal = function(req, res) { - let {currentPlayer} = req.game; - if (currentPlayer.gotDeal) { - req.game.handleBigDeal(); - currentPlayer.gotDeal = false; - } + req.game.handleBigDeal(); res.end(); }; @@ -105,7 +97,6 @@ const isAbleToDoCharity = function(req, res) { const acceptSmallDeal = function(req, res) { const {activeCard} = req.game; - if (activeCard.dealDone) return res.end(); let isSuccessful = true; if (activeCard.data.relatedTo == "realEstate") { isSuccessful = req.game.currentPlayer.addRealEstate(activeCard.data); @@ -116,43 +107,31 @@ const acceptSmallDeal = function(req, res) { if (!isSuccessful) return res.send({isSuccessful}); let requestedPlayer = req.cookies["playerName"]; req.game.addActivity(`${requestedPlayer} has accepted the deal`); - if (activeCard.data.relatedTo == "shares") { - return handleShareSmallDeal(req, res); - } req.game.nextPlayer(); - activeCard.dealDone = true; res.send({isSuccessful}); }; const rejectSmallDeal = function(req, res) { - const {activeCard} = req.game; - if (activeCard.dealDone) return res.end(); let requestedPlayer = req.cookies["playerName"]; req.game.addActivity(`${requestedPlayer} has rejected the deal`); req.game.nextPlayer(); - activeCard.dealDone = true; res.end(); }; const acceptBigDeal = function(req, res) { const {activeCard} = req.game; - if (activeCard.dealDone) return res.end(); const isSuccessful = req.game.currentPlayer.addRealEstate(activeCard.data); if (!isSuccessful) return res.send({isSuccessful}); let requestedPlayer = req.cookies["playerName"]; req.game.addActivity(`${requestedPlayer} has accepted the deal`); req.game.nextPlayer(); - activeCard.dealDone = true; res.end(); }; const rejectBigDeal = function(req, res) { - const {activeCard} = req.game; - if (activeCard.dealDone) return res.end(); let requestedPlayer = req.cookies["playerName"]; req.game.addActivity(`${requestedPlayer} has rejected the deal`); req.game.nextPlayer(); - activeCard.dealDone = true; res.end(); }; @@ -193,7 +172,7 @@ const sellShares = function(req, res) { }; const completeTurn = function(req, res) { - const { playerName } = req.cookies; + const {playerName} = req.cookies; const player = req.game.getPlayerByName(playerName); player.completeTurn(); res.end(); @@ -203,7 +182,7 @@ 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); @@ -211,7 +190,7 @@ const sellEstate = function(req, res) { }; const provideCommonEstates = function(req, res) { - const { playerName } = req.cookies; + const {playerName} = req.cookies; const commonEstates = req.game.getCommonEstates(playerName); res.send(JSON.stringify(commonEstates)); }; diff --git a/test/gameHandlersTest.js b/test/gameHandlersTest.js index e5ccde4..ad63ce4 100644 --- a/test/gameHandlersTest.js +++ b/test/gameHandlersTest.js @@ -145,15 +145,10 @@ describe("selectSmallDeal", function() { }; res.end = sinon.spy(); }); - it("should call handleSmallDeal when current player got deal", function() { + it("should call handleSmallDeal when current player wants to take small deal", function() { selectSmallDeal(req, res); expect(req.game.handleSmallDeal.calledOnce).to.be.true; }); - it("should not call handleSmallDeal when current player didntgot deal", function() { - req.game.currentPlayer.gotDeal = false; - selectSmallDeal(req, res); - expect(req.game.handleSmallDeal.calledOnce).to.be.false; - }); }); describe("selectBigDeal", function() { const req = {}, @@ -165,15 +160,10 @@ describe("selectBigDeal", function() { }; res.end = sinon.spy(); }); - it("should call handleBigDeal when current player got deal", function() { + it("should call handleBigDeal when player selected big deal", function() { selectBigDeal(req, res); expect(req.game.handleBigDeal.calledOnce).to.be.true; }); - it("should not call handleBigDeal when current player didntgot deal", function() { - req.game.currentPlayer.gotDeal = false; - selectBigDeal(req, res); - expect(req.game.handleBigDeal.calledOnce).to.be.false; - }); }); describe("isAbleToDoCharity", function() { @@ -316,12 +306,6 @@ describe("acceptSmallDeal", function() { res.end = sinon.spy(); res.send = sinon.spy(); }); - it("should return nothing if deal is already done ", function() { - req.game.activeCard.dealDone = true; - const output = acceptSmallDeal(req, res); - expect(output).to.be.undefined; - expect(req.game.nextPlayer.calledOnce).to.be.false; - }); it("should call nextPlayer if deal is not done ", function() { acceptSmallDeal(req, res); expect(req.game.nextPlayer.calledOnce).to.be.true; @@ -342,12 +326,7 @@ describe("rejectSmallDeal", function() { res.end = sinon.spy(); res.send = sinon.spy(); }); - it("should return nothing if deal is already done ", function() { - req.game.activeCard.dealDone = true; - const output = rejectSmallDeal(req, res); - expect(output).to.be.undefined; - expect(req.game.nextPlayer.calledOnce).to.be.false; - }); + it("should call nextPlayer if deal is not done ", function() { rejectSmallDeal(req, res); expect(req.game.nextPlayer.calledOnce).to.be.true; @@ -397,12 +376,6 @@ describe("rejectBigDeal", function() { req.game.nextPlayer = sinon.spy(); res.end = sinon.spy(); }); - it("should return nothing if deal is already done ", function() { - req.game.activeCard.dealDone = true; - const output = rejectBigDeal(req, res); - expect(output).to.be.undefined; - expect(req.game.nextPlayer.calledOnce).to.be.false; - }); it("should call nextPlayer if deal is not done ", function() { rejectBigDeal(req, res); expect(req.game.nextPlayer.calledOnce).to.be.true;