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

Commit

Permalink
* refactoring, optimizing,
Browse files Browse the repository at this point in the history
 * RTC messaging bugfix
  • Loading branch information
alexstep committed Oct 27, 2017
1 parent aa1da1e commit 430a18f
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 92 deletions.
Binary file removed DApps/TestDApp.zip
Binary file not shown.
97 changes: 49 additions & 48 deletions DApps/TestDApp/client.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,61 @@
const GameLogic = function(){
var balance = 0
var history = []

var setBalance = function(deposit){
balance = deposit*1
return balance
}

var getBalance = function(){
return balance
}
window.MyDApp = (function(){

var Roll = function(user_bet, user_num, random_hash){
let profit = -user_bet

const random_num = DCLib.Utils.bigInt(random_hash,16).divmod(65536).remainder.value

if (user_num > random_num) {
profit = (user_bet * (65536 - 1310) / user_num) - user_bet
}
if (user_num == random_num) {
profit = user_bet
}

balance += profit*1
const GameLogic = function(){
var balance = 0
var history = []

const roll_item = {
timestamp : new Date().getTime(),
user_bet : user_bet,
profit : profit,
user_num : user_num,
balance : balance,
random_hash : random_hash,
random_num : random_num,
var setBalance = function(deposit){
balance = deposit*1
return balance
}

history.push(roll_item)
var getBalance = function(){
return balance
}

return roll_item
}
var Roll = function(user_bet, user_num, random_hash){
let profit = -user_bet

const random_num = DCLib.Utils.bigInt(random_hash,16).divmod(65536).remainder.value

if (user_num > random_num) {
profit = (user_bet * (65536 - 1310) / user_num) - user_bet
}
if (user_num == random_num) {
profit = user_bet
}

balance += profit*1

const roll_item = {
timestamp : new Date().getTime(),
user_bet : user_bet,
profit : profit,
user_num : user_num,
balance : balance,
random_hash : random_hash,
random_num : random_num,
}

history.push(roll_item)

return roll_item
}

return {
setBalance: setBalance,
getBalance: getBalance,
roll: Roll,
history: history,
return {
setBalance: setBalance,
getBalance: getBalance,
roll: Roll,
history: history,
}
}
}

window.MyDApp = new DCLib.DApp({
code : 'dicegame_v2',
logic : GameLogic,
})


console.log('Use console to see what inside MyDApp object')
console.log('MyDApp', MyDApp)

return new DCLib.DApp({
code : 'dicegame_v2',
logic : GameLogic,
})

})()
58 changes: 58 additions & 0 deletions DApps/TestDApp2/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
window.MyDApp2 = (function(){

return new DCLib.DApp({
code : 'test_v2',

logic : function(){
var balance = 0
var history = []

var setBalance = function(deposit){
balance = deposit*1
return balance
}

var getBalance = function(){
return balance
}

var Roll = function(user_bet, user_num, random_hash){
let profit = -user_bet

const random_num = DCLib.Utils.bigInt(random_hash,16).divmod(65536).remainder.value

if (user_num > random_num) {
profit = (user_bet * (65536 - 1310) / user_num) - user_bet
}
if (user_num == random_num) {
profit = user_bet
}

balance += profit*1

const roll_item = {
user_bet : user_bet,
profit : profit,
user_num : user_num,
balance : balance,
random_hash : random_hash,
random_num : random_num,
}

history.push(roll_item)

return roll_item
}

return {
setBalance: setBalance,
getBalance: getBalance,
roll: Roll,
}
}
})

})()



8 changes: 8 additions & 0 deletions DApps/TestDApp2/dapp.manifest
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name" : "TestDapp2",
"code" : "testdapp_v2",

"run" : {
"client" : "client.js"
}
}
21 changes: 14 additions & 7 deletions src/model/DApps/DApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,9 @@ export default class DApp {
room : new Rtc( Eth.Wallet.get().openkey, this.hash+'_'+connection_id )
}

this.response(params, {id:connection_id})
this.response(params, {id:connection_id}, this.sharedRoom)

console.log('User '+user_id+' connected')

console.log('User '+user_id+' connected to '+this.code)


const signMsg = async (rawMsg=false)=>{
Expand Down Expand Up @@ -173,7 +172,7 @@ export default class DApp {
this.response(data, {
args : args,
returns : returns
})
}, User.room)

return
}
Expand All @@ -182,7 +181,7 @@ export default class DApp {
console.log('User '+data.user_id+' disconnected')
User.room.off('all', listen_all)
delete(this.users[data.user_id])
this.response(data, {disconnected:true})
this.response(data, {disconnected:true}, User.room)
return
}
}
Expand All @@ -194,7 +193,10 @@ export default class DApp {

// Send message and wait response
request(params, callback=false, Room=false){
Room = Room || this.Room || this.sharedRoom
if (!Room) {
console.error('request roo not set!')
return
}

return new Promise((resolve, reject) => {

Expand Down Expand Up @@ -223,9 +225,14 @@ export default class DApp {

// Response to request-message
response(request_data, response, Room=false){
Room = Room || this.Room || this.sharedRoom
if (!Room) {
console.error('request roo not set!')
return
}

request_data.response = response
request_data.type = 'response'

Room.send(request_data)
}

Expand Down
9 changes: 0 additions & 9 deletions src/model/event.js

This file was deleted.

Loading

0 comments on commit 430a18f

Please sign in to comment.