Commit fddd06f8 authored by vipwzw's avatar vipwzw

hello

parent de416732
...@@ -64,14 +64,14 @@ func Key(id string) (key []byte) { ...@@ -64,14 +64,14 @@ func Key(id string) (key []byte) {
func readGame(db dbm.KV, id string) (*pkt.PokerBull, error) { func readGame(db dbm.KV, id string) (*pkt.PokerBull, error) {
data, err := db.Get(Key(id)) data, err := db.Get(Key(id))
if err != nil { if err != nil {
logger.Error("query data have err:", err.Error()) logger.Error("query data have err:", "err", err)
return nil, err return nil, err
} }
var game pkt.PokerBull var game pkt.PokerBull
//decode //decode
err = types.Decode(data, &game) err = types.Decode(data, &game)
if err != nil { if err != nil {
logger.Error("decode game have err:", err.Error()) logger.Error("decode game have err:", "err", err)
return nil, err return nil, err
} }
return &game, nil return &game, nil
...@@ -102,7 +102,6 @@ func getGameListByAddr(db dbm.Lister, addr string, index int64) (types.Message, ...@@ -102,7 +102,6 @@ func getGameListByAddr(db dbm.Lister, addr string, index int64) (types.Message,
if err != nil { if err != nil {
return nil, err return nil, err
} }
var gameIds []*pkt.PBGameRecord var gameIds []*pkt.PBGameRecord
for _, value := range values { for _, value := range values {
var record pkt.PBGameRecord var record pkt.PBGameRecord
...@@ -112,7 +111,9 @@ func getGameListByAddr(db dbm.Lister, addr string, index int64) (types.Message, ...@@ -112,7 +111,9 @@ func getGameListByAddr(db dbm.Lister, addr string, index int64) (types.Message,
} }
gameIds = append(gameIds, &record) gameIds = append(gameIds, &record)
} }
if len(gameIds) == 0 {
return nil, types.ErrNotFound
}
return &pkt.PBGameRecords{Records: gameIds}, nil return &pkt.PBGameRecords{Records: gameIds}, nil
} }
......
...@@ -29,7 +29,6 @@ func (g *PokerBull) Query_QueryGameByAddr(in *pkt.QueryPBGameInfo) (types.Messag ...@@ -29,7 +29,6 @@ func (g *PokerBull) Query_QueryGameByAddr(in *pkt.QueryPBGameInfo) (types.Messag
if err != nil { if err != nil {
return nil, err return nil, err
} }
return gameIds, nil return gameIds, nil
} }
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
package rpc_test package rpc_test
import ( import (
"strings" "fmt"
"testing" "testing"
commonlog "github.com/33cn/chain33/common/log" commonlog "github.com/33cn/chain33/common/log"
...@@ -41,51 +41,65 @@ func TestJRPCChannel(t *testing.T) { ...@@ -41,51 +41,65 @@ func TestJRPCChannel(t *testing.T) {
{fn: testStartRawTxCmd}, {fn: testStartRawTxCmd},
{fn: testContinueRawTxCmd}, {fn: testContinueRawTxCmd},
{fn: testQuitRawTxCmd}, {fn: testQuitRawTxCmd},
}
for _, testCase := range testCases {
err := testCase.fn(t, jrpcClient)
assert.Nil(t, err)
}
testCases = []struct {
fn func(*testing.T, *jsonclient.JSONClient) error
}{
{fn: testQueryGameByID}, {fn: testQueryGameByID},
{fn: testQueryGameByAddr}, {fn: testQueryGameByAddr},
{fn: testQueryGameByIDs},
{fn: testQueryGameByStatus}, {fn: testQueryGameByStatus},
{fn: testQueryGameByRound}, {fn: testQueryGameByRound},
} }
for index, testCase := range testCases { for index, testCase := range testCases {
err := testCase.fn(t, jrpcClient) err := testCase.fn(t, jrpcClient)
if err == nil { assert.Equal(t, err, types.ErrNotFound, fmt.Sprint(index))
continue }
}
assert.NotEqualf(t, err, types.ErrActionNotSupport, "test index %d", index) testCases = []struct {
if strings.Contains(err.Error(), "rpc: can't find") { fn func(*testing.T, *jsonclient.JSONClient) error
assert.FailNowf(t, err.Error(), "test index %d", index) }{
} {fn: testQueryGameByIDs},
}
for index, testCase := range testCases {
err := testCase.fn(t, jrpcClient)
assert.Equal(t, err, nil, fmt.Sprint(index))
} }
} }
func testStartRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error { func testStartRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
payload := &pty.PBGameStart{Value: 123}
params := &rpctypes.CreateTxIn{ params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX), Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreateStartTx, ActionName: pty.CreateStartTx,
Payload: []byte(""), Payload: types.MustPBToJSON(payload),
} }
var res string var res string
return jrpc.Call("Chain33.CreateTransaction", params, &res) return jrpc.Call("Chain33.CreateTransaction", params, &res)
} }
func testContinueRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error { func testContinueRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
payload := &pty.PBGameContinue{GameId: "123"}
params := &rpctypes.CreateTxIn{ params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX), Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreateContinueTx, ActionName: pty.CreateContinueTx,
Payload: []byte(""), Payload: types.MustPBToJSON(payload),
} }
var res string var res string
return jrpc.Call("Chain33.CreateTransaction", params, &res) return jrpc.Call("Chain33.CreateTransaction", params, &res)
} }
func testQuitRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error { func testQuitRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
payload := &pty.PBGameQuit{GameId: "123"}
params := &rpctypes.CreateTxIn{ params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX), Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreatequitTx, ActionName: pty.CreatequitTx,
Payload: []byte(""), Payload: types.MustPBToJSON(payload),
} }
var res string var res string
return jrpc.Call("Chain33.CreateTransaction", params, &res) return jrpc.Call("Chain33.CreateTransaction", params, &res)
} }
......
...@@ -53,11 +53,11 @@ const ( ...@@ -53,11 +53,11 @@ const (
// FuncNameQueryGameByRound 查询某一回合游戏结果 // FuncNameQueryGameByRound 查询某一回合游戏结果
FuncNameQueryGameByRound = "QueryGameByRound" FuncNameQueryGameByRound = "QueryGameByRound"
// CreateStartTx 创建开始交易 // CreateStartTx 创建开始交易
CreateStartTx = "CreateStartTx" CreateStartTx = "Start"
// CreateContinueTx 创建继续交易 // CreateContinueTx 创建继续交易
CreateContinueTx = "CreateContinueTx" CreateContinueTx = "Continue"
// CreatequitTx 创建退出交易 // CreatequitTx 创建退出交易
CreatequitTx = "CreatequitTx" CreatequitTx = "Quit"
) )
const ( const (
......
...@@ -7,9 +7,6 @@ package types ...@@ -7,9 +7,6 @@ package types
import ( import (
"reflect" "reflect"
"encoding/json"
"errors"
log "github.com/33cn/chain33/common/log/log15" log "github.com/33cn/chain33/common/log/log15"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
) )
...@@ -66,79 +63,3 @@ func (t *PokerBullType) GetLogMap() map[int64]*types.LogInfo { ...@@ -66,79 +63,3 @@ func (t *PokerBullType) GetLogMap() map[int64]*types.LogInfo {
TyLogPBGameQuery: {Ty: reflect.TypeOf(ReceiptPBGame{}), Name: "TyLogPBGameQuery"}, TyLogPBGameQuery: {Ty: reflect.TypeOf(ReceiptPBGame{}), Name: "TyLogPBGameQuery"},
} }
} }
// CreateTx method
func (t *PokerBullType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
llog.Debug("pokerbull.CreateTx", "action", action)
if action == CreateStartTx {
var param PBGameStart
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawPBStartTx(&param)
} else if action == CreateContinueTx {
var param PBGameContinue
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawPBContinueTx(&param)
} else if action == CreatequitTx {
var param PBGameQuit
err := json.Unmarshal(message, &param)
if err != nil {
llog.Error("CreateTx", "Error", err)
return nil, types.ErrInvalidParam
}
return CreateRawPBQuitTx(&param)
}
return nil, types.ErrNotSupport
}
// CreateRawPBStartTx method
func CreateRawPBStartTx(head *PBGameStart) (*types.Transaction, error) {
if head.PlayerNum > MaxPlayerNum {
return nil, errors.New("Player number should be maximum 5")
}
val := &PBGameAction{
Ty: PBGameActionStart,
Value: &PBGameAction_Start{Start: head},
}
tx, err := types.CreateFormatTx(types.ExecName(PokerBullX), types.Encode(val))
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawPBContinueTx method
func CreateRawPBContinueTx(head *PBGameContinue) (*types.Transaction, error) {
val := &PBGameAction{
Ty: PBGameActionContinue,
Value: &PBGameAction_Continue{Continue: head},
}
tx, err := types.CreateFormatTx(types.ExecName(PokerBullX), types.Encode(val))
if err != nil {
return nil, err
}
return tx, nil
}
// CreateRawPBQuitTx method
func CreateRawPBQuitTx(head *PBGameQuit) (*types.Transaction, error) {
val := &PBGameAction{
Ty: PBGameActionQuit,
Value: &PBGameAction_Quit{Quit: head},
}
tx, err := types.CreateFormatTx(types.ExecName(PokerBullX), types.Encode(val))
if err != nil {
return nil, err
}
return tx, nil
}
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