Skip to content
This repository has been archived by the owner on May 14, 2021. It is now read-only.

Dev #1

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var createError = require('http-errors')
var express = require('express')
var path = require('path')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
var indexRouter = require('./routes/index')
var usersRouter = require('./routes/users')
const config = require('./config')
const gameEngine = require('./game/gameEngine')
const socketManager = require('./game/socketManager')
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
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(express.urlencoded({ extended: false }))
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')))

app.use('/', indexRouter);
app.use('/users', usersRouter);
Expand Down
7 changes: 5 additions & 2 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ let result = figlet("Burger Kin",(error,result) => {
console.log("written by Oren Zakay and Alon Genosar, Kin.org\n\nconfig:")
jclrz(config)
console.log('\n')

start()
})

require('../game/socketManager')(server)
const start = async () => {
await require('../core/blockchain').init()
require('../game/socketManager')(server)
}
12 changes: 9 additions & 3 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ module.exports = { master_public_address: process.env.hasOwnProperty('master_pub
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_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) : 5,
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,
pre_result_timeout:200,
result_timout:2000,
server_version:0.1,
turn_timeout: process.env.hasOwnProperty('turn_timeout') ? parseInt(process.env.turn_timeout) : 10 * 1000,
totalChannels: process.env.hasOwnProperty('totalChannels') ? parseInt(process.env.totalChannels) : 10
}
96 changes: 60 additions & 36 deletions core/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,38 @@
*
* Desc
*
* @author Oren Zakay.
* @author Oren Zakay, Alon Genosar
*/

const KinClient = require('@kinecosystem/kin-sdk-node').KinClient;
const Environment = require('@kinecosystem/kin-sdk-node').Environment;
const { KinClient, Transaction, Environment, Channels } = require('@kinecosystem/kin-sdk-node')
const config = require('../config')

const client = new KinClient(Environment.Testnet);

let masterAccount

async function getMasterAccount() {
if (!masterAccount) {
masterAccount = await client.createKinAccount({
seed: config.master_seed,
appId: config.appId
});
}
return masterAccount
async function init() {
console.log("Creating channels")
let keepers = await Channels.createChannels({
environment: Environment.Testnet,
baseSeed: config.master_seed,
salt: "Dubon Haya Po",
channelsCount: config.totalChannels,
startingBalance: 0
})

let keys = keepers.map( item => {
return item.seed
})
console.log("Creating master account")
masterAccount = await client.createKinAccount({
seed: config.master_seed,
appId: config.appId,
channelSecretKeys:keys
});
console.log("Channels created succsesfully")
}



async function isAccountExisting(wallet_address) {
try {
const result = await client.isAccountExisting(wallet_address)
Expand All @@ -34,26 +45,30 @@ async function isAccountExisting(wallet_address) {
return false
}
}
//console.log(Environment.Testnet.passphrase)

async function validateTransaction(transactionId) {
const data = await client.getTransactionData(transactionId)

return data
//check for correct amount
&& data.hasOwnProperty('amount')
&& data.amount === config.game_fee
//check for transaction date
&& data.hasOwnProperty('timeStamp')
&& new Date() - Date(data.timestamp) < 10
try {
const data = await client.getTransactionData(transactionId)
return data
//check for correct amount
&& data.hasOwnProperty('amount')
&& data.amount === config.game_fee
//check for transaction date
&& data.hasOwnProperty('timeStamp')
&& new Date() - Date(data.timestamp) < config.transaction_experation_in_sec // 10 sec
}
catch {
return false
}
}

async function createAccount(wallet_address) {
console.log("buildCreateAccount -> " + wallet_address)
// Sign the account creation transaction
const masterAccount = await getMasterAccount()
let createAccountBuilder = await masterAccount.buildCreateAccount({
address: wallet_address,
startingBalance: 100,
fee: 100,
fee: 0,
memoText: "C" + createID(9)
})

Expand All @@ -63,18 +78,25 @@ async function createAccount(wallet_address) {
console.log("createAccount transaction id -> ", id)
}

async function whitelistTransaction(walletPayload) {
try {
const whitelistTx = await masterAccount.whitelistTransaction(walletPayload)
return whitelistTx
} catch(error) {
throw error
}
}
async function payToUser(wallet_address, amount) {
//console.log("payToUser -> " + wallet_address + " with amount = " + amount)
const masterAccount = await getMasterAccount()
const transactionBuilder = await masterAccount.buildSendKin({
address: wallet_address,
amount: amount,
fee: 100,
memoText: createID(10)
masterAccount.channelsPool.acquireChannel( async channel => {
const transactionBuilder = await masterAccount.buildSendKin({
address: wallet_address,
amount: amount,
fee: 0,
memoText: createID(10),
channel: channel
})
return await masterAccount.submitTransaction(transactionBuilder)
})

await masterAccount.submitTransaction(transactionBuilder)
console.log("payToUser submitTransaction -> ", transactionBuilder)
}

function createID(length) {
Expand All @@ -91,5 +113,7 @@ module.exports = {
validateTransaction,
isAccountExisting,
createAccount,
payToUser
payToUser,
whitelistTransaction,
init
}
8 changes: 4 additions & 4 deletions game/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ 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', TURN: 'turn', RESULT: 'result', 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
this.stateValue = null
this.boardSize = [config.board_width,config.board_height]
this.board = this.shuffle(JSON.parse(JSON.stringify(symbols)))
}
Expand All @@ -46,12 +47,11 @@ class Game {
let cpy = JSON.parse(JSON.stringify(this))
cpy.board = cpy.board.map( item => { return item == null ? null : Math.min(item,0) } )
this.flipped.forEach( index => { cpy.board[index] = this.board[index] });
delete cpy.flipped
return cpy
}

cardsLeft() {
return this.board.filter( item => { return item != null }).length
return this.board.filter( item => { return item != null })
}

}
Expand Down
4 changes: 3 additions & 1 deletion game/Player.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

class Player {

constructor({id,name}) {
constructor({id,name,facebookId,avatar}) {
this.id = id
this.name = name
this.score = 0
this.facebookId = facebookId
this.avatar = avatar
}
}
module.exports = Player
Loading