func (self *Turn) Save(c common.Context, parent *datastore.Key) *Turn { self.SerializedState = common.MustMarshal(self.State) var err error if self.Id == nil { self.CreatedAt = time.Now() self.Id, err = datastore.Put(c, datastore.NewKey(c, TurnKind, "", 0, parent), self) common.AssertOkError(err) common.MemPut(c, latestTurnKeyForParent(parent), self) } else { _, err = datastore.Put(c, self.Id, self) common.AssertOkError(err) } common.MemDel(c, turnsKeyForParent(parent)) return self }
func findGamePage(c common.Context, offset, limit int) (result GamePage) { query := datastore.NewQuery(GameKind) var err error result.Total, err = query.Count(c) common.AssertOkError(err) var ids []*datastore.Key ids, err = query.Limit(limit).Offset(offset).Order("-CreatedAt").GetAll(c, &result.Content) common.AssertOkError(err) for index, id := range ids { result.Content[index].Id = id } if result.Content == nil { result.Content = Games{} } return }
func (self *AI) findErrors(c common.Context) (result AIErrors) { _, err := datastore.NewQuery(AIErrorKind).Ancestor(self.Id).Order("-CreatedAt").Limit(50).GetAll(c, &result) common.AssertOkError(err) if result == nil { result = AIErrors{} } return }
func findGameById(c common.Context, id *datastore.Key) *Game { var game Game err := datastore.Get(c, id, &game) if err == datastore.ErrNoSuchEntity { return nil } common.AssertOkError(err) game.Id = id return &game }
func findAIById(c common.Context, id *datastore.Key) *AI { var ai AI err := datastore.Get(c, id, &ai) if err == datastore.ErrNoSuchEntity { return nil } common.AssertOkError(err) ai.Id = id return &ai }
func findTurnsByParent(c common.Context, parent *datastore.Key) (result Turns) { ids, err := datastore.NewQuery(TurnKind).Ancestor(parent).GetAll(c, &result) common.AssertOkError(err) for index, id := range ids { result[index].Id = id } if result == nil { result = Turns{} } return }
func findAllAIs(c common.Context) (result AIs) { ids, err := datastore.NewQuery(AIKind).GetAll(c, &result) common.AssertOkError(err) for index, id := range ids { result[index].Id = id } if result == nil { result = AIs{} } return }
func findLatestTurnByParent(c common.Context, parent *datastore.Key) *Turn { var turns Turns ids, err := datastore.NewQuery(TurnKind).Ancestor(parent).Order("-CreatedAt").Limit(1).GetAll(c, &turns) common.AssertOkError(err) for index, id := range ids { turns[index].Id = id } if len(turns) == 0 { return nil } return &turns[0] }
func (self *AI) Save(c common.Context) *AI { var err error if self.Id == nil { self.CreatedAt = time.Now() self.Id, err = datastore.Put(c, datastore.NewKey(c, AIKind, "", 0, nil), self) } else { _, err = datastore.Put(c, self.Id, self) } common.AssertOkError(err) common.MemDel(c, AllAIsKey, aIByIdKey(self.Id)) return self }
func (self *Game) Save(c common.Context) *Game { var err error if self.Id == nil { self.setPlayerNames(c) err = common.Transaction(c, func(c common.Context) (err error) { self.CreatedAt = time.Now() self.State = StateCreated self.Length = 1 self.Id, err = datastore.Put(c, datastore.NewKey(c, GameKind, "", 0, nil), self) if err != nil { return } playerIds := make([]state.PlayerId, 0, len(self.Players)) for _, id := range self.Players { playerIds = append(playerIds, state.PlayerId(id.Encode())) } turn := &Turn{ State: state.RandomState(common.GAELogger{c}, playerIds), } turn.Save(c, self.Id) self.Turns = Turns{*turn} nextTurnFunc.Call(c, self.Id, self.PlayerNames) return nil }) if err == nil { for _, playerId := range self.Players { common.Transaction(c, func(common.Context) error { if ai := GetAIById(c, playerId); ai != nil { ai.Games += 1 ai.Save(c) } return nil }) } } } else { _, err = datastore.Put(c, self.Id, self) } common.AssertOkError(err) common.MemDel(c, allGamesKey, gameKeyForId(self.Id)) return self.process(c) }