-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
381 lines (338 loc) · 11.2 KB
/
app.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var helmet = require('helmet')
var axios = require('axios')
require('dotenv').config()
const PORT = process.env.PORT || 5001
var Web3 = require('web3')
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var app = express();
app.use(helmet());
const server = app.listen(PORT, () => {
console.log("Listening on PORT: " + PORT);
});
const io = require('socket.io')(server);
if (process.env.RAILWAY_ENVIRONMENT_NAME === 'production') {
console.log("Running in production mode")
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
} else {
app.use(express.static(path.join(__dirname, 'public')));
}
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use('/', indexRouter);
app.use('/users', usersRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
//--------------------------------------------
const contractAbi = require('./contractData')
const web3 = new Web3("https://data-seed-prebsc-2-s2.bnbchain.org:8545")
web3.eth.accounts.wallet.add(process.env.PRIVATE_KEY);
const contractAddress = "0x4EfE0866A22cF8062efAEF973E50B534B14133D7"
let contract = new web3.eth.Contract(contractAbi, contractAddress)
const address = '0xE3C455Da5824bb6E41686c824A7dDCc815fe38B0'
const state = {
ready: 'ready', // before first player has joined
setup: 'setup', // after first player has joined and timer is started
play: 'play', // timer has ended
end: 'end' // game has ended
}
let networkId
let gameNumber
let gasIncrement = 0
const games = []
setupContract()
async function setupContract() {
networkId = await web3.eth.net.getId()
gameNumber = await contract.methods.getNumberOfGames().call()
console.log(`gameNumber=${gameNumber}`)
}
function orderOfNumbers() {
const arr = [];
while (arr.length < 90) {
const r = Math.floor(Math.random() * 90) + 1;
if (arr.indexOf(r) === -1) arr.push(r);
}
return arr
}
async function getEthPrice() {
try {
const { data } = await axios.get('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD')
const ethPrice = parseFloat( 10 * (1 / data.USD)).toFixed(4) // ethPrice = 10 usd
return ethPrice
} catch (error) {
console.log(error)
}
}
async function createNewGame() {
try {
const tx = contract.methods.newGame((ethPrice * 10**18).toString(10))
let gas = await tx.estimateGas({from: address});
// gas = gas + gasIncrement
console.log(`gas=${gas}`)
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI();
const nonce = await web3.eth.getTransactionCount(address);
const txData = {
from: address,
to: contract.options.address,
data: data,
gas,
gasPrice,
nonce
};
const receipt = await web3.eth.sendTransaction(txData);
const gameId = parseInt(receipt.logs[0].data)
const transactionHash = receipt.transactionHash
return {gameId, transactionHash}
} catch (error) {
console.log('new game transaction errored')
throw error
}
}
async function createNewGame_old() {
games.push({
state: state.ready,
})
let ethPrice
let gameId
try {
const { data } = await axios.get('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD')
ethPrice = parseFloat( 10 * (1 / data.USD)).toFixed(4)// ethPrice = 10 usd
console.log(`ethPrice=${ethPrice}`)
} catch (error) {
console.log(error)
}
try {
const tx = contract.methods.newGame((ethPrice * 10**18).toString(10))
let gas = await tx.estimateGas({from: address});
// gas = gas + gasIncrement
console.log(`gas=${gas}`)
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI();
const nonce = await web3.eth.getTransactionCount(address);
const txData = {
from: address,
to: contract.options.address,
data: data,
gas,
gasPrice,
nonce
};
const receipt = await web3.eth.sendTransaction(txData);
gameId = parseInt(receipt.logs[0].data)
console.log(`gameId=${parseInt(receipt.logs[0].data)}`)
console.log(`Transaction hash: ${receipt.transactionHash}`);
// games.push({
// gameId: gameId,
// escrow: ethPrice,
// state: state.ready,
// timer: 90000,
// contractAddress: process.env.CONTRACT_ADDRESS,
// contractAbi: contractAbi,
// players: []
// })
games[games.length -1 ] = {
gameId: gameId,
escrow: ethPrice,
state: state.ready,
timer: 90000,
contractAddress: process.env.CONTRACT_ADDRESS,
contractAbi: contractAbi,
players: []
}
// gasIncrement++
} catch (error) {
console.log('!!!!!!')
console.log(error)
// throw error
}
}
let transactionPromise
io.on('connection', (socket) => {
socket.on('request-game-data', async (data) => {
console.log('Housie: NEW GAME REQUEST')
// socket.emit('game-data', "your game data#####")
if(!(games[games.length - 1]) || (games[games.length -1].state === state.play) || (games[games.length -1].state === state.end) ){
try {
transactionPromise = createNewGame_old()
await transactionPromise
// const ethPrice = await getEthPrice();
if(games[games.length -1]){
if(games[games.length -1].gameId){
console.log(`emiting game data - ${data.socketId}`)
socket.emit('game-data', games[games.length - 1])
}
}
} catch (error) {
console.log('!!!!!!')
console.log(error)
}
} else {
await transactionPromise
socket.emit('game-data', games[games.length - 1])
}
})
socket.on('game-joined', (data) => {
const gameIndex = data.gameId - gameNumber - 1 // index of the games array
console.log(`data.gameId=${data.gameId}`)
console.log(`gameIndex${gameIndex}`)
// console.log(games[gameIndex])
socket.join(gameIndex)
if(games[gameIndex].players.length === 0){
games[gameIndex].players.push({
socketId: data.socketId,
playerAddress: data.playerAddress,
ticket: data.ticket
})
transactionPromise = null
games[gameIndex].state = state.setup
try {
// io.to(gameIndex).emit('first-player-joined', {socketId: data.socketId, gameState: games[gameIndex].state})
io.emit('first-player-joined', {firstPlayerSocketId: data.socketId, gameState: games[gameIndex].state})
} catch (error) {
console.log(error)
}
const t = setInterval( () => {
games[gameIndex].timer -= 1000
if(games[gameIndex].timer <= 0){
clearInterval(t)
games[gameIndex].state = state.play
io.to(gameIndex).emit('begin-game', {gameState: games[gameIndex].state})
const nums = orderOfNumbers()
let i = 0
const tt = setInterval( () => {
io.to(gameIndex).emit('new-number', nums[i])
console.log(`number send= ${nums[i]}`)
i++;
if( i >= 90 || (games[gameIndex].state == state.end)) {
console.log(`stopping sending numbers= ${i}`)
clearInterval(tt)
}
}, 1000)
}
},1000)
} else {
games[gameIndex].players.push({
socketId: data.socketId,
playerAddress: data.playerAddress,
ticket: data.ticket
})
}
console.log('ticket confirmation line 198')
io.to(gameIndex).emit('ticket-confirmation', {address: data.playerAddress})
})
socket.on('game-won', async (data) => {
const gameIndex = data.gameId - gameNumber - 1 // index of the games array
if(games[gameIndex].state === state.end) return
games[gameIndex].state = state.end
console.log(`Game Won, ${data.playerAddress}`)
const id = data.gameId
const playerAddress = data.playerAddress
const amt = games[gameIndex].escrow * games[gameIndex].players.length
io.to(gameIndex).emit('game-over', {socketId: data.socketId})
// try {
// const tx = await web3.eth.sendTransaction({from: address, to:playerAddress, value: web3.utils.toWei(amt, 'ether')})
// console.log(tx)
// } catch (error) {
// console.log(error)
// }
try {
const tx = contract.methods.gameEnd(id, playerAddress)
const gas = await tx.estimateGas({from: address});
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI();
const nonce = await web3.eth.getTransactionCount(address);
const txData = {
from: address,
to: contract.options.address,
data: data,
gas,
gasPrice,
nonce
};
const receipt = await web3.eth.sendTransaction(txData);
console.log(`Game end Transaction hash: ${receipt.transactionHash}`);
} catch (error) {
console.log(error)
}
})
})
module.exports = app;
/* Rough notes
31485957222547002965592896112208672472574545410244409416298869655371806684874
84801875899405486937029902710782621651880054895970939277174430588210583553177
1 eth = x usd
1/x eth = usd
class Game {
constructor(ticketPrice) {
// create new game
// set game id
// set participant sockets
// set ticket price
// set number of participants
// set 90 array
// game state
this.usdPrice = ticketPrice
this.gameState = state.ready
console.log(this.usdPrice)
this.newGame()
}
newGame = async () => {
try {
const { data } = await axios.get('https://min-api.cryptocompare.com/data/price?fsym=ETH&tsyms=USD')
this.ethPrice = parseFloat( this.usdPrice * (1 / data.USD)).toFixed(4)
console.log(this.ethPrice)
} catch (error) {
console.log(error)
}
try {
// const tx = await contract.methods.newGame(this.ethPrice * 10**18).send({from: address})
// console.log(tx)
const tx = contract.methods.newGame((this.ethPrice * 10**18).toString(10))
const gas = await tx.estimateGas({from: address});
const gasPrice = await web3.eth.getGasPrice();
const data = tx.encodeABI();
const nonce = await web3.eth.getTransactionCount(address);
const txData = {
from: address,
to: contract.options.address,
data: data,
gas,
gasPrice,
nonce
};
const receipt = await web3.eth.sendTransaction(txData);
this.gameId = parseInt(receipt.logs[0].data)
// console.log(parseInt(receipt.logs[0].data))
console.log(`Transaction hash: ${receipt.transactionHash}`);
gameNumber++
} catch (error) {
console.log(error)
}
}
}
*/