Commit a74b1837 authored by vipwzw's avatar vipwzw Committed by 33cn

all test pass

parent 878799ee
...@@ -35,7 +35,11 @@ Table.prototype.get = function(key, row) { ...@@ -35,7 +35,11 @@ Table.prototype.get = function(key, row) {
if (!isstring(row)) { if (!isstring(row)) {
row = tojson(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) { function query_list(indexName, prefix, primaryKey, count, direction) {
...@@ -58,6 +62,14 @@ function query_list(indexName, prefix, primaryKey, count, direction) { ...@@ -58,6 +62,14 @@ function query_list(indexName, prefix, primaryKey, count, direction) {
if (q.err) { if (q.err) {
return null 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 return q
} }
...@@ -119,7 +131,6 @@ function print(obj) { ...@@ -119,7 +131,6 @@ function print(obj) {
JoinTable.prototype.save = function() { JoinTable.prototype.save = function() {
var ret = table_save(this.id) var ret = table_save(this.id)
print(ret)
if (this.kvc) { if (this.kvc) {
this.kvc.save(ret) this.kvc.save(ret)
} }
...@@ -130,7 +141,11 @@ JoinTable.prototype.get = function(key, row) { ...@@ -130,7 +141,11 @@ JoinTable.prototype.get = function(key, row) {
if (!isstring(row)) { if (!isstring(row)) {
row = tojson(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) { JoinTable.prototype.query = function(indexName, prefix, primaryKey, count, direction) {
...@@ -273,7 +288,7 @@ account.prototype.execTransFrozenToActive = function(execer, from, to, amount) { ...@@ -273,7 +288,7 @@ account.prototype.execTransFrozenToActive = function(execer, from, to, amount) {
if (err) { if (err) {
return err return err
} }
return this.execTransfer(execer, from, to, amount) err = this.execTransfer(execer, from, to, amount)
} }
//from frozen -> to frozen //from frozen -> to frozen
...@@ -457,9 +472,9 @@ Query.prototype.JoinKey = function(args) { ...@@ -457,9 +472,9 @@ Query.prototype.JoinKey = function(args) {
return table_joinkey(args.left, args.right).value return table_joinkey(args.left, args.right).value
} }
function throwerr(err) { function throwerr(err, msg) {
if (err) { if (err) {
throw new Error(err) throw new Error(err + ":" + msg)
} }
} }
...@@ -612,8 +627,6 @@ Exec.prototype.CloseGame = function(args) { ...@@ -612,8 +627,6 @@ Exec.prototype.CloseGame = function(args) {
throwerr("game id not found") throwerr("game id not found")
} }
var querykey = local.get("gameid", args) var querykey = local.get("gameid", args)
print("---------")
print(querykey)
var matches = local.query("gameid", querykey, "", 0, 1) var matches = local.query("gameid", querykey, "", 0, 1)
if (!matches) { if (!matches) {
matches = [] matches = []
...@@ -632,12 +645,12 @@ Exec.prototype.CloseGame = function(args) { ...@@ -632,12 +645,12 @@ Exec.prototype.CloseGame = function(args) {
throwerr("close game must wait "+MIN_WAIT_BLOCK+" block") throwerr("close game must wait "+MIN_WAIT_BLOCK+" block")
} }
for (var i = 0; i < matches.length; i++) { for (var i = 0; i < matches.length; i++) {
var match = matches[i] var match = matches[i].left
if (match.num == n) { if (match.num == n) {
//不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this //不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this
win(this, game, match) win.call(this, game, match)
} else { } else {
fail(this, game, match) fail.call(this, game, match)
} }
} }
if (game.bet > 0) { if (game.bet > 0) {
...@@ -651,24 +664,24 @@ Exec.prototype.CloseGame = function(args) { ...@@ -651,24 +664,24 @@ Exec.prototype.CloseGame = function(args) {
return this.kvc.receipt() return this.kvc.receipt()
} }
function win(othis, game, match) { function win(game, match) {
var amount = (RAND_MAX - 1) * match.bet var amount = (RAND_MAX - 1) * match.bet
if (game.bet - amount < 0) { if (game.bet - amount < 0) {
amount = game.bet amount = game.bet
} }
var err var err
if (amount > 0) { if (amount > 0) {
err = this.acc.execTransFrozenToActive(othis.name, game.addr, match.addr, amount) err = this.acc.execTransFrozenToActive(this.name, game.addr, match.addr, amount)
throwerr(err) throwerr(err, "execTransFrozenToActive")
game.bet -= amount game.bet -= amount
} }
err = othis.acc.execActive(match.addr, match.bet) err = this.acc.execActive(this.name, match.addr, match.bet)
throwerr(err) throwerr(err, "execActive")
} }
function fail(othis, game, match) { function fail(game, match) {
var amount = match.bet var amount = match.bet
err = othis.acc.execTransFrozenToFrozen(othis.name, match.addr, game.addr, amount) err = this.acc.execTransFrozenToFrozen(this.name, match.addr, game.addr, amount)
throwerr(err) throwerr(err)
game.bet += amount game.bet += amount
} }
...@@ -688,7 +701,7 @@ Exec.prototype.ForceCloseGame = function(args) { ...@@ -688,7 +701,7 @@ Exec.prototype.ForceCloseGame = function(args) {
} }
for (var i = 0; i < matches.length; i++) { for (var i = 0; i < matches.length; i++) {
var match = matches[i] var match = matches[i]
win(this.kvc, game, match) win.call(this.kvc, game, match)
} }
if (game.bet > 0) { if (game.bet > 0) {
var err = this.acc.execActive(this.name, game.addr, game.bet) var err = this.acc.execActive(this.name, game.addr, game.bet)
......
...@@ -113,8 +113,6 @@ Exec.prototype.CloseGame = function(args) { ...@@ -113,8 +113,6 @@ Exec.prototype.CloseGame = function(args) {
throwerr("game id not found") throwerr("game id not found")
} }
var querykey = local.get("gameid", args) var querykey = local.get("gameid", args)
print("---------")
print(querykey)
var matches = local.query("gameid", querykey, "", 0, 1) var matches = local.query("gameid", querykey, "", 0, 1)
if (!matches) { if (!matches) {
matches = [] matches = []
...@@ -133,12 +131,12 @@ Exec.prototype.CloseGame = function(args) { ...@@ -133,12 +131,12 @@ Exec.prototype.CloseGame = function(args) {
throwerr("close game must wait "+MIN_WAIT_BLOCK+" block") throwerr("close game must wait "+MIN_WAIT_BLOCK+" block")
} }
for (var i = 0; i < matches.length; i++) { for (var i = 0; i < matches.length; i++) {
var match = matches[i] var match = matches[i].left
if (match.num == n) { if (match.num == n) {
//不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this //不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this
win(this, game, match) win.call(this, game, match)
} else { } else {
fail(this, game, match) fail.call(this, game, match)
} }
} }
if (game.bet > 0) { if (game.bet > 0) {
...@@ -152,24 +150,24 @@ Exec.prototype.CloseGame = function(args) { ...@@ -152,24 +150,24 @@ Exec.prototype.CloseGame = function(args) {
return this.kvc.receipt() return this.kvc.receipt()
} }
function win(othis, game, match) { function win(game, match) {
var amount = (RAND_MAX - 1) * match.bet var amount = (RAND_MAX - 1) * match.bet
if (game.bet - amount < 0) { if (game.bet - amount < 0) {
amount = game.bet amount = game.bet
} }
var err var err
if (amount > 0) { if (amount > 0) {
err = this.acc.execTransFrozenToActive(othis.name, game.addr, match.addr, amount) err = this.acc.execTransFrozenToActive(this.name, game.addr, match.addr, amount)
throwerr(err) throwerr(err, "execTransFrozenToActive")
game.bet -= amount game.bet -= amount
} }
err = othis.acc.execActive(match.addr, match.bet) err = this.acc.execActive(this.name, match.addr, match.bet)
throwerr(err) throwerr(err, "execActive")
} }
function fail(othis, game, match) { function fail(game, match) {
var amount = match.bet var amount = match.bet
err = othis.acc.execTransFrozenToFrozen(othis.name, match.addr, game.addr, amount) err = this.acc.execTransFrozenToFrozen(this.name, match.addr, game.addr, amount)
throwerr(err) throwerr(err)
game.bet += amount game.bet += amount
} }
...@@ -189,7 +187,7 @@ Exec.prototype.ForceCloseGame = function(args) { ...@@ -189,7 +187,7 @@ Exec.prototype.ForceCloseGame = function(args) {
} }
for (var i = 0; i < matches.length; i++) { for (var i = 0; i < matches.length; i++) {
var match = matches[i] var match = matches[i]
win(this.kvc, game, match) win.call(this.kvc, game, match)
} }
if (game.bet > 0) { if (game.bet > 0) {
var err = this.acc.execActive(this.name, game.addr, game.bet) var err = this.acc.execActive(this.name, game.addr, game.bet)
......
package executor package executor
import ( import (
"fmt"
"github.com/33cn/chain33/system/dapp" "github.com/33cn/chain33/system/dapp"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
ptypes "github.com/33cn/plugin/plugin/dapp/js/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 ...@@ -30,8 +28,5 @@ func (c *js) ExecLocal_Call(payload *jsproto.Call, tx *types.Transaction, receip
kvc.AddRollbackKV() kvc.AddRollbackKV()
r := &types.LocalDBSet{} r := &types.LocalDBSet{}
r.KV = kvc.KVList() r.KV = kvc.KVList()
for i := 0; i < len(r.KV); i++ {
fmt.Println(string(r.KV[i].Key))
}
return r, nil return r, nil
} }
...@@ -94,12 +94,12 @@ Exec.prototype.CloseGame = function(args) { ...@@ -94,12 +94,12 @@ Exec.prototype.CloseGame = function(args) {
throwerr("close game must wait "+MIN_WAIT_BLOCK+" block") throwerr("close game must wait "+MIN_WAIT_BLOCK+" block")
} }
for (var i = 0; i < matches.length; i++) { for (var i = 0; i < matches.length; i++) {
var match = matches[i] var match = matches[i].left
if (match.num == n) { if (match.num == n) {
//不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this //不能随便添加辅助函数,因为可以被外界调用到,所以辅助函数都是传递 this
win(this, game, match) win.call(this, game, match)
} else { } else {
fail(this, game, match) fail.call(this, game, match)
} }
} }
if (game.bet > 0) { if (game.bet > 0) {
...@@ -113,24 +113,24 @@ Exec.prototype.CloseGame = function(args) { ...@@ -113,24 +113,24 @@ Exec.prototype.CloseGame = function(args) {
return this.kvc.receipt() return this.kvc.receipt()
} }
function win(othis, game, match) { function win(game, match) {
var amount = (RAND_MAX - 1) * match.bet var amount = (RAND_MAX - 1) * match.bet
if (game.bet - amount < 0) { if (game.bet - amount < 0) {
amount = game.bet amount = game.bet
} }
var err var err
if (amount > 0) { if (amount > 0) {
err = this.acc.execTransFrozenToActive(othis.name, game.addr, match.addr, amount) err = this.acc.execTransFrozenToActive(this.name, game.addr, match.addr, amount)
throwerr(err) throwerr(err, "execTransFrozenToActive")
game.bet -= amount game.bet -= amount
} }
err = othis.acc.execActive(match.addr, match.bet) err = this.acc.execActive(this.name, match.addr, match.bet)
throwerr(err) throwerr(err, "execActive")
} }
function fail(othis, game, match) { function fail(game, match) {
var amount = match.bet var amount = match.bet
err = othis.acc.execTransFrozenToFrozen(othis.name, match.addr, game.addr, amount) err = this.acc.execTransFrozenToFrozen(this.name, match.addr, game.addr, amount)
throwerr(err) throwerr(err)
game.bet += amount game.bet += amount
} }
...@@ -150,7 +150,7 @@ Exec.prototype.ForceCloseGame = function(args) { ...@@ -150,7 +150,7 @@ Exec.prototype.ForceCloseGame = function(args) {
} }
for (var i = 0; i < matches.length; i++) { for (var i = 0; i < matches.length; i++) {
var match = matches[i] var match = matches[i]
win(this.kvc, game, match) win.call(this.kvc, game, match)
} }
if (game.bet > 0) { if (game.bet > 0) {
var err = this.acc.execActive(this.name, game.addr, game.bet) var err = this.acc.execActive(this.name, game.addr, game.bet)
......
...@@ -32,7 +32,11 @@ Table.prototype.get = function(key, row) { ...@@ -32,7 +32,11 @@ Table.prototype.get = function(key, row) {
if (!isstring(row)) { if (!isstring(row)) {
row = tojson(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) { function query_list(indexName, prefix, primaryKey, count, direction) {
...@@ -55,6 +59,14 @@ function query_list(indexName, prefix, primaryKey, count, direction) { ...@@ -55,6 +59,14 @@ function query_list(indexName, prefix, primaryKey, count, direction) {
if (q.err) { if (q.err) {
return null 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 return q
} }
...@@ -116,7 +128,6 @@ function print(obj) { ...@@ -116,7 +128,6 @@ function print(obj) {
JoinTable.prototype.save = function() { JoinTable.prototype.save = function() {
var ret = table_save(this.id) var ret = table_save(this.id)
print(ret)
if (this.kvc) { if (this.kvc) {
this.kvc.save(ret) this.kvc.save(ret)
} }
...@@ -127,7 +138,11 @@ JoinTable.prototype.get = function(key, row) { ...@@ -127,7 +138,11 @@ JoinTable.prototype.get = function(key, row) {
if (!isstring(row)) { if (!isstring(row)) {
row = tojson(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) { JoinTable.prototype.query = function(indexName, prefix, primaryKey, count, direction) {
...@@ -270,7 +285,7 @@ account.prototype.execTransFrozenToActive = function(execer, from, to, amount) { ...@@ -270,7 +285,7 @@ account.prototype.execTransFrozenToActive = function(execer, from, to, amount) {
if (err) { if (err) {
return err return err
} }
return this.execTransfer(execer, from, to, amount) err = this.execTransfer(execer, from, to, amount)
} }
//from frozen -> to frozen //from frozen -> to frozen
...@@ -454,9 +469,9 @@ Query.prototype.JoinKey = function(args) { ...@@ -454,9 +469,9 @@ Query.prototype.JoinKey = function(args) {
return table_joinkey(args.left, args.right).value return table_joinkey(args.left, args.right).value
} }
function throwerr(err) { function throwerr(err, msg) {
if (err) { if (err) {
throw new Error(err) throw new Error(err + ":" + msg)
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment