Commit 09d43a1a authored by harrylee2015's avatar harrylee2015 Committed by vipwzw

rejust code for golint,vet,ineffassign

parent 1dd4066a
......@@ -6,6 +6,7 @@ package commands
import "github.com/spf13/cobra"
// Cmd The command line is not used.
func Cmd() *cobra.Command {
return nil
}
......@@ -26,3 +26,8 @@ status: Create 1 -> Match 2 -> Cancel 3 -> Close 4
//1. 我的所有赌局,按照状态进行分类 (按照地址查询)
//2. 系统所有正在进行的赌局 (按照时间进行排序)
*/
//game 的状态变化:
// staus == 1 (创建,开始猜拳游戏)
// status == 2 (匹配,参与)
// status == 3 (取消)
// status == 4 (Close的情况)
\ No newline at end of file
......@@ -9,21 +9,25 @@ import (
gt "github.com/33cn/plugin/plugin/dapp/game/types"
)
// Exec_Create Create game
func (g *Game) Exec_Create(payload *gt.GameCreate, tx *types.Transaction, index int) (*types.Receipt, error) {
action := NewAction(g, tx, index)
return action.GameCreate(payload)
}
// Exec_Cancel Cancel game
func (g *Game) Exec_Cancel(payload *gt.GameCancel, tx *types.Transaction, index int) (*types.Receipt, error) {
action := NewAction(g, tx, index)
return action.GameCancel(payload)
}
// Exec_Close Close game
func (g *Game) Exec_Close(payload *gt.GameClose, tx *types.Transaction, index int) (*types.Receipt, error) {
action := NewAction(g, tx, index)
return action.GameClose(payload)
}
// Exec_Match Match game
func (g *Game) Exec_Match(payload *gt.GameMatch, tx *types.Transaction, index int) (*types.Receipt, error) {
action := NewAction(g, tx, index)
return action.GameMatch(payload)
......
......@@ -9,6 +9,7 @@ import (
gt "github.com/33cn/plugin/plugin/dapp/game/types"
)
// roll back local db data
func (g *Game) execDelLocal(receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
if receiptData.GetTy() != types.ExecOk {
......@@ -28,18 +29,22 @@ func (g *Game) execDelLocal(receiptData *types.ReceiptData) (*types.LocalDBSet,
return dbSet, nil
}
// ExecDelLocal_Create roll back local db data for create
func (g *Game) ExecDelLocal_Create(payload *gt.GameCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execDelLocal(receiptData)
}
// ExecDelLocal_Cancel roll back local db data for cancel
func (g *Game) ExecDelLocal_Cancel(payload *gt.GameCancel, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execDelLocal(receiptData)
}
// ExecDelLocal_Close roll back local db data for close
func (g *Game) ExecDelLocal_Close(payload *gt.GameClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execDelLocal(receiptData)
}
// ExecDelLocal_Match roll back local db data for match
func (g *Game) ExecDelLocal_Match(payload *gt.GameMatch, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execDelLocal(receiptData)
}
......@@ -9,6 +9,7 @@ import (
gt "github.com/33cn/plugin/plugin/dapp/game/types"
)
// save receiptData to local db
func (g *Game) execLocal(receiptData *types.ReceiptData) (*types.LocalDBSet, error) {
dbSet := &types.LocalDBSet{}
if receiptData.GetTy() != types.ExecOk {
......@@ -28,18 +29,22 @@ func (g *Game) execLocal(receiptData *types.ReceiptData) (*types.LocalDBSet, err
return dbSet, nil
}
// ExecLocal_Create save receiptData for create
func (g *Game) ExecLocal_Create(payload *gt.GameCreate, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execLocal(receiptData)
}
// ExecLocal_Cancel save receiptData for cancel
func (g *Game) ExecLocal_Cancel(payload *gt.GameCancel, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execLocal(receiptData)
}
// ExecLocal_Close save receiptData for close
func (g *Game) ExecLocal_Close(payload *gt.GameClose, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execLocal(receiptData)
}
// ExecLocal_Match save receiptData for Match
func (g *Game) ExecLocal_Match(payload *gt.GameMatch, tx *types.Transaction, receiptData *types.ReceiptData, index int) (*types.LocalDBSet, error) {
return g.execLocal(receiptData)
}
......@@ -22,32 +22,36 @@ func init() {
ety.InitFuncList(types.ListMethod(&Game{}))
}
// Init register dapp
func Init(name string, sub []byte) {
drivers.Register(GetName(), newGame, types.GetDappFork(driverName, "Enable"))
}
// Game the game inherits all the attributes of the driverBase.
type Game struct {
drivers.DriverBase
}
func newGame() drivers.Driver {
t := &Game{}
t.SetChild(t)
t.SetExecutorType(types.LoadExecutorType(driverName))
return t
g := &Game{}
g.SetChild(g)
g.SetExecutorType(types.LoadExecutorType(driverName))
return g
}
// GetName get name
func GetName() string {
return newGame().GetName()
}
// GetDriverName get driver name
func (g *Game) GetDriverName() string {
return driverName
}
//更新索引
// update Index
func (g *Game) updateIndex(log *gt.ReceiptGame) (kvs []*types.KeyValue) {
//先保存本次Action产生的索引
// save the index generated by this action first.
kvs = append(kvs, addGameAddrIndex(log.Status, log.GameId, log.Addr, log.Index))
kvs = append(kvs, addGameStatusIndex(log.Status, log.GameId, log.Index))
if log.Status == gt.GameActionMatch {
......@@ -69,9 +73,9 @@ func (g *Game) updateIndex(log *gt.ReceiptGame) (kvs []*types.KeyValue) {
return kvs
}
//回滚索引
// rollback Index
func (g *Game) rollbackIndex(log *gt.ReceiptGame) (kvs []*types.KeyValue) {
//先删除本次Action产生的索引
kvs = append(kvs, delGameAddrIndex(log.Status, log.Addr, log.Index))
kvs = append(kvs, delGameStatusIndex(log.Status, log.Index))
......@@ -112,21 +116,21 @@ func calcGameAddrIndexPrefix(status int32, addr string) []byte {
key := fmt.Sprintf("LODB-game-addr:%d:%s:", status, addr)
return []byte(key)
}
func addGameStatusIndex(status int32, gameId string, index int64) *types.KeyValue {
func addGameStatusIndex(status int32, gameID string, index int64) *types.KeyValue {
kv := &types.KeyValue{}
kv.Key = calcGameStatusIndexKey(status, index)
record := &gt.GameRecord{
GameId: gameId,
GameId: gameID,
Index: index,
}
kv.Value = types.Encode(record)
return kv
}
func addGameAddrIndex(status int32, gameId, addr string, index int64) *types.KeyValue {
func addGameAddrIndex(status int32, gameID, addr string, index int64) *types.KeyValue {
kv := &types.KeyValue{}
kv.Key = calcGameAddrIndexKey(status, addr, index)
record := &gt.GameRecord{
GameId: gameId,
GameId: gameID,
Index: index,
}
kv.Value = types.Encode(record)
......@@ -146,19 +150,23 @@ func delGameAddrIndex(status int32, addr string, index int64) *types.KeyValue {
return kv
}
// ReplyGameList the data structure returned when querying the game list.
type ReplyGameList struct {
Games []*Game `json:"games"`
}
// ReplyGame the data structure returned when querying a single game.
type ReplyGame struct {
Game *Game `json:"game"`
}
func (c *Game) GetPayloadValue() types.Message {
// GetPayloadValue get payload value
func (g *Game) GetPayloadValue() types.Message {
return &gt.GameAction{}
}
func (c *Game) GetTypeMap() map[string]int32 {
// GetTypeMap get TypeMap
func (g *Game) GetTypeMap() map[string]int32 {
return map[string]int32{
"Create": gt.GameActionCreate,
"Match": gt.GameActionMatch,
......
This diff is collapsed.
......@@ -9,18 +9,22 @@ import (
gt "github.com/33cn/plugin/plugin/dapp/game/types"
)
// Query_QueryGameListByIds query game list by gameIDs
func (g *Game) Query_QueryGameListByIds(in *gt.QueryGameInfos) (types.Message, error) {
return Infos(g.GetStateDB(), in)
return QueryGameListByIds(g.GetStateDB(), in)
}
// Query_QueryGameListCount query game count by status and addr
func (g *Game) Query_QueryGameListCount(in *gt.QueryGameListCount) (types.Message, error) {
return QueryGameListCount(g.GetStateDB(), in)
}
// Query_QueryGameListByStatusAndAddr query game list by status and addr
func (g *Game) Query_QueryGameListByStatusAndAddr(in *gt.QueryGameListByStatusAndAddr) (types.Message, error) {
return List(g.GetLocalDB(), g.GetStateDB(), in)
}
// Query_QueryGameById query game by gameID
func (g *Game) Query_QueryGameById(in *gt.QueryGameInfo) (types.Message, error) {
game, err := readGame(g.GetStateDB(), in.GetGameId())
if err != nil {
......
......@@ -26,16 +26,18 @@ var (
ExecerGame = []byte(GameX)
)
// action name
const (
Action_CreateGame = "createGame"
Action_MatchGame = "matchGame"
Action_CancelGame = "cancelGame"
Action_CloseGame = "closeGame"
ActionCreateGame = "createGame"
ActionMatchGame = "matchGame"
ActionCancelGame = "cancelGame"
ActionCloseGame = "closeGame"
)
// query func name
const (
FuncName_QueryGameListByIds = "QueryGameListByIds"
FuncName_QueryGameListCount = "QueryGameListCount"
FuncName_QueryGameListByStatusAndAddr = "QueryGameListByStatusAndAddr"
FuncName_QueryGameById = "QueryGameById"
FuncNameQueryGameListByIds = "QueryGameListByIds"
FuncNameQueryGameListCount = "QueryGameListCount"
FuncNameQueryGameListByStatusAndAddr = "QueryGameListByStatusAndAddr"
FuncNameQueryGameByID = "QueryGameById"
)
......@@ -4,15 +4,16 @@
package types
import "errors"
import "fmt"
// some errors definition
var (
ErrGameCreateAmount = errors.New("You fill in more than the maximum number of games.")
ErrGameCancleAddr = errors.New("You don't have permission to cancel someone else's game.")
ErrGameCloseAddr = errors.New("The game time has not yet expired,You don't have permission to call yet.")
ErrGameTimeOut = errors.New("The game has expired.,You don't have permission to call.")
ErrGameMatchStatus = errors.New("can't join the game, the game has matched or finished!")
ErrGameMatch = errors.New("can't join the game, You can't match the game you created!")
ErrGameCancleStatus = errors.New("can't cancle the game, the game has matched!")
ErrGameCloseStatus = errors.New("can't close the game again, the game has finished!")
ErrGameCreateAmount = fmt.Errorf("%s","You fill in more than the maximum number of games.")
ErrGameCancleAddr = fmt.Errorf("%s","You don't have permission to cancel someone else's game.")
ErrGameCloseAddr = fmt.Errorf("%s","The game time has not yet expired,You don't have permission to call yet.")
ErrGameTimeOut = fmt.Errorf("%s","The game has expired.,You don't have permission to call.")
ErrGameMatchStatus = fmt.Errorf("%s","can't join the game, the game has matched or finished!")
ErrGameMatch = fmt.Errorf("%s","can't join the game, You can't match the game you created!")
ErrGameCancleStatus = fmt.Errorf("%s","can't cancle the game, the game has matched!")
ErrGameCloseStatus = fmt.Errorf("%s","can't close the game again, the game has finished!")
)
......@@ -4,6 +4,7 @@
package types
// GamePreCreateTx pre create game,unused
type GamePreCreateTx struct {
//Secret string `json:"secret"`
//下注必须时偶数,不能时级数
......@@ -14,59 +15,23 @@ type GamePreCreateTx struct {
Fee int64 `json:"fee"`
}
// GamePreMatchTx pre match game,unused
type GamePreMatchTx struct {
GameId string `json:"gameId"`
GameID string `json:"game_i_d"`
Guess int32 `json:"guess"`
Fee int64 `json:"fee"`
}
// GamePreCancelTx pre cancel tx,unused
type GamePreCancelTx struct {
GameId string `json:"gameId"`
GameID string `json:"game_i_d"`
Fee int64 `json:"fee"`
}
// GamePreCloseTx pre close game, unused
type GamePreCloseTx struct {
GameId string `json:"gameId"`
GameID string `json:"game_i_d"`
Secret string `json:"secret"`
Result int32 `json:"result"`
Fee int64 `json:"fee"`
}
type GameData struct {
// 默认是由创建这局游戏的txHash作为gameId
GameId string `json:"gameId"`
// create 1 -> Match 2 -> Cancel 3 -> Close 4 Pending 5 //表示有人参与游戏,但还未打包
Status int32 `json:"status"`
// 创建时间
CreateTime int64 `json:"createTime"`
// 匹配时间(何时参与对赌)
MatchTime int64 `json:"matchTime"`
// 状态close的时间(包括cancel)
Closetime int64 `json:"closetime"`
// 赌注
Value int64 `json:"value"`
// 发起者账号地址
CreateAddress string `json:"createAddress"`
// 对赌者账号地址
MatchAddress string `json:"matchAddress"`
// hash 类型,预留字段
HashType string `json:"hashType"`
// 庄家创建游戏时,庄家自己出拳结果加密后的hash值
HashValue []byte `json:"hashValue"`
// 用来公布庄家出拳结果的私钥
Secret string `json:"secret"`
// 1平局,2 庄家获胜,3 matcher获胜,4 庄家开奖超时,matcher获胜,并获得本局所有赌资
Result int32 `json:"result"`
// matcher 出拳结果
MatcherGuess int32 `json:"matcherGuess"`
// create txHash
CreateTxHash string `json:"createTxHash"`
// matche交易hash
MatchTxHash string `json:"matchTxHash"`
// close txhash
CloseTxHash string `json:"closeTxHash"`
// cancel txhash
CancelTxHash string `json:"cancelTxHash"`
CreatorGuess int32 `json:"creatorGuess"`
Index int64 `json:"index"`
}
......@@ -30,18 +30,20 @@ func getRealExecName(paraName string) string {
return types.ExecName(paraName + GameX)
}
// NewType new type
func NewType() *GameType {
c := &GameType{}
c.SetChild(c)
return c
}
// exec
// GameType execType
type GameType struct {
types.ExecTypeBase
}
func (at *GameType) GetLogMap() map[int64]*types.LogInfo {
// GetLogMap get log
func (gt *GameType) GetLogMap() map[int64]*types.LogInfo {
return map[int64]*types.LogInfo{
TyLogCreateGame: {reflect.TypeOf(ReceiptGame{}), "LogLotteryCreate"},
TyLogCancleGame: {reflect.TypeOf(ReceiptGame{}), "LogCancleGame"},
......@@ -50,11 +52,13 @@ func (at *GameType) GetLogMap() map[int64]*types.LogInfo {
}
}
func (g *GameType) GetPayload() types.Message {
// GetPayload get payload
func (gt *GameType) GetPayload() types.Message {
return &GameAction{}
}
func (g *GameType) GetTypeMap() map[string]int32 {
// GetTypeMap get typeMap
func (gt *GameType) GetTypeMap() map[string]int32 {
return map[string]int32{
"Create": GameActionCreate,
"Cancel": GameActionCancel,
......@@ -63,11 +67,10 @@ func (g *GameType) GetTypeMap() map[string]int32 {
}
}
// TODO createTx接口暂时没法用,作为一个预留接口
func (game GameType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
// CreateTx unused,just empty implementation
func (gt GameType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) {
tlog.Debug("Game.CreateTx", "action", action)
var tx *types.Transaction
if action == Action_CreateGame {
if action == ActionCreateGame {
var param GamePreCreateTx
err := json.Unmarshal(message, &param)
if err != nil {
......@@ -76,7 +79,7 @@ func (game GameType) CreateTx(action string, message json.RawMessage) (*types.Tr
}
return CreateRawGamePreCreateTx(&param)
} else if action == Action_MatchGame {
} else if action == ActionMatchGame {
var param GamePreMatchTx
err := json.Unmarshal(message, &param)
if err != nil {
......@@ -85,7 +88,7 @@ func (game GameType) CreateTx(action string, message json.RawMessage) (*types.Tr
}
return CreateRawGamePreMatchTx(&param)
} else if action == Action_CancelGame {
} else if action == ActionCancelGame {
var param GamePreCancelTx
err := json.Unmarshal(message, &param)
if err != nil {
......@@ -94,7 +97,7 @@ func (game GameType) CreateTx(action string, message json.RawMessage) (*types.Tr
}
return CreateRawGamePreCancelTx(&param)
} else if action == Action_CloseGame {
} else if action == ActionCloseGame {
var param GamePreCloseTx
err := json.Unmarshal(message, &param)
if err != nil {
......@@ -103,13 +106,11 @@ func (game GameType) CreateTx(action string, message json.RawMessage) (*types.Tr
}
return CreateRawGamePreCloseTx(&param)
} else {
return nil, types.ErrNotSupport
}
return tx, nil
return nil, types.ErrNotSupport
}
// CreateRawGamePreCreateTx unused,just empty implementation
func CreateRawGamePreCreateTx(parm *GamePreCreateTx) (*types.Transaction, error) {
if parm == nil {
tlog.Error("CreateRawGamePreCreateTx", "parm", parm)
......@@ -139,13 +140,14 @@ func CreateRawGamePreCreateTx(parm *GamePreCreateTx) (*types.Transaction, error)
return tx, nil
}
// CreateRawGamePreMatchTx unused,just empty implementation
func CreateRawGamePreMatchTx(parm *GamePreMatchTx) (*types.Transaction, error) {
if parm == nil {
return nil, types.ErrInvalidParam
}
v := &GameMatch{
GameId: parm.GameId,
GameId: parm.GameID,
Guess: parm.Guess,
}
game := &GameAction{
......@@ -166,12 +168,13 @@ func CreateRawGamePreMatchTx(parm *GamePreMatchTx) (*types.Transaction, error) {
return tx, nil
}
// CreateRawGamePreCancelTx unused,just empty implementation
func CreateRawGamePreCancelTx(parm *GamePreCancelTx) (*types.Transaction, error) {
if parm == nil {
return nil, types.ErrInvalidParam
}
v := &GameCancel{
GameId: parm.GameId,
GameId: parm.GameID,
}
cancel := &GameAction{
Ty: GameActionCancel,
......@@ -191,13 +194,13 @@ func CreateRawGamePreCancelTx(parm *GamePreCancelTx) (*types.Transaction, error)
return tx, nil
}
//CreateRawGamePreCloseTx
// CreateRawGamePreCloseTx unused,just empty implementation
func CreateRawGamePreCloseTx(parm *GamePreCloseTx) (*types.Transaction, error) {
if parm == nil {
return nil, types.ErrInvalidParam
}
v := &GameClose{
GameId: parm.GameId,
GameId: parm.GameID,
Secret: parm.Secret,
}
close := &GameAction{
......
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