Gustavrohss / EverythingIsWrong

0 stars 0 forks source link

Show player answers when done #29

Closed majate closed 4 years ago

majate commented 4 years ago

Players should not be possible to see the correct answer before everyone has answered.

Gustavrohss commented 4 years ago

This is more complicated than it first appears.

In its' current state, this is the structure: GameRoundComponent has a showResults argument. GameRoundContainer maps state to showResults through getShowAnswers(state) (a selector). getShowAnswers gets this info from state.gameSession.showAnswers (a part of the Redux tree). The only place in gameSessionReducer where showAnswers is set to anything but false is in the SET_SHOW_ANSWERS on switch(action.type). This is called by the answerQuestion action simply as dispatch(setShowAnswers(true)).

Preferably, this dispatch would be conditional. const show = Object.keys(state.gameSession.players) .map(player => state.gameSession.players[player].status) .every(status => status = STATUS.ready)) dispatch(setShowAnswers(show))

However, even when last to answer, the "show" boolean will evaluate to false. It has something to do with the update in the redux state being delayed.

I've tried a few workarounds. This includes messing around in the modifyPlayer action (and it's corresponding redux reducer state/switch) and setShowAnswers.

It seems the issue is very intertwined with how backend.js works, and I'm not very familiar.

What we'd like to happen is - whenever a player answers a question (or possibly just whenever a player state is updated) we would like to check if all players are STATUS.ready (defined in gameSessionActions as "READY") (this can be done with the above code snippet) and dispatch the result. I'm not entirely sure what callbacks are triggered by updates caused by the backend.js listeners. I'm assigning this to Maja and Daniel as you're more familiar with the architecture of this. Hopefully this comment speeds up the process.

majate commented 4 years ago

My guess is that it should be quite easy to update. Instead of using the showAnswer state that is set to true whenever the player answers a question, we can make the getShowAnswer selector check if all players have answered or not.

majate commented 4 years ago

Made a quick test in another branch and it worked 😃 Need to clean it up before I can merge it though.

Gustavrohss commented 4 years ago

Nice! You could just push it and leave the cleanup for another day. Guessing it's mostly removing the old "show" part of the redux tree, and it's late. :)