Beispiel #1
0
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)
}
Beispiel #2
0
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)
}
Beispiel #3
0
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)
}
Beispiel #4
0
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)
}