示例#1
0
文件: game.go 项目: zannet/hangman
func (this Game) Find(c appengine.Context, id string, player *Player) (*Game, api.Error) {
	game := &Game{}

	key := keyFromId(c, id, player)

	err := datastore.Get(c, key, game)

	game.Id = key.IntID()

	return game, api.DevError(err)
}
示例#2
0
文件: game.go 项目: zannet/hangman
func (this Game) Create(c appengine.Context, player *Player) (*Game, api.Error) {
	game := Game{}.New()

	incompleteKey := datastore.NewIncompleteKey(c, kindGames, player.Key(c))

	key, err := datastore.Put(c, incompleteKey, game)

	game.Id = key.IntID()

	return game, api.DevError(err)
}
示例#3
0
文件: game.go 项目: zannet/hangman
func (this Game) FindAll(c appengine.Context, player *Player, f func(*Game)) ([]*Game, api.Error) {
	games := []*Game{}

	keys, err := datastore.NewQuery(kindGames).Ancestor(player.Key(c)).GetAll(c, &games)

	for i, game := range games {
		game.Id = keys[i].IntID()
		if f != nil {
			f(game)
		}
	}

	return games, api.DevError(err)
}
示例#4
0
文件: game.go 项目: zannet/hangman
func (this Game) Guess(c appengine.Context, player *Player, p api.M) api.Error {
	game, err := this.Find(c, p["id"], player)

	if err.Any() {
		return err
	}

	if game.IsOver() {
		return api.UserError("Game ended wit status: " + this.Status)
	}

	if !game.ValidGuess(p["guess"]) {
		return api.UserError("Sorry, try guessing one lowercase letter per time.")
	}

	game.GuessALetter(p["guess"])

	_, error := datastore.Put(c, keyFromId(c, p["id"], player), game)

	return api.DevError(error)
}