-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (157 loc) · 6.06 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//HTML page environement variables
var game = document.getElementById('game');
var boxes = document.querySelectorAll('li');
var turnDisplay = document.getElementById('whos-turn');
var gameMessages = document.getElementById('game-messages');
var newGame = document.getElementById('new-game');
var joinGame = document.getElementById('join-game');
var player;
var gameOver = false;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
//this Dapp requires the use of metamask
alert('please install metamask')
}
const eth = new Eth(web3.currentProvider);
var TicTacToeContract;
var TicTacToe;
//Play functions
var init = async function() {
let response = await fetch('/artifacts/contracts/TicTacToe.sol/TicTacToe.json');
const data = await response.json()
const abi = data.abi
const byteCode = data.bytecode
await ethereum.request({ method: 'eth_requestAccounts' });
TicTacToeContract = eth.contract(abi, byteCode, { from: ethereum.selectedAddress, gas: '3000000' });
ethereum.on('accountsChanged', async function (accounts) {
await ethereum.request({ method: 'eth_requestAccounts' });
TicTacToeContract = eth.contract(abi, byteCode, { from: ethereum.selectedAddress, gas: '3000000' });
});
//the user can first create or join a game
newGame.addEventListener('click',newGameHandler,false);
joinGame.addEventListener('click',joinGameHandler, false);
//events listeners for user to click on the board
for(var i = 0; i < 9; i++) {
boxes[i].addEventListener('click', clickHandler, false);
}
renderInterval = setInterval(render, 1000);
render();
}
var checkWin = function(){
//checks the contract on the blockchain to verify if there is a winner or not
if (typeof TicTacToe != 'undefined'){
var win;
TicTacToe.status().then(function(res){
win = res[0].words[0];
console.log(win)
var displayResult;
if (win>0){
if (win==3){
displayResult = "Draw ! game is over";
} else if (win == 2){
displayResult = "Player 2 wins ! game is over";
} else if (win ==1) {
displayResult = "Player 1 wins ! game is over";
}
gameOver = true;
document.querySelector('#game-messages').innerHTML = displayResult;
for (var i = 0; i < 9; i++){
boxes[i].removeEventListener('click', clickHandler);
}
}
});
if (win>0){
return true;
} else {
return false;
}
} else {
return false;
}
}
var render = function(){
//renders the board byt fetching the state of the board from the blockchain
if (typeof TicTacToe != 'undefined'){
TicTacToe.showBoard().then(function(res){
for (var i = 0; i < 9; i++){
var state = res[0][i].words[0];
if (state>0){
if (state==1){
boxes[i].className = 'x';
boxes[i].innerHTML = 'x';
} else{
boxes[i].className = 'o';
boxes[i].innerHTML = 'o';
}
}
}
});
checkWin();
if (!gameOver){
TicTacToe.turn().then(function(res){
if (res[0].words[0] == player){
document.querySelector('#game-messages').innerHTML = "Your turn !";
} else {
document.querySelector('#game-messages').innerHTML = "Not your turn !";
}
});
}
}
}
var newGameHandler = function(){
//creates a new contract based on the user input of their opponent's address
if (typeof TicTacToe != 'undefined'){
console.log("There seems to be an existing game going on already");
} else{
var opponentAddress = document.getElementById('opponentAdress').value
console.log(opponentAddress)
TicTacToeContract.new(opponentAddress).then(function(txHash){
var contractAddress;
var waitForTransaction = setInterval(function(){
eth.getTransactionReceipt(txHash, function(err, receipt){
if (receipt) {
clearInterval(waitForTransaction);
TicTacToe = TicTacToeContract.at(receipt.contractAddress);
//display the contract address to share with the opponent
document.querySelector('#newGameAddress').innerHTML =
"Share the contract address with your opponnent: " + String(receipt.contractAddress) + "<br><br>";
document.querySelector('#player').innerHTML ="Player1"
player = 1;
}
})
}, 300);
})
}
}
var joinGameHandler = function(){
//idem for joining a game
var contractAddress = document.getElementById('contract-ID-tojoin').value.trim();
TicTacToe = TicTacToeContract.at(contractAddress);
document.querySelector('#player').innerHTML ="Player2"
player = 2;
}
var clickHandler = function() {
//called when the user clicks a cell on the board
if (typeof TicTacToe != 'undefined'){
if (checkWin()){
return;
}
var target = this.getAttribute('data-pos');
TicTacToe.validMove(target).then(function(res){
if (res[0]) {
TicTacToe.turn().then(function(res) {
if (res[0].words[0] == player) {
TicTacToe.move(target).catch(function(err){
console.log('something went wrong ' +String(err));
}).then(function(res){
this.removeEventListener('click', clickHandler);
render();
});
}
});
}
});
}
}
init();