Commit c40dba14 authored by pengjun's avatar pengjun

pokerbull由统一接口创建交易

parent b4efbed2
......@@ -56,19 +56,15 @@ func pokerbullStart(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
value, _ := cmd.Flags().GetUint64("value")
playerCount, _ := cmd.Flags().GetUint32("playerCount")
fee, _ := cmd.Flags().GetFloat64("fee")
feeInt64 := int64(fee * 1e4)
amountInt64 := int64(value)
params := &pkt.PBStartTxReq{
Value: amountInt64 * types.Coin,
PlayerNum: int32(playerCount),
Fee: feeInt64,
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.PokerBullX),
ActionName: pkt.CreateStartTx,
Payload: []byte(fmt.Sprintf("{\"value\":%d,\"playerNum\":%d}", int64(value) * types.Coin, int32(playerCount))),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "pokerbull.PokerBullStartTx", params, &res)
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
......@@ -91,17 +87,15 @@ func addPokerbullContinueFlags(cmd *cobra.Command) {
func pokerbullContinue(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
gameID, _ := cmd.Flags().GetString("gameID")
fee, _ := cmd.Flags().GetFloat64("fee")
feeInt64 := int64(fee * 1e4)
params := &pkt.PBContinueTxReq{
GameId: gameID,
Fee: feeInt64,
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.PokerBullX),
ActionName: pkt.CreateContinueTx,
Payload: []byte(fmt.Sprintf("{\"gameId\":\"%s\"}", gameID)),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "pokerbull.PokerBullContinueTx", params, &res)
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
......@@ -124,17 +118,15 @@ func addPokerbullQuitFlags(cmd *cobra.Command) {
func pokerbullQuit(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
gameID, _ := cmd.Flags().GetString("gameID")
fee, _ := cmd.Flags().GetFloat64("fee")
feeInt64 := int64(fee * 1e4)
params := &pkt.PBContinueTxReq{
GameId: gameID,
Fee: feeInt64,
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pkt.PokerBullX),
ActionName: pkt.CreatequitTx,
Payload: []byte(fmt.Sprintf("{\"gameId\":\"%s\"}", gameID)),
}
var res string
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "pokerbull.PokerBullQuitTx", params, &res)
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.CreateTransaction", params, &res)
ctx.RunWithoutMarshal()
}
......
......@@ -8,7 +8,6 @@ import (
"github.com/33cn/chain33/pluginmgr"
"github.com/33cn/plugin/plugin/dapp/pokerbull/commands"
"github.com/33cn/plugin/plugin/dapp/pokerbull/executor"
"github.com/33cn/plugin/plugin/dapp/pokerbull/rpc"
"github.com/33cn/plugin/plugin/dapp/pokerbull/types"
)
......@@ -18,6 +17,5 @@ func init() {
ExecName: executor.GetName(),
Exec: executor.Init,
Cmd: commands.PokerBullCmd,
RPC: rpc.Init,
})
}
syntax = "proto3";
import "transaction.proto";
package types;
//斗牛游戏内容
......@@ -187,12 +185,3 @@ message PBQueryReq {
int64 fee = 2;
}
// pokerbull 对外提供服务的接口
service pokerbull {
//游戏开始
rpc Start(PBGameStart) returns (UnsignTx) {}
//游戏继续
rpc Continue(PBGameContinue) returns (UnsignTx) {}
//游戏结束
rpc Quit(PBGameQuit) returns (UnsignTx) {}
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"context"
"encoding/hex"
"github.com/33cn/chain33/types"
pb "github.com/33cn/plugin/plugin/dapp/pokerbull/types"
)
// PokerBullStartTx 创建游戏开始交易
func (c *Jrpc) PokerBullStartTx(parm *pb.PBStartTxReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
}
head := &pb.PBGameStart{
Value: parm.Value,
PlayerNum: parm.PlayerNum,
}
reply, err := c.cli.Start(context.Background(), head)
if err != nil {
return err
}
*result = hex.EncodeToString(reply.Data)
return nil
}
// PokerBullContinueTx 创建游戏继续交易
func (c *Jrpc) PokerBullContinueTx(parm *pb.PBContinueTxReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
}
head := &pb.PBGameContinue{
GameId: parm.GameId,
}
reply, err := c.cli.Continue(context.Background(), head)
if err != nil {
return err
}
*result = hex.EncodeToString(reply.Data)
return nil
}
// PokerBullQuitTx 创建游戏推出交易
func (c *Jrpc) PokerBullQuitTx(parm *pb.PBQuitTxReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
}
head := &pb.PBGameQuit{
GameId: parm.GameId,
}
reply, err := c.cli.Quit(context.Background(), head)
if err != nil {
return err
}
*result = hex.EncodeToString(reply.Data)
return nil
}
// PokerBullQueryTx 创建游戏查询交易
func (c *Jrpc) PokerBullQueryTx(parm *pb.PBQueryReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
}
head := &pb.PBGameQuery{
GameId: parm.GameId,
}
reply, err := c.cli.Show(context.Background(), head)
if err != nil {
return err
}
*result = hex.EncodeToString(reply.Data)
return nil
}
......@@ -43,6 +43,9 @@ func TestJRPCChannel(t *testing.T) {
{fn: testQuitRawTxCmd},
{fn: testQueryGameByID},
{fn: testQueryGameByAddr},
{fn: testQueryGameByIDs},
{fn: testQueryGameByStatus},
{fn: testQueryGameByRound},
}
for index, testCase := range testCases {
err := testCase.fn(t, jrpcClient)
......@@ -57,29 +60,42 @@ func TestJRPCChannel(t *testing.T) {
}
func testStartRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
params := pty.PBStartTxReq{}
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreateStartTx,
Payload: []byte(""),
}
var res string
return jrpc.Call("pokerbull.PokerBullStartTx", params, &res)
return jrpc.Call("Chain33.CreateTransaction", params, &res)
}
func testContinueRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
params := pty.PBContinueTxReq{}
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreateContinueTx,
Payload: []byte(""),
}
var res string
return jrpc.Call("pokerbull.PokerBullContinueTx", params, &res)
return jrpc.Call("Chain33.CreateTransaction", params, &res)
}
func testQuitRawTxCmd(t *testing.T, jrpc *jsonclient.JSONClient) error {
params := pty.PBContinueTxReq{}
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreatequitTx,
Payload: []byte(""),
}
var res string
return jrpc.Call("pokerbull.PokerBullQuitTx", params, &res)
return jrpc.Call("Chain33.CreateTransaction", params, &res)
}
func testQueryGameByID(t *testing.T, jrpc *jsonclient.JSONClient) error {
var rep interface{}
var params rpctypes.Query4Jrpc
req := &pty.QueryPBGameInfo{}
params.Execer = "pokerbull"
params.FuncName = "QueryGameByID"
params.Execer = pty.PokerBullX
params.FuncName = pty.FuncNameQueryGameByID
params.Payload = types.MustPBToJSON(req)
rep = &pty.ReplyPBGame{}
return jrpc.Call("Chain33.Query", params, rep)
......@@ -89,9 +105,43 @@ func testQueryGameByAddr(t *testing.T, jrpc *jsonclient.JSONClient) error {
var rep interface{}
var params rpctypes.Query4Jrpc
req := &pty.QueryPBGameInfo{}
params.Execer = "pokerbull"
params.FuncName = "QueryGameByAddr"
params.Execer = pty.PokerBullX
params.FuncName = pty.FuncNameQueryGameByAddr
params.Payload = types.MustPBToJSON(req)
rep = &pty.PBGameRecords{}
return jrpc.Call("Chain33.Query", params, rep)
}
func testQueryGameByIDs(t *testing.T, jrpc *jsonclient.JSONClient) error {
var rep interface{}
var params rpctypes.Query4Jrpc
req := &pty.QueryPBGameInfos{}
params.Execer = pty.PokerBullX
params.FuncName = pty.FuncNameQueryGameListByIDs
params.Payload = types.MustPBToJSON(req)
rep = &pty.ReplyPBGameList{}
return jrpc.Call("Chain33.Query", params, rep)
}
func testQueryGameByStatus(t *testing.T, jrpc *jsonclient.JSONClient) error {
var rep interface{}
var params rpctypes.Query4Jrpc
req := &pty.QueryPBGameInfo{}
params.Execer = pty.PokerBullX
params.FuncName = pty.FuncNameQueryGameByStatus
params.Payload = types.MustPBToJSON(req)
rep = &pty.PBGameRecords{}
return jrpc.Call("Chain33.Query", params, rep)
}
func testQueryGameByRound(t *testing.T, jrpc *jsonclient.JSONClient) error {
var rep interface{}
var params rpctypes.Query4Jrpc
req := &pty.QueryPBGameByRound{}
params.Execer = pty.PokerBullX
params.FuncName = pty.FuncNameQueryGameByRound
params.Payload = types.MustPBToJSON(req)
rep = &pty.PBGameRecords{}
return jrpc.Call("Chain33.Query", params, rep)
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"context"
"github.com/33cn/chain33/types"
pb "github.com/33cn/plugin/plugin/dapp/pokerbull/types"
"github.com/pkg/errors"
)
func (c *channelClient) Start(ctx context.Context, head *pb.PBGameStart) (*types.UnsignTx, error) {
if head.PlayerNum > pb.MaxPlayerNum {
return nil, errors.New("Player number should be maximum 5")
}
val := &pb.PBGameAction{
Ty: pb.PBGameActionStart,
Value: &pb.PBGameAction_Start{Start: head},
}
tx, err := types.CreateFormatTx(types.ExecName(pb.PokerBullX), types.Encode(val))
if err != nil {
return nil, err
}
data := types.Encode(tx)
return &types.UnsignTx{Data: data}, nil
}
func (c *channelClient) Continue(ctx context.Context, head *pb.PBGameContinue) (*types.UnsignTx, error) {
val := &pb.PBGameAction{
Ty: pb.PBGameActionContinue,
Value: &pb.PBGameAction_Continue{Continue: head},
}
tx, err := types.CreateFormatTx(types.ExecName(pb.PokerBullX), types.Encode(val))
if err != nil {
return nil, err
}
data := types.Encode(tx)
return &types.UnsignTx{Data: data}, nil
}
func (c *channelClient) Quit(ctx context.Context, head *pb.PBGameQuit) (*types.UnsignTx, error) {
val := &pb.PBGameAction{
Ty: pb.PBGameActionQuit,
Value: &pb.PBGameAction_Quit{Quit: head},
}
tx, err := types.CreateFormatTx(types.ExecName(pb.PokerBullX), types.Encode(val))
if err != nil {
return nil, err
}
data := types.Encode(tx)
return &types.UnsignTx{Data: data}, nil
}
func (c *channelClient) Show(ctx context.Context, head *pb.PBGameQuery) (*types.UnsignTx, error) {
val := &pb.PBGameAction{
Ty: pb.PBGameActionQuery,
Value: &pb.PBGameAction_Query{Query: head},
}
tx, err := types.CreateFormatTx(types.ExecName(pb.PokerBullX), types.Encode(val))
if err != nil {
return nil, err
}
data := types.Encode(tx)
return &types.UnsignTx{Data: data}, nil
}
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rpc
import (
"github.com/33cn/chain33/rpc/types"
)
// Jrpc jrpc句柄
type Jrpc struct {
cli *channelClient
}
// Grpc grpc句柄
type Grpc struct {
*channelClient
}
type channelClient struct {
types.ChannelClient
}
// Init 初始化rpc
func Init(name string, s types.RPCServer) {
cli := &channelClient{}
grpc := &Grpc{channelClient: cli}
cli.Init(name, s, &Jrpc{cli: cli}, grpc)
}
This diff is collapsed.
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