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)
......
......@@ -19,15 +19,16 @@ import (
)
const (
// ListDESC 降序
ListDESC = int32(0)
ListASC = int32(1)
DefaultCount = int32(20) //默认一次取多少条记录
MAX_PLAYER_NUM = 5
MIN_PLAY_VALUE = 10 * types.Coin
MaxPlayerNum = 5
MinPlayValue = 10 * types.Coin
DefaultStyle = pkt.PlayStyleDefault
)
// Action 斗牛action结构
type Action struct {
coinsAccount *account.DB
db dbm.KV
......@@ -39,7 +40,7 @@ type Action struct {
localDB dbm.Lister
index int
}
// NewAction 创建action
func NewAction(pb *PokerBull, tx *types.Transaction, index int) *Action {
hash := tx.Hash()
fromaddr := tx.From()
......@@ -48,10 +49,11 @@ func NewAction(pb *PokerBull, tx *types.Transaction, index int) *Action {
pb.GetBlockTime(), pb.GetHeight(), dapp.ExecAddress(string(tx.Execer)), pb.GetLocalDB(), index}
}
// CheckExecAccountBalance 检查账户余额
func (action *Action) CheckExecAccountBalance(fromAddr string, ToFrozen, ToActive int64) bool {
// 赌注为零,按照最小赌注冻结
if ToFrozen == 0 {
ToFrozen = MIN_PLAY_VALUE
ToFrozen = MinPlayValue
}
acc := action.coinsAccount.LoadExecAccount(fromAddr, action.execaddr)
......@@ -61,6 +63,7 @@ func (action *Action) CheckExecAccountBalance(fromAddr string, ToFrozen, ToActiv
return false
}
// Key 获取key值
func Key(id string) (key []byte) {
key = append(key, []byte("mavl-"+types.ExecName(pkt.PokerBullX)+"-")...)
key = append(key, []byte(id)...)
......@@ -83,19 +86,7 @@ func readGame(db dbm.KV, id string) (*pkt.PokerBull, error) {
return &game, nil
}
//安全批量查询方式,防止因为脏数据导致查询接口奔溃
func GetGameList(db dbm.KV, values []string) []*pkt.PokerBull {
var games []*pkt.PokerBull
for _, value := range values {
game, err := readGame(db, value)
if err != nil {
continue
}
games = append(games, game)
}
return games
}
// Infos 获取gameinfo
func Infos(db dbm.KV, infos *pkt.QueryPBGameInfos) (types.Message, error) {
var games []*pkt.PokerBull
for i := 0; i < len(infos.GameIds); i++ {
......@@ -189,6 +180,7 @@ func (action *Action) getIndex(game *pkt.PokerBull) int64 {
return action.height*types.MaxTxsPerBlock + int64(action.index)
}
// GetReceiptLog 获取receipt日志
func (action *Action) GetReceiptLog(game *pkt.PokerBull) *types.ReceiptLog {
log := &types.ReceiptLog{}
r := &pkt.ReceiptPBGame{}
......@@ -343,7 +335,7 @@ func (action *Action) settleDealerAccount(lastAddress string, game *pkt.PokerBul
for _, hand := range result.Hands {
// 最后一名玩家没有冻结
if hand.Address != lastAddress {
receipt, err := action.coinsAccount.ExecActive(hand.Address, action.execaddr, game.GetValue()*POKERBULL_LEVERAGE_MAX)
receipt, err := action.coinsAccount.ExecActive(hand.Address, action.execaddr, game.GetValue()*PokerbullLeverageMax)
if err != nil {
logger.Error("GameSettleDealer.ExecActive", "addr", hand.Address, "execaddr", action.execaddr, "amount", game.GetValue(),
"err", err)
......@@ -391,10 +383,10 @@ func (action *Action) settleDefaultAccount(lastAddress string, game *pkt.PokerBu
for _, player := range game.Players {
// 最后一名玩家没有冻结
if player.Address != lastAddress {
receipt, err := action.coinsAccount.ExecActive(player.GetAddress(), action.execaddr, game.GetValue()*POKERBULL_LEVERAGE_MAX)
receipt, err := action.coinsAccount.ExecActive(player.GetAddress(), action.execaddr, game.GetValue()*PokerbullLeverageMax)
if err != nil {
logger.Error("GameSettleDefault.ExecActive", "addr", player.GetAddress(), "execaddr", action.execaddr,
"amount", game.GetValue()*POKERBULL_LEVERAGE_MAX, "err", err)
"amount", game.GetValue()*PokerbullLeverageMax, "err", err)
return nil, nil, err
}
logs = append(logs, receipt.Logs...)
......@@ -421,9 +413,8 @@ func (action *Action) settleDefaultAccount(lastAddress string, game *pkt.PokerBu
func (action *Action) settleAccount(lastAddress string, game *pkt.PokerBull) ([]*types.ReceiptLog, []*types.KeyValue, error) {
if DefaultStyle == pkt.PlayStyleDealer {
return action.settleDealerAccount(lastAddress, game)
} else {
return action.settleDefaultAccount(lastAddress, game)
}
return action.settleDefaultAccount(lastAddress, game)
}
func (action *Action) genTxRnd(txhash []byte) (int64, error) {
......@@ -453,28 +444,28 @@ func (action *Action) checkDupPlayerAddress(id string, pbPlayers []*pkt.PBPlayer
}
// 新建一局游戏
func (action *Action) newGame(gameId string, start *pkt.PBGameStart) (*pkt.PokerBull, error) {
func (action *Action) newGame(gameID string, start *pkt.PBGameStart) (*pkt.PokerBull, error) {
var game *pkt.PokerBull
// 不指定赌注,默认按照最低赌注
if start.GetValue() == 0 {
start.Value = MIN_PLAY_VALUE
start.Value = MinPlayValue
}
//TODO 庄家检查闲家数量倍数的资金
if DefaultStyle == pkt.PlayStyleDealer {
if !action.CheckExecAccountBalance(action.fromaddr, start.GetValue()*POKERBULL_LEVERAGE_MAX*int64(start.PlayerNum-1), 0) {
if !action.CheckExecAccountBalance(action.fromaddr, start.GetValue()*PokerbullLeverageMax*int64(start.PlayerNum-1), 0) {
logger.Error("GameStart", "addr", action.fromaddr, "execaddr", action.execaddr, "id",
gameId, "err", types.ErrNoBalance)
gameID, "err", types.ErrNoBalance)
return nil, types.ErrNoBalance
}
}
game = &pkt.PokerBull{
GameId: gameId,
GameId: gameID,
Status: pkt.PBGameActionStart,
StartTime: action.blocktime,
StartTxHash: gameId,
StartTxHash: gameID,
Value: start.GetValue(),
Poker: NewPoker(),
PlayerNum: start.PlayerNum,
......@@ -491,7 +482,7 @@ func (action *Action) newGame(gameId string, start *pkt.PBGameStart) (*pkt.Poker
// 筛选合适的牌局
func (action *Action) selectGameFromIds(ids []string, value int64) *pkt.PokerBull {
var gameRet *pkt.PokerBull = nil
var gameRet *pkt.PokerBull
for _, id := range ids {
game, err := action.readGame(id)
if err != nil {
......@@ -505,7 +496,7 @@ func (action *Action) selectGameFromIds(ids []string, value int64) *pkt.PokerBul
}
//选择合适赌注的游戏
if value == 0 && game.GetValue() != MIN_PLAY_VALUE {
if value == 0 && game.GetValue() != MinPlayValue {
if !action.CheckExecAccountBalance(action.fromaddr, game.GetValue(), 0) {
logger.Error("GameStart", "addr", action.fromaddr, "execaddr", action.execaddr, "id", id, "err", types.ErrNoBalance)
continue
......@@ -536,19 +527,20 @@ func (action *Action) checkPlayerExistInGame() bool {
return true
}
// GameStart 游戏开始
func (action *Action) GameStart(start *pkt.PBGameStart) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
if start.PlayerNum > MAX_PLAYER_NUM {
if start.PlayerNum > MaxPlayerNum {
logger.Error("GameStart", "addr", action.fromaddr, "execaddr", action.execaddr,
"err", fmt.Sprintf("The maximum player number is %d", MAX_PLAYER_NUM))
"err", fmt.Sprintf("The maximum player number is %d", MaxPlayerNum))
return nil, types.ErrInvalidParam
}
gameId := common.ToHex(action.txhash)
if !action.CheckExecAccountBalance(action.fromaddr, start.GetValue()*POKERBULL_LEVERAGE_MAX, 0) {
logger.Error("GameStart", "addr", action.fromaddr, "execaddr", action.execaddr, "id", gameId, "err", types.ErrNoBalance)
gameID := common.ToHex(action.txhash)
if !action.CheckExecAccountBalance(action.fromaddr, start.GetValue()*PokerbullLeverageMax, 0) {
logger.Error("GameStart", "addr", action.fromaddr, "execaddr", action.execaddr, "id", gameID, "err", types.ErrNoBalance)
return nil, types.ErrNoBalance
}
......@@ -557,14 +549,14 @@ func (action *Action) GameStart(start *pkt.PBGameStart) (*types.Receipt, error)
return nil, fmt.Errorf("Address is already in a game")
}
var game *pkt.PokerBull = nil
var game *pkt.PokerBull
ids, err := queryGameListByStatusAndPlayer(action.localDB, pkt.PBGameActionStart, start.PlayerNum, start.Value)
if err != nil || len(ids) == 0 {
if err != types.ErrNotFound {
return nil, err
}
game, err = action.newGame(gameId, start)
game, err = action.newGame(gameID, start)
if err != nil {
return nil, err
}
......@@ -572,7 +564,7 @@ func (action *Action) GameStart(start *pkt.PBGameStart) (*types.Receipt, error)
game = action.selectGameFromIds(ids, start.GetValue())
if game == nil {
// 如果也没有匹配到游戏,则按照最低赌注创建
game, err = action.newGame(gameId, start)
game, err = action.newGame(gameID, start)
if err != nil {
return nil, err
}
......@@ -607,7 +599,7 @@ func (action *Action) GameStart(start *pkt.PBGameStart) (*types.Receipt, error)
game.PreStatus = pkt.PBGameActionStart
game.IsWaiting = false
} else {
receipt, err := action.coinsAccount.ExecFrozen(action.fromaddr, action.execaddr, start.GetValue()*POKERBULL_LEVERAGE_MAX) //冻结子账户资金, 最后一位玩家不需要冻结
receipt, err := action.coinsAccount.ExecFrozen(action.fromaddr, action.execaddr, start.GetValue()*PokerbullLeverageMax) //冻结子账户资金, 最后一位玩家不需要冻结
if err != nil {
logger.Error("GameCreate.ExecFrozen", "addr", action.fromaddr, "execaddr", action.execaddr, "amount", start.GetValue(), "err", err.Error())
return nil, err
......@@ -641,6 +633,7 @@ func getPlayerFromAddress(players []*pkt.PBPlayer, addr string) *pkt.PBPlayer {
return nil
}
// GameContinue 继续游戏
func (action *Action) GameContinue(pbcontinue *pkt.PBGameContinue) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
......@@ -659,7 +652,7 @@ func (action *Action) GameContinue(pbcontinue *pkt.PBGameContinue) (*types.Recei
}
// 检查余额,庄家检查闲家数量倍数的资金
checkValue := game.GetValue() * POKERBULL_LEVERAGE_MAX
checkValue := game.GetValue() * PokerbullLeverageMax
if action.fromaddr == game.DealerAddr {
checkValue = checkValue * int64(game.PlayerNum-1)
}
......@@ -702,7 +695,7 @@ func (action *Action) GameContinue(pbcontinue *pkt.PBGameContinue) (*types.Recei
game.IsWaiting = false
game.PreStatus = pkt.PBGameActionContinue
} else {
receipt, err := action.coinsAccount.ExecFrozen(action.fromaddr, action.execaddr, game.GetValue()*POKERBULL_LEVERAGE_MAX) //冻结子账户资金,最后一位玩家不需要冻结
receipt, err := action.coinsAccount.ExecFrozen(action.fromaddr, action.execaddr, game.GetValue()*PokerbullLeverageMax) //冻结子账户资金,最后一位玩家不需要冻结
if err != nil {
logger.Error("GameCreate.ExecFrozen", "addr", action.fromaddr, "execaddr", action.execaddr, "amount", game.GetValue(), "err", err.Error())
return nil, err
......@@ -719,6 +712,7 @@ func (action *Action) GameContinue(pbcontinue *pkt.PBGameContinue) (*types.Recei
return &types.Receipt{types.ExecOk, kv, logs}, nil
}
// GameQuit 退出游戏
func (action *Action) GameQuit(pbend *pkt.PBGameQuit) (*types.Receipt, error) {
var logs []*types.ReceiptLog
var kv []*types.KeyValue
......@@ -734,7 +728,7 @@ func (action *Action) GameQuit(pbend *pkt.PBGameQuit) (*types.Receipt, error) {
if game.IsWaiting {
if game.Status == pkt.PBGameActionStart {
for _, player := range game.Players {
receipt, err := action.coinsAccount.ExecActive(player.Address, action.execaddr, game.GetValue()*POKERBULL_LEVERAGE_MAX)
receipt, err := action.coinsAccount.ExecActive(player.Address, action.execaddr, game.GetValue()*PokerbullLeverageMax)
if err != nil {
logger.Error("GameSettleDealer.ExecActive", "addr", player.Address, "execaddr", action.execaddr, "amount", game.GetValue(),
"err", err)
......@@ -749,7 +743,7 @@ func (action *Action) GameQuit(pbend *pkt.PBGameQuit) (*types.Receipt, error) {
continue
}
receipt, err := action.coinsAccount.ExecActive(player.Address, action.execaddr, game.GetValue()*POKERBULL_LEVERAGE_MAX)
receipt, err := action.coinsAccount.ExecActive(player.Address, action.execaddr, game.GetValue()*PokerbullLeverageMax)
if err != nil {
logger.Error("GameSettleDealer.ExecActive", "addr", player.Address, "execaddr", action.execaddr, "amount", game.GetValue(),
"err", err)
......@@ -774,6 +768,7 @@ func (action *Action) GameQuit(pbend *pkt.PBGameQuit) (*types.Receipt, error) {
return &types.Receipt{types.ExecOk, kv, logs}, nil
}
// HandSlice 一手牌
type HandSlice []*pkt.PBHand
func (h HandSlice) Len() int {
......
......@@ -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