Example #1
0
func createGame(c common.Context) {
	if c.Authenticated() {
		var game models.Game
		aiCommon.MustDecodeJSON(c.Req.Body, &game)
		if len(game.Players) > 0 {
			c.RenderJSON(game.Save(c))
		}
	}
}
Example #2
0
func createAI(c common.Context) {
	if c.Authenticated() {
		var ai models.AI
		aiCommon.MustDecodeJSON(c.Req.Body, &ai)
		if ai.Name != "" && ai.URL != "" {
			ai.Owner = c.User.Email
			ai.Id = nil
			c.RenderJSON(ai.Save(c))
		}
	}
}
Example #3
0
/*
HTTPHandlerFunc returns an http.HandlerFunc to use when hosting an AI.
*/
func HTTPHandlerFunc(lf common.LoggerFactory, ai AI) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		logger := lf(r)
		defer func() {
			if e := recover(); e != nil {
				w.WriteHeader(500)
				logger.Printf("Error delivering orders: %v", e)
			}
		}()
		var req OrderRequest
		common.MustDecodeJSON(r.Body, &req)
		w.Header().Set("Content-Type", "application/json; charset=UTF-8")
		common.MustEncodeJSON(w, ai.Orders(lf(r), req.Me, req.State))
	}
}