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
e650737b
Commit
e650737b
authored
Jan 04, 2019
by
vipwzw
Committed by
33cn
Jan 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add test
parent
ba5708f6
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
128 additions
and
56 deletions
+128
-56
game.js
plugin/dapp/js/executor/game.js
+62
-36
runtime.js
plugin/dapp/js/executor/runtime.js
+66
-2
test.js
plugin/dapp/js/executor/test.js
+0
-18
No files found.
plugin/dapp/js/executor/game.js
View file @
e650737b
...
...
@@ -8,33 +8,26 @@ function Init(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
function
ExecInit
(
execthis
)
{
execthis
.
acc
=
new
account
(
this
.
kvc
,
"coins"
,
"bty"
)
}
Exec
.
prototype
.
NewGame
=
function
(
args
)
{
var
game
=
{
__type__
:
"game"
}
game
.
id
=
this
.
context
.
txhash
game
.
index
=
this
.
context
.
height
*
100000
+
this
.
index
game
.
index
=
this
.
txID
()
game
.
height
=
this
.
context
.
height
game
.
randhash
=
args
.
hash
game
.
bet
=
args
.
bet
game
.
obet
=
game
.
bet
game
.
addr
=
this
.
context
.
from
game
.
status
=
1
//open
if
(
game
.
bet
<
10
)
{
throw
new
Error
(
"bet too low"
)
//最大值是 9000万,否则js到 int 会溢出
if
(
game
.
bet
<
10
*
COINS
||
game
.
bet
>
10000000
*
COINS
)
{
throw
new
Error
(
"bet low than 10 or hight than 10000000"
)
}
var
err
=
this
.
acc
.
execFrozen
(
this
.
name
,
this
.
context
.
from
,
game
.
bet
)
throwerr
(
err
)
this
.
kvc
.
add
(
game
.
id
,
game
)
this
.
kvc
.
addlog
(
game
)
return
this
.
kvc
.
receipt
()
...
...
@@ -44,8 +37,7 @@ 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
.
id
=
this
.
txID
()
match
.
addr
=
this
.
context
.
from
var
game
=
this
.
kvc
.
get
(
match
.
gameid
)
if
(
!
game
)
{
...
...
@@ -54,6 +46,11 @@ Exec.prototype.Guess = function(args) {
if
(
game
.
status
!=
1
)
{
throw
new
Error
(
"game status not open"
)
}
if
(
match
.
bet
<
1
*
COINS
||
match
.
bet
>
game
.
bet
/
10
)
{
throw
new
Error
(
"match bet litte than 1 or big than game.bet/10"
)
}
var
err
=
this
.
acc
.
execFrozen
(
this
.
name
,
this
.
context
.
from
,
game
.
bet
)
throwerr
(
err
)
this
.
kvc
.
add
(
match
.
id
,
match
)
this
.
kvc
.
addlog
(
match
)
return
this
.
kvc
.
receipt
()
...
...
@@ -63,7 +60,7 @@ Exec.prototype.CloseGame = function(args) {
var
local
=
new
MatchLocalTable
(
this
.
kvc
)
var
game
=
this
.
kvc
.
get
(
args
.
id
)
if
(
!
game
)
{
throw
new
Erro
r
(
"game id not found"
)
throw
er
r
(
"game id not found"
)
}
var
matches
=
local
.
getmath
(
args
.
id
)
if
(
!
matches
)
{
...
...
@@ -76,42 +73,75 @@ Exec.prototype.CloseGame = function(args) {
}
}
if
(
n
==
-
1
)
{
throw
new
Erro
r
(
"err rand str"
)
throw
er
r
(
"err rand str"
)
}
if
(
this
.
context
.
height
-
game
.
height
<
10
)
{
throw
new
Erro
r
(
"close game must wait 10 block"
)
throw
er
r
(
"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
)
//不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this
win
(
this
,
game
,
match
)
}
else
{
fail
(
this
.
kvc
,
game
,
match
)
fail
(
this
,
game
,
match
)
}
}
if
(
game
.
bet
>
0
)
{
var
err
=
this
.
acc
.
execActive
(
this
.
name
,
game
.
addr
,
game
.
bet
)
throwerr
(
err
)
game
.
bet
=
0
}
game
.
status
=
2
this
.
kvc
.
add
(
game
.
id
,
game
)
this
.
kvc
.
addlog
(
game
)
return
this
.
kvc
.
receipt
()
}
function
win
(
this
,
game
,
match
)
{
var
amount
=
9
*
match
.
bet
if
(
game
.
bet
-
amount
<
0
)
{
amount
=
game
.
bet
}
var
err
if
(
amount
>
0
)
{
err
=
this
.
acc
.
execTransFrozenToActive
(
this
.
name
,
game
.
addr
,
match
.
addr
,
amount
)
throwerr
(
err
)
game
.
bet
-=
amount
}
err
=
this
.
acc
.
execActive
(
match
.
addr
,
match
.
bet
)
throwerr
(
err
)
}
function
fail
(
this
,
game
,
match
)
{
var
amount
=
match
.
bet
err
=
this
.
acc
.
execTransFrozenToFrozen
(
this
.
name
,
match
.
addr
,
game
.
addr
,
amount
)
throwerr
(
err
)
game
.
bet
+=
amount
}
Exec
.
prototype
.
ForceCloseGame
=
function
(
args
)
{
var
local
=
new
MatchLocalTable
(
this
.
kvc
)
var
game
=
this
.
kvc
.
get
(
args
.
id
)
if
(
!
game
)
{
throw
new
Erro
r
(
"game id not found"
)
throw
er
r
(
"game id not found"
)
}
var
matches
=
local
.
getmath
(
args
.
id
)
if
(
!
matches
)
{
matches
=
[]
}
if
(
this
.
context
.
height
-
game
.
height
<
100
)
{
throw
new
Erro
r
(
"force close game must wait 100 block"
)
throw
er
r
(
"force close game must wait 100 block"
)
}
for
(
var
i
=
0
;
i
<
matches
.
length
;
i
++
)
{
var
match
=
matches
[
i
]
win
(
this
.
kvc
,
game
,
match
)
}
if
(
game
.
bet
>
0
)
{
var
err
=
this
.
acc
.
execActive
(
this
.
name
,
game
.
addr
,
game
.
bet
)
throwerr
(
err
)
game
.
bet
=
0
}
game
.
status
=
2
this
.
kvc
.
add
(
game
.
id
,
game
)
this
.
kvc
.
addlog
(
game
)
...
...
@@ -154,11 +184,9 @@ Query.prototype.ListGameByAddr = function(args) {
/*
game ->(1 : n) match
game.id -> primary
game.index -> index
match.gameid -> fk
match.id -> primary
match.index -> index
*/
function
GameLocalTable
(
kvc
)
{
...
...
@@ -166,14 +194,14 @@ function GameLocalTable(kvc) {
"#tablename"
:
"game"
,
"#primary"
:
"id"
,
"#db"
:
"localdb"
,
"id"
:
"%s"
,
"index"
:
"%18d"
,
"id"
:
"%018d"
,
"status"
:
"%d"
,
"addr"
:
"%s"
,
}
this
.
defaultvalue
=
{
"id"
:
"0"
,
"index"
:
0
,
"status"
:
0
,
"addr"
:
""
,
}
this
.
kvc
=
kvc
this
.
table
=
new
Table
(
this
.
kvc
,
this
.
config
,
this
.
defaultvalue
)
...
...
@@ -184,15 +212,13 @@ function MatchLocalTable(kvc) {
"#tablename"
:
"match"
,
"#primary"
:
"id"
,
"#db"
:
"localdb"
,
"id"
:
"%
s
"
,
"id"
:
"%
018d
"
,
"gameid"
:
"%s"
,
"index"
:
"%18d"
,
"addr"
:
"%s"
,
}
this
.
defaultvalue
=
{
"id"
:
"0"
,
"index"
:
0
,
"gameid"
:
"0"
,
"id"
:
0
,
"gameid"
:
0
,
"addr"
:
""
,
}
this
.
kvc
=
kvc
...
...
plugin/dapp/js/executor/runtime.js
View file @
e650737b
...
...
@@ -179,6 +179,32 @@ account.prototype.execTransfer = function(execer, from, to, amount) {
return
ret
.
err
}
//from frozen -> to active
account
.
prototype
.
execTransFrozenToActive
=
function
(
execer
,
from
,
to
,
amount
)
{
var
err
err
=
this
.
execActive
(
execer
,
from
,
amount
)
if
(
err
)
{
return
err
}
return
this
.
execTransfer
(
execer
,
from
,
to
,
amount
)
}
//from frozen -> to frozen
account
.
prototype
.
execTransFrozenToFrozen
=
function
(
execer
,
from
,
to
,
amount
)
{
var
err
err
=
this
.
execActive
(
execer
,
from
,
amount
)
if
(
err
)
{
return
err
}
err
=
this
.
execTransfer
(
execer
,
from
,
to
,
amount
)
if
(
err
)
{
return
err
}
return
this
.
execFrozen
(
execer
,
to
,
amount
)
}
COINS
=
100000000
function
kvcreator
(
dbtype
)
{
this
.
data
=
{}
this
.
kvs
=
[]
...
...
@@ -212,7 +238,7 @@ kvcreator.prototype.get = function(k, prefix) {
v
=
this
.
data
[
k
]
}
else
{
var
dbvalue
=
this
.
get
(
k
,
!!
prefix
)
if
(
dbvalue
.
err
!=
""
)
{
if
(
dbvalue
.
err
)
{
return
null
}
v
=
dbvalue
.
value
...
...
@@ -240,7 +266,7 @@ kvcreator.prototype.save = function(receipt) {
kvcreator
.
prototype
.
listvalue
=
function
(
prefix
,
key
,
count
,
direction
)
{
var
dbvalues
=
this
.
list
(
prefix
,
key
,
count
,
direction
)
if
(
dbvalues
.
err
!=
""
)
{
if
(
dbvalues
.
err
)
{
return
[]
}
var
values
=
dbvalues
.
value
...
...
@@ -274,6 +300,44 @@ kvcreator.prototype.receipt = function() {
return
{
kvs
:
this
.
kvs
,
logs
:
this
.
logs
}
}
function
Exec
(
context
)
{
this
.
kvc
=
new
kvcreator
(
"exec"
)
this
.
context
=
context
this
.
name
=
execname
()
if
(
typeof
ExecInit
==
"function"
)
{
ExecInit
(
this
)
}
}
Exec
.
prototype
.
txID
=
function
()
{
return
this
.
context
.
height
*
100000
+
this
.
index
}
function
ExecLocal
(
context
,
logs
)
{
this
.
kvc
=
new
kvcreator
(
"local"
)
this
.
context
=
context
this
.
logs
=
logs
this
.
name
=
execname
()
if
(
typeof
ExecLocalInit
==
"function"
)
{
ExecLocalInit
(
this
)
}
}
function
Query
(
context
)
{
this
.
kvc
=
new
kvcreator
(
"query"
)
this
.
context
=
context
this
.
name
=
execname
()
if
(
typeof
QueryInit
==
"function"
)
{
QueryInit
(
this
)
}
}
function
throwerr
(
err
)
{
if
(
err
)
{
throw
new
Error
(
err
)
}
}
function
callcode
(
context
,
f
,
args
,
loglist
)
{
if
(
f
==
"init"
)
{
return
Init
(
JSON
.
parse
(
context
))
...
...
plugin/dapp/js/executor/test.js
View file @
e650737b
//数据结构设计
//kvlist [{key:"key1", value:"value1"},{key:"key2", value:"value2"}]
//log 设计 {json data}
function
Init
(
context
)
{
this
.
kvc
=
new
kvcreator
(
"init"
)
this
.
context
=
context
...
...
@@ -9,22 +7,6 @@ function Init(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
.
hello
=
function
(
args
)
{
this
.
kvc
.
add
(
"args"
,
args
)
this
.
kvc
.
add
(
"action"
,
"exec"
)
...
...
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