Commit 0c401767 authored by pengjun's avatar pengjun Committed by vipwzw

pokerbull代码的linter告警整改

parent f2a39b71
......@@ -14,6 +14,7 @@ import (
"github.com/spf13/cobra"
)
// PokerBullCmd 斗牛游戏命令行
func PokerBullCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "pokerbull",
......@@ -31,6 +32,7 @@ func PokerBullCmd() *cobra.Command {
return cmd
}
// PokerBullStartRawTxCmd 生成开始交易命令行
func PokerBullStartRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "start",
......@@ -69,6 +71,7 @@ func pokerbullStart(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// PokerBullContinueRawTxCmd 生成继续游戏命令行
func PokerBullContinueRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "continue",
......@@ -101,6 +104,7 @@ func pokerbullContinue(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// PokerBullQuitRawTxCmd 生成继续游戏命令行
func PokerBullQuitRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "quit",
......@@ -133,6 +137,7 @@ func pokerbullQuit(cmd *cobra.Command, args []string) {
ctx.RunWithoutMarshal()
}
// PokerBullQueryResultRawTxCmd 查询命令行
func PokerBullQueryResultRawTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "query",
......@@ -171,22 +176,22 @@ func pokerbullQuery(cmd *cobra.Command, args []string) {
}
params.Payload = req
if gameID != "" {
params.FuncName = pkt.FuncName_QueryGameById
params.FuncName = pkt.FuncNameQueryGameById
var res pkt.ReplyPBGame
ctx := jsonrpc.NewRpcCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else if address != "" {
params.FuncName = pkt.FuncName_QueryGameByAddr
params.FuncName = pkt.FuncNameQueryGameByAddr
var res pkt.PBGameRecords
ctx := jsonrpc.NewRpcCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else if statusStr != "" {
params.FuncName = pkt.FuncName_QueryGameByStatus
params.FuncName = pkt.FuncNameQueryGameByStatus
var res pkt.PBGameRecords
ctx := jsonrpc.NewRpcCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run()
} else if gameIDs != "" {
params.FuncName = pkt.FuncName_QueryGameListByIds
params.FuncName = pkt.FuncNameQueryGameListByIds
var gameIDsS []string
gameIDsS = append(gameIDsS, gameIDs)
gameIDsS = append(gameIDsS, gameIDs)
......
......@@ -9,16 +9,19 @@ import (
pkt "github.com/33cn/plugin/plugin/dapp/pokerbull/types"
)
// Exec_Start 开始游戏交易执行
func (c *PokerBull) Exec_Start(payload *pkt.PBGameStart, tx *types.Transaction, index int) (*types.Receipt, error) {
action := NewAction(c, tx, index)
return action.GameStart(payload)
}
// Exec_Continue 继续游戏交易执行
func (c *PokerBull) Exec_Continue(payload *pkt.PBGameContinue, tx *types.Transaction, index int) (*types.Receipt, error) {
action := NewAction(c, tx, index)
return action.GameContinue(payload)
}
// Exec_Quit 退出游戏交易执行
func (c *PokerBull) Exec_Quit(payload *pkt.PBGameQuit, tx *types.Transaction, index int) (*types.Receipt, error) {
action := NewAction(c, tx, index)
return action.GameQuit(payload)
......
......@@ -42,14 +42,17 @@ func (g *PokerBull) execDelLocal(receiptData *types.ReceiptData) (*types.LocalDB
return dbSet, nil
}
// ExecDelLocal_Start 开始游戏交易回滚
func (g *PokerBull) ExecDelLocal_Start(payload *pkt.PBGameStart, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execDelLocal(receiptData)
}
// ExecDelLocal_Continue 继续游戏交易回滚
func (g *PokerBull) ExecDelLocal_Continue(payload *pkt.PBGameContinue, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execDelLocal(receiptData)
}
// ExecDelLocal_Quit 退出游戏交易回滚
func (g *PokerBull) ExecDelLocal_Quit(payload *pkt.PBGameQuit, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execDelLocal(receiptData)
}
......@@ -73,14 +73,17 @@ func (c *PokerBull) execLocal(receipt *types.ReceiptData) (*types.LocalDBSet, er
return dbSet, nil
}
// ExecLocal_Start 开始游戏交易local执行
func (c *PokerBull) ExecLocal_Start(payload *pkt.PBGameStart, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(receiptData)
}
// ExecLocal_Continue 继续游戏交易local执行
func (c *PokerBull) ExecLocal_Continue(payload *pkt.PBGameContinue, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(receiptData)
}
// ExecLocal_Quit 退出游戏交易local执行
func (c *PokerBull) ExecLocal_Quit(payload *pkt.PBGameQuit, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return c.execLocal(receiptData)
}
......@@ -12,58 +12,67 @@ import (
"github.com/33cn/plugin/plugin/dapp/pokerbull/types"
)
var POKER_CARD_NUM = 52 //4 * 13 不带大小王
var COLOR_OFFSET uint32 = 8
var COLOR_BIT_MAST = 0xFF
var COLOR_NUM = 4
var CARD_NUM_PER_COLOR = 13
var CARD_NUM_PER_GAME = 5
// 牌数,4 * 13 不带大小王
var PokerCardNum = 52
// 牌花色偏移
var ColorOffset uint32 = 8
// 牌花色bit掩码
var ColorBitMask = 0xFF
// 每种花色的牌数
var CardNumPerColor = 13
// 一手牌的牌数
var CardNumPerGame = 5
const (
POKERBULL_RESULT_X1 = 1
POKERBULL_RESULT_X2 = 2
POKERBULL_RESULT_X3 = 3
POKERBULL_RESULT_X4 = 4
POKERBULL_RESULT_X5 = 5
POKERBULL_LEVERAGE_MAX = POKERBULL_RESULT_X1
PokerbullResultX1 = 1
PokerbullResultX2 = 2
PokerbullResultX3 = 3
PokerbullResultX4 = 4
PokerbullResultX5 = 5
PokerbullLeverageMax = PokerbullResultX1
)
// NewPoker 创建一副牌
func NewPoker() *types.PBPoker {
poker := new(types.PBPoker)
poker.Cards = make([]int32, POKER_CARD_NUM)
poker.Pointer = int32(POKER_CARD_NUM - 1)
poker.Cards = make([]int32, PokerCardNum)
poker.Pointer = int32(PokerCardNum - 1)
for i := 0; i < POKER_CARD_NUM; i++ {
color := i / CARD_NUM_PER_COLOR
num := i%CARD_NUM_PER_COLOR + 1
poker.Cards[i] = int32(color<<COLOR_OFFSET + num)
for i := 0; i < PokerCardNum; i++ {
color := i / CardNumPerColor
num := i%CardNumPerColor + 1
poker.Cards[i] = int32(color<<ColorOffset + num)
}
return poker
}
// 洗牌
// Shuffle 洗牌
func Shuffle(poker *types.PBPoker, rng int64) {
rndn := rand.New(rand.NewSource(rng))
for i := 0; i < POKER_CARD_NUM; i++ {
idx := rndn.Intn(POKER_CARD_NUM - 1)
for i := 0; i < PokerCardNum; i++ {
idx := rndn.Intn(PokerCardNum - 1)
tmpV := poker.Cards[idx]
poker.Cards[idx] = poker.Cards[POKER_CARD_NUM-i-1]
poker.Cards[POKER_CARD_NUM-i-1] = tmpV
poker.Cards[idx] = poker.Cards[PokerCardNum-i-1]
poker.Cards[PokerCardNum-i-1] = tmpV
}
poker.Pointer = int32(POKER_CARD_NUM - 1)
poker.Pointer = int32(PokerCardNum - 1)
}
// 发牌
// Deal 发牌
func Deal(poker *types.PBPoker, rng int64) []int32 {
if poker.Pointer < int32(CARD_NUM_PER_GAME) {
logger.Error(fmt.Sprintf("Wait to be shuffled: deal cards [%d], left [%d]", CARD_NUM_PER_GAME, poker.Pointer+1))
if poker.Pointer < int32(CardNumPerGame) {
logger.Error(fmt.Sprintf("Wait to be shuffled: deal cards [%d], left [%d]", CardNumPerGame, poker.Pointer+1))
Shuffle(poker, rng+int64(poker.Cards[0]))
}
rndn := rand.New(rand.NewSource(rng))
res := make([]int32, CARD_NUM_PER_GAME)
for i := 0; i < CARD_NUM_PER_GAME; i++ {
res := make([]int32, CardNumPerGame)
for i := 0; i < CardNumPerGame; i++ {
idx := rndn.Intn(int(poker.Pointer))
tmpV := poker.Cards[poker.Pointer]
res[i] = poker.Cards[idx]
......@@ -75,7 +84,7 @@ func Deal(poker *types.PBPoker, rng int64) []int32 {
return res
}
// 计算斗牛结果
// Result 计算斗牛结果
func Result(cards []int32) int32 {
temp := 0
r := -1 //是否有牛标志
......@@ -125,45 +134,45 @@ func Result(cards []int32) int32 {
return int32(result[0])
}
// 计算结果倍数
// Leverage 计算结果倍数
func Leverage(hand *types.PBHand) int32 {
result := hand.Result
// 小牛 [1, 6]
if result < 7 {
return POKERBULL_RESULT_X1
return PokerbullResultX1
}
// 大牛 [7, 9]
if result >= 7 && result < 10 {
return POKERBULL_RESULT_X2
return PokerbullResultX2
}
var flowers = 0
if result == 10 {
for _, card := range hand.Cards {
if (int(card) & COLOR_BIT_MAST) > 10 {
if (int(card) & ColorBitMask) > 10 {
flowers++
}
}
// 牛牛
if flowers < 4 {
return POKERBULL_RESULT_X3
return PokerbullResultX3
}
// 四花
if flowers == 4 {
return POKERBULL_RESULT_X4
return PokerbullResultX4
}
// 五花
if flowers == 5 {
return POKERBULL_RESULT_X5
return PokerbullResultX5
}
}
return POKERBULL_RESULT_X1
return PokerbullResultX1
}
type pokerCard struct {
......@@ -193,14 +202,15 @@ func (p colorCardSlice) Less(i, j int) bool {
func newcolorCard(a []int32) colorCardSlice {
var cardS []*pokerCard
for i := 0; i < len(a); i++ {
num := int(a[i]) & COLOR_BIT_MAST
color := int(a[i]) >> COLOR_OFFSET
num := int(a[i]) & ColorBitMask
color := int(a[i]) >> ColorOffset
cardS = append(cardS, &pokerCard{num, color})
}
return cardS
}
// CompareResult 两手牌比较结果
func CompareResult(i, j *types.PBHand) bool {
if i.Result < j.Result {
return true
......@@ -213,6 +223,7 @@ func CompareResult(i, j *types.PBHand) bool {
return false
}
// Compare 比较两手牌的斗牛结果
func Compare(a []int32, b []int32) bool {
cardA := newcolorCard(a)
cardB := newcolorCard(b)
......
......@@ -15,6 +15,7 @@ import (
var logger = log.New("module", "execs.pokerbull")
// Init 执行器初始化
func Init(name string, sub []byte) {
drivers.Register(newPBGame().GetName(), newPBGame, types.GetDappFork(driverName, "Enable"))
}
......@@ -26,6 +27,7 @@ func init() {
ety.InitFuncList(types.ListMethod(&PokerBull{}))
}
// PokerBull 斗牛执行器结构
type PokerBull struct {
drivers.DriverBase
}
......@@ -37,10 +39,12 @@ func newPBGame() drivers.Driver {
return t
}
// GetName 获取斗牛执行器名
func GetName() string {
return newPBGame().GetName()
}
// GetDriverName 获取斗牛执行器名
func (g *PokerBull) GetDriverName() string {
return pkt.PokerBullX
}
......@@ -118,11 +122,11 @@ func delPBGameAddrIndexKey(addr string, index int64) *types.KeyValue {
return kv
}
func addPBGameStatusAndPlayer(status int32, player int32, value, index int64, gameId string) *types.KeyValue {
func addPBGameStatusAndPlayer(status int32, player int32, value, index int64, gameID string) *types.KeyValue {
kv := &types.KeyValue{}
kv.Key = calcPBGameStatusAndPlayerKey(status, player, value, index)
record := &pkt.PBGameIndexRecord{
GameId: gameId,
GameId: gameID,
Index: index,
}
kv.Value = types.Encode(record)
......
......@@ -9,10 +9,12 @@ import (
pkt "github.com/33cn/plugin/plugin/dapp/pokerbull/types"
)
// Query_QueryGameListByIds 根据id列表查询游戏
func (g *PokerBull) Query_QueryGameListByIds(in *pkt.QueryPBGameInfos) (types.Message, error) {
return Infos(g.GetStateDB(), in)
}
// Query_QueryGameById 根据id查询游戏
func (g *PokerBull) Query_QueryGameById(in *pkt.QueryPBGameInfo) (types.Message, error) {
game, err := readGame(g.GetStateDB(), in.GetGameId())
if err != nil {
......@@ -21,6 +23,7 @@ func (g *PokerBull) Query_QueryGameById(in *pkt.QueryPBGameInfo) (types.Message,
return &pkt.ReplyPBGame{game}, nil
}
// Query_QueryGameByAddr 根据地址查询游戏
func (g *PokerBull) Query_QueryGameByAddr(in *pkt.QueryPBGameInfo) (types.Message, error) {
gameIds, err := getGameListByAddr(g.GetLocalDB(), in.Addr, in.Index)
if err != nil {
......@@ -30,6 +33,7 @@ func (g *PokerBull) Query_QueryGameByAddr(in *pkt.QueryPBGameInfo) (types.Messag
return gameIds, nil
}
// Query_QueryGameByStatus 根据状态查询游戏
func (g *PokerBull) Query_QueryGameByStatus(in *pkt.QueryPBGameInfo) (types.Message, error) {
gameIds, err := getGameListByStatus(g.GetLocalDB(), in.Status, in.Index)
if err != nil {
......
......@@ -12,6 +12,7 @@ import (
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
......@@ -28,6 +29,7 @@ func (c *Jrpc) PokerBullStartTx(parm *pb.PBStartTxReq, result *interface{}) erro
return nil
}
// PokerBullContinueTx 创建游戏继续交易
func (c *Jrpc) PokerBullContinueTx(parm *pb.PBContinueTxReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
......@@ -46,6 +48,7 @@ func (c *Jrpc) PokerBullContinueTx(parm *pb.PBContinueTxReq, result *interface{}
return nil
}
// PokerBullQuitTx 创建游戏推出交易
func (c *Jrpc) PokerBullQuitTx(parm *pb.PBQuitTxReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
......@@ -63,6 +66,7 @@ func (c *Jrpc) PokerBullQuitTx(parm *pb.PBQuitTxReq, result *interface{}) error
return nil
}
// PokerBullQueryTx 创建游戏查询交易
func (c *Jrpc) PokerBullQueryTx(parm *pb.PBQueryReq, result *interface{}) error {
if parm == nil {
return types.ErrInvalidParam
......
......@@ -14,7 +14,7 @@ import (
)
func (c *channelClient) Start(ctx context.Context, head *pb.PBGameStart) (*types.UnsignTx, error) {
if head.PlayerNum > executor.MAX_PLAYER_NUM {
if head.PlayerNum > executor.MaxPlayerNum {
return nil, errors.New("Player number should be maximum 5")
}
......
// 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
type PokerBullStartTx struct {
Value int64 `json:"value"`
PlayerNum int32 `json:"playerNum"`
Fee int64 `json:"fee"`
}
type PBContinueTxReq struct {
GameId string `json:"gameId"`
Fee int64 `json:"fee"`
}
type PBQuitTxReq struct {
GameId string `json:"gameId"`
Fee int64 `json:"fee"`
}
type PBQueryReq struct {
GameId string `json:"GameId"`
Fee int64 `json:"fee"`
}
......@@ -8,10 +8,12 @@ import (
"github.com/33cn/chain33/rpc/types"
)
// Jrpc jrpc句柄
type Jrpc struct {
cli *channelClient
}
// Grpc grpc句柄
type Grpc struct {
*channelClient
}
......@@ -20,6 +22,7 @@ type channelClient struct {
types.ChannelClient
}
// Init 初始化rpc
func Init(name string, s types.RPCServer) {
cli := &channelClient{}
grpc := &Grpc{channelClient: cli}
......
......@@ -13,12 +13,13 @@ const (
)
const (
// PlayStyleDefault 游戏类型
PlayStyleDefault = iota + 1
PlayStyleDealer
)
const (
//log for PBgame
// TyLogPBGameStart log for PBgame
TyLogPBGameStart = 721
TyLogPBGameContinue = 722
TyLogPBGameQuit = 723
......@@ -35,9 +36,10 @@ var (
)
const (
//查询方法名
FuncName_QueryGameListByIds = "QueryGameListByIds"
FuncName_QueryGameById = "QueryGameById"
FuncName_QueryGameByAddr = "QueryGameByAddr"
FuncName_QueryGameByStatus = "QueryGameByStatus"
// FuncNameQueryGameListByIds 根据id列表查询game列表
FuncNameQueryGameListByIds = "QueryGameListByIds"
// FuncNameQueryGameById 根据id查询game
FuncNameQueryGameById = "QueryGameById"
FuncNameQueryGameByAddr = "QueryGameByAddr"
FuncNameQueryGameByStatus = "QueryGameByStatus"
)
......@@ -17,21 +17,24 @@ func init() {
types.RegisterDappFork(PokerBullX, "Enable", 0)
}
// exec
// PokerBullType 斗牛执行器类型
type PokerBullType struct {
types.ExecTypeBase
}
// NewType 创建pokerbull执行器类型
func NewType() *PokerBullType {
c := &PokerBullType{}
c.SetChild(c)
return c
}
// GetPayload 获取payload
func (t *PokerBullType) GetPayload() types.Message {
return &PBGameAction{}
}
// GetTypeMap 获取类型map
func (t *PokerBullType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Start": PBGameActionStart,
......@@ -41,6 +44,7 @@ func (t *PokerBullType) GetTypeMap() map[string]int32 {
}
}
// GetLogMap 获取日志map
func (t *PokerBullType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{
TyLogPBGameStart: {reflect.TypeOf(ReceiptPBGame{}), "TyLogPBGameStart"},
......
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