Example #1
0
func postSampleCity(w http.ResponseWriter, r *http.Request, ctx *context) (status int, response interface{}) {
	var in cityParams
	status = 302
	var url string
	err := json.Unmarshal(ctx.Body, &in)
	if in.Name == "" || err != nil {
		status = 400
		response = "Bad json"
		return
	}

	if sessions[in.Name] != nil {
		status = 403
		response = "city already exists"
		return
	}

	sessions[in.Name], err = factory.CreateRectangularCity(in.SizeHorizontal, in.SizeVertical, in.Name)
	if err != nil {
		status = 400
		response = err.Error()
		return
	}

	if status != 302 {
		url = "/error"
	} else {
		url = fmt.Sprintf("/city/%s", in.Name)
	}
	http.Redirect(w, r, url, status)
	return -1, nil
}
Example #2
0
func main() {
	var port int
	var err error
	flag.IntVar(&port, "port", 2489, "port server will be launched")
	flag.Parse()

	sessions = make(map[string]*models.City)
	sessions["default"], err = factory.CreateRectangularCity(10, 10, "default")
	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}
	time.Sleep(2 * time.Second)

	muxRouter := mux.NewRouter()
	muxRouter.StrictSlash(false)

	muxRouter.Handle("/mobile/city/{cityID}", handler(getMobileCity)).Methods("GET")
	muxRouter.Handle("/sample-city", handler(postSampleCity)).Methods("POST")
	muxRouter.Handle("/sample-city", handler(getSampleCity)).Methods("GET")
	muxRouter.Handle("/emergency/{cityID}", handler(postEmergency)).Methods("POST")
	muxRouter.Handle("/city/{cityID}", handler(getIndex)).Methods("GET")
	muxRouter.HandleFunc("/", handleFile("main.html"))
	muxRouter.HandleFunc("/city/img/0.jpg", handleFile("img/0.jpg"))
	muxRouter.HandleFunc("/city/img/1.jpg", handleFile("img/1.jpg"))
	muxRouter.HandleFunc("/city/img/2.jpg", handleFile("img/2.jpg"))
	muxRouter.HandleFunc("/city/img/3.jpg", handleFile("img/3.jpg"))
	muxRouter.HandleFunc("/city/img/-1.jpg", handleFile("img/-1.jpg"))

	http.Handle("/", muxRouter)
	listener, err := net.Listen("tcp", fmt.Sprintf("0.0.0.0:%d", port))
	if err != nil {
		fmt.Println("Cannot launch server:", err)
		os.Exit(2)
	}
	fmt.Printf("Listening on port %d...\n", port)
	http.Serve(listener, nil)
}
func main() {

	var i int
	city, _ := factory.CreateRectangularCity(3, 3, "Fake Buenos Aires")
	vehicle, e := city.CallService("Medic")
	if e != nil {
		fmt.Println(e)
		os.Exit(2)
	}
	paths, e := algorithm.GetPaths(city, vehicle.Position.ID, 3)
	if e != nil {
		fmt.Printf("Ohh no... %+v\n", e)
		os.Exit(2)
	}
	path := algorithm.SortCandidates(algorithm.CalcEstimatesForVehicle(vehicle, paths))[0]
	fmt.Println("24")
	vehicle.Alert <- path
	fmt.Scanf("%d", &i)
	/*for i := 0; i < 1; i++ {
	  for j := 0; j < len(paths[0].Links); j++ {
	    fmt.Printf("Link #%d: %+v\n", j, paths[0].Links[j])
	  }
	}*/
}