question advance

This commit is contained in:
Antti Pilto 2016-08-03 00:18:46 +03:00
parent 7f3d4781ea
commit 46b38b843f
4 changed files with 105 additions and 48 deletions

View File

@ -1,50 +1,61 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import { kanaDictionary } from '../../data/kanaDictionary'; import { kanaDictionary } from '../../data/kanaDictionary';
import { arrayContains, shuffle } from '../../data/helperFuncs'; import { removeFromArray, arrayContains, shuffle } from '../../data/helperFuncs';
import './Question.scss'; import './Question.scss';
class Question extends Component { class Question extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = { this.state = {
previousQuestion: {}, previousQuestion: [],
currentQuestion: '' previousAnswer: '',
currentQuestion: [],
answerOptions: []
} }
this.setNewQuestion = this.setNewQuestion.bind(this);
} }
// getQuestionIndex(question) { getRandomKanas(amount, include, exclude) {
// return this.state.selectedGroups.indexOf(question); // console.log(this.askableKanaKeys);
// } let randomizedKanas = this.askableKanaKeys.slice();
// console.log(randomizedKanas);
// removeSelect(groupName) { if(exclude && exclude.length > 0) randomizedKanas = removeFromArray(exclude, randomizedKanas);
// if(this.getQuestionIndex(groupName)<0) return; if(include && include.length > 0) {
// let newSelectedGroups = this.state.selectedGroups.slice(); randomizedKanas = removeFromArray(include, randomizedKanas);
// newSelectedGroups.splice(this.getIndex(groupName), 1); // console.log(randomizedKanas);
// this.setState({selectedGroups: newSelectedGroups}); shuffle(randomizedKanas);
// } randomizedKanas = randomizedKanas.slice(0,amount-1);
randomizedKanas.push(include);
getRandomKanas(amount) { shuffle(randomizedKanas);
let randomizedKanas = []; }
if(this.state.previousQuestion.length > 0) else {
removeFromAskable(this.state.previousQuestion); shuffle(randomizedKanas);
shuffle(this.askableCharacterKeys); randomizedKanas = randomizedKanas.slice(0,amount);
for(let i = 0; i < amount; i++) {
randomizedKanas.push(this.askableCharacterKeys[i]);
} }
return randomizedKanas; return randomizedKanas;
// console.log(this.askableCharacters); //return this.askableKanaKeys[Math.floor(Math.random() * this.askableKanaKeys.length)];
//return this.askableCharacterKeys[Math.floor(Math.random() * this.askableCharacterKeys.length)];
} }
removeFromAskable(question) { setPreviousQuestion() {
console.log('here'); // console.log("settings previous");
//console.log(this.getQuestionIndex(question)); this.previousQuestion = this.currentQuestion;
this.setState({previousQuestion: this.previousQuestion});
} }
setNewQuestion() { setNewQuestion() {
let question = this.getRandomKanas(1); if(this.props.stage<=2) document.activeElement.blur(); // reset answer button's :active
this.setState({currentQuestion: question[0]}); if(this.currentQuestion) this.setPreviousQuestion();
//console.log(question); this.currentQuestion = this.getRandomKanas(1,false,this.previousQuestion);
this.setState({currentQuestion: this.currentQuestion});
this.setAnswerOptions();
this.setAllowedAnswers();
// console.log(this.currentQuestion);
}
setAnswerOptions() {
this.answerOptions = this.getRandomKanas(3,this.currentQuestion[0],false);
this.setState({answerOptions: this.answerOptions});
// console.log(this.answerOptions);
} }
setAllowedAnswers() { setAllowedAnswers() {
@ -52,51 +63,64 @@ class Question extends Component {
} }
initializeCharacters() { initializeCharacters() {
this.askableCharacters = {}; this.askableKanas = {};
this.askableCharacterKeys = []; this.askableKanaKeys = [];
this.askableRomajis = [];
this.previousQuestion = '';
Object.keys(kanaDictionary).map(function(whichKana) { Object.keys(kanaDictionary).map(function(whichKana) {
// console.log(whichKana); // 'hiragana' or 'katakana' // console.log(whichKana); // 'hiragana' or 'katakana'
Object.keys(kanaDictionary[whichKana]).map(function(groupName) { Object.keys(kanaDictionary[whichKana]).map(function(groupName) {
// console.log(groupName); // 'h_group1', ... // console.log(groupName); // 'h_group1', ...
// do we want to include this group? // do we want to include this group?
if(arrayContains(groupName, this.props.decidedGroups)) { if(arrayContains(groupName, this.props.decidedGroups)) {
// let's merge the group to our askableCharacters // let's merge the group to our askableKanas
this.askableCharacters = Object.assign(this.askableCharacters, kanaDictionary[whichKana][groupName]['characters']); this.askableKanas = Object.assign(this.askableKanas, kanaDictionary[whichKana][groupName]['characters']);
Object.keys(kanaDictionary[whichKana][groupName]['characters']).map(function(key) { Object.keys(kanaDictionary[whichKana][groupName]['characters']).map(function(key) {
// let's add all askable kana keys to array // let's add all askable kana keys to array
this.askableCharacterKeys.push(key); this.askableKanaKeys.push(key);
kanaDictionary[whichKana][groupName]['characters'][key].map(function(romaji) { this.askableRomajis.push(kanaDictionary[whichKana][groupName]['characters'][key][0]);
console.log(romaji); // console.log(kanaDictionary[whichKana][groupName]['characters'][key][0]);
}, this); // kanaDictionary[whichKana][groupName]['characters'][key].map(function(romaji) {
// console.log(romaji);
// }, this);
}, this); }, this);
} }
}, this); }, this);
}, this); }, this);
// console.log(this.askableCharacters); // console.log(this.askableKanas);
} }
componentWillMount() { componentWillMount() {
this.initializeCharacters();
} }
componentDidMount() { componentDidMount() {
this.initializeCharacters();
this.setNewQuestion(); this.setNewQuestion();
this.setAnswerOptions();
this.setAllowedAnswers();
} }
render() { render() {
let btnClass = "btn btn-default answer-button";
if ('ontouchstart' in window)
btnClass += " no-hover"; // disables hover effect on touch screens
return ( return (
<div className="text-center question"> <div className="text-center question">
<div className="big-character">{this.state.currentQuestion}</div> <div className="big-character">{this.state.currentQuestion}</div>
{this.state.answerOptions.map(function(answer, idx) {
return <AnswerButton answer={answer} className={btnClass} key={idx} onClick={this.setNewQuestion} />;
}, this)}
</div> </div>
); );
} }
} }
class AnswerButton extends Component {
render() {
return (
<button className={this.props.className}>{this.props.answer}</button>;
)
}
}
export default Question; export default Question;

View File

@ -2,5 +2,20 @@
.big-character { .big-character {
font-size: 5em; font-size: 5em;
} }
.answer-button {
min-width: 90px;
font-size: 2em;
@media (min-width: 768px) {
margin: 30px 15px 10px;
}
@media (max-width: 768px) {
margin: 30px 5px 10px;
}
}
.no-hover {
/* disables hover effect on touch screens */
background-color: #fff;
border-color: #ccc;
}
margin-top: 70px; margin-top: 70px;
} }

View File

@ -8,7 +8,7 @@ class GameContainer extends Component {
super(props); super(props);
this.startGame = this.startGame.bind(this); this.startGame = this.startGame.bind(this);
this.state = { this.state = {
decidedGroups: ['h_group1', 'h_group2'] decidedGroups: ['h_group1', 'h_group3']
} }
} }

View File

@ -1,7 +1,25 @@
export function arrayContains(needle, arrhaystack) { export function arrayContains(needle, haystack) {
return (arrhaystack.indexOf(needle) > -1); return (haystack.indexOf(needle) > -1) ? true : false;
} }
export function removeFromArray(needle, haystack) {
if(typeof needle === 'object')
needle = needle[0];
if(arrayContains(needle, haystack)) {
haystack.splice(haystack.indexOf(needle), 1);
}
return haystack;
}
// export function getRandomFromArray(arr) {
// return arr[Math.floor(Math.random() * arr.length)];
// }
// export function getIndex(needle, haystack) {
// return haystack.indexOf(needle);
// }
export function shuffle(array) { export function shuffle(array) {
var i = 0 var i = 0
, j = 0 , j = 0