From 7aff6833bdf89245d87794ba32506b096332ee65 Mon Sep 17 00:00:00 2001 From: OrenZak Date: Sun, 11 Aug 2019 11:08:18 +0300 Subject: [PATCH 01/30] add route - is player in game --- core/blockchain.js | 3 ++- game/gameEngine.js | 2 +- routes/users.js | 48 +++++++++++++++++++++++++++++++--------------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/core/blockchain.js b/core/blockchain.js index c58e05f..39bcf9d 100644 --- a/core/blockchain.js +++ b/core/blockchain.js @@ -44,8 +44,9 @@ async function validateTransaction(transactionId) { && data.amount === config.game_fee //check for transaction date && data.hasOwnProperty('timeStamp') - && new Date() - Date(data.timestamp) < 10 + && new Date() - Date(data.timestamp) < 10 // 10 sec } + async function createAccount(wallet_address) { console.log("buildCreateAccount -> " + wallet_address) // Sign the account creation transaction diff --git a/game/gameEngine.js b/game/gameEngine.js index e71d657..8632b3c 100644 --- a/game/gameEngine.js +++ b/game/gameEngine.js @@ -34,7 +34,7 @@ function gameEmit( {gameId, action, sender = "server", callerId, value } ) { module.exports = { eventEmitter: new events.EventEmitter() ,actions: actions - ,isInGame(callerId) { + ,isPlayerInGame(callerId) { return (gamesByUserId[callerId]) } ,reset: () => { diff --git a/routes/users.js b/routes/users.js index f23d048..c471bc7 100644 --- a/routes/users.js +++ b/routes/users.js @@ -11,21 +11,39 @@ var express = require('express'); var router = express.Router(); const blockchain = require('../core/blockchain') const config = require('../config') +const gameEngine = require('../game/gameEngine') -router.get('/login', async function(req, res, next) { - const public_key = req.query.public_key - try { - if(!public_key) - throw new Error("missing public_key") - - if(!await blockchain.isAccountExisting(public_key)) - await blockchain.createAccount(public_key) - - res.json({ wallet_address: config.master_public_address}) - } - catch (error) { - next(error) - //res.status(400).json({error:{message:error.message}}); - } +router.get('/login', async function (req, res, next) { + const public_key = req.query.public_key + try { + if (!public_key) + throw new Error("missing public_key") + + if (!await blockchain.isAccountExisting(public_key)) + await blockchain.createAccount(public_key) + + res.json({ + wallet_address: config.master_public_address + }) + } catch (error) { + next(error) + } }); + +router.get('/is-player-in-game', async function (req, res, next) { + const public_key = req.query.public_key + try { + if (!public_key) + throw new Error("missing public_key") + + const is_player_in_game = gameEngine.isPlayerInGame(public_key) + + res.json({ + is_player_in_game: is_player_in_game + }) + } catch (error) { + next(error) + } +}); + module.exports = router; \ No newline at end of file From 0e41efca2c1b7986311a635914228d5106d4116c Mon Sep 17 00:00:00 2001 From: ass Date: Mon, 12 Aug 2019 10:49:37 +0300 Subject: [PATCH 02/30] fixed isPlayerInGame in game engine --- game/gameEngine.js | 2 +- routes/users.js | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/game/gameEngine.js b/game/gameEngine.js index 8632b3c..b16b02f 100644 --- a/game/gameEngine.js +++ b/game/gameEngine.js @@ -35,7 +35,7 @@ module.exports = { eventEmitter: new events.EventEmitter() ,actions: actions ,isPlayerInGame(callerId) { - return (gamesByUserId[callerId]) + return gamesByUserId[callerId] !== undefined } ,reset: () => { games = [] diff --git a/routes/users.js b/routes/users.js index c471bc7..7013d20 100644 --- a/routes/users.js +++ b/routes/users.js @@ -36,13 +36,11 @@ router.get('/is-player-in-game', async function (req, res, next) { if (!public_key) throw new Error("missing public_key") - const is_player_in_game = gameEngine.isPlayerInGame(public_key) - - res.json({ - is_player_in_game: is_player_in_game - }) + const result = gameEngine.isPlayerInGame(public_key) + console.log(result) + res.json({ result: result }) } catch (error) { - next(error) + next(error) } }); From 5ca9a9dbe8ec968cd1e2dde1a90e19c2b2fd17df Mon Sep 17 00:00:00 2001 From: Alon Genosar Date: Tue, 13 Aug 2019 08:53:27 +0300 Subject: [PATCH 03/30] fixed join bug where game allowed more then two players to join --- config.js | 7 ++++--- core/blockchain.js | 2 +- game/gameEngine.js | 27 +++++++++++++-------------- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/config.js b/config.js index 37a426a..b630a88 100644 --- a/config.js +++ b/config.js @@ -12,10 +12,11 @@ module.exports = { master_public_address: process.env.hasOwnProperty('master_pub appId: 'bkin', board_width: process.env.hasOwnProperty('board_width') ? parseInt(process.env.board_width) : 4, board_height: process.env.hasOwnProperty('board_height') ? parseInt(process.env.board_height) : 5, - monitor_tables: process.env.hasOwnProperty('monitor_tables') ? parseInt(process.env.monitor_tables) : false, + monitor_tables: process.env.hasOwnProperty('monitor_tables') ? parseInt(process.env.monitor_tables) : true, monitor_tables_interval: process.env.hasOwnProperty('monitor_tables_interval') ? parseInt(process.env.monitor_tables_interval) : 2000, - game_fee: process.env.hasOwnProperty('game_fee') ? parseFloat(process.env.game_fee) : 10, + game_fee: process.env.hasOwnProperty('game_fee') ? parseFloat(process.env.game_fee) : 1, bad_card_symbol_index: 1, total_bad_card_pairs: 1, - flipped_card_symbol_index: 0 + flipped_card_symbol_index: 0, + transaction_experation_in_sec: 10 }; diff --git a/core/blockchain.js b/core/blockchain.js index 39bcf9d..afea426 100644 --- a/core/blockchain.js +++ b/core/blockchain.js @@ -44,7 +44,7 @@ async function validateTransaction(transactionId) { && data.amount === config.game_fee //check for transaction date && data.hasOwnProperty('timeStamp') - && new Date() - Date(data.timestamp) < 10 // 10 sec + && new Date() - Date(data.timestamp) < config.transaction_experation_in_sec // 10 sec } async function createAccount(wallet_address) { diff --git a/game/gameEngine.js b/game/gameEngine.js index b16b02f..fb55d70 100644 --- a/game/gameEngine.js +++ b/game/gameEngine.js @@ -72,19 +72,19 @@ module.exports = { // Join // case actions.JOIN: - const result = await blockchain.isAccountExisting(callerId) - if(!result) throw new Error("Invalid public id") - - game = game || games.filter( game => game.state == Game.states.PENDING )[0] || new Game() + + if(game) + return game + + if(!await blockchain.isAccountExisting(callerId) ) throw new Error("Invalid public id") + //if( !value ) throw new Error("Missing transaction id" ) + //if( await !blockchain.validateTransaction(value) ) throw new Error("Invalid transaction Id") + + game = games.filter( game => game.state == Game.states.PENDING && Object.keys(game.players).length < 2 )[0] || new Game() + if(games.indexOf(game) < 0 ) games.push(game) - - // If new game created , check player's transaction - if( game.players.length == 0 ) { - if( !value ) throw new Error("Missing transaction id") - if( await !blockchain.validateTransaction(value)) - throw new Error("Invalid transaction Id") - } + game.players[callerId] = new Player( { id:callerId, name:value } ) gamesByUserId[callerId] = game @@ -170,8 +170,7 @@ module.exports = { games.splice(games.indexOf(game),1) blockchain.payToUser(winnerId,config.game_fee) return winnerId - break - + // // Leave // @@ -185,7 +184,7 @@ module.exports = { break default: - throw new Error("Action",action," not supported") + throw new Error("Action",action,"not supported") } } } \ No newline at end of file From 4e9dcbf3f0786da8ce3427c60e57640356dcd8b9 Mon Sep 17 00:00:00 2001 From: Alon Genosar Date: Tue, 13 Aug 2019 12:24:45 +0300 Subject: [PATCH 04/30] fixed bug where game wasn"t abscring board --- game/Game.js | 4 ++-- game/gameEngine.js | 44 ++++++++++++++++++++++++++++++++----------- game/socketManager.js | 15 ++++++++++----- public/socketio.html | 4 +++- 4 files changed, 48 insertions(+), 19 deletions(-) diff --git a/game/Game.js b/game/Game.js index cd5182a..fb8a048 100644 --- a/game/Game.js +++ b/game/Game.js @@ -15,14 +15,14 @@ for(var i = 0; i < config.board_width * config.board_height / 2.0; i++) { symbols.push(i + 1) symbols.push(i + 1) } -const states = Object.freeze({ PENDING: 'pending', PLAYING: 'playing', COMPLETED: 'completed' }) +const states = Object.freeze({ PENDING: 'pending', STARTING:'starting', PLAYING: 'playing', COMPLETED: 'completed' }) class Game { static get states() { return states } constructor() { this.id = newId(5) - this.state = 'pending' + this.state = states.PENDING this.players = {} this.flipped = [] this.turn = null diff --git a/game/gameEngine.js b/game/gameEngine.js index fb55d70..f43f740 100644 --- a/game/gameEngine.js +++ b/game/gameEngine.js @@ -71,38 +71,60 @@ module.exports = { // // Join // - case actions.JOIN: - + case actions.JOIN: if(game) - return game + return game//.userFriendly() + //Validate + if( !value ) throw new Error("Missing transaction id" ) + if( !value.name ) throw new Error("Missing name" ) + //if( !value.transactionId ) throw new Error("Missing transaction id" ) if(!await blockchain.isAccountExisting(callerId) ) throw new Error("Invalid public id") - //if( !value ) throw new Error("Missing transaction id" ) - //if( await !blockchain.validateTransaction(value) ) throw new Error("Invalid transaction Id") + //Validate transaction + try { + console.log(value) + // let result = await !blockchain.validateTransaction(value.transactionId) + } catch ( error ) { + // console.log(error) + } + + //Check for pending games game = games.filter( game => game.state == Game.states.PENDING && Object.keys(game.players).length < 2 )[0] || new Game() + //Push game to games list if not already there if(games.indexOf(game) < 0 ) games.push(game) - - game.players[callerId] = new Player( { id:callerId, name:value } ) + + //Add player to game object + game.players[callerId] = new Player( { id:callerId, name:value.name } ) + + //Index games by player gamesByUserId[callerId] = game + + //Change state to starting if two player's has joind + console.log(Object.keys(game.players).length) + if( Object.keys(game.players).length == 2 ) + game.state = Game.states.STARTING + + + - game = game.userFriendly() - if( game.state == Game.states.PENDING && Object.keys(game.players).length == 2 ) { + //Start game + if( game.state == Game.states.STARTING ) { setTimeout( async function() { let result = await module.exports.doAction({ action:actions.TURN, callerId:callerId } ) gameEmit( { gameId:game.id, action:actions.TURN, value:result, callerId:callerId } ) }, 1000); } - return game + return game.userFriendly() // // Turn // case actions.TURN: if(!game) throw new Error("User not in game") - if(game.state != Game.states.PENDING && game.state != Game.states.PLAYING ) throw new Error("Turn not allowed") + if(game.state != Game.states.STARTING && game.state != Game.states.PLAYING ) throw new Error("Turn not allowed") const playersId = Object.keys(game.players) const i = playersId.indexOf(game.turn) diff --git a/game/socketManager.js b/game/socketManager.js index cf8aa8a..350835d 100644 --- a/game/socketManager.js +++ b/game/socketManager.js @@ -24,7 +24,7 @@ eventEmitter.on("action",( {gameId,action,callerId,value} ) => { // API module.exports = function (server,options,cb) { io = require('socket.io')(server) - io.on('connection', async function (socket,next) { + io.on('connection', async function (socket,next,a) { //console.log("Connecting",socket.handshake.query.token, socket.handshake.query.name) if (socket.handshake.query && socket.handshake.query.token && @@ -32,15 +32,19 @@ module.exports = function (server,options,cb) { socket.handshake.query.name && socket.handshake.query.name != 'undefined') { try { - //console.log(socket.handshake.query.token ,socket.handshake.query.name) - let game = await doAction({ action:actions.JOIN, callerId: socket.handshake.query.token ,value:socket.handshake.query.name,socket:socket }) + let value = { + name: socket.handshake.query.name, + transactionId: socket.handshake.query.transactionId + } + + let game = await doAction({ action:actions.JOIN, callerId:socket.handshake.query.token, value:value, socket:socket }) socket.gameId = game.id socket.join(game.id) socket.token = socket.handshake.query.token io.to(game.id).emit("action",{action:actions.JOIN, callerId:socket.token,value:game}) } catch(error) { - console.log("error",error) + console.log("*** error",error) socket.disconnect() } } else { @@ -50,7 +54,8 @@ module.exports = function (server,options,cb) { // console.log("socket recieved aciton",action,value, socket.isAuthorized) if( socket.token && allowedUserActions.indexOf(action) > -1 ) { try { - let result = await doAction({action:action,callerId:socket['token'], value:value,socket:socket}) + let result = await doAction({action:action,callerId:socket['token'], value:value,socket:socket}) + if(cb) cb(result) if(socket.gameId) diff --git a/public/socketio.html b/public/socketio.html index 9829897..cc841be 100644 --- a/public/socketio.html +++ b/public/socketio.html @@ -1,7 +1,9 @@