Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
plugin
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
link33
plugin
Commits
ba5708f6
Commit
ba5708f6
authored
Jan 04, 2019
by
vipwzw
Committed by
33cn
Jan 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add game demo
parent
742b96b5
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
231 additions
and
0 deletions
+231
-0
game.js
plugin/dapp/js/executor/game.js
+216
-0
js.go
plugin/dapp/js/executor/js.go
+2
-0
jsvm.go
plugin/dapp/js/executor/jsvm.go
+1
-0
runtime.go
plugin/dapp/js/executor/runtime.go
+12
-0
No files found.
plugin/dapp/js/executor/game.js
0 → 100644
View file @
ba5708f6
//简单的猜数字游戏
//游戏规则: 庄家出一个 0 - 10 的数字 hash(随机数 + 9) (一共的赔偿金额) NewGame()
//用户可以猜这个数字,多个用户都可以猜测。 Guess()
//开奖 CloseGame()
function
Init
(
context
)
{
this
.
kvc
=
new
kvcreator
(
"init"
)
this
.
context
=
context
return
this
.
kvc
.
receipt
()
}
function
Exec
(
context
)
{
this
.
kvc
=
new
kvcreator
(
"exec"
)
this
.
context
=
context
}
function
ExecLocal
(
context
,
logs
)
{
this
.
kvc
=
new
kvcreator
(
"local"
)
this
.
context
=
context
this
.
logs
=
logs
}
function
Query
(
context
)
{
this
.
kvc
=
new
kvcreator
(
"query"
)
this
.
context
=
context
}
Exec
.
prototype
.
NewGame
=
function
(
args
)
{
var
game
=
{
__type__
:
"game"
}
game
.
id
=
this
.
context
.
txhash
game
.
index
=
this
.
context
.
height
*
100000
+
this
.
index
game
.
height
=
this
.
context
.
height
game
.
randhash
=
args
.
hash
game
.
bet
=
args
.
bet
game
.
status
=
1
//open
if
(
game
.
bet
<
10
)
{
throw
new
Error
(
"bet too low"
)
}
this
.
kvc
.
add
(
game
.
id
,
game
)
this
.
kvc
.
addlog
(
game
)
return
this
.
kvc
.
receipt
()
}
Exec
.
prototype
.
Guess
=
function
(
args
)
{
var
match
=
{
__type__
:
"match"
}
match
.
gameid
=
args
.
gameid
match
.
bet
=
args
.
bet
match
.
id
=
this
.
context
.
txhash
match
.
index
=
this
.
context
.
height
*
100000
+
this
.
index
match
.
addr
=
this
.
context
.
from
var
game
=
this
.
kvc
.
get
(
match
.
gameid
)
if
(
!
game
)
{
throw
new
Error
(
"game id not found"
)
}
if
(
game
.
status
!=
1
)
{
throw
new
Error
(
"game status not open"
)
}
this
.
kvc
.
add
(
match
.
id
,
match
)
this
.
kvc
.
addlog
(
match
)
return
this
.
kvc
.
receipt
()
}
Exec
.
prototype
.
CloseGame
=
function
(
args
)
{
var
local
=
new
MatchLocalTable
(
this
.
kvc
)
var
game
=
this
.
kvc
.
get
(
args
.
id
)
if
(
!
game
)
{
throw
new
Error
(
"game id not found"
)
}
var
matches
=
local
.
getmath
(
args
.
id
)
if
(
!
matches
)
{
matches
=
[]
}
var
n
=
-
1
for
(
var
i
=
0
;
i
<
10
;
i
++
)
{
if
(
sha256
(
args
.
randstr
+
i
)
==
game
.
randhash
)
{
n
=
i
}
}
if
(
n
==
-
1
)
{
throw
new
Error
(
"err rand str"
)
}
if
(
this
.
context
.
height
-
game
.
height
<
10
)
{
throw
new
Error
(
"close game must wait 10 block"
)
}
for
(
var
i
=
0
;
i
<
matches
.
length
;
i
++
)
{
var
match
=
matches
[
i
]
if
(
match
.
num
==
n
)
{
win
(
this
.
kvc
,
game
,
match
)
}
else
{
fail
(
this
.
kvc
,
game
,
match
)
}
}
game
.
status
=
2
this
.
kvc
.
add
(
game
.
id
,
game
)
this
.
kvc
.
addlog
(
game
)
return
this
.
kvc
.
receipt
()
}
Exec
.
prototype
.
ForceCloseGame
=
function
(
args
)
{
var
local
=
new
MatchLocalTable
(
this
.
kvc
)
var
game
=
this
.
kvc
.
get
(
args
.
id
)
if
(
!
game
)
{
throw
new
Error
(
"game id not found"
)
}
var
matches
=
local
.
getmath
(
args
.
id
)
if
(
!
matches
)
{
matches
=
[]
}
if
(
this
.
context
.
height
-
game
.
height
<
100
)
{
throw
new
Error
(
"force close game must wait 100 block"
)
}
for
(
var
i
=
0
;
i
<
matches
.
length
;
i
++
)
{
var
match
=
matches
[
i
]
win
(
this
.
kvc
,
game
,
match
)
}
game
.
status
=
2
this
.
kvc
.
add
(
game
.
id
,
game
)
this
.
kvc
.
addlog
(
game
)
return
this
.
kvc
.
receipt
()
}
ExecLocal
.
prototype
.
NewGame
=
function
(
args
)
{
var
local
=
new
MatchGameTable
(
this
.
kvc
)
local
.
add
(
this
.
logs
)
local
.
table
.
save
()
return
this
.
kvc
.
receipt
()
}
ExecLocal
.
prototype
.
Guess
=
function
(
args
)
{
var
local
=
new
MatchGameTable
(
this
.
kvc
)
local
.
add
(
this
.
logs
)
local
.
table
.
save
()
return
this
.
kvc
.
receipt
()
}
ExecLocal
.
prototype
.
CloseGame
=
function
(
args
)
{
var
local
=
new
MatchGameTable
(
this
.
kvc
)
local
.
add
(
this
.
logs
)
local
.
table
.
save
()
return
this
.
kvc
.
receipt
()
}
ExecLocal
.
prototype
.
ForceCloseGame
=
function
(
args
)
{
var
local
=
new
GameLocalTable
(
this
.
kvc
)
local
.
add
(
this
.
logs
)
local
.
table
.
save
()
return
this
.
kvc
.
receipt
()
}
Query
.
prototype
.
ListGameByAddr
=
function
(
args
)
{
var
local
=
new
GameLocalTable
(
this
.
kvc
)
return
local
.
query
(
args
)
}
/*
game ->(1 : n) match
game.id -> primary
game.index -> index
match.gameid -> fk
match.id -> primary
match.index -> index
*/
function
GameLocalTable
(
kvc
)
{
this
.
config
=
{
"#tablename"
:
"game"
,
"#primary"
:
"id"
,
"#db"
:
"localdb"
,
"id"
:
"%s"
,
"index"
:
"%18d"
,
"status"
:
"%d"
,
}
this
.
defaultvalue
=
{
"id"
:
"0"
,
"index"
:
0
,
"status"
:
0
,
}
this
.
kvc
=
kvc
this
.
table
=
new
Table
(
this
.
kvc
,
this
.
config
,
this
.
defaultvalue
)
}
function
MatchLocalTable
(
kvc
)
{
this
.
config
=
{
"#tablename"
:
"match"
,
"#primary"
:
"id"
,
"#db"
:
"localdb"
,
"id"
:
"%s"
,
"gameid"
:
"%s"
,
"index"
:
"%18d"
,
"addr"
:
"%s"
,
}
this
.
defaultvalue
=
{
"id"
:
"0"
,
"index"
:
0
,
"gameid"
:
"0"
,
"addr"
:
""
,
}
this
.
kvc
=
kvc
this
.
table
=
new
Table
(
this
.
kvc
,
this
.
config
,
this
.
defaultvalue
)
}
function
MatchGameTable
(
kvc
)
{
this
.
left
=
MatchLocalTable
(
kvc
)
this
.
right
=
GameLocalTable
(
kvc
)
this
.
table
=
new
JoinTable
(
left
,
right
,
"addr#status"
)
}
MatchGameTable
.
prototype
.
add
=
function
(
data
)
{
if
(
data
.
__type__
==
"match"
)
{
this
.
left
.
table
.
replace
(
data
)
}
if
(
data
.
__type__
==
"game"
)
{
this
.
right
.
table
.
replace
(
data
)
}
}
\ No newline at end of file
plugin/dapp/js/executor/js.go
View file @
ba5708f6
...
...
@@ -29,6 +29,7 @@ func init() {
}
execaddressFunc
(
basevm
)
registerTableFunc
(
basevm
)
sha256Func
(
basevm
)
}
var
isinit
int64
...
...
@@ -173,6 +174,7 @@ func (u *js) getContext(tx *types.Transaction, index int64) *blockContext {
Difficulty
:
u
.
GetDifficulty
(),
TxHash
:
common
.
ToHex
(
hash
[
:
]),
Index
:
index
,
From
:
tx
.
From
(),
}
}
...
...
plugin/dapp/js/executor/jsvm.go
View file @
ba5708f6
...
...
@@ -22,6 +22,7 @@ type blockContext struct {
Difficulty
uint64
`json:"difficulty"`
TxHash
string
`json:"txhash"`
Index
int64
`json:"index"`
From
string
`json:"from"`
}
func
parseJsReturn
(
prefix
[]
byte
,
jsvalue
*
otto
.
Object
)
(
kvlist
[]
*
types
.
KeyValue
,
logs
[]
*
types
.
ReceiptLog
,
err
error
)
{
...
...
plugin/dapp/js/executor/runtime.go
View file @
ba5708f6
package
executor
import
(
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/address"
"github.com/robertkrimen/otto"
)
...
...
@@ -17,6 +18,17 @@ func execaddressFunc(vm *otto.Otto) {
})
}
func
sha256Func
(
vm
*
otto
.
Otto
)
{
vm
.
Set
(
"sha256"
,
func
(
call
otto
.
FunctionCall
)
otto
.
Value
{
key
,
err
:=
call
.
Argument
(
0
)
.
ToString
()
if
err
!=
nil
{
return
errReturn
(
vm
,
err
)
}
var
hash
=
common
.
Sha256
([]
byte
(
key
))
return
okReturn
(
vm
,
common
.
ToHex
(
hash
))
})
}
/*
//获取系统随机数的接口
//randnum
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment