2020-01-30 10:23:56 +01:00
|
|
|
import React, {useContext, useEffect} from 'react';
|
2020-01-09 10:06:38 +01:00
|
|
|
import '../css/App.css';
|
2020-01-13 09:03:58 +01:00
|
|
|
import morseCode from '../data/morse-reverse.json'
|
2020-01-19 04:35:11 +01:00
|
|
|
import ChallengeWord from '../components/ChallengeWord'
|
2020-01-13 09:03:58 +01:00
|
|
|
import ChallengeBufferDisplay from '../components/ChallengeBufferDisplay';
|
2020-01-19 04:35:11 +01:00
|
|
|
import { MorseBufferContext } from '../contexts/morseBufferContext';
|
2020-01-22 07:58:35 +01:00
|
|
|
import { WordFeederContext } from '../contexts/wordFeederContext';
|
|
|
|
|
import { WordListPickerContext } from '../contexts/wordListPickerContext';
|
2020-01-09 10:06:38 +01:00
|
|
|
|
|
|
|
|
|
2020-01-21 12:43:21 +01:00
|
|
|
export default React.memo(function ChallengeMode(props) {
|
2020-01-09 10:06:38 +01:00
|
|
|
|
2020-01-21 11:04:46 +01:00
|
|
|
console.log("ChallengeMode loaded");
|
2020-01-21 12:43:21 +01:00
|
|
|
|
2020-01-22 07:58:35 +01:00
|
|
|
const {word, getNextWord} = useContext(WordFeederContext)
|
|
|
|
|
|
|
|
|
|
let challengeWordClass = ''
|
2020-01-19 19:26:55 +01:00
|
|
|
|
2020-01-21 12:43:21 +01:00
|
|
|
const {morseCharBuffer, setMorseCharBuffer} = useContext(MorseBufferContext)
|
2020-01-21 11:04:46 +01:00
|
|
|
let morseArray = morseCharBuffer.split('_').filter(l => l !== '')
|
2020-01-13 09:03:58 +01:00
|
|
|
|
2020-01-21 11:04:46 +01:00
|
|
|
let correctCharIndexes = [] // Indexes of correct letters in Challenge Word
|
|
|
|
|
let incorrectCharIndex = null
|
|
|
|
|
let incorrectMorseIndexes = [] // Indexes of incorrect morse characters in morse character buffer
|
2020-01-22 07:58:35 +01:00
|
|
|
|
2020-01-21 12:43:21 +01:00
|
|
|
let offset = 0
|
2020-01-30 10:23:56 +01:00
|
|
|
|
|
|
|
|
|
2020-01-22 07:58:35 +01:00
|
|
|
if (!word) {
|
|
|
|
|
console.log('FINISHED ALL WORDS!')
|
|
|
|
|
alert('No more words.')
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-02-01 03:09:54 +01:00
|
|
|
console.log('word:', word);
|
2020-01-30 10:23:56 +01:00
|
|
|
|
2020-01-22 07:58:35 +01:00
|
|
|
let challengeLetters = word.split('')
|
2020-01-30 10:23:56 +01:00
|
|
|
|
2020-01-21 11:04:46 +01:00
|
|
|
|
2020-01-21 12:43:21 +01:00
|
|
|
// Iterate through the morse character buffer and compare with each letter of challenge word
|
|
|
|
|
morseArray.forEach((item, index) => {
|
2020-01-22 07:58:35 +01:00
|
|
|
if (morseCharBuffer.slice(-1) === '_') { // If end of morse character
|
|
|
|
|
|
|
|
|
|
let morseLetter = morseCode[morseArray[index]] || '[?]'
|
2020-02-01 03:09:54 +01:00
|
|
|
let challengeLetter = challengeLetters[index-offset].toLowerCase()
|
2020-01-21 11:04:46 +01:00
|
|
|
|
2020-01-21 12:43:21 +01:00
|
|
|
if (morseLetter === challengeLetter) {
|
|
|
|
|
correctCharIndexes.push(index-offset)
|
|
|
|
|
incorrectCharIndex = null
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
incorrectCharIndex = index-offset
|
|
|
|
|
incorrectMorseIndexes.push(index)
|
2020-01-22 07:58:35 +01:00
|
|
|
if (incorrectMorseIndexes.length > 0) {
|
|
|
|
|
setMorseCharBuffer(prev => {
|
|
|
|
|
let newState = prev.split('_').filter(l => l !== '')
|
2020-01-28 02:39:11 +01:00
|
|
|
newState.splice(incorrectMorseIndexes[0], 1)
|
2020-01-22 07:58:35 +01:00
|
|
|
newState = newState.join('_') + '_'
|
|
|
|
|
|
|
|
|
|
return newState
|
|
|
|
|
})
|
|
|
|
|
incorrectMorseIndexes.splice(1,incorrectMorseIndexes.length)
|
|
|
|
|
}
|
2020-01-21 12:43:21 +01:00
|
|
|
offset = incorrectMorseIndexes.length
|
|
|
|
|
}
|
2020-01-22 07:58:35 +01:00
|
|
|
}
|
2020-01-13 09:03:58 +01:00
|
|
|
})
|
2020-01-22 07:58:35 +01:00
|
|
|
|
|
|
|
|
function timeout(delay) {
|
|
|
|
|
new Promise((resolve) => {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
resolve()
|
|
|
|
|
}, delay)
|
|
|
|
|
})
|
|
|
|
|
}
|
2020-01-21 12:43:21 +01:00
|
|
|
|
|
|
|
|
// Next word once all correct
|
|
|
|
|
if (correctCharIndexes.length === challengeLetters.length) {
|
2020-01-22 07:58:35 +01:00
|
|
|
//
|
|
|
|
|
challengeWordClass = 'correct'
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setMorseCharBuffer('')
|
2020-02-01 03:09:54 +01:00
|
|
|
}, 800)
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
getNextWord()
|
|
|
|
|
|
|
|
|
|
}, 1000)
|
2020-01-21 12:43:21 +01:00
|
|
|
}
|
2020-01-13 09:03:58 +01:00
|
|
|
|
2020-01-09 10:06:38 +01:00
|
|
|
return (
|
|
|
|
|
<>
|
2020-01-22 07:58:35 +01:00
|
|
|
<ChallengeWord className={challengeWordClass} word={word} correctCharIndexes={correctCharIndexes} incorrectCharIndex={incorrectCharIndex} />
|
2020-01-21 11:04:46 +01:00
|
|
|
<ChallengeBufferDisplay morseArray={morseArray} incorrectMorseIndexes={incorrectMorseIndexes} />
|
|
|
|
|
{/* <button onClick={() => console.log(morseCharBuffer)}>morseCharBuffer</button> */}
|
2020-01-09 10:06:38 +01:00
|
|
|
</>
|
2020-01-22 07:58:35 +01:00
|
|
|
)
|
2020-01-21 11:04:46 +01:00
|
|
|
});
|