github.com/loomnetwork/gamechain@v0.0.0-20200406110549-36c47eb97a92/cli/cmd/end_match.go (about)

     1  package cmd
     2  
     3  import (
     4  	"encoding/json"
     5  	"errors"
     6  	"fmt"
     7  	"github.com/loomnetwork/gamechain/types/zb/zb_calls"
     8  	"strings"
     9  
    10  	"github.com/loomnetwork/go-loom/auth"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  var endMatchCmdArgs struct {
    15  	userID                 string
    16  	matchID                int64
    17  	winnerID               string
    18  	playerMatchExperiences *[]int64
    19  }
    20  
    21  var endMatchCmd = &cobra.Command{
    22  	Use:   "end_match",
    23  	Short: "end match for zombiebattleground",
    24  	RunE: func(cmd *cobra.Command, args []string) error {
    25  		if len(*endMatchCmdArgs.playerMatchExperiences) != 2 {
    26  			return errors.New("'playerMatchExperience' length must be 2")
    27  		}
    28  
    29  		signer := auth.NewEd25519Signer(commonTxObjs.privateKey)
    30  		var req = zb_calls.EndMatchRequest{
    31  			UserId:           endMatchCmdArgs.userID,
    32  			MatchId:          endMatchCmdArgs.matchID,
    33  			WinnerId:         endMatchCmdArgs.winnerID,
    34  			MatchExperiences: *endMatchCmdArgs.playerMatchExperiences,
    35  		}
    36  		var resp zb_calls.EndMatchResponse
    37  
    38  		_, err := commonTxObjs.contract.Call("EndMatch", &req, signer, &resp)
    39  		if err != nil {
    40  			return err
    41  		}
    42  
    43  		switch strings.ToLower(rootCmdArgs.outputFormat) {
    44  		case "json":
    45  			output, err := json.Marshal(map[string]interface{}{"success": true})
    46  			if err != nil {
    47  				return err
    48  			}
    49  			fmt.Println(string(output))
    50  		default:
    51  			fmt.Printf("end match %v successfully\n", req.MatchId)
    52  		}
    53  
    54  		return nil
    55  	},
    56  }
    57  
    58  func init() {
    59  	endMatchCmdArgs.playerMatchExperiences = &[]int64{0, 0}
    60  
    61  	rootCmd.AddCommand(endMatchCmd)
    62  	endMatchCmd.Flags().StringVarP(&endMatchCmdArgs.userID, "userId", "u", "loom", "UserId of account")
    63  	endMatchCmd.Flags().Int64VarP(&endMatchCmdArgs.matchID, "matchId", "m", 0, "Match ID")
    64  	endMatchCmd.Flags().StringVar(&endMatchCmdArgs.winnerID, "winnerId", "loom", "Winner ID")
    65  	endMatchCmd.Flags().Int64SliceVarP(endMatchCmdArgs.playerMatchExperiences, "playerMatchExperience", "p", []int64{0, 0},  "Players Match Experiences")
    66  }