package types import ( "encoding/json" "reflect" log "github.com/inconshreveable/log15" "gitlab.33.cn/chain33/chain33/common/address" "gitlab.33.cn/chain33/chain33/types" ) var tlog = log.New("module", GameX) func init() { // init executor type types.AllowUserExec = append(types.AllowUserExec, []byte(GameX)) types.RegistorExecutor(GameX, NewType()) types.RegisterDappFork(GameX, "Enable", 0) } //getRealExecName //如果paraName == "", 那么自动用 types.ExecName("game") //如果设置了paraName , 那么强制用paraName //也就是说,我们可以构造其他平行链的交易 func getRealExecName(paraName string) string { return types.ExecName(paraName + GameX) } func NewType() *GameType { c := &GameType{} c.SetChild(c) return c } // exec type GameType struct { types.ExecTypeBase } func (at *GameType) GetLogMap() map[int64]*types.LogInfo { return map[int64]*types.LogInfo{ TyLogCreateGame: {reflect.TypeOf(ReceiptGame{}), "LogLotteryCreate"}, TyLogCancleGame: {reflect.TypeOf(ReceiptGame{}), "LogCancleGame"}, TyLogMatchGame: {reflect.TypeOf(ReceiptGame{}), "LogMatchGame"}, TyLogCloseGame: {reflect.TypeOf(ReceiptGame{}), "LogCloseGame"}, } } func (g *GameType) GetPayload() types.Message { return &GameAction{} } func (g *GameType) GetTypeMap() map[string]int32 { return map[string]int32{ "Create": GameActionCreate, "Cancel": GameActionCancel, "Close": GameActionClose, "Match": GameActionMatch, } } // TODO createTx接口暂时没法用,作为一个预留接口 func (game GameType) CreateTx(action string, message json.RawMessage) (*types.Transaction, error) { tlog.Debug("Game.CreateTx", "action", action) var tx *types.Transaction if action == Action_CreateGame { var param GamePreCreateTx err := json.Unmarshal(message, ¶m) if err != nil { tlog.Error("CreateTx", "Error", err) return nil, types.ErrInvalidParam } return CreateRawGamePreCreateTx(¶m) } else if action == Action_MatchGame { var param GamePreMatchTx err := json.Unmarshal(message, ¶m) if err != nil { tlog.Error("CreateTx", "Error", err) return nil, types.ErrInvalidParam } return CreateRawGamePreMatchTx(¶m) } else if action == Action_CancelGame { var param GamePreCancelTx err := json.Unmarshal(message, ¶m) if err != nil { tlog.Error("CreateTx", "Error", err) return nil, types.ErrInvalidParam } return CreateRawGamePreCancelTx(¶m) } else if action == Action_CloseGame { var param GamePreCloseTx err := json.Unmarshal(message, ¶m) if err != nil { tlog.Error("CreateTx", "Error", err) return nil, types.ErrInvalidParam } return CreateRawGamePreCloseTx(¶m) } else { return nil, types.ErrNotSupport } return tx, nil } func CreateRawGamePreCreateTx(parm *GamePreCreateTx) (*types.Transaction, error) { if parm == nil { tlog.Error("CreateRawGamePreCreateTx", "parm", parm) return nil, types.ErrInvalidParam } v := &GameCreate{ Value: parm.Amount, HashType: parm.HashType, HashValue: parm.HashValue, } precreate := &GameAction{ Ty: GameActionCreate, Value: &GameAction_Create{v}, } tx := &types.Transaction{ Execer: []byte(getRealExecName(types.GetParaName())), Payload: types.Encode(precreate), Fee: parm.Fee, To: address.ExecAddress(getRealExecName(types.GetParaName())), } name := getRealExecName(types.GetParaName()) tx, err := types.FormatTx(name, tx) if err != nil { return nil, err } return tx, nil } func CreateRawGamePreMatchTx(parm *GamePreMatchTx) (*types.Transaction, error) { if parm == nil { return nil, types.ErrInvalidParam } v := &GameMatch{ GameId: parm.GameId, Guess: parm.Guess, } game := &GameAction{ Ty: GameActionMatch, Value: &GameAction_Match{v}, } tx := &types.Transaction{ Execer: []byte(getRealExecName(types.GetParaName())), Payload: types.Encode(game), Fee: parm.Fee, To: address.ExecAddress(getRealExecName(types.GetParaName())), } name := getRealExecName(types.GetParaName()) tx, err := types.FormatTx(name, tx) if err != nil { return nil, err } return tx, nil } func CreateRawGamePreCancelTx(parm *GamePreCancelTx) (*types.Transaction, error) { if parm == nil { return nil, types.ErrInvalidParam } v := &GameCancel{ GameId: parm.GameId, } cancel := &GameAction{ Ty: GameActionCancel, Value: &GameAction_Cancel{v}, } tx := &types.Transaction{ Execer: []byte(getRealExecName(types.GetParaName())), Payload: types.Encode(cancel), Fee: parm.Fee, To: address.ExecAddress(getRealExecName(types.GetParaName())), } name := getRealExecName(types.GetParaName()) tx, err := types.FormatTx(name, tx) if err != nil { return nil, err } return tx, nil } //CreateRawGamePreCloseTx func CreateRawGamePreCloseTx(parm *GamePreCloseTx) (*types.Transaction, error) { if parm == nil { return nil, types.ErrInvalidParam } v := &GameClose{ GameId: parm.GameId, Secret: parm.Secret, } close := &GameAction{ Ty: GameActionClose, Value: &GameAction_Close{v}, } tx := &types.Transaction{ Execer: []byte(getRealExecName(types.GetParaName())), Payload: types.Encode(close), Fee: parm.Fee, To: address.ExecAddress(getRealExecName(types.GetParaName())), } name := getRealExecName(types.GetParaName()) tx, err := types.FormatTx(name, tx) if err != nil { return nil, err } return tx, nil }