コード例 #1
0
func postEmergency(w http.ResponseWriter, r *http.Request, ctx *context) (status int, response interface{}) {
	var emergency emergencyRequest
	e := json.Unmarshal(ctx.Body, &emergency)
	if e != nil {
		status = 400
		response = e.Error()
		return
	}

	emergency.Where, e = strconv.Atoi(emergency.WhereRequest)
	if e != nil {
		status = 400
		response = e.Error()
		return
	}

	city := sessions[ctx.CityID]
	if city == nil {
		status = 404
		response = "city not found"
		return
	}

	defer city.CleanError()
	vehicle, e := city.CallService(emergency.Service)
	if e != nil {
		status = 400
		response = e.Error()
		return
	}

	paths, e := algorithm.GetPaths(city, vehicle.Position.ID, emergency.Where)
	if e != nil {
		status = 400
		response = e.Error()
		return
	}

	paths = algorithm.CalcEstimatesForVehicle(vehicle, paths)
	if len(paths) == 0 {
		status = 400
		response = "Oh no... there is no way to go"
		return
	}

	toRun1 := algorithm.ChooseBest(paths)
	paths, _ = algorithm.GetPaths(city, emergency.Where, vehicle.BasePosition.ID)
	paths = algorithm.CalcEstimatesForVehicle(vehicle, paths)

	if len(paths) == 0 {
		status = 400
		response = "Oh no... there is no way to come back"
		return
	}

	vehicle.Alert <- toRun1
	vehicle.Alert <- algorithm.ChooseBest(paths)
	response = fmt.Sprintf("%s on the way to %d. It is %d blocks away", emergency.Service, emergency.Where, len(toRun1.Links))
	return
}
コード例 #2
0
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])
	  }
	}*/
}