Skip to content

nvzard/Casino-Royale

Repository files navigation

Maintainability

Screenshot 2022-05-17 at 1 08 21 PM

Documentation


Summary

This project exposes an API to simulate deck of cards which can be used for card games.

Tech Stack

Written in Go (Golang 1.18) using gin as web framework, Postgres as database, zap for logging, prometheus and grafana for monitoring.

  • Use make build to build the web server and make start to start the server. Server runs on localhost:8080.
  • Use make test runs the tests in a seperate docker container for isolation.
  • Air and docker-sync are used for live reloading during development.
  • Grafana dashboard is avaialble on localhost:3004
  • Prometheus UI is accessible on localhost:9090

Development

Requirements

git
docker
docker-compose
docker-sync (0.5.14) [Use `gem install docker-sync -v 0.5.14` for mac]

Getting Started with Development

  1. Clone repository
git clone https://github.com/nvzard/Casino-Royale.git
  1. Change directory to the cloned repository
cd Casino-Royale
  1. Build image
make build
  1. Run start command
make start

Run Tests

make test

Remove all artifacts and dependencies

make clean

API Documentation

GET   /healthcheck          # ok
POST  /deck                 # create new deck (query parameters: shuffle, cards)
GET   /deck/:deck_id        # open deck by id
POST  /deck/:deck_id/draw   # draw cards from the deck (query parameters: count)

1. Create a Deck

POST /deck
Parameter Type Description
shuffle bool Optional. true or false
cards string Optional. Custom Cards Code: AS,1S,2S, etc

Example Request 1

POST /deck

Response

{
    "deck_id": "a251071b-662f-44b6-ba11-e24863039c59",
    "shuffled": false,
    "remaining": 52
}

Example Request 2

POST /deck?shuffle=true&cards=AS,2S

Response

{
    "deck_id": "9bc9d90e-3056-40c7-94bc-241915cbee4d",
    "shuffled": true,
    "remaining": 2
}

2. Open a Deck

GET /deck/:deck_id

Example Request 1

GET /deck/a251071b-662f-44b6-ba11-e24863039c59

Response

{
    "deck_id": "a251071b-662f-44b6-ba11-e24863039c59",
    "shuffled": false,
    "remaining": 3,
    "cards": [
        {
            "value": "ACE",
            "suit": "SPADES",
            "code": "AS"
        },
	    {
            "value": "KING",
            "suit": "HEARTS",
            "code": "KH"
        },
        {
            "value": "8",
            "suit": "CLUBS",
            "code": "8C"
        }
    ]
}

3. Draw a Card

POST /deck/:deck_id/draw
Parameter Type Description
count string Optional. Number of cards to draw [default=1]

Example Request 1

POST /deck/a251071b-662f-44b6-ba11-e24863039c59/draw

Response

{
    "cards": [
        {
            "value": "QUEEN",
            "suit": "HEARTS",
            "code": "QH"
        }
    ]
}

Example Request 2

POST /deck/:deck_id/draw?count=2

Response

{
    "cards": [
	    {
            "value": "KING",
            "suit": "HEARTS",
            "code": "KH"
        },
        {
            "value": "8",
            "suit": "CLUBS",
            "code": "8C"
        }
    ]
}

Future Improvements

Some of the improvements that can be made but were skipped due to time constraints.

  • Refactor project structure to follow go guidelines. Currently it is similar to Rails/JS ecosystem.
  • Write more tests and improve code coverage.
  • Add error handling mechanism and single store for all strings.
  • Setup CI/CD pipeline for testing and deployment.
  • Add some kind of authentication mechanism. Basic Auth or JWT.

Side Note

Please open an issue and let me know incase I messed something up. Some feedback would really help me up my go game.