Skip to content

Commit

Permalink
Revert "Fix the way of seding keyboard inputs"
Browse files Browse the repository at this point in the history
This reverts commit 3c759b7.
  • Loading branch information
konantian committed Nov 25, 2020
1 parent 3c759b7 commit 26fbede
Showing 1 changed file with 21 additions and 19 deletions.
40 changes: 21 additions & 19 deletions src/components/Game/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class Game extends React.Component{
isPause : false,
displayData : null,
inMessage : [],
outMessage : [],
holdKey : null
outMessage : []
}

componentDidMount() {
Expand All @@ -50,6 +49,22 @@ class Game extends React.Component{
},1000)
}

//Running a check every 1/100 second(10 millisecond)
//If allData is not null then send the message
//otherwise just wait until next checking
this.sendData = setInterval(() => {
if(this.state.allData && this.state.isConnection){
this.websocket.send(JSON.stringify(this.state.allData));
//record every message send to the server
if(DEBUG){
this.setState(prevState => ({
outMessage : [prevState.allData,...prevState.outMessage],
}))
}
this.setState(({allData : null}));
}
}, 10);

//To update the progress of loading game content
//Since we always need to wait 30 seconds before the game
//content get loaded, we update the progress (100/30) per second
Expand Down Expand Up @@ -149,27 +164,13 @@ class Game extends React.Component{
}

if(this.state.UIlist.includes(dataToSend.action)){
if(this.state.holdKey !== dataToSend.actionType){
this.setState(({holdKey : dataToSend.actionType}));
this.sendMessage(dataToSend);
}
}
})

document.addEventListener('keyup', (event) => {
//Used to prevent arrow keys and space key from scrolling the page
let dataToSend = getKeyInput(event.code);
if(this.state.UIlist.includes(dataToSend.action)){
dataToSend.action = 'noop';
if(this.state.holdKey === dataToSend.actionType){
this.setState({holdKey : null});
}
this.sendMessage(dataToSend);
}
})
}

componentWillUnmount() {
clearInterval(this.sendData);
if(this.setInMessage) clearInterval(this.setInMessage);
}

Expand Down Expand Up @@ -198,8 +199,9 @@ class Game extends React.Component{
frameCount : this.state.frameCount,
frameId : this.state.frameId
}
console.log(allData);
this.websocket.send(JSON.stringify(allData));
this.setState(({
allData : allData
}))
}
}

Expand Down

0 comments on commit 26fbede

Please sign in to comment.