Esempio n. 1
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
}
Esempio n. 2
0
func (self *AI) AddError(c common.Context, turnId *datastore.Key, err error) {
	_, e := datastore.Put(c, datastore.NewKey(c, AIErrorKind, "", 0, self.Id), &AIError{
		CreatedAt:         time.Now(),
		Turn:              turnId,
		ErrorBytes:        []byte(err.Error()),
		ErrorDetail1Bytes: []byte(fmt.Sprintf("%+v", err)),
		ErrorDetail2Bytes: []byte(fmt.Sprintf("%#v", err)),
	})
	if e != nil {
		c.Errorf("Got %+v when trying to save a new error!", e)
	}
	common.MemDel(c, aiErrorsKeyByParent(self.Id))
}
Esempio n. 3
0
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
}
Esempio n. 4
0
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)
}
Esempio n. 5
0
func (self *AI) Delete(c common.Context) {
	datastore.Delete(c, self.Id)
	common.MemDel(c, AllAIsKey, aIByIdKey(self.Id))
}