This is a sample implementation of Option 2 of the Cenith take-home GridGame assignment - which is to design a backend that could support the playing of the GridGame.
In a 100 by 100 2-D grid world, you are given a starting point A on one side of the grid, and an ending point B on the other side of the grid. Your objective is to get from point A to point B.
Each grid space can be in a state of [“Blank”, “Speeder”, “Lava”, “Mud”]. You start out with 200 points of health and 450 moves. Below is a mapping of how much your health and moves are affected by landing on a grid space.
[
“Blank”: {“Health”: 0, “Moves”: -1},
“Speeder”: {“Health”: -5, “Moves”: 0},
“Lava”: {“Health”: -50, “Moves”: -10},
“Mud”: {“Health”: -10, “Moves”: -5},
]
Build a backend HTTP API using Node.js, or similar, and Typescript, that allows a player to save the game and come back to it later. It should also return any relevant data to the frontend, such as where the player is on the board, what the board is configured like, how much health or moves are left, etc. Try to include business logic such as how the board is initially configured and the change of state of the board / players as well. We expect to be able to play the game over your HTTP API.
The solution was implemented as a Next.js app. It's written in Typescript and uses Drizzle as an ORM and for other general databasing needs (schema definition, migrations, and seeds).
.
├── app # Main application directory - it contains all of the source code for our solution
│ ├── db # Directory containing database
│ │ ├── migrations # Used by Drizzle to output automagically generated migration files.
│ │ ├── schema # Database schema definitions used by our app. These originate our tables in Postgres.
│ │ ├── seeds # Houses sample/input files that can be seeded into the database.
│ │ ├── drizzle.ts # The primary database file. It establishes our DB connection and contains queries.
│ │ ├── drizzle.ts # The primary database file. It establishes our DB connection and contains queries.
├── env.example # A sample .env file that can be used as the basis for configuring the application.
├── README.md # You are here!
├── openapi.yaml # An OpenAPI specification that describes how the application's API will behave.
Out of the box
- docker-compose --profile next up
Local dev
- Create a new .env file:
cp .env.example .env - Start the Poostgres and SwaggerUI services:
docker-compose --profile dev up - Generate migrations and apply them:
npm run db:generate && npm run db:migrate( Should only need to be run once ) - (Optional) Seed the database:
npm run db:seed(Should only be run once) - Start the app in dev mode with watch, to hot reload code.
npm run dev
- The app can be visited here: localhost:3000
- The app's API is at localhost:3000/api
- You can view the API's spec via the Swagger UI service at http://localhost:3001