diff options
-rw-r--r-- | src/quiz-bunny/index.html | 38 | ||||
-rw-r--r-- | src/quiz-bunny/prompts.js | 55 | ||||
-rw-r--r-- | src/quiz-bunny/server.js | 70 | ||||
-rw-r--r-- | src/quiz-bunny/static/main.js | 3 | ||||
-rw-r--r-- | src/quiz-bunny/static/styles.css | 1 |
5 files changed, 151 insertions, 16 deletions
diff --git a/src/quiz-bunny/index.html b/src/quiz-bunny/index.html index eadba94..127db65 100644 --- a/src/quiz-bunny/index.html +++ b/src/quiz-bunny/index.html @@ -27,7 +27,8 @@ :disabled="username.length == 0"> </div> <div> - <input type="text" placeholder="game code" v-model="gameCode" style="width: 250px;" v-on:keyup.enter="joinGame"> + <input type="text" placeholder="game code" v-model="gameCode" style="width: 250px;" + v-on:keyup.enter="joinGame"> <input type="button" value="Join game" v-on:click="joinGame" :disabled="username.length == 0 || gameCode.length == 0"> </div> @@ -39,7 +40,7 @@ <h2>{{game.gameCode}}</h2> <h1>Players</h1> <ul v-for="player in game.players"> - <li>{{player}}</li> + <li>{{player.name}}</li> </ul> <div v-if="game.host"> <input type="button" value="Start game" v-on:click="startGame"> @@ -49,15 +50,33 @@ <div v-if="game.state == STATES.TYPING" class="typing"> <h1>Answer:</h1> <h2>{{game.prompt}}</h2> - <input type="text" placeholder="answer" v-model="submitText" :disabled="game.submitted" v-on:keyup.enter="submitAnswer"> + <input type="text" placeholder="answer" v-model="submitText" :disabled="game.submitted" + v-on:keyup.enter="submitAnswer"> <input type="button" value="Submit" v-on:click="submitAnswer" :disabled="submitText.length == 0" v-if="!game.submitted"> + <div class="readyList"> + <h4>Waiting:</h4> + <ul> + <li v-for="player in nonReady(game.players)"> + {{player.name}} + </li> + </ul> + </div> </div> <div v-if="game.state == STATES.VOTING" class="voting"> <h1>Vote:</h1> <h2>{{game.prompt}}</h2> <div v-for="(answer, i) in game.answers"> - <input style="width: 100%" type="button" :value="answer" v-on:click="submitVote(i)" :disabled="game.voted"> + <input style="width: 100%" type="button" :value="answer" v-on:click="submitVote(i)" + :disabled="game.voted"> + </div> + <div class="readyList"> + <h4>Waiting:</h4> + <ul> + <li v-for="player in nonReady(game.players)"> + {{player.name}} + </li> + </ul> </div> </div> <div v-if="game.state == STATES.WAITING" class="waiting"> @@ -67,11 +86,20 @@ <span>{{answer.text}}</span><span>{{answer.voteCount}} votes</span> </div> <input type="button" value="Ready" v-on:click="ready" :disabled="game.ready"> + <div class="readyList"> + <h4>Waiting:</h4> + <ul> + <li v-for="player in nonReady(game.players)"> + {{player.name}} + </li> + </ul> + </div> </div> <div v-if="game.state == STATES.OVER"> <h1>Scores:</h1> <ol> - <li v-for="(score, i) in game.scores"><span>{{score.name}}</span><span>{{score.score}}</span></li> + <li v-for="(score, i) in game.scores"><span>{{score.name}}</span><span>{{score.score}}</span> + </li> </ol> <input type="button" value="Leave" v-on:click="leave"> </div> diff --git a/src/quiz-bunny/prompts.js b/src/quiz-bunny/prompts.js new file mode 100644 index 0000000..c6326cd --- /dev/null +++ b/src/quiz-bunny/prompts.js @@ -0,0 +1,55 @@ +var prompts = [ + "@ reminds you most of what animal?", + "If @ was a movie genre, what would they be?", + "If @ was a breakfast cereal, what would they be?", + "What would a documentary about @ be titled?", + "If @ and @ formed a band, what would it be called?", + "Describe @'s dream house", + "What will it say on @'s tombstone", + "If @ sued @, what would be the reason", + "What would be the name of @'s podcast", + "@ starts a store, selling what?", + "What is @ hiding in their browser history?", + "What was the last video @ watched?", + "What does @ do to get rid of stress?", + "What is something @ is obssessed with?", + "What three words best describe @?", + "What is the most useful thing @ owns?", + "What popular thing annoys @?", + "If @ had intro music, what would it be?", + "If @ taught a class, what would it be in?", + "If @ had to spend the weekend at @'s house, what would they do?", + "If @ got @ a gift, what would it be?", + "What do @ and @ have in common?", + "What would @ wear to a fashion show?", + "What is @'s go to curse word?", + "What is @'s catchphrase?", + "What does @ say to @ most frequently?", + "What is on @'s bucket list", + "What will @ never do?", + "What is @'s favorite TV show?", + "If @ was the host of a reality TV show, what would it be about?", + "What is the last song @ listened to?", + "What is @'s go to restaurant owner?", + "What would @ cook for a dinner party?", + "What is the coolest thing @ has done?", + "What is at the top of @'s grocery list?", + "If @ and @ were in a movie together, what would the title be?", + "@ and @ are cast in a movie remake, what movie would it be?", + "What game has @ played the most", + "If @ wrote the next ten commandments, what would the first one be?", + "What did @ last search for online?", + "If @ had children, what will their parenting catchphrase be?", + "In a movie about @, who would be the lead?", + "What would happen to @ during the apocalypse?", + "What is @'s ideal vacation?", + "What movie quote best describes @", + "If @ changed their profession to an area they haven't shown interest in, what would it be?", + "What smell is @'s favorite?", + "What conspiracy theory is @ most likely to believe in?", + "Which national park would @ like to visit most?", + "If @ had a different first name, what would it be?", +] +module.exports = { + prompts +}
\ No newline at end of file diff --git a/src/quiz-bunny/server.js b/src/quiz-bunny/server.js index 12a0090..3a36baf 100644 --- a/src/quiz-bunny/server.js +++ b/src/quiz-bunny/server.js @@ -1,5 +1,6 @@ const uuidv4 = require('uuid/v4'); const words = require('./words').words; +const prompts = require('./prompts').prompts; function setUpRoutes(server, models, jwtFunctions, database) { // simple send files @@ -15,9 +16,6 @@ function setUpRoutes(server, models, jwtFunctions, database) { OVER: 2, WAITING: 3, } - setInterval(function(){ - console.log(games) - }, 3000) // generates a game code function generateGameCode() { @@ -54,7 +52,10 @@ function setUpRoutes(server, models, jwtFunctions, database) { return game.players.find(player => player.cookie == cookie) } function getPlayerNames(players) { - return players.map(player => player.name) + // return players.map(player => player.name) + return players.map(player => { + return {name: player.name, ready: player.ready} + }) } // Turn the game into a public game object (no cookies, etc.) function getPublicGame(cookie) { @@ -65,6 +66,7 @@ function setUpRoutes(server, models, jwtFunctions, database) { let isHost = cookie == game.host let username = game.players.find(player => player.cookie == cookie).name let players = getPlayerNames(game.players) + // console.log(players) var newGame = { host: isHost, players: players, @@ -76,12 +78,28 @@ function setUpRoutes(server, models, jwtFunctions, database) { if (game.state == STATES.TYPING) { newGame.submitted = game.answers.some(answer => answer.cookie == cookie) newGame.prompt = game.prompts[game.round] + newGame.players.forEach(player => { + var playerWithCookie = game.players.find(p => { + return p.name == player.name + }) + if(game.answers.some(answer => answer.cookie == playerWithCookie.cookie)){ + player.ready = true + } else { + player.ready = false + } + }) } else if (game.state == STATES.VOTING) { newGame.answers = game.answers.map(answer => answer.text) newGame.prompt = game.prompts[game.round] var game = findGameByCookie(cookie) var player = findPlayerByCookie(game, cookie) newGame.voted = player.voted + newGame.players.forEach(player => { + var playerWithCookie = game.players.find(p => { + return p.name == player.name + }) + player.ready = playerWithCookie.voted + }) } else if (game.state == STATES.WAITING) { newGame.answers = game.answers.map(answer => { return { text: answer.text, voteCount: answer.votes.length } @@ -102,17 +120,46 @@ function setUpRoutes(server, models, jwtFunctions, database) { return newGame } } + function shuffle(a) { + var j, x, i; + for (i = a.length - 1; i > 0; i--) { + j = Math.floor(Math.random() * (i + 1)); + x = a[i]; + a[i] = a[j]; + a[j] = x; + } + return a; + } + function getRandomPlayer(stack, playersList){ + if(stack.length == 0){ + for(var i = 0; i < playersList.length; i++){ + stack.push(playersList[i]) + } + shuffle(stack) + } + return stack.shift() + } // gets a game prompt - function getPrompts(players) { - // temporary hard coded 2 rounds - return [`What would ${players[0]} eat for breakfast?`, `What would ${players[1]} be famous for?`] + function getPrompts(playerNames, size) { + var gamePrompts = [] + var stack = [] + for(var i = 0; i < size; i++){ + var randomIndex = Math.floor(Math.random() * prompts.length) + var prompt = prompts[randomIndex] + while(prompt.includes("@")){ + prompt = prompt.replace("@", getRandomPlayer(stack, playerNames)) + } + gamePrompts.push(prompt) + } + return gamePrompts } // marks the game as started function startGame(cookie) { let game = games.find(el => el.host == cookie) - if (game) { + if (game && game.players.length >= 2) { game.gameStarted = true - game.prompts = getPrompts(getPlayerNames(game.players)) + var playerNames = game.players.map(player => player.name) + game.prompts = getPrompts(playerNames, 8) game.round = 0; game.players.forEach(player => { player.score = 0 @@ -174,11 +221,12 @@ function setUpRoutes(server, models, jwtFunctions, database) { function voteFor(cookie, answerIndex) { let game = findGameByCookie(cookie) let playerWhoVoted = findPlayerByCookie(game, cookie) - if (playerWhoVoted.voted) { + let answer = game.answers[answerIndex] + // If already voted, or voted for self, ignore + if (playerWhoVoted.voted || answer.cookie == cookie) { return false } playerWhoVoted.voted = true - let answer = game.answers[answerIndex] answer.votes.push(playerWhoVoted) game.voteCount++ if (game.voteCount == game.players.length) { diff --git a/src/quiz-bunny/static/main.js b/src/quiz-bunny/static/main.js index cb4740e..a7309bb 100644 --- a/src/quiz-bunny/static/main.js +++ b/src/quiz-bunny/static/main.js @@ -15,6 +15,9 @@ window.onload = function () { interval: undefined }, methods: { + nonReady: function (players) { + return players.filter(player => !player.ready) + }, startStatusLoop: function(){ // event loop that runs while waiting for host to start var loadStatus = function (vue_object) { diff --git a/src/quiz-bunny/static/styles.css b/src/quiz-bunny/static/styles.css index 9734c87..e128102 100644 --- a/src/quiz-bunny/static/styles.css +++ b/src/quiz-bunny/static/styles.css @@ -17,6 +17,7 @@ input[type=button]{ cursor: pointer; background-color: coral; color: white; + white-space: normal; } input[type=button]:disabled { color: #eeeeee; |