Skip to content

Commit

Permalink
Add keyboard controls for use during game
Browse files Browse the repository at this point in the history
* Ensure auto generated names are always lowercase
  • Loading branch information
justitsi committed Oct 25, 2023
1 parent 77dc66e commit 8c5360d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
2 changes: 1 addition & 1 deletion frontend/src/components/GameComponents/Card/Card.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next';
import ManagerComponent from './../../../assets/cards/ManagerComponent'


function Card(props) {
const Card = (props) => {
const { t } = useTranslation('translations');

const handleOnCLick = () => {
Expand Down
63 changes: 55 additions & 8 deletions frontend/src/components/GameComponents/Hand/Hand.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import styles from './Hand.module.scss'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import Card from '../Card'
import { useTranslation } from 'react-i18next';
import { sortCards } from './../../../modules/GameFunctions';
import { ReactComponent as ArrowIcon } from './../../../assets/icons/ArrowCardOrder.svg';
import { Button, Container } from 'react-bootstrap'
import { Button } from 'react-bootstrap'
import { log } from '../../../modules/util';

function Hand(props) {
const Hand = (props) => {
const { t } = useTranslation('translations');
const [selected, setSelected] = useState(-1);
const [reverseCardOrder, setReverseCardOrder] = useState(false);
Expand All @@ -28,25 +29,30 @@ function Hand(props) {


let cardsToShow = []
if (props.roundStatus === 'in_progress' || props.roundStatus === 'started_selecting_suit' || props.roundStatus === 'suit_selected') {
let activeCards = []
if (props.roundStatus === 'in_progress' ||
props.roundStatus === 'started_selecting_suit' ||
props.roundStatus === 'suit_selected') {

for (let i = 0; i < props.cardCount; i++) {
let cardElement = null;
let cardShouldBeActive = false

if (props.showCards === true) {
const selectHandler = (index) => {
if (index !== selected) setSelected(index)
else setSelected(-1)
console.log(index, selected,)
log("debug", `${index}, ${selected}`)
}

let cardShouldBeActive = false
for (const activeCard of props.validOptions) {
if (activeCard.suit === cardsInHand[i].suit && activeCard.rank === cardsInHand[i].rank)
cardShouldBeActive = true
}

cardElement =
<div className={props.vertical ? styles.verticalListItem : styles.horizontalListItem} key={i}>
<div className={props.vertical ? styles.verticalListItem : styles.horizontalListItem}
key={i}>
<Card
rank={cardsInHand[i].rank}
suit={cardsInHand[i].suit}
Expand All @@ -59,14 +65,55 @@ function Hand(props) {
}
else {
cardElement =
<div className={props.vertical ? styles.verticalListItem : styles.horizontalListItem} key={i}>
<div className={props.vertical ? styles.verticalListItem : styles.horizontalListItem}
key={i}>
<Card />
</div >
}
// store selectable card list for keyboard controls
activeCards.push(cardShouldBeActive && props.roundStatus === 'in_progress')
cardsToShow.push(cardElement)
}
}

// setup event listeners for keyboard controls for game
useEffect(() => {
const handleKeyDown = (event) => {
console.log(event)
// handle selecting cards with keyboard
if (event.code.includes("Digit")) {
const digit = parseInt(event.key)
log("debug", `Handling selecting card ${digit} using keyboard`)
if (digit > 0) {
if (digit < cardsInHand.length + 1) {
// check if card can even be selected
const toSelectIndex = digit - 1;
if (activeCards[toSelectIndex] === true)
setSelected(toSelectIndex);
}
}
}
// handle playing selected card with keyboard
if (event.code.toLowerCase() === "enter") {
log("debug", 'Handling playing selected card from ENTER key');
playSelectedCard();
}
if (event.code.toLowerCase() === "space") {
log("debug", 'Handling playing selected card from SPACE key');
playSelectedCard();
}
}

// handle adding event listener
document.addEventListener('keydown', handleKeyDown);

return () => {
// remove event listener after component unmount
document.removeEventListener('keydown', handleKeyDown);
}

}, [])

return (
<div className={styles.handContainer}>
<div className={props.vertical ? styles.verticalList : styles.horizontalList}>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/modules/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ export const getWordCombo = () => {
export const getAdjective = () => {
return ADJ_LIST['adjs'][
(Math.floor(Math.random() * ADJ_LIST['adjs'].length))
];
].toLowerCase();
}

export const getNoun = () => {
return NOUN_LIST['nouns'][
(Math.floor(Math.random() * NOUN_LIST['nouns'].length))
];
].toLowerCase();
}

export const generateRoomName = () => {
Expand Down

0 comments on commit 8c5360d

Please sign in to comment.