Пример #1
0
func handleCreate(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, "Method Not Allowed", 405)
		return
	}
	log.Print("POST /create")
	var create struct {
		Size int
	}
	if body, err := ioutil.ReadAll(r.Body); err != nil {
		http.Error(w, "Internal Server Error", 500)
		return
	} else if err := json.Unmarshal(body, &create); err != nil {
		http.Error(w, "Bad Request\n"+err.Error(), 400)
		return
	}
	if create.Size == 0 {
		create.Size = ayu.DefaultSize
	}
	if !ayu.IsValidSize(create.Size) {
		http.Error(w, "Bad Request\nInvalid board size.", 400)
		return
	}
	id := createRandomKey()
	games_mutex.Lock()
	defer games_mutex.Unlock()
	if games[id] != nil {
		// This should be extremely improbable!
		http.Error(w, "Internal Server Error", 500)
		return
	}
	games[id] = &game{ayu.CreateState(create.Size),
		[2]time.Duration{0, 0}, time.Time{},
		[2]string{createRandomKey(), createRandomKey()},
		list.New(), sync.Mutex{}}
	writeJsonResponse(w, map[string]interface{}{
		"game": id,
		"keys": games[id].Keys,
		"size": create.Size,
	})
}
Пример #2
0
func (ag AyuGame) CreateState() GameState {
	return ayu.CreateState()
}