const dem = 0 const com = 1 const dem_name= "Democrat" const com_name= "Communist" const stability=0 const US_tiananmen=0 const USSR_tiananmen=0 const toolbar = document.getElementById('toolbar') const vpMarker = document.getElementById('vp') const mapContainer = document.querySelector('.map') let handActive=0 let doInfl=0 let doSupportCheck=0 let availableOps=0 let demTSTPos=0 let comTSTPos=0 let demTSTPrev=0 let comTSTPrev=0 let turn=1 let actionRound=1 let supportCheckTargetName='' let supportCheckTargetCountry=0 let vp = 0 let germanyRevolution=0 let polandRevolution=0 let czechRevolution=0 let hungaryRevolution=0 let romaniaRevolution=0 let buglariaRevolution=0 let germanyHeld=0 let polandHeld=0 let czechHeld=0 let hungaryHeld=0 let romaniaHeld=0 let bulgariaHeld=0 let presidentialVisit=0 const countryNames= [ null, "East Germany", "Poland", "Czechoslovakia", "Hungary", "Romania", "Bulgaria" ] const overlay = document.getElementById('overlay'); const spaceNameElement = document.getElementById('space-name'); const spaceCharacteristicsElement = document.getElementById('space-characteristics'); // Event listener to track mouse movement over the map document.querySelector('.map').addEventListener('mousemove', function(event) { const x = event.offsetX; // X-coordinate of mouse relative to container const y = event.offsetY; // Y-coordinate of mouse relative to container spaceCharacteristicsElement.innerText = `X: ${x}, Y: ${y}`; }) // Create map areas dynamically based on coordinates function createMap() { mapContainer.querySelectorAll('.space-area').forEach(spaceArea => { spaceArea.remove() }) spaces.forEach((space) => { if (space && space.box) { const { x, y, h, w } = space.box; const spaceArea = document.createElement('div'); spaceArea.classList.add('space-area', space.country) spaceArea.id=space.name; spaceArea.style.left = x + 'px'; spaceArea.style.top = y + 'px'; spaceArea.style.width = w + 'px'; spaceArea.style.height = h + 'px'; spaceArea.addEventListener('click', changeInfl) spaceArea.addEventListener('click', finishSupportCheck) spaceArea.querySelectorAll('img').forEach(img => { img.remove(); }); spaceArea.querySelectorAll('p').forEach(p => { p.remove(); }); if (space.demInfl > 0 && space.demInfl - space.comInfl >= space.stability) { const img = document.createElement('img') img.classList.add('demInfl', space.country) img.id=space.name img.src = `images/US_${space.demInfl}.gif` spaceArea.appendChild(img) } else if (space.demInfl > 0) { const img = document.createElement('img') img.classList.add('demInfl', space.country) img.id=space.name img.src = `images/USd_blank.gif` spaceArea.appendChild(img) const demInflValue = document.createElement('p') demInflValue.className='demInflValue' demInflValue.id=space.name demInflValue.innerText=space.demInfl spaceArea.appendChild(demInflValue) } spaceArea.querySelectorAll('img.comInfl').forEach(img => { img.remove(); }) if (space.comInfl === 0) { spaceArea.querySelectorAll('img.comInfl').forEach(img => { img.remove(); }) } else if (space.comInfl > 0 && space.comInfl - space.demInfl >= space.stability) { const img = document.createElement('img') img.className='comInfl' img.id=space.name img.src = `images/SV_${space.comInfl}.gif` spaceArea.appendChild(img) } else if (space.comInfl > 0) { const img = document.createElement('img') img.className='comInfl' img.id=space.name img.src = `images/SVd_blank.gif` spaceArea.appendChild(img) const comInflValue = document.createElement('p') comInflValue.className='comInflValue' comInflValue.id=space.name comInflValue.innerText=space.comInfl spaceArea.appendChild(comInflValue) } mapContainer.appendChild(spaceArea); } }); } /* Create hands */ const handLimit = 8; const comHandLimit = 8 + presidentialVisit; const hand =[]; const discard =[]; /* Step 1 create a draw deck */ const deck = cards .filter(card => card.period === 1) .map(card => card.number); console.log(deck); function drawCards(deck, handLimit) { while (hand.length < handLimit && deck.length >0) { let randomIndex = Math.floor(Math.random()*deck.length); let drawnCard = deck.splice(randomIndex, 1)[0]; hand.push(drawnCard) } console.log('Hand: ', hand) } drawCards(deck, handLimit); function displayHand(hand) { const handDiv = document.getElementById('hand'); hand.forEach(card => { const img = document.createElement('img') img.src = `cards/e${card}.gif` img.className='handCard' img.alt = `Card ${card}` img.addEventListener('click', playCard) handDiv.appendChild(img) }) } displayHand(hand); /*---------- INFLUENCE ON THE BOARD ---------------*/ /*---------- INITIALIZE GAME ---------------*/ function startNewRound() { toolbar.querySelectorAll('button').forEach(button => { button.remove(); }); createMap() const prompt = document.createElement('p'); prompt.textContent = 'Select a card'; prompt.id='prompt'; toolbar.appendChild(prompt); handActive=1; } function playCard(event) { if (handActive===1) { /* Update the toolbar */ toolbar.querySelectorAll('button').forEach(button => { button.remove(); }); clearPrompt() const buttonOps = document.createElement('button'); buttonOps.textContent = 'Add Influence'; buttonOps.id='button'; buttonOps.onclick = influence; toolbar.appendChild(buttonOps); const buttonEvent = document.createElement('button'); buttonEvent.textContent = 'Play for event'; buttonEvent.id='button'; buttonEvent.onclick = event; toolbar.appendChild(buttonEvent); const buttonSC = document.createElement('button'); buttonSC.textContent = 'Play for a Support Check'; buttonSC.id='button'; buttonSC.onclick = supportCheck; toolbar.appendChild(buttonSC); const buttonTian = document.createElement('button'); buttonTian.textContent = 'Play to the Tiananmen Square Track'; buttonTian.id='button'; buttonTian.onclick = tiananmenSquareTrack; toolbar.appendChild(buttonTian); const buttonPoland = document.createElement('button'); buttonPoland.textContent = 'Score Poland'; buttonPoland.id='button'; buttonPoland.onclick = scorePoland; toolbar.appendChild(buttonPoland); /* Update the hand */ const getLink = event.target.src; const match = getLink.match(/\/e(\d+)\.gif$/); const playedCard = parseInt(match[1]); //const playedCard = parseFloat(event.target.src.split('e')[1].split('.gif')[0]); console.log('playedCard: ', playedCard) console.log('match: ', match) console.log('playedCard: ', playedCard) const indexToRemove = hand.indexOf(playedCard); if (indexToRemove !== -1) { hand.splice(indexToRemove, 1); discard.push(playedCard); event.target.remove(); handActive=0; availableOps=getOpsValue(playedCard) console.log('Available ops: ', availableOps) const playedCardName=getCardName(playedCard) console.log('Played card name: ', playedCardName) const eventPanel = document.getElementById('events'); const activeCard = document.createElement('img') activeCard.src = `cards/e${playedCard}.gif` activeCard.className='playedCard' activeCard.alt = `Card ${playedCard}` eventPanel.appendChild(activeCard) } console.log('New Hand:', hand); console.log('Discard:', discard); } } function influence() { clearPrompt() clearButtons() const prompt=document.getElementById('prompt') prompt.innerText='Add Influence' doInfl=1 } function getOpsValue(cardNumber) { const cardOps = cards.find(card => card.number === cardNumber); return cardOps ? cardOps.ops : null; // Return null if the card is not found } function getCardName(cardNumber) { const cardName = cards.find(card => card.number === cardNumber); return cardName ? cardName.name : null; // Return null if the card is not found } function rolld6() { const d6 = Math.floor(Math.random()*6+1) return d6; } function event() {} function supportCheck() { clearPrompt() clearButtons() const prompt=document.getElementById('prompt') prompt.innerText='Select First Target' doSupportCheck=2 } function finishSupportCheck(event) { if (doSupportCheck>0) { doSupportCheck-- const roll = rolld6() console.log('You rolled a: ',roll) const spaceName = event.target.id; console.log('spaceName: ', spaceName) const spaceCountry = parseInt(event.target.classList[1]); // The country of the clicked space, 2nd element in the classList. console.log('spaceCountry: ', spaceCountry) const space = spaces.find(space => space && space.name === spaceName && space.country === spaceCountry); // Find the corresponding space object. const stability=space.stability * 2 const supportCheckValue = availableOps + roll const startingOppInf = space.comInfl const initialChange = supportCheckValue - stability console.log('support check value =',availableOps, ' ops + ', roll,' + from roll = ', supportCheckValue) console.log('stability: ', stability) console.log('change: ', initialChange) console.log('starting opp inf: ', startingOppInf) if (space) { if (supportCheckValue > stability){ if (space.comInfl - initialChange >= 0) { space.comInfl -=initialChange console.log('new Com influence: ', space.comInfl) } else { space.comInfl = 0 console.log('new Com influence: ', space.comInfl) const remainingChange = initialChange-startingOppInf space.demInfl += remainingChange } } } const prompt=document.getElementById('prompt') if (doSupportCheck===1) { prompt.innerText='Select Second Target' } if (doSupportCheck===0) { endRound() } createMap() } } function tiananmenSquareTrack() { clearButtons () const buttonRoll = document.createElement('button'); buttonRoll.textContent = 'Roll a Die'; buttonRoll.id='button'; buttonRoll.onclick = finishTiananmenSquareTrack; toolbar.appendChild(buttonRoll); } function finishTiananmenSquareTrack() { const demTSTMarker=document.getElementById('dem-TST') const roll = rolld6() console.log('You rolled a: ',roll) const attemptTST = availableOps+roll+demTSTPrev console.log('Total TST attempt value: ', attemptTST) if (attemptTST >= demTST[demTSTPos]) { console.log('TST success') demTSTPos++ demTSTMarker.style.left=`38px` } else { console.log('TST fail') demTSTPrev=1 } clearButtons() clearEvents() const endRound = document.createElement('button'); endRound.textContent = 'End Action Round'; endRound.id='button'; endRound.onclick = endActionRound; toolbar.appendChild(endRound); } function clearButtons () { toolbar.querySelectorAll('button').forEach(button => { button.remove(); }); } function clearEvents () { events.querySelectorAll('img').forEach(img => { img.remove() }) } function clearPrompt() { const prompt=document.getElementById('prompt') prompt.innerText='' } function endActionRound() { clearButtons() clearEvents() actionRound++ const actionRoundTracker=document.querySelector('.action-round-tracker') const actionRoundPos= 511+actionRound*42 console.log('Action Round Position: ', actionRoundPos) actionRoundTracker.style.left=actionRoundPos+'px' startNewRound() } function changeInfl(event) { if (availableOps>0 && doInfl===1) { const spaceName = event.target.id; console.log('spaceName: ', spaceName) const spaceCountry = parseInt(event.target.classList[1]); // The country of the clicked space, 2nd element in the classList. console.log('spaceCountry: ', spaceCountry) const space = spaces.find(space => space && space.name === spaceName && space.country === spaceCountry); // Find the corresponding space object. if (space) { space.demInfl++ availableOps-- } if (availableOps===0) { clearPrompt() endRound() doInfl=0 } } createMap() } function endRound() { clearPrompt() clearButtons() const endRound = document.createElement('button'); endRound.textContent = 'End Action Round'; endRound.id='button'; endRound.onclick = endActionRound; toolbar.appendChild(endRound); } function scorePoland() { let demVP = 0 let comVP = 0 const demPresence = spaces.filter(space => space && space.country === 2 && space.demCtrl === 1) const comPresence = spaces.filter(space => space && space.country === 2 && space.comCtrl === 1) if (demPresence.length>0) { demVP+=3 } if (comPresence.length>0) { comVP+=3 } const demBattlegroundCheck = spaces.filter(space => space && space.country === 2 && space.demCtrl === 1 && space.battleground === 1) demBattlegrounds = demBattlegroundCheck.length demVP += demBattlegrounds const comBattlegroundCheck = spaces.filter(space => space && space.country === 2 && space.comCtrl === 1 && space.battleground === 1) comBattlegrounds = demBattlegroundCheck.length comVP += comBattlegrounds const demDomination = demBattlegrounds > comBattlegrounds && demPresence > comPresence const comDomination = comBattlegrounds > demBattlegrounds && comPresence > demPresence if (demDomination) { demVP += 3 } if (comDomination) { comVP += 3 } const demControl = demBattlegrounds === 6 && demPresence > comPresence const comControl = demBattlegrounds === 6 && comPresence > demPresence if (demControl) { demVP += 3 } if (comControl) { comVP += 3 } if (polandRevolution===0) { polandHeld++ comVP += polandHeld*3 const img = document.createElement('img') img.classList.add('powerToken', 2) img.src = `images/SV_1.gif` mapContainer.appendChild(img) } changeVP = demVP - comVP vp += changeVP console.log('Democrats have presence: ', demPresence, ' ', demBattlegrounds, ' battlegrounds') console.log('Democrat VP: ', demVP) console.log('Communist VP: ', comVP) console.log('change VP: ', changeVP) console.log('VP: ', vp) console.log('vp =ve odd: ', isPositiveAndOdd(vp)) const vpSteps = Math.ceil(Math.abs(vp)/2) if (vp === 0) { vpMarker.style.top = '1912px' vpMarker.style.left = '661px' } else if (isPositiveAndEven(vp)) { vpMarker.style.top = '1937px' const vpMarkerPos = 666 + vpSteps*50 vpMarker.style.left = vpMarkerPos + 'px' } else if (isPositiveAndOdd(vp)) { vpMarker.style.top = '1889px' const vpMarkerPos = 642 + vpSteps*50 vpMarker.style.left = vpMarkerPos + 'px' } else if (isNegativeAndEven(vp)) { vpMarker.style.top = '1889px' const vpMarkerPos = 656 - vpSteps*50 vpMarker.style.left = vpMarkerPos + 'px' } else if (isNegativeAndOdd(vp)) { vpMarker.style.top = '1937px' const vpMarkerPos = 679 - vpSteps*50 vpMarker.style.left = vpMarkerPos + 'px' } createMap() endRound() } function isPositiveAndEven(number) { return number > 0 && number % 2 === 0; } function isPositiveAndOdd(number) { return number > 0 && number % 2 !== 0; } function isNegativeAndEven(number) { return number < 0 && number % 2 === 0; } function isNegativeAndOdd(number) { return number < 0 && number % 2 !== 0; } startNewRound()