Skip to content

Instantly share code, notes, and snippets.

@merlox
Created October 17, 2018 16:49
Show Gist options
  • Save merlox/2f4f7fbadd5bcc46a0420e08c018ecf1 to your computer and use it in GitHub Desktop.
Save merlox/2f4f7fbadd5bcc46a0420e08c018ecf1 to your computer and use it in GitHub Desktop.
start()
async function start() {
io.on('connection', socket => {
console.log('User connected', socket.id)
socket.on('disconnect', () => {
console.log('User disconnected', socket.id)
})
socket.on('setup-player-1', data => {
console.log('1. Received setup-player-1')
game = {
contractAddress: data.contractAddress,
escrowPlayer1: data.escrowPlayer1,
balancePlayer1: data.balancePlayer1,
addressPlayer1: data.addressPlayer1,
socketPlayer1: data.socketPlayer1
}
})
socket.on('setup-player-2', async data => {
console.log('2. Received setup-player-2')
game.escrowPlayer2 = data.escrowPlayer2
game.balancePlayer2 = data.balancePlayer2
game.addressPlayer2 = data.addressPlayer2
game.socketPlayer2 = data.socketPlayer2
game.sequence = 0
console.log('3. Emitting start-game')
io.emit('start-game')
})
socket.on('setup-game', data => {
console.log('4. Received setup-game')
if(data.address == game.addressPlayer1) {
game.socketPlayer1 = data.socket
} else {
game.socketPlayer2 = data.socket
}
console.log('5. Emmiting initial game data for both players with the updated sockets')
io.emit('initial-game-data', game)
})
socket.on('signed-message-player-1', message => {
if(message.sender != game.addressPlayer1) {
return io.to(game.socketPlayer1).emit('error', 'The received address of the first player is invalid')
}
const isValid = verifyMessage(message.signedMessage, message.nonce, message.call, message.bet, game.balancePlayer1, message.sequence, game.addressPlayer1)
if(!isValid) return io.to(game.socketPlayer1).emit('error', 'The received message is not valid, generate a new one again')
game.signedMessage1 = message.signedMessage
game.betPlayer1 = message.bet
game.callPlayer1 = message.call
game.nonce1 = message.nonce
game.sequence = message.sequence
// If we have both messages already, distribute the updated game object
if(game.signedMessage2) {
games.push(game)
if(game.callPlayer1 == game.callPlayer2) {
game.balancePlayer2 += game.betPlayer2
game.balancePlayer1 -= game.betPlayer2
game.winner = 2
} else {
game.balancePlayer1 += game.betPlayer1
game.balancePlayer2 -= game.betPlayer1
game.winner = 1
}
io.emit('received-both-messages', game)
game = resetGame(game)
}
})
socket.on('signed-message-player-2', message => {
if(message.sender != game.addressPlayer2) {
return io.to(game.socketPlayer2).emit('error', 'The received address of the second player is invalid')
}
const isValid = verifyMessage(message.signedMessage, message.nonce, message.call, message.bet, game.balancePlayer2, message.sequence, game.addressPlayer2)
if(!isValid) return io.to(game.socketPlayer2).emit('error', 'The received message is not valid, generate a new one again')
game.signedMessage2 = message.signedMessage
game.betPlayer2 = message.bet
game.callPlayer2 = message.call
game.nonce2 = message.nonce
game.sequence = message.sequence
// If we have both messages already, distribute the updated game object
if(game.signedMessage1) {
games.push(game)
game.balancePlayer2 = parseInt(game.balancePlayer2)
game.balancePlayer1 = parseInt(game.balancePlayer1)
if(game.callPlayer1 == game.callPlayer2) {
game.balancePlayer2 += parseInt(game.betPlayer2)
game.balancePlayer1 -= parseInt(game.betPlayer2)
game.winner = 2
} else {
game.balancePlayer1 += parseInt(game.betPlayer1)
game.balancePlayer2 -= parseInt(game.betPlayer1)
game.winner = 1
}
io.emit('received-both-messages', game)
game = resetGame(game)
}
})
})
http.listen(port, '0.0.0.0')
console.log(`Listening on localhost:${port}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment