2020-01-18 19:35:11 -08:00
|
|
|
import React, {useContext} from 'react';
|
2020-01-09 01:06:38 -08:00
|
|
|
import '../css/App.css';
|
2020-01-13 00:03:58 -08:00
|
|
|
import morseCode from '../data/morse-reverse.json'
|
2020-01-18 19:35:11 -08:00
|
|
|
import ChallengeWord from '../components/ChallengeWord'
|
2020-01-13 00:03:58 -08:00
|
|
|
import ChallengeBufferDisplay from '../components/ChallengeBufferDisplay';
|
2020-01-18 19:35:11 -08:00
|
|
|
import { MorseBufferContext } from '../contexts/morseBufferContext';
|
2020-01-09 01:06:38 -08:00
|
|
|
|
|
|
|
|
|
2020-01-21 03:43:21 -08:00
|
|
|
export default React.memo(function ChallengeMode(props) {
|
2020-01-09 01:06:38 -08:00
|
|
|
|
2020-01-21 02:04:46 -08:00
|
|
|
console.log("ChallengeMode loaded");
|
|
|
|
|
|
|
|
|
|
let wordList = ['morse', 'code', 'hello', 'gene']
|
2020-01-21 03:43:21 -08:00
|
|
|
// let word = wordList.shift()
|
|
|
|
|
|
|
|
|
|
// let word = props.word
|
|
|
|
|
let getNextWord = props.getNextWord
|
|
|
|
|
let word = getNextWord()
|
2020-01-13 00:03:58 -08:00
|
|
|
let challengeLetters = word.split('')
|
2020-01-19 10:26:55 -08:00
|
|
|
|
2020-01-21 03:43:21 -08:00
|
|
|
const {morseCharBuffer, setMorseCharBuffer} = useContext(MorseBufferContext)
|
2020-01-21 02:04:46 -08:00
|
|
|
let morseArray = morseCharBuffer.split('_').filter(l => l !== '')
|
2020-01-13 00:03:58 -08:00
|
|
|
|
2020-01-21 02:04:46 -08: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-21 03:43:21 -08:00
|
|
|
let offset = 0
|
2020-01-21 02:04:46 -08:00
|
|
|
|
2020-01-21 03:43:21 -08:00
|
|
|
// Iterate through the morse character buffer and compare with each letter of challenge word
|
|
|
|
|
morseArray.forEach((item, index) => {
|
2020-01-21 02:04:46 -08:00
|
|
|
|
2020-01-21 03:43:21 -08:00
|
|
|
// if (morseCharBuffer.slice(-1) === '_') { // If end of morse character
|
|
|
|
|
let morseLetter = morseCode[morseArray[index]]
|
|
|
|
|
let challengeLetter = challengeLetters[index-offset]
|
2020-01-21 02:04:46 -08:00
|
|
|
|
2020-01-21 03:43:21 -08:00
|
|
|
if (morseLetter === challengeLetter) {
|
|
|
|
|
correctCharIndexes.push(index-offset)
|
|
|
|
|
incorrectCharIndex = null
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
incorrectCharIndex = index-offset
|
|
|
|
|
incorrectMorseIndexes.push(index)
|
|
|
|
|
offset = incorrectMorseIndexes.length
|
|
|
|
|
}
|
2020-01-21 02:04:46 -08:00
|
|
|
// }
|
2020-01-13 00:03:58 -08:00
|
|
|
})
|
2020-01-21 03:43:21 -08:00
|
|
|
console.log('morseArray', morseArray);
|
|
|
|
|
|
|
|
|
|
// Next word once all correct
|
|
|
|
|
if (correctCharIndexes.length === challengeLetters.length) {
|
|
|
|
|
word = wordList.shift()
|
|
|
|
|
|
|
|
|
|
setMorseCharBuffer('')
|
|
|
|
|
}
|
2020-01-13 00:03:58 -08:00
|
|
|
|
|
|
|
|
|
2020-01-09 01:06:38 -08:00
|
|
|
return (
|
|
|
|
|
<>
|
2020-01-21 02:04:46 -08:00
|
|
|
<ChallengeWord word={word} correctCharIndexes={correctCharIndexes} incorrectCharIndex={incorrectCharIndex} />
|
|
|
|
|
<ChallengeBufferDisplay morseArray={morseArray} incorrectMorseIndexes={incorrectMorseIndexes} />
|
|
|
|
|
{/* <button onClick={() => console.log(morseCharBuffer)}>morseCharBuffer</button> */}
|
2020-01-09 01:06:38 -08:00
|
|
|
</>
|
|
|
|
|
);
|
2020-01-21 02:04:46 -08:00
|
|
|
});
|