Skip to content

amoloye/games

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 

Repository files navigation

games

#GAME BACKEND API

This project is a backend API for a game that allows players to send a bet and a whole number from 1 to 100 to a server. The server then generates a random whole number from 1 to 100, and if the player's number is greater, calculates the win and sends it back to the client. The win depends on chance, which is calculated using the formula: bet * (99 / (100 - number)). For example, if a player selects the number 50 and bets 40.5, the win would be 80.19.

The API is built with Java 11 and Spring Boot 2 and uses REST with JSON. The project includes unit and integration tests, data validation, and optional tasks such as playing 1 million rounds in parallel and calculating the return to player (RTP).

#API Endpoints

POST /games: Accepts a JSON body with a GameRequest object containing the player's number and bet. The API returns a GameDto object with the calculated win if the player's number is greater than the random number generated by the server. Otherwise, it returns a 400 Bad Request response. GET /games/{playerNumber}: Accepts a path variable with the player's number and returns a GameDto object with the details of the last game played by the player. If no game is found, the API returns a 404 Not Found response. GET /error: Returns a string message indicating that an error has occurred. Swagger Documentation This project includes Swagger documentation to provide detailed information about the API endpoints. To access the Swagger documentation, run the API and navigate to http://localhost:8080/swagger-ui.html in your web browser.

#Testing

The project includes integration tests that simulate playing 1 million rounds in parallel with 24 threads. The GameControllerTest class uses WebTestClient to make POST requests to the /games endpoint and collect the results in a list. The executor creates a thread pool of 24 threads to run the requests in parallel. After all the requests have been completed, the total bet, total win, and RTP are calculated and printed to the console. The test ensures that the RTP is greater than 99%.

#Data Mapping

The project uses bean.utils to map the GameRequest and Game objects to GameDto objects. The GameMapper interface defines the mapping methods between the objects.

#Exception Handling

The project includes an @ControllerAdvice class with exception handling methods for MethodArgumentNotValidException, IllegalArgumentException, ChangeSetPersister.NotFoundException, and Exception. These methods return appropriate error responses for each exception.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages