This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let Contract | |
let contractInstance | |
let escrowPlayer1 | |
let escrowPlayer2 | |
let balancePlayer1 | |
let balancePlayer2 | |
start() | |
function start() { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en" dir="ltr"> | |
<head> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" href="game.css"> | |
<title>Dice ethereum game</title> | |
</head> | |
<body> | |
<div class="main-container-game"> | |
<h1>Ethereum Dice</h1> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
body { | |
font-family: sans-serif; | |
} | |
.hidden { | |
display: none; | |
} | |
.main-container-game { | |
margin: auto; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
start() | |
// To setup the main variables when starting the game | |
function start() {} | |
// To set the required listeners that will make the game functionality | |
function setListeners() {} | |
// To show updated notifications as they happen to the user | |
function status(message) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function placeBet(bet) { | |
if(parseInt(bet) > parseInt(getGameBalance())) return status("You can't bet more than your current balance") | |
if(parseInt(bet) > parseInt(getOtherGameBalance())) return status("You can't bet more than your opponent's current balance") | |
if(parseInt(bet) > parseInt(getGameEscrow())) return status("You can't bet more than your escrow") | |
const nonce = Math.floor(Math.random() * 1e16) | |
const hash = generateHash(nonce, activeDice, bet, getGameBalance(), sequence) | |
const signedMessage = await signMessage(hash) | |
let data = { | |
signedMessage: signedMessage, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let game = { | |
contractAddress: '', | |
addressPlayer1: '', | |
addressPlayer2: '', | |
socketPlayer1: '', | |
socketPlayer2: '', | |
escrowPlayer1: '', | |
escrowPlayer2: '', | |
balancePlayer1: '', | |
balancePlayer2: '', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function status(message) { | |
document.querySelector('.status').innerHTML = message | |
setTimeout(() => { | |
document.querySelector('.status').innerHTML = '' | |
}, 3e3) | |
} | |
function getGameBalance() { | |
if(isThisPlayer1) return game.balancePlayer1 | |
else return game.balancePlayer2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let activeDice = 0 | |
let game = {} | |
let isThisPlayer1 = false | |
let isThisPlayer2 = false | |
let sequence = 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function generateHash(nonce, call, bet, balance, sequence) { | |
const hash = '0x' + ethereumjs.ABI.soliditySHA3( | |
['uint256', 'uint256', 'uint256', 'uint256', 'uint256'], | |
[String(nonce), String(call), String(bet), String(balance), String(sequence)] | |
).toString('hex') | |
return hash | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function signMessage(hash) { | |
return new Promise((resolve, reject) => { | |
web3.personal.sign(hash, web3.eth.defaultAccount, (err, result) => { | |
if(err) return reject(err) | |
resolve(result) | |
}) | |
}) | |
} |