コード例 #1
0
ファイル: games_controller.go プロジェクト: zannet/hangman
func (this GamesController) Show(ginContext *gin.Context) {
	game, err := models.Game{}.Find(this.Context.AeContext, ginContext.Params.ByName("id"), this.Player())

	game.Url = this.ResourceUrl(baseURL, game.Id)

	if err.Any() {
		this.RespondWith.UnexpectedError(err)
		return
	}

	this.RespondWith.JSON(http.StatusOK, game)
}
コード例 #2
0
ファイル: games_controller.go プロジェクト: zannet/hangman
func (this GamesController) Index(ginContext *gin.Context) {
	games, err := models.Game{}.FindAll(this.Context.AeContext, this.Player(), func(g *models.Game) {
		g.Url = this.ResourceUrl(baseURL, g.Id)
	})

	if err.Any() {
		this.RespondWith.UnexpectedError(err)
		return
	}

	this.RespondWith.JSON(http.StatusOK, games)
}
コード例 #3
0
ファイル: games_controller.go プロジェクト: zannet/hangman
func (this GamesController) Guess(ginContext *gin.Context) {
	err := models.Game{}.Guess(this.Context.AeContext, this.Player(), api.M{
		"id":    ginContext.Param("id"),
		"guess": this.Context.Body("char"),
	})

	if err.Any() {
		ginContext.AbortWithStatus(http.StatusNotModified)
		return
	}

	ginContext.AbortWithStatus(http.StatusNoContent)
}
コード例 #4
0
ファイル: games_controller.go プロジェクト: zannet/hangman
func (this GamesController) Create(ginContext *gin.Context) {
	game, err := models.Game{}.Create(this.Context.AeContext, this.Player())

	game.Url = this.ResourceUrl(baseURL, game.Id)

	if err.Any() {
		this.RespondWith.UnexpectedError(err)
		return
	}

	ginContext.Header("location", game.Url)
	ginContext.JSON(http.StatusCreated, game)
}
コード例 #5
0
ファイル: game_test.go プロジェクト: zannet/hangman
func TestValidGuess(t *testing.T) {
	expect := goexpect.New(t)

	game := models.Game{}.New()

	// Invalid guess

	expect(game.ValidGuess("1")).ToBe(false)
	expect(game.ValidGuess("as")).ToBe(false)
	expect(game.ValidGuess("[")).ToBe(false)
	expect(game.ValidGuess("A")).ToBe(false)

	// Valid guess

	expect(game.ValidGuess("a")).ToBe(true)
	expect(game.ValidGuess("z")).ToBe(true)
}
コード例 #6
0
ファイル: game_test.go プロジェクト: zannet/hangman
func TestGuessALetter(t *testing.T) {
	expect := goexpect.New(t)

	game := models.Game{}.New()

	// Make a correct guess

	existingLetter := string(game.Word[0])
	game.GuessALetter(existingLetter)

	expect(game.TriesLeft).ToBe(11)
	expect(strings.Contains(game.LettersRemaining, existingLetter)).ToBe(false)

	// Incorrect guess

	game.GuessALetter(existingLetter)
	expect(game.TriesLeft).ToBe(10)

	// Failed game

	loopUntil := game.TriesLeft
	for i := 0; i < loopUntil; i++ {
		game.GuessALetter(existingLetter)
	}
	expect(game.TriesLeft).ToBe(0)
	expect(game.Status).ToBe(models.GameStatus.Failed)

	// Success game

	game = models.Game{}.New()

	for i, _ := range game.Word {
		game.GuessALetter(string(game.Word[i]))
	}
	expect(game.Status).ToBe(models.GameStatus.Success)
}