This is a game with client react app and java spring boot app using kafka and mongodb. Spring boot has a REST API and a web socket to connect with the client app.
-
Download the source folder or git clone using the url assignment-game
-
You need to have Kafka and MongoDB running to setup these. You can use the docker-compose to run them.
-
Get your terminal app and go inside assignment-game folder. run,
docker-compose up --build
It'll start kafka with zookeeper and mongodb. Now you have kafka and mongodb running.. 🎉
-
Now let's start the backend server. Go to the game-server folder and run
mvn clean install
The test build connects to kafka and mongodb on run. If you need to skip them run
mvn clean install -DskipTests
. Now you have the spring boot app ready to run mode. -
Let's start the spring boot app, run this command inside game-server
mvn spring-boot:run
It will start the spring boot app and you would get the server running on
http://localhost:8080/
. 🎉Mongo DB name which would automatically create -
game
-
Go to game-client folder and run these commands to run the react web app on
localhost:3000
.npm install npm start
Now the web app is running on
http://localhost:3000
and you can start to play. 🎉 🎉
- Identify the requirements.
- Create diagram to identify the bounded contexts, domains and relationship between them.
- Sketch the domains and relationships among them.
- Identify the sequence of the steps in the game.
- Decide the technologies and concepts to follow. Mainly DDD is followed.
- Game is the main domain I've used in modeling the use cases.
- Identify the test cases and test scenarios and implement them.
- Implementation.
- Testing.
- Fixes and enhancements.
- Domain Driven Design concepts. CQRS, event sourcing and event driven design approach.
- Microservices design and flexibility to structure the implementation to such applications.
- REST api, Kakfa messaging and web socket.
- Mongo DB as the db.
- Flexibility to containerise the apps using docker.
I’ve identified three bounded contexts. As accordingly the folder structure was created.
Server connectivity related classes are added in this context.
Game related logic and domain models, repository classes are in this folder.
Client application user action related context.
**These separated contexts give the flexibility to change. As an example we can create seperate micro service applications and connect them through kafka event streams.
- This contains the game controller, Kafka producer to send message to topic, Kafka consumer to consume topic messages, and websocket.
- The separated contexts give the flexibility to change. As an example if we want to replace web socket and REST API with gRPC we can easily do it.
- It has infrastructure configuration classes in a separate folder.
- When sending messages to the Kafka topic it was implemented in a separate thread to handle it gracefully.
- In this package the Game and GameTurn are the domains used to model.
- Game is the aggregate root class around which all the logic is created.
- GameService contains all the logic related to Game and GameTurn. This is the core of the game logic rules.
- Domain classes are built without any coupling. There are separate repositories for write and read data to Mongodb. CommandRepository to write, QueryRepository to read. Commands Query segregation is applied like that. This gives the flexibility to separate them and create event sourcing.
- Only Service classes use repository classes.
- GameServiceTest has all the tests for GamerService.
- When running them, the Kafka server needs to be running.
- This contains the LoginPlayer and PlayerAction models.
- There are separate repositories for write and read data to db. CommandRepository to write and QueryRepository to read. Commands and Query responsibilty segregation is applied like that. This gives the flexibility to separate them and create event sourcing if needed.
- All the user actions are saved in the DB. Flexible to use as an event store, easily upgradable to more extensive version of event sourcing and CQRS.
These can be set on application.properties file on game-server.
game.start.minimum.value=10
game.start.maximum.value=100
It indicates the number range of the game start random number.
game.divideBy.value=3
This indicates the divide by value.
Login
Login button press
--> get the request to server
--> validate and save login data
--> create the game
--> send the message to Kafka topic through producer
--> Kafka consumer get the message
--> write to websocket
--> client show the message on client game panel.
- Both players are automatic players -
- Player 1 enters name, proceeds with default Auto checked configuration.
- Player 2 enters name, proceeds with default Auto checked configuration.
- Player 1 presses Start.
- The results are shown in the game panel.
AutoPlay.mov
- Player 1 - Auto and Player 2 - Manual
- Player 1 enters name, proceeds with default Auto checked configuration.
- Player 2 enters the name, proceeds with Manual checked configuration.
- Player 1 presses Start.
- Player two selects one of these { 0, -1, 1 }
- The results are shown in the game panel.
manualplay_XHl1CUxY.mp4
- Player 1 and Player 2 both select Manual
- Player 1 and Player 2 enter one of these { 0, -1, 1 }
- Game panel show details.
bothmanual_r605B2Cn.mp4
Thank you 🙏 ! for reading and your feedback and suggestions are welcome..! 🙏 🙏