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
a74b1837
Commit
a74b1837
authored
Jan 06, 2019
by
vipwzw
Committed by
33cn
Jan 07, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
all test pass
parent
878799ee
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
75 additions
and
54 deletions
+75
-54
const.go
plugin/dapp/js/executor/const.go
+32
-19
const_test.go
plugin/dapp/js/executor/const_test.go
+11
-13
exec_local.go
plugin/dapp/js/executor/exec_local.go
+0
-5
game.js
plugin/dapp/js/executor/game.js
+11
-11
runtime.js
plugin/dapp/js/executor/runtime.js
+21
-6
No files found.
plugin/dapp/js/executor/const.go
View file @
a74b1837
...
...
@@ -35,7 +35,11 @@ Table.prototype.get = function(key, row) {
if (!isstring(row)) {
row = tojson(row)
}
return table_get(this.id, key, row)
var ret = table_get(this.id, key, row)
if (ret.err) {
throwerr(ret.err)
}
return ret.value
}
function query_list(indexName, prefix, primaryKey, count, direction) {
...
...
@@ -58,6 +62,14 @@ function query_list(indexName, prefix, primaryKey, count, direction) {
if (q.err) {
return null
}
for (var i = 0; i < q.length; i++) {
if (q[i].left) {
q[i].left = JSON.parse(q[i].left)
}
if (q[i].right) {
q[i].right = JSON.parse(q[i].right)
}
}
return q
}
...
...
@@ -119,7 +131,6 @@ function print(obj) {
JoinTable.prototype.save = function() {
var ret = table_save(this.id)
print(ret)
if (this.kvc) {
this.kvc.save(ret)
}
...
...
@@ -130,7 +141,11 @@ JoinTable.prototype.get = function(key, row) {
if (!isstring(row)) {
row = tojson(row)
}
return table_get(this.id, key, row)
var ret = table_get(this.id, key, row)
if (ret.err) {
throwerr(ret.err)
}
return ret.value
}
JoinTable.prototype.query = function(indexName, prefix, primaryKey, count, direction) {
...
...
@@ -273,7 +288,7 @@ account.prototype.execTransFrozenToActive = function(execer, from, to, amount) {
if (err) {
return err
}
return
this.execTransfer(execer, from, to, amount)
err =
this.execTransfer(execer, from, to, amount)
}
//from frozen -> to frozen
...
...
@@ -457,9 +472,9 @@ Query.prototype.JoinKey = function(args) {
return table_joinkey(args.left, args.right).value
}
function throwerr(err) {
function throwerr(err
, msg
) {
if (err) {
throw new Error(err)
throw new Error(err
+ ":" + msg
)
}
}
...
...
@@ -612,8 +627,6 @@ Exec.prototype.CloseGame = function(args) {
throwerr("game id not found")
}
var querykey = local.get("gameid", args)
print("---------")
print(querykey)
var matches = local.query("gameid", querykey, "", 0, 1)
if (!matches) {
matches = []
...
...
@@ -632,12 +645,12 @@ Exec.prototype.CloseGame = function(args) {
throwerr("close game must wait "+MIN_WAIT_BLOCK+" block")
}
for (var i = 0; i < matches.length; i++) {
var match = matches[i]
var match = matches[i]
.left
if (match.num == n) {
//不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this
win(this, game, match)
win
.call
(this, game, match)
} else {
fail(this, game, match)
fail
.call
(this, game, match)
}
}
if (game.bet > 0) {
...
...
@@ -651,24 +664,24 @@ Exec.prototype.CloseGame = function(args) {
return this.kvc.receipt()
}
function win(
othis,
game, match) {
function win(game, match) {
var amount = (RAND_MAX - 1) * match.bet
if (game.bet - amount < 0) {
amount = game.bet
}
var err
if (amount > 0) {
err = this.acc.execTransFrozenToActive(
o
this.name, game.addr, match.addr, amount)
throwerr(err)
err = this.acc.execTransFrozenToActive(this.name, game.addr, match.addr, amount)
throwerr(err
, "execTransFrozenToActive"
)
game.bet -= amount
}
err =
othis.acc.execActive(
match.addr, match.bet)
throwerr(err)
err =
this.acc.execActive(this.name,
match.addr, match.bet)
throwerr(err
, "execActive"
)
}
function fail(
othis,
game, match) {
function fail(game, match) {
var amount = match.bet
err =
othis.acc.execTransFrozenToFrozen(o
this.name, match.addr, game.addr, amount)
err =
this.acc.execTransFrozenToFrozen(
this.name, match.addr, game.addr, amount)
throwerr(err)
game.bet += amount
}
...
...
@@ -688,7 +701,7 @@ Exec.prototype.ForceCloseGame = function(args) {
}
for (var i = 0; i < matches.length; i++) {
var match = matches[i]
win(this.kvc, game, match)
win
.call
(this.kvc, game, match)
}
if (game.bet > 0) {
var err = this.acc.execActive(this.name, game.addr, game.bet)
...
...
plugin/dapp/js/executor/const_test.go
View file @
a74b1837
...
...
@@ -113,8 +113,6 @@ Exec.prototype.CloseGame = function(args) {
throwerr("game id not found")
}
var querykey = local.get("gameid", args)
print("---------")
print(querykey)
var matches = local.query("gameid", querykey, "", 0, 1)
if (!matches) {
matches = []
...
...
@@ -133,12 +131,12 @@ Exec.prototype.CloseGame = function(args) {
throwerr("close game must wait "+MIN_WAIT_BLOCK+" block")
}
for (var i = 0; i < matches.length; i++) {
var match = matches[i]
var match = matches[i]
.left
if (match.num == n) {
//不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this
win(this, game, match)
win
.call
(this, game, match)
} else {
fail(this, game, match)
fail
.call
(this, game, match)
}
}
if (game.bet > 0) {
...
...
@@ -152,24 +150,24 @@ Exec.prototype.CloseGame = function(args) {
return this.kvc.receipt()
}
function win(
othis,
game, match) {
function win(game, match) {
var amount = (RAND_MAX - 1) * match.bet
if (game.bet - amount < 0) {
amount = game.bet
}
var err
if (amount > 0) {
err = this.acc.execTransFrozenToActive(
o
this.name, game.addr, match.addr, amount)
throwerr(err)
err = this.acc.execTransFrozenToActive(this.name, game.addr, match.addr, amount)
throwerr(err
, "execTransFrozenToActive"
)
game.bet -= amount
}
err =
othis.acc.execActive(
match.addr, match.bet)
throwerr(err)
err =
this.acc.execActive(this.name,
match.addr, match.bet)
throwerr(err
, "execActive"
)
}
function fail(
othis,
game, match) {
function fail(game, match) {
var amount = match.bet
err =
othis.acc.execTransFrozenToFrozen(o
this.name, match.addr, game.addr, amount)
err =
this.acc.execTransFrozenToFrozen(
this.name, match.addr, game.addr, amount)
throwerr(err)
game.bet += amount
}
...
...
@@ -189,7 +187,7 @@ Exec.prototype.ForceCloseGame = function(args) {
}
for (var i = 0; i < matches.length; i++) {
var match = matches[i]
win(this.kvc, game, match)
win
.call
(this.kvc, game, match)
}
if (game.bet > 0) {
var err = this.acc.execActive(this.name, game.addr, game.bet)
...
...
plugin/dapp/js/executor/exec_local.go
View file @
a74b1837
package
executor
import
(
"fmt"
"github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types"
ptypes
"github.com/33cn/plugin/plugin/dapp/js/types"
...
...
@@ -30,8 +28,5 @@ func (c *js) ExecLocal_Call(payload *jsproto.Call, tx *types.Transaction, receip
kvc
.
AddRollbackKV
()
r
:=
&
types
.
LocalDBSet
{}
r
.
KV
=
kvc
.
KVList
()
for
i
:=
0
;
i
<
len
(
r
.
KV
);
i
++
{
fmt
.
Println
(
string
(
r
.
KV
[
i
]
.
Key
))
}
return
r
,
nil
}
plugin/dapp/js/executor/game.js
View file @
a74b1837
...
...
@@ -94,12 +94,12 @@ Exec.prototype.CloseGame = function(args) {
throwerr
(
"close game must wait "
+
MIN_WAIT_BLOCK
+
" block"
)
}
for
(
var
i
=
0
;
i
<
matches
.
length
;
i
++
)
{
var
match
=
matches
[
i
]
var
match
=
matches
[
i
]
.
left
if
(
match
.
num
==
n
)
{
//不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this
win
(
this
,
game
,
match
)
win
.
call
(
this
,
game
,
match
)
}
else
{
fail
(
this
,
game
,
match
)
fail
.
call
(
this
,
game
,
match
)
}
}
if
(
game
.
bet
>
0
)
{
...
...
@@ -113,24 +113,24 @@ Exec.prototype.CloseGame = function(args) {
return
this
.
kvc
.
receipt
()
}
function
win
(
othis
,
game
,
match
)
{
function
win
(
game
,
match
)
{
var
amount
=
(
RAND_MAX
-
1
)
*
match
.
bet
if
(
game
.
bet
-
amount
<
0
)
{
amount
=
game
.
bet
}
var
err
if
(
amount
>
0
)
{
err
=
this
.
acc
.
execTransFrozenToActive
(
o
this
.
name
,
game
.
addr
,
match
.
addr
,
amount
)
throwerr
(
err
)
err
=
this
.
acc
.
execTransFrozenToActive
(
this
.
name
,
game
.
addr
,
match
.
addr
,
amount
)
throwerr
(
err
,
"execTransFrozenToActive"
)
game
.
bet
-=
amount
}
err
=
othis
.
acc
.
execActive
(
match
.
addr
,
match
.
bet
)
throwerr
(
err
)
err
=
this
.
acc
.
execActive
(
this
.
name
,
match
.
addr
,
match
.
bet
)
throwerr
(
err
,
"execActive"
)
}
function
fail
(
othis
,
game
,
match
)
{
function
fail
(
game
,
match
)
{
var
amount
=
match
.
bet
err
=
othis
.
acc
.
execTransFrozenToFrozen
(
o
this
.
name
,
match
.
addr
,
game
.
addr
,
amount
)
err
=
this
.
acc
.
execTransFrozenToFrozen
(
this
.
name
,
match
.
addr
,
game
.
addr
,
amount
)
throwerr
(
err
)
game
.
bet
+=
amount
}
...
...
@@ -150,7 +150,7 @@ Exec.prototype.ForceCloseGame = function(args) {
}
for
(
var
i
=
0
;
i
<
matches
.
length
;
i
++
)
{
var
match
=
matches
[
i
]
win
(
this
.
kvc
,
game
,
match
)
win
.
call
(
this
.
kvc
,
game
,
match
)
}
if
(
game
.
bet
>
0
)
{
var
err
=
this
.
acc
.
execActive
(
this
.
name
,
game
.
addr
,
game
.
bet
)
...
...
plugin/dapp/js/executor/runtime.js
View file @
a74b1837
...
...
@@ -32,7 +32,11 @@ Table.prototype.get = function(key, row) {
if
(
!
isstring
(
row
))
{
row
=
tojson
(
row
)
}
return
table_get
(
this
.
id
,
key
,
row
)
var
ret
=
table_get
(
this
.
id
,
key
,
row
)
if
(
ret
.
err
)
{
throwerr
(
ret
.
err
)
}
return
ret
.
value
}
function
query_list
(
indexName
,
prefix
,
primaryKey
,
count
,
direction
)
{
...
...
@@ -55,6 +59,14 @@ function query_list(indexName, prefix, primaryKey, count, direction) {
if
(
q
.
err
)
{
return
null
}
for
(
var
i
=
0
;
i
<
q
.
length
;
i
++
)
{
if
(
q
[
i
].
left
)
{
q
[
i
].
left
=
JSON
.
parse
(
q
[
i
].
left
)
}
if
(
q
[
i
].
right
)
{
q
[
i
].
right
=
JSON
.
parse
(
q
[
i
].
right
)
}
}
return
q
}
...
...
@@ -116,7 +128,6 @@ function print(obj) {
JoinTable
.
prototype
.
save
=
function
()
{
var
ret
=
table_save
(
this
.
id
)
print
(
ret
)
if
(
this
.
kvc
)
{
this
.
kvc
.
save
(
ret
)
}
...
...
@@ -127,7 +138,11 @@ JoinTable.prototype.get = function(key, row) {
if
(
!
isstring
(
row
))
{
row
=
tojson
(
row
)
}
return
table_get
(
this
.
id
,
key
,
row
)
var
ret
=
table_get
(
this
.
id
,
key
,
row
)
if
(
ret
.
err
)
{
throwerr
(
ret
.
err
)
}
return
ret
.
value
}
JoinTable
.
prototype
.
query
=
function
(
indexName
,
prefix
,
primaryKey
,
count
,
direction
)
{
...
...
@@ -270,7 +285,7 @@ account.prototype.execTransFrozenToActive = function(execer, from, to, amount) {
if
(
err
)
{
return
err
}
return
this
.
execTransfer
(
execer
,
from
,
to
,
amount
)
err
=
this
.
execTransfer
(
execer
,
from
,
to
,
amount
)
}
//from frozen -> to frozen
...
...
@@ -454,9 +469,9 @@ Query.prototype.JoinKey = function(args) {
return
table_joinkey
(
args
.
left
,
args
.
right
).
value
}
function
throwerr
(
err
)
{
function
throwerr
(
err
,
msg
)
{
if
(
err
)
{
throw
new
Error
(
err
)
throw
new
Error
(
err
+
":"
+
msg
)
}
}
...
...
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