diff --git a/src/BoatGame.js b/src/BoatGame.js index d48a65781..323cf1542 100644 --- a/src/BoatGame.js +++ b/src/BoatGame.js @@ -15,21 +15,6 @@ class BoatGame extends Component{ this.cache = new LocalStorageCache(); this.gameService = new GameService(); - this.endGames=this.endGames.bind(this) - this.exitMainPage=this.exitMainPage.bind(this) - this.incrementDifficulty=this.incrementDifficulty.bind(this) - this.totalTime=this.totalTime.bind(this) - this.showStatspage=this.showStatspage.bind(this) - this.parse=this.parse.bind(this) - this.cleanContent=this.cleanContent.bind(this) - this.playAgain=this.playAgain.bind(this) - this.setTotalWords=this.setTotalWords.bind(this) - this.limitWords=this.limitWords.bind(this) - this.setInitContent=this.setInitContent.bind(this) - this.setLimitedContent=this.setLimitedContent.bind(this) - this.assignPlayerPlace=this.assignPlayerPlace.bind(this) - this.recordHighScore = this.recordHighScore.bind(this); - this.state={ isPlayerReady:false, beginCountDown:false, @@ -41,6 +26,7 @@ class BoatGame extends Component{ originalContent:"", content:"", accuracy:0, + timeOutInstance:0, gameStart:false, playerPlace:0, playerDifficulty:1, @@ -50,6 +36,8 @@ class BoatGame extends Component{ username: this.cache.get("username"), headerLinks: ["Stats" ,"Games", "Learn", "Home"], }; + + } async recordHighScore() { @@ -70,10 +58,13 @@ class BoatGame extends Component{ this.setInitContent(data.games.boatrace[randIndex]) } + componentWillUnmount = () => { + clearTimeout(this.state.timeOutInstance) + } exitMainPage = (difficulty) => { - var diffString = difficulty - var diffNum = 1 + let diffString = difficulty + let diffNum = 1 if( diffString === "easy" ){ diffNum = 1 @@ -198,7 +189,7 @@ class BoatGame extends Component{ } totalTime = (time) => { - var minutes=time/60 + let minutes=time/60 this.setState({totalMinutes:minutes}) } @@ -206,20 +197,26 @@ class BoatGame extends Component{ this.setState({playerPlace:position}) } - endGames= (state, time) => { - var minutes=time/60 - var totalChars= state.incorrect.length + state.correct.length - var playerAccuracy= Math.floor((1- state.incorrect.length/totalChars)*100) - var wpm = Math.floor(totalChars/(5*minutes)) - this.setState({showSign:true,inputOff:true}) - setTimeout(() => { - this.setState({ + initiateTimeOut = (playerAccuracy,wpm) => { + return setTimeout(() => { + this.setState({ playerHasLost:true, isPlayerReady:false, gameStart:false, accuracy:playerAccuracy, - wordsPerMinute:wpm, - })},6000) + wordsPerMinute:wpm + }) + }, 6000); + } + + endGames= (state, time) => { + let minutes=time/60 + let totalChars= state.incorrect.length + state.correct.length + let playerAccuracy= Math.floor((1- state.incorrect.length/totalChars)*100) + let wpm = Math.floor(totalChars/(5*minutes)) + this.setState({showSign:true,inputOff:true}) + + this.setState({timeOutInstance:this.initiateTimeOut(playerAccuracy,wpm)}) } showStatspage = () => { diff --git a/src/CocoType.js b/src/CocoType.js deleted file mode 100644 index 6dfe30703..000000000 --- a/src/CocoType.js +++ /dev/null @@ -1,136 +0,0 @@ -import React, { Component } from 'react'; -import ShowSpinner from './components/spinner'; -import './style/CocoType.css' -import styled from 'styled-components'; - - -const GameText = styled.div` - margin-top:4vh; - height: 10vh; - width: 80vw; - border: 5px solid #F5A623; - border-radius: 10px; - background-color: #FFFFFF; - font-size: 3.5rem; - display:flex; - justify-content:center; - -` - -class CocoType extends Component { - constructor(props) { - super(props); - this.typeString=this.typeString.bind(this) - this.updateIndex=this.updateIndex.bind(this) - this.updateString=this.updateString.bind(this) - this.updateLevel=this.updateLevel.bind(this) - this.back=this.back.bind(this) - this.state ={ - headerLinks: ["Stats" ,"Games", "Learn", "Home"], - Time:"00:15", - currLine:"", - currLinenum:0, - fullText:this.props.fullText, - currChar:"", - Level:1, - Streak:0, - indexptr:0, - LongestStreak:0 - } - } - - - onKeyPressed = (e) => { - let currindex= this.state.indexptr - let isRightKey =e.key; - if(isRightKey=="Shift"){ - console.log("Waiting for input to uppercase") - }else if(isRightKey=="Backspace"){ - this.setState({Streak:0}) - this.back() - }else if(isRightKey == this.state.currChar){ - console.log(true) - this.setState({Streak:this.state.Streak + 1}) - this.updateIndex() - }else{ - console.log(false) - if(this.state.LongestStreak <= this.state.Streak){ - this.setState({LongestStreak:this.state.Streak}) - } - this.setState({Streak:0}) - this.updateIndex() - } - }; - - updateIndex(){ - var newIndex=this.state.indexptr; - var newIndex= newIndex +1; - this.setState({indexptr:newIndex}) - this.setState({currChar:this.state.currLine[newIndex]}) - if(newIndex==this.state.currLine.length){ - this.updateString() - } - - } - back(){ - if(this.state.indexptr == 0){ - this.setState({currChar:this.state.currLine[0]}) - } - var newIndex= this.state.indexptr-1 - this.setState({indexptr:newIndex, currChar:this.state.currLine[newIndex]}) - } - - updateString() - { - var nextLinenum = this.state.currLinenum + 1 - this.setState({currLinenum:nextLinenum, indexptr:0}) - this.typeString() - } - - typeString(){ - var lines= this.state.fullText.split("\n") - console.log(this.state.currLinenum) - console.log(lines.length) - if(this.state.currLinenum > lines.length-1){ - this.updateLevel() - } - var nextLine= lines[this.state.currLinenum].trim() - this.setState({currLine:nextLine,currChar:nextLine[this.state.indexptr]}) - }; - - updateLevel(){ - - } - - componentDidMount() { - this.typeString() - document.addEventListener('keydown', this.onKeyPressed); - } - - componentWillUnmount() { - document.removeEventListener('keydown', this.onKeyPressed); - } - - render() { - const { - isLoading, - } = this.props; - - - if(isLoading) { - return - } - - const { - headerLinks, - } = this.state; - return ( -
- -
- ) - } -} - - -export default (CocoType); \ No newline at end of file diff --git a/src/KeyTracking.js b/src/KeyTracking.js index 38667b48c..418f89b63 100644 --- a/src/KeyTracking.js +++ b/src/KeyTracking.js @@ -22,16 +22,17 @@ class KeyTracking extends Component{ isPlayerReady:false, beginCountDown:false, inputOff:false, + gameStart:false, + showMainPage:true, + showSign:false, + timeOutInstance:0, beginningDifficulty:1, totalMinutes:0, wordsPerMinute:0, accuracy:0, - gameStart:false, playerDifficulty:1, - showMainPage:true, username: this.cache.get("username"), headerLinks: ["Stats", "Games", "Learn", "Home"], - showSign:false, jsonArray:[], dataArray:[] }; @@ -39,10 +40,15 @@ class KeyTracking extends Component{ this.endGames = this.endGames.bind(this); this.exitMainPage = this.exitMainPage.bind(this); this.incrementDifficulty = this.incrementDifficulty.bind(this); + this.initiateTimeOut = this.initiateTimeOut.bind(this); this.totalTime = this.totalTime.bind(this); this.playAgain = this.playAgain.bind(this); } + componentWillUnmount(){ + clearTimeout(this.state.timeOutInstance) + } + exitMainPage = (difficulty) => { let diffString = difficulty let diffNum = 1 @@ -73,8 +79,18 @@ class KeyTracking extends Component{ let minutes = time/60 this.setState({totalMinutes:minutes}) } - + initiateTimeOut = (playerAccuracy,wpm) => { + return setTimeout(() => { + this.setState({ + playerHasLost:true, + isPlayerReady:false, + gameStart:false, + accuracy:playerAccuracy, + wordsPerMinute:wpm + }) + }, 6000); + } endGames = async (state, time) => { let minutes = time/60 @@ -89,15 +105,8 @@ class KeyTracking extends Component{ { wpm, accuracy:playerAccuracy, level:this.state.playerDifficulty } ); - setTimeout(() => { - this.setState({ - playerHasLost:true, - isPlayerReady:false, - gameStart:false, - accuracy:playerAccuracy, - wordsPerMinute:wpm - }) - }, 6000); + this.setState({timeOutInstance:this.initiateTimeOut(playerAccuracy,wpm)}) + } playAgain = () => { diff --git a/src/SpaceraceGame.js b/src/SpaceraceGame.js index 3a8514da2..384cb2def 100644 --- a/src/SpaceraceGame.js +++ b/src/SpaceraceGame.js @@ -135,6 +135,7 @@ class SpaceraceGame extends React.Component { startPresses:0, totalCorrect:0, playerAccuracy:0, + timeOutInstance:0, ref1:0, ref2:0, ref3:0, @@ -148,22 +149,23 @@ class SpaceraceGame extends React.Component { difficultySelected:"", } - this.doesWordExist = this.doesWordExist.bind(this) - this.spawnRocket = this.spawnRocket.bind(this) - this.destroyRocket = this.destroyRocket.bind(this) - this.subtractLife = this.subtractLife.bind(this) - this.incrementDifficulty = this.incrementDifficulty.bind(this) - this.tick = this.tick.bind(this) - this.checkDifficultyIncrement = this.checkDifficultyIncrement.bind(this) - this.calculateStats = this.calculateStats.bind(this) - this.playAgain = this.playAgain.bind(this) - this.getParentDiv = this.getParentDiv.bind(this) - this.exitGame = this.exitGame.bind(this) - this.returnMainPage = this.returnMainPage.bind(this) - this.exitMainPage = this.exitMainPage.bind(this) - this.setAvailableWords = this.setAvailableWords.bind(this) - this.createStyler = this.createStyler.bind(this) + this.doesWordExist = this.doesWordExist.bind(this); + this.spawnRocket = this.spawnRocket.bind(this); + this.destroyRocket = this.destroyRocket.bind(this); + this.subtractLife = this.subtractLife.bind(this); + this.incrementDifficulty = this.incrementDifficulty.bind(this); + this.tick = this.tick.bind(this); + this.checkDifficultyIncrement = this.checkDifficultyIncrement.bind(this); + this.calculateStats = this.calculateStats.bind(this); + this.playAgain = this.playAgain.bind(this); + this.getParentDiv = this.getParentDiv.bind(this); + this.exitGame = this.exitGame.bind(this); + this.returnMainPage = this.returnMainPage.bind(this); + this.exitMainPage = this.exitMainPage.bind(this); + this.setAvailableWords = this.setAvailableWords.bind(this); + this.createStyler = this.createStyler.bind(this); this.recordHighScore = this.recordHighScore.bind(this); + this.initiateTimeOut = this.initiateTimeOut.bind(this); this.attachEventListener(); } @@ -174,6 +176,7 @@ class SpaceraceGame extends React.Component { componentWillUnmount() { document.removeEventListener('keydown', this.registerUserKeyPress); + clearTimeout(this.state.timeOutInstance) } exitMainPage = (difficulty) => { @@ -314,6 +317,10 @@ class SpaceraceGame extends React.Component { } } + initiateTimeOut = () => { + return setTimeout(() => {this.setState({playerHasLost:true})},6000) + } + subtractLife = () => { if (this.state.lives === 3){ this.setState({live3: null}) @@ -330,7 +337,7 @@ class SpaceraceGame extends React.Component { clearInterval(this.state.ref3) document.removeEventListener("keydown", this.registerUserKeyPress); this.calculateStats() - setTimeout(() => {this.setState({playerHasLost:true})},6000) + this.setState({timeOutInstance:this.initiateTimeOut()}) } }