Unverified Commit afbd0de6 authored by 33cn's avatar 33cn Committed by GitHub

Merge pull request #141 from jpeng-go/pokerbull

Pokerbull问题修复
parents 62cbbdc6 66bcc4fb
......@@ -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()
}
......@@ -155,6 +147,7 @@ func addPokerbullQueryFlags(cmd *cobra.Command) {
cmd.Flags().StringP("index", "i", "", "index")
cmd.Flags().StringP("status", "s", "", "status")
cmd.Flags().StringP("gameIDs", "d", "", "gameIDs")
cmd.Flags().StringP("round", "r", "", "round")
}
func pokerbullQuery(cmd *cobra.Command, args []string) {
......@@ -166,6 +159,7 @@ func pokerbullQuery(cmd *cobra.Command, args []string) {
indexstr, _ := cmd.Flags().GetString("index")
index, _ := strconv.ParseInt(indexstr, 10, 64)
gameIDs, _ := cmd.Flags().GetString("gameIDs")
round, _ := cmd.Flags().GetString("round")
var params rpctypes.Query4Jrpc
params.Execer = pkt.PokerBullX
......@@ -177,10 +171,27 @@ func pokerbullQuery(cmd *cobra.Command, args []string) {
}
params.Payload = types.MustPBToJSON(req)
if gameID != "" {
params.FuncName = pkt.FuncNameQueryGameByID
var res pkt.ReplyPBGame
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
if round == "" {
params.FuncName = pkt.FuncNameQueryGameByID
var res pkt.ReplyPBGame
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else {
params.FuncName = pkt.FuncNameQueryGameByRound
roundInt, err := strconv.ParseInt(round, 10, 32)
if err != nil {
fmt.Println(err)
return
}
req := &pkt.QueryPBGameByRound{
GameId: gameID,
Round: int32(roundInt),
}
params.Payload = types.MustPBToJSON(req)
var res pkt.ReplyPBGameByRound
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
}
} else if address != "" {
params.FuncName = pkt.FuncNameQueryGameByAddr
var res pkt.PBGameRecords
......
......@@ -70,7 +70,7 @@ func calcPBGameStatusKey(status int32, index int64) []byte {
}
func calcPBGameStatusAndPlayerKey(status, player int32, value, index int64) []byte {
key := fmt.Sprintf("LODB-pokerbull-status:%d:%d:%d:%018d", status, player, value, index)
key := fmt.Sprintf("LODB-pokerbull-status:%d:%d:%015d:%018d", status, player, value, index)
return []byte(key)
}
......@@ -79,7 +79,7 @@ func calcPBGameStatusAndPlayerPrefix(status, player int32, value int64) []byte {
if value == 0 {
key = fmt.Sprintf("LODB-pokerbull-status:%d:%d:", status, player)
} else {
key = fmt.Sprintf("LODB-pokerbull-status:%d:%d:%d", status, player, value)
key = fmt.Sprintf("LODB-pokerbull-status:%d:%d:%015d", status, player, value)
}
return []byte(key)
......
......@@ -29,7 +29,6 @@ func (g *PokerBull) Query_QueryGameByAddr(in *pkt.QueryPBGameInfo) (types.Messag
if err != nil {
return nil, err
}
return gameIds, nil
}
......@@ -42,3 +41,49 @@ func (g *PokerBull) Query_QueryGameByStatus(in *pkt.QueryPBGameInfo) (types.Mess
return gameIds, nil
}
// Query_QueryGameByRound 查询某一回合游戏结果
func (g *PokerBull) Query_QueryGameByRound(in *pkt.QueryPBGameByRound) (types.Message, error) {
game, err := readGame(g.GetStateDB(), in.GetGameId())
if err != nil {
return nil, err
}
if in.Round > game.Round {
return nil, types.ErrInvalidParam
}
var roundPlayers []*pkt.PBPlayer
for _, player := range game.Players {
var isReady bool
if in.Round == game.Round {
isReady = player.Ready
} else {
isReady = false
}
roundPlayer := &pkt.PBPlayer{
Address: player.Address,
Ready: isReady,
}
roundPlayers = append(roundPlayers, roundPlayer)
}
var result *pkt.PBResult
if len(game.Results) < int(in.Round) {
result = nil
} else {
result = game.Results[in.Round-1]
}
gameInfo := &pkt.ReplyPBGameByRound{
GameId: game.GameId,
Status: game.Status,
Result: result,
IsWaiting: game.IsWaiting,
Value: game.Value,
Players: roundPlayers,
Return: (game.Value / types.Coin) * pkt.WinnerReturn,
}
return gameInfo, nil
}
......@@ -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;
//斗牛游戏内容
message PokerBull {
string gameId = 1; //默认是由创建这局游戏的txHash作为gameId
int32 status = 2; // Start 1 -> Continue 2 -> Quit 3
int64 startTime = 3; //开始时间
string startTxHash = 4; //游戏启动交易hash
int64 value = 5; //赌注
PBPoker poker = 6; //扑克牌
repeated PBPlayer players = 7; //玩家历史牌和结果集
int32 playerNum = 8; //玩家数
repeated PBResult results = 9; //游戏结果集
int64 index = 10; //索引
int64 prevIndex = 11; //上级索引
int64 quitTime = 12; //游戏结束时间
string quitTxHash = 13; //游戏结束交易hash
string dealerAddr = 14; //下局庄家地址
bool isWaiting = 15; //游戏是否处于等待状态
int32 preStatus = 16; //上一index的状态
string gameId = 1; //默认是由创建这局游戏的txHash作为gameId
int32 status = 2; // Start 1 -> Continue 2 -> Quit 3
int64 startTime = 3; //开始时间
string startTxHash = 4; //游戏启动交易hash
int64 value = 5; //赌注
PBPoker poker = 6; //扑克牌
repeated PBPlayer players = 7; //玩家历史牌和结果集
int32 playerNum = 8; //玩家数
repeated PBResult results = 9; //游戏结果集
int64 index = 10; //索引
int64 prevIndex = 11; //上级索引
int64 quitTime = 12; //游戏结束时间
string quitTxHash = 13; //游戏结束交易hash
string dealerAddr = 14; //下局庄家地址
bool isWaiting = 15; //游戏是否处于等待状态
int32 preStatus = 16; //上一index的状态
int32 round = 17; //当前游戏回合数
}
//一把牌
......@@ -111,6 +110,10 @@ message PBGameRecords {
repeated PBGameRecord records = 1;
}
message PBGameIndexRecords {
repeated PBGameIndexRecord records = 1;
}
message QueryPBGameInfo {
string gameId = 1;
string addr = 2;
......@@ -130,6 +133,23 @@ message ReplyPBGameList {
repeated PokerBull games = 1;
}
// QueryPBGameByRound 根据gameId和回合数查询某回合的游戏结果
message QueryPBGameByRound {
string gameId = 1;
int32 round = 2;
}
// ReplyPBGameByRound 某一回合游戏结果
message ReplyPBGameByRound {
string gameId = 1;
int32 status = 2;
PBResult result = 3;
bool isWaiting = 4;
int64 value = 5;
repeated PBPlayer players = 6;
int64 return = 7;
}
message ReceiptPBGame {
string gameId = 1;
int32 status = 2;
......@@ -140,7 +160,8 @@ message ReceiptPBGame {
int64 value = 7;
bool isWaiting = 8;
repeated string players = 9;
int32 preStatus = 10;
int32 preStatus = 10;
int32 round = 11;
}
message PBStartTxReq {
......@@ -164,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
}
......@@ -5,7 +5,7 @@
package rpc_test
import (
"strings"
"fmt"
"testing"
commonlog "github.com/33cn/chain33/common/log"
......@@ -41,45 +41,75 @@ func TestJRPCChannel(t *testing.T) {
{fn: testStartRawTxCmd},
{fn: testContinueRawTxCmd},
{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: testQueryGameByAddr},
{fn: testQueryGameByStatus},
{fn: testQueryGameByRound},
}
for index, testCase := range testCases {
err := testCase.fn(t, jrpcClient)
if err == nil {
continue
}
assert.NotEqualf(t, err, types.ErrActionNotSupport, "test index %d", index)
if strings.Contains(err.Error(), "rpc: can't find") {
assert.FailNowf(t, err.Error(), "test index %d", index)
}
assert.Equal(t, err, types.ErrNotFound, fmt.Sprint(index))
}
testCases = []struct {
fn func(*testing.T, *jsonclient.JSONClient) error
}{
{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 {
params := pty.PBStartTxReq{}
payload := &pty.PBGameStart{Value: 123}
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreateStartTx,
Payload: types.MustPBToJSON(payload),
}
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{}
payload := &pty.PBGameContinue{GameId: "123"}
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreateContinueTx,
Payload: types.MustPBToJSON(payload),
}
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{}
payload := &pty.PBGameQuit{GameId: "123"}
params := &rpctypes.CreateTxIn{
Execer: types.ExecName(pty.PokerBullX),
ActionName: pty.CreatequitTx,
Payload: types.MustPBToJSON(payload),
}
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,8 +119,41 @@ 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"
"github.com/33cn/plugin/plugin/dapp/pokerbull/executor"
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 > executor.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(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(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(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(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)
}
......@@ -4,6 +4,8 @@
package types
import "github.com/33cn/chain33/types"
//game action ty
const (
PBGameActionStart = iota + 1
......@@ -48,4 +50,35 @@ const (
FuncNameQueryGameByAddr = "QueryGameByAddr"
// FuncNameQueryGameByStatus 根据status查询game
FuncNameQueryGameByStatus = "QueryGameByStatus"
// FuncNameQueryGameByRound 查询某一回合游戏结果
FuncNameQueryGameByRound = "QueryGameByRound"
// CreateStartTx 创建开始交易
CreateStartTx = "Start"
// CreateContinueTx 创建继续交易
CreateContinueTx = "Continue"
// CreatequitTx 创建退出交易
CreatequitTx = "Quit"
)
const (
// ListDESC 降序
ListDESC = int32(0)
// DefaultCount 默认一次取多少条记录
DefaultCount = int32(20)
// MaxPlayerNum 最大玩家数
MaxPlayerNum = 5
// MinPlayValue 最小赌注
MinPlayValue = 10 * types.Coin
// DefaultStyle 默认游戏类型
DefaultStyle = PlayStyleDefault
// PlatformAddress 平台地址
PlatformAddress = "1PHtChNt3UcfssR7v7trKSk3WJtAWjKjjX"
// PlatformFee 平台佣金
PlatformFee = int64(0.005 * float64(types.Coin))
// DeveloperAddress 开发着地址
DeveloperAddress = "1D6RFZNp2rh6QdbcZ1d7RWuBUz61We6SD7"
// DeveloperFee 开发者佣金
DeveloperFee = int64(0.005 * float64(types.Coin))
// WinnerReturn 赢家回报率
WinnerReturn = types.Coin - DeveloperFee
)
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