Skip to content

Commit

Permalink
add check for non board word
Browse files Browse the repository at this point in the history
  • Loading branch information
armaanshah96 committed Nov 5, 2017
1 parent c910de3 commit 03980d6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 7 additions & 3 deletions game/components/Receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ export class Receiver {
case "sendClue":
console.log('Case sendClue reached');
let legalClue: boolean = RuleEnforcer.isLegalClue(message.clue);
let isWordOnBoard: boolean = RuleEnforcer.isWordOnBoard(message.clue, this.game.board.cards);
let theirTurn: boolean = RuleEnforcer.isPlayerTurn(this.game, this.game.getPlayerById(message.id));
let validNum: boolean = RuleEnforcer.isValidNum(message.clue)
if(legalClue && theirTurn && validNum) {
if(legalClue && !isWordOnBoard && theirTurn && validNum) {
this.game.initializeClue(message.clue);
} else {
if(!legalClue) {
socket.send(JSON.stringify({ action: "invalidClueWord", }));
if(isWordOnBoard){
socket.send(JSON.stringify({ action: "invalidClueWord", reason: "wordOnBoard"}));
}
else if(!legalClue) {
socket.send(JSON.stringify({ action: "invalidClueWord", reason: "notWord"}));
}
else if(!validNum) {
socket.send(JSON.stringify({ action: "invalidClueNum", }));
Expand Down
6 changes: 5 additions & 1 deletion game/components/RuleEnforcer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Team, Turn } from '../constants/Constants';
import { Game } from './Game'

export module RuleEnforcer {
export function isValidName(name) { return name.length > 0; }
export function isValidName(name) { return name.length > 0 ; }

export function isValidNum(clue) {
return clue.num >= 0 && clue.num <= 9;
Expand All @@ -21,6 +21,10 @@ export module RuleEnforcer {
return words.check(clue.word.toLocaleLowerCase());
}

export function isWordOnBoard(clue: Clue, cards: Card[]): boolean {
return cards.filter(card => card.word.toLocaleLowerCase() === clue.word.toLocaleLowerCase()).length > 0;
}

export function isPlayerTurn(game: Game, player: SPlayer) {
return game.currTeam === player.team && game.turn === player.role;
}
Expand Down

0 comments on commit 03980d6

Please sign in to comment.