Exemplo n.º 1
0
func play(s game.State, limit time.Duration) game.State {
	f := s.Futures()
	if len(f) == 0 {
		panic("Ah! There are no future states. Why'd the server send this to me? =( ")
	}
	return *f[0]
}
Exemplo n.º 2
0
func gameFromAI(host string, state *game.State) (*game.State, error) {
	mr := api.MoveRequest{
		State:      *state,
		LimitMilli: toMillisecondCount(api.DefaultMoveLimit),
	}
	var buf bytes.Buffer
	if err := json.NewEncoder(&buf).Encode(&mr); err != nil {
		return nil, err
	}

	var rawResponse bytes.Buffer // will contain response if HTTP request is successful
	f := func() error {
		resp, err := http.Post("http://"+host+api.MovePath, "application/json", bytes.NewBuffer(buf.Bytes()))
		if err != nil {
			return err
		}
		defer resp.Body.Close()
		io.Copy(&rawResponse, resp.Body)
		return nil
	}

	if err := withRetries(f); err != nil {
		return nil, err
	}

	responseGame := &game.State{}
	if err := json.NewDecoder(&rawResponse).Decode(responseGame); err != nil {
		return nil, err
	}
	if !state.ValidFuture(responseGame) {
		return nil, fmt.Errorf("game parsed correctly, but isn't a valid future")
	}
	return responseGame, nil
}