Skip to content
This repository has been archived by the owner on Jan 9, 2019. It is now read-only.

Commit

Permalink
run external gamescripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex authored and Alex committed Oct 9, 2017
1 parent 9d1bb4a commit 641b429
Show file tree
Hide file tree
Showing 13 changed files with 141 additions and 40 deletions.
2 changes: 2 additions & 0 deletions DApps/TestGame/b.client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

console.log('my test dapp client')
7 changes: 7 additions & 0 deletions DApps/TestGame/b.server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


console.log('my test dapp server')

setInterval(function(){
console.log('ok')
}, 2500)
8 changes: 8 additions & 0 deletions DApps/TestGame/dapp.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name" : "TestGame",

"run" : {
"client" : "b.client.js",
"server" : "b.server.js"
}
}
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"orientation": "portrait"
},
"scripts": {
"postinstall": "npm run favicons && npm run build && npm start",
"postinstall": "cd ./node_modules/web3/ && npm i && npm run build && cd ../../ && npm run favicons && npm run build && npm start",
"start": "npm run webserver",
"webserver": "npm run server & npm run serve_web",
"server": "NODE_ENV=server npm run build_server && node ./build/server.electron.js",
Expand All @@ -31,10 +31,12 @@
"build_server": "npm run cp_electron & node scripts/build_server.js",
"cp_electron": "cp ./src/index.electron.js ./public/index.electron.js & cp ./src/index.electron.js ./build/index.electron.js & cp ./src/server.electron.js ./public/server.electron.js & cp ./src/server.electron.js ./build/server.electron.js & cp ./src/config.electron.js ./build/config.electron.js ",
"run_electron": "npm run cp_electron; electron .",

"build_electron": "build -mwl --x64 --ia32",
"build_electron_windows": "build -w --x64",
"build_electron_linux": "build -l --x64",
"build_electron_mac": "build -m --x64",

"favicons": "node scripts/favicons.js",
"lesslint": "lessc --lint src/view/styles/index.less ",
"eslint_fix": "eslint --fix --ext=.js,.tag ./src",
Expand All @@ -45,8 +47,7 @@
"lint-staged": "lint-staged",
"check_build": "serve -s build -p 9000"
},

"__icons_creator":"https://iconverticons.com/online/",
"__icons_creator": "https://iconverticons.com/online/",
"build": {
"appId": "com.daocasino.bankroller",
"files": [
Expand Down Expand Up @@ -135,6 +136,8 @@
"utf-8-validate": "^3.0.3",
"fs-extra": "^3.0.1",
"web3": "github:ethereum/web3.js#1.0",
"devtron": "^1.4.0",
"electron-debug": "^1.4.0",
"ydn.db": "^1.3.0"
},
"devDependencies": {
Expand Down
14 changes: 10 additions & 4 deletions public/index.electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ global.Tray = Tray
const path = require('path')
const url = require('url')

require('electron-debug')({
enabled: true ,
showDevTools: false ,
});


// Check updates
// require('electron-simple-updater').init({
Expand All @@ -43,7 +48,8 @@ if (shouldQuit) {

function createWindow () {
mainWindow = new BrowserWindow({
resizable: false, width: 670, height: 530,
// resizable: false,
width: 670, height: 530,

icon: path.join(__dirname, 'static/icons/icon-128.png'),

Expand Down Expand Up @@ -81,9 +87,9 @@ function createWindow () {
const trayIcon = new Tray(path.join(__dirname, 'static/icons/icon-38.png'))
trayIcon.setToolTip('Dao.Casino')
trayIcon.setContextMenu(Menu.buildFromTemplate([
{ label: 'Show App', click:() => {
mainWindow.show()
} },
// { label: 'Show App', click:() => {
// mainWindow.show()
// } },
{ label: 'Quit', click:() => {
app.isQuiting = true
app.quit()
Expand Down
33 changes: 25 additions & 8 deletions public/server.electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ const fse = require('fs-extra')

const {app} = require('electron')

const Gun = require('gun')
const Gun = require('gun')

console.log('')
console.log('Start electron server with config', _config)
console.log('')


let dapps_path = __dirname+_config.dapps_path
if (typeof app != 'undefined') {
dapps_path = (app.getPath('appData') + _config.dapps_path).split('//').join('/')
}


/*
* HTTP static file server
* + Gun websocket
Expand Down Expand Up @@ -59,6 +66,10 @@ const server = http.createServer(function (request, response) {
filePath = __dirname+'/index.html'
}

if (request.url.indexOf('DApps')>-1) {
filePath = dapps_path + request.url.replace('/DApps/','')
}

let contentType = filetypes[path.extname(filePath)] || false

fs.readFile(filePath, function(error, content) {
Expand Down Expand Up @@ -131,8 +142,7 @@ setTimeout(()=>{



// let dapps_path = (app.getAppPath() + _config.dapps_path).split('//').join('/')
let dapps_path = (app.getPath('appData') + _config.dapps_path).split('//').join('/')


const readManifest = function(path){
try {
Expand Down Expand Up @@ -165,16 +175,15 @@ const uploadGame = function(data){


// Run games
const runGames = function(){
const runGames = function(){
let dappsDirs = false
try {
console.log(dapps_path)
dappsDirs = fse.readdirSync(dapps_path)
} catch(e) {
return
}

console.log(dappsDirs)

dappsDirs.forEach(dir=>{
const full_path = dapps_path+dir+'/'
const dapp_config = readManifest( full_path+'dapp.manifest' )
Expand All @@ -183,11 +192,19 @@ const runGames = function(){
}

console.log('dapp_config', dapp_config)

GunDB.get('DApps').get(dir).put({
config : JSON.stringify(dapp_config),
path : full_path,
})

const module_path = full_path + dapp_config.run.server

if (!dapp_config.run.server) {
return
}

const module_path = full_path + dapp_config.run.server
console.log('module_path', module_path)

require(module_path)
})
}
Expand Down
37 changes: 37 additions & 0 deletions src/app.client.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _config from 'app.config'
import View from 'view/app.view'
import Games from 'games'
import DB from 'DB/DB'

import DiceGameChannel from 'dice_gamechannel'
import FlipGameChannel from 'flip_gamechannel'
Expand All @@ -10,6 +11,9 @@ if (window) {
}

document.addEventListener('DOMContentLoaded',()=>{

runDapps()

if (typeof DiceGameChannel!='undefined') {
window.DiceGameChannel = DiceGameChannel
}
Expand Down Expand Up @@ -43,3 +47,36 @@ document.addEventListener('DOMContentLoaded',()=>{

// Background job's
// require('./app.background.js')

// Load DApps
const runDapps = function(){
let loaded_dapps = []
const injectScript = function(url){
console.log('inject', url)
var script = document.createElement('script')
script.src = url
script.onload = script.onreadystatechange = function() {
console.log('script '+url+' loaded')
}
document.body.appendChild(script)
}

DB.data.get('DApps').map((dapp, key)=>{
let dapp_config = JSON.parse(dapp.config)

let base = '/'
if (location.port*1 !== 9999) {
base = 'http://localhost:9999/'
}

let script_url = base + 'DApps/' + key +'/'+ dapp_config.run.client

if (loaded_dapps.indexOf(script_url)>-1) {
return
}

loaded_dapps.push(script_url)

injectScript(script_url)
})
}
12 changes: 6 additions & 6 deletions src/app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ const networks = {
etherscan_url: 'https://rinkeby.etherscan.io'
},

'mainnet': {
enabled: false,
name: 'Main Ethereum Network',
rpc_url: 'https://infura.io/JCnK5ifEPH9qcQkX0Ahl',
etherscan_url: 'https://etherscan.io'
},
// 'mainnet': {
// enabled: false,
// name: 'Main Ethereum Network',
// rpc_url: 'https://infura.io/JCnK5ifEPH9qcQkX0Ahl',
// etherscan_url: 'https://etherscan.io'
// },
}


Expand Down
14 changes: 10 additions & 4 deletions src/index.electron.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ global.Tray = Tray
const path = require('path')
const url = require('url')

require('electron-debug')({
enabled: true ,
showDevTools: false ,
});


// Check updates
// require('electron-simple-updater').init({
Expand All @@ -43,7 +48,8 @@ if (shouldQuit) {

function createWindow () {
mainWindow = new BrowserWindow({
resizable: false, width: 670, height: 530,
// resizable: false,
width: 670, height: 530,

icon: path.join(__dirname, 'static/icons/icon-128.png'),

Expand Down Expand Up @@ -81,9 +87,9 @@ function createWindow () {
const trayIcon = new Tray(path.join(__dirname, 'static/icons/icon-38.png'))
trayIcon.setToolTip('Dao.Casino')
trayIcon.setContextMenu(Menu.buildFromTemplate([
{ label: 'Show App', click:() => {
mainWindow.show()
} },
// { label: 'Show App', click:() => {
// mainWindow.show()
// } },
{ label: 'Quit', click:() => {
app.isQuiting = true
app.quit()
Expand Down
4 changes: 2 additions & 2 deletions src/model/Eth/RPC.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export default class ethRPC {
}

request(method_name=false, params=[], id=1){
console.log('request ', method_name, params)
return new Promise((resolve, reject) => {
try {
let res = this.callMethod(method_name, params, id, (response)=>{
Expand Down Expand Up @@ -52,14 +51,15 @@ export default class ethRPC {
'params': params
})
}).then( response => {
// console.log(response)
return response.json()
}).then( obj => {
if (obj.error) {
console.info(method_name, params)
console.error(obj)
}
callback( obj )
}).catch( err => {
console.info(method_name, params)
console.error(err)
callback( err )
})
Expand Down
5 changes: 2 additions & 3 deletions src/model/dice_gamechannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const GameLogic = function(deposit){
}

balance += profit

const roll_item = {
timestamp : new Date().getTime(),
user_bet : user_bet,
Expand Down Expand Up @@ -353,9 +353,8 @@ class DiceGameChannel {

// Проверяем разрешил ли игрок списывать бэты контракту
setGameContract(address, callback){
console.log('setGameContract')
this.getAllowance(address, allowance_bets =>{
console.log('allowance_bets',allowance_bets)
// console.log('allowance_bets',allowance_bets)
if (allowance_bets < 1000000) {
this.approveContract(address, 2000000, ()=>{
this.setGameContract(address, callback)
Expand Down
3 changes: 1 addition & 2 deletions src/model/flip_gamechannel.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,8 @@ class DiceGameChannel {

// Проверяем разрешил ли игрок списывать бэты контракту
setGameContract(address, callback){
console.log('setGameContract')
this.getAllowance(address, allowance_bets =>{
console.log('allowance_bets',allowance_bets)
// console.log('allowance_bets',allowance_bets)
if (allowance_bets < 1000000) {
this.approveContract(address, 2000000, ()=>{
this.setGameContract(address, callback)
Expand Down
Loading

0 comments on commit 641b429

Please sign in to comment.