diff --git a/src/components/App/App.jsx b/src/components/App/App.jsx index 35955b7..a3437f5 100644 --- a/src/components/App/App.jsx +++ b/src/components/App/App.jsx @@ -3,6 +3,7 @@ import AuthService from '../../utils/AuthService' import './App.scss'; import Navbar from '../Navbar/Navbar'; import GameContainer from '../GameContainer/GameContainer'; +import { removeHash } from '../../data/helperFuncs'; const options = {}; const auth = new AuthService(__AUTH0_CLIENT_ID__, __AUTH0_DOMAIN__, options); @@ -13,11 +14,9 @@ class App extends Component { this.state = { isAuthenticated: this.isAuthenticated(), gameState: 'game' - /*profile: auth.getProfile()*/ } auth.on('profile_updated', (newProfile) => { - // do i need a check for valid profile? - this.setState({isAuthenticated: this.isAuthenticated(), /*profile: newProfile*/}); + this.setState({isAuthenticated: this.isAuthenticated()}); }) this.logout = this.logout.bind(this); this.getNickName = this.getNickName.bind(this); @@ -26,11 +25,12 @@ class App extends Component { } isAuthenticated() { - return auth.getProfile() && auth.getProfile().hasOwnProperty('user_id'); + removeHash(); + return auth.loggedIn() && auth.getProfile() && auth.getProfile().hasOwnProperty('user_id'); } getNickName() { - return auth.getProfile() && auth.getProfile().hasOwnProperty('nickname')?auth.getProfile().nickname:''; + return auth.loggedIn() && auth.getProfile() && auth.getProfile().hasOwnProperty('nickname')?auth.getProfile().nickname:''; } startGame() { @@ -48,8 +48,7 @@ class App extends Component { render() { let loginButton = !this.state.isAuthenticated ? -

Log in to save your progress!

: ''; - +

Log in to save your progress!

: ''; return (
-
{loginButton}
+
{loginButton}
) } diff --git a/src/components/App/App.scss b/src/components/App/App.scss index d957c78..7f48e9a 100644 --- a/src/components/App/App.scss +++ b/src/components/App/App.scss @@ -12,7 +12,7 @@ body { background-color: #f5f5f5; padding-bottom: 20px; border-bottom: 1px #dadada solid; - min-height: 690px; + min-height: 500px; /* 690 */ } .container { margin: 0 auto; diff --git a/src/components/ChooseCharacters/ChooseCharacters.jsx b/src/components/ChooseCharacters/ChooseCharacters.jsx index 8afcbeb..5d9ffe1 100644 --- a/src/components/ChooseCharacters/ChooseCharacters.jsx +++ b/src/components/ChooseCharacters/ChooseCharacters.jsx @@ -95,7 +95,7 @@ class ChooseCharacters extends Component {
-

Welcome to Kana Quiz{this.props.nickName !== '' && typeof this.props.nickName !== 'undefined'?', '+this.props.nickName:''}!

+

Welcome to Kana Quiz{this.props.isAuthenticated && this.props.nickName !== '' && typeof this.props.nickName !== 'undefined'?', '+this.props.nickName:''}!

Please choose the groups of characters that you'd like to be studying.

diff --git a/src/components/Game/Game.jsx b/src/components/Game/Game.jsx index a81ab34..21b6dbc 100644 --- a/src/components/Game/Game.jsx +++ b/src/components/Game/Game.jsx @@ -8,17 +8,23 @@ class Game extends Component { constructor(props) { super(props); this.state = { - stage:1, + stage:0, + isLocked: false, showScreen: '' } this.showQuestion = this.showQuestion.bind(this); this.stageUp = this.stageUp.bind(this); + this.lockStage = this.lockStage.bind(this); } stageUp() { this.setState({stage: this.state.stage+1, showScreen: 'stage'}); } + lockStage(stage) { + this.setState({stage: stage, showScreen: 'question', isLocked: true}); + } + showQuestion() { this.setState({showScreen: 'question'}) } @@ -30,8 +36,8 @@ class Game extends Component { render() { return (
- { this.state.showScreen==='stage' ? : '' } - { this.state.showScreen==='question' ? : '' } + { this.state.showScreen==='stage' ? : '' } + { this.state.showScreen==='question' ? : '' }
); } diff --git a/src/components/Game/Question.jsx b/src/components/Game/Question.jsx index 6eeccbd..5f4a9aa 100644 --- a/src/components/Game/Question.jsx +++ b/src/components/Game/Question.jsx @@ -1,7 +1,7 @@ import React, { Component } from 'react'; import { kanaDictionary } from '../../data/kanaDictionary'; import { quizSettings } from '../../data/quizSettings'; -import { findRomajisAtKanaKey, removeFromArray, arrayContains, shuffle } from '../../data/helperFuncs'; +import { findRomajisAtKanaKey, removeFromArray, arrayContains, shuffle, cartesianProduct } from '../../data/helperFuncs'; import './Question.scss'; class Question extends Component { @@ -10,12 +10,15 @@ class Question extends Component { this.state = { previousQuestion: [], previousAnswer: '', + currentAnswer: '', currentQuestion: [], answerOptions: [], stageProgress: 0 } this.setNewQuestion = this.setNewQuestion.bind(this); this.handleAnswer = this.handleAnswer.bind(this); + this.handleAnswerChange = this.handleAnswerChange.bind(this); + this.handleSubmit = this.handleSubmit.bind(this); } getRandomKanas(amount, include, exclude) { @@ -40,7 +43,10 @@ class Question extends Component { } setNewQuestion() { - this.currentQuestion = this.getRandomKanas(1, false, this.previousQuestion); + if(this.props.stage!=4) + this.currentQuestion = this.getRandomKanas(1, false, this.previousQuestion); + else + this.currentQuestion = this.getRandomKanas(3, false, this.previousQuestion); this.setState({currentQuestion: this.currentQuestion}); this.setAnswerOptions(); this.setAllowedAnswers(); @@ -54,9 +60,24 @@ class Question extends Component { } setAllowedAnswers() { - if(this.props.stage==1) this.allowedAnswers = findRomajisAtKanaKey(this.currentQuestion); - if(this.props.stage==2) this.allowedAnswers = this.currentQuestion; - // console.log("allowed answers: "+this.allowedAnswers); + // console.log(this.currentQuestion); + this.allowedAnswers = []; + if(this.props.stage==1 || this.props.stage==3) + this.allowedAnswers = findRomajisAtKanaKey(this.currentQuestion, kanaDictionary); + else if(this.props.stage==2) + this.allowedAnswers = this.currentQuestion; + else if(this.props.stage==4) { + let tempAllowedAnswers = []; + + this.currentQuestion.map(function(key, idx) { + tempAllowedAnswers.push(findRomajisAtKanaKey(key, kanaDictionary)); + }, this); + + cartesianProduct(tempAllowedAnswers).map(function(answer) { + this.allowedAnswers.push(answer.join('')); + }, this); + } + console.log(this.allowedAnswers); } handleAnswer(answer) { @@ -67,10 +88,17 @@ class Question extends Component { this.setState({previousAnswer: this.previousAnswer}); this.previousAllowedAnswers = this.allowedAnswers; if(this.isInAllowedAnswers(this.previousAnswer)) - this.setState({stageProgress: this.state.stageProgress+1}); + this.stageProgress = this.stageProgress+1; else - this.setState({stageProgress: this.state.stageProgress > 0 ? this.state.stageProgress-1 : 0}); - this.setNewQuestion(); + this.stageProgress = this.stageProgress > 0 ? this.stageProgress - 1 : 0; + this.setState({stageProgress: this.stageProgress}); + if(this.stageProgress >= quizSettings.stageLength[this.props.stage] && + !this.props.isLocked) { + let that = this; + setTimeout(function() { that.props.handleStageUp(); }, 300); + } + else + this.setNewQuestion(); } initializeCharacters() { @@ -79,6 +107,7 @@ class Question extends Component { this.askableRomajis = []; this.previousQuestion = ''; this.previousAnswer = ''; + this.stageProgress = 0; Object.keys(kanaDictionary).map(function(whichKana) { // console.log(whichKana); // 'hiragana' or 'katakana' Object.keys(kanaDictionary[whichKana]).map(function(groupName) { @@ -105,7 +134,7 @@ class Question extends Component { getShowableQuestion() { if(this.getAnswerType()=='kana') - return findRomajisAtKanaKey(this.state.currentQuestion)[0]; + return findRomajisAtKanaKey(this.state.currentQuestion, kanaDictionary)[0]; else return this.state.currentQuestion; } @@ -115,7 +144,7 @@ class Question extends Component { if(this.previousQuestion=='') resultString =
Let's go! Which character is this?
else { - let rightAnswer = (this.props.stage==2?findRomajisAtKanaKey(this.previousQuestion)[0]:this.previousQuestion)+' = '+ + let rightAnswer = (this.props.stage==2?findRomajisAtKanaKey(this.previousQuestion, kanaDictionary)[0]:this.previousQuestion.join(''))+' = '+ this.previousAllowedAnswers[0]; if(this.isInAllowedAnswers(this.previousAnswer)) resultString =
{rightAnswer}
@@ -125,9 +154,6 @@ class Question extends Component { return resultString; } - getStageProgress() { - } - isInAllowedAnswers(previousAnswer) { // console.log(previousAnswer); // console.log(this.allowedAnswers); @@ -136,6 +162,18 @@ class Question extends Component { else return false; } + handleAnswerChange(e) { + this.setState({currentAnswer: e.target.value.replace(/\s+/g, '')}); + } + + handleSubmit(e) { + e.preventDefault(); + if(this.state.currentAnswer!='') { + this.handleAnswer(this.state.currentAnswer.toLowerCase()); + this.setState({currentAnswer: ''}); + } + } + componentWillMount() { this.initializeCharacters(); } @@ -155,13 +193,19 @@ class Question extends Component { {this.getPreviousResult()}
{this.getShowableQuestion()}
- {this.state.answerOptions.map(function(answer, idx) { + {this.props.stage<3?this.state.answerOptions.map(function(answer, idx) { return - }, this)} + }, this): +
+
+ +
+
+ }

Stage {this.props.stage}

{stageDescription}

{stageSecondaryDescription?

{stageSecondaryDescription}

:''}
; + if(this.props.stage===4) { stageDescription = 'Write the answer'; stageSecondaryDescription = 'Three at once'; } + let stageContent = (
+

Stage {this.props.stage}

+

{stageDescription}

+ {stageSecondaryDescription?

{stageSecondaryDescription}

:''} +
); + + if(this.props.stage===5) { + stageContent = (
+

Congratulations!

+

You have passed all 4 stages.

+

Would you like to keep playing or go back to menu?

+

+

+
) + } this.setState({stageContent: stageContent}); } componentDidMount() { - this.timeoutID = setTimeout(this.removeStage, 1); // how soon we start fading out (1500) + if(this.props.stage<=4) + this.timeoutID = setTimeout(this.removeStage, 1500); // how soon we start fading out (1500) this.showStage(); } @@ -40,11 +55,9 @@ class ShowStage extends Component { render() { return ( -
- - {this.state.stageContent} - -
+ + {this.state.stageContent} + ); } } diff --git a/src/components/Game/ShowStage.scss b/src/components/Game/ShowStage.scss index e1770e1..e9224b3 100644 --- a/src/components/Game/ShowStage.scss +++ b/src/components/Game/ShowStage.scss @@ -1,9 +1,21 @@ +.keep-playing { + margin: 20px 0 20px; +} +.back-to-menu { + margin: 0 0 20px; +} .show-stage { - margin-top: 200px; + margin-top: 130px; h3 { margin-top: 30px; } } +.show-end { + margin-top: 80px; + h3, h4 { + margin-top: 20px; + } +} .stage-enter { opacity: 0.01; diff --git a/src/components/GameContainer/GameContainer.jsx b/src/components/GameContainer/GameContainer.jsx index 7b4c298..284ee73 100644 --- a/src/components/GameContainer/GameContainer.jsx +++ b/src/components/GameContainer/GameContainer.jsx @@ -21,13 +21,15 @@ class GameContainer extends Component { render() { return (
- { this.props.gameState==='chooseCharacters' ? - : '' } - { this.props.gameState==='game' ? - : '' }
) diff --git a/src/components/Navbar/Navbar.jsx b/src/components/Navbar/Navbar.jsx index 68e19b8..bb31570 100644 --- a/src/components/Navbar/Navbar.jsx +++ b/src/components/Navbar/Navbar.jsx @@ -10,11 +10,11 @@ class Navbar extends Component { leftLink = break; case 'game': - leftLink = + leftLink = } let profileButton = this.props.isAuthenticated ? - Log out : - Log in; + Log out : + Log in; return (