The problem statement is to develop an HTTP endpoint that accepts JSON data and returns JSON data. The endpoint should accept a POST request with a body containing a list of points extracted from a vision module. The endpoint should return the coordinates of the next visible target that must be attacked, based on the provided protocols.
The solution is a web service that exposes a single HTTP endpoint (POST /radar) for accepting the scan data and returning the coordinates of the next target to be attacked. The service is built using the Fastify framework, with TypeScript for type safety and SOLID principles for code organization.
The service consists of several layers:
- Controller layer: The entry point of the service. It receives the HTTP request, validates the request data, and passes it to the service layer for processing. It returns the HTTP response generated by the service layer.
- Service layer: The business logic of the service. It receives the scan data, uses the Pythagorean theorem to get distance, applies the provided protocols to determine the next target to be attacked, and returns the coordinates of that target.
- Protocol layer: Contains the implementations of the available protocols used by the service layer to determine the next target to be attacked.
- Infrastructure layer: Contains the dependencies used by the service layer, such as the HTTP client used to make external API calls.
To use the code test, you need to run npm i && npm run dev then the server could be accessed on port http://localhost:8888/radar
To run all tests, run the command npm test
Sample body:
{ "protocols":["avoid-mech"], "scan":[ {"coordinates":{"x":0,"y":40},"enemies":{"type":"soldier","number":10}},{"coordinates":{"x":0,"y":80},"allies":5,"enemies":{"type":"mech","number":1}} ] }
return: { "x": 0, "y": 40 }
The solution provides a scalable and maintainable approach to solving the problem statement. The use of TypeScript and SOLID principles makes the codebase easy to reason about, while the use of Fastify provides a high-performance web server for handling incoming HTTP requests.