Giter Club home page Giter Club logo

card-dec-go's Introduction

Card Deck Service: Go & SQLite Implementation

A versatile Card Deck Service implemented in Go and SQLite, designed to be used in card games such as Poker, Bridge, 29, etc.

Table of Contents

Prerequisites

Ensure GoLang is installed on your system.

Installation

Ubuntu

  1. Update the packages index and install the required dependencies:
sudo apt update
sudo apt install wget
  1. Download the Go binary:
wget https://golang.org/dl/go1.17.1.linux-amd64.tar.gz

Note: Replace the URL with the latest version available from the official Go website.

  1. Extract the tarball:
sudo tar -C /usr/local -xzf go1.17.1.linux-amd64.tar.gz
  1. Add the Go binary to your PATH:
echo "export PATH=$PATH:/usr/local/go/bin" >> ~/.profile
  1. Source the ~/.profile file to apply the changes:
source ~/.profile
  1. Verify the installation by checking the Go version:
go version

macOS

  1. Install Homebrew if it's not already installed on your system:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. Update Homebrew:
brew update
  1. Install Go using Homebrew:
brew install go
  1. Verify the installation by checking the Go version:
go version

Build and Run

Build

make build

Run

make run

Run Tests

make test

Engineering Design Choices

Functional Requirements

  • Build a New Deck: Ability to build a new card deck that can be acted on.
  • Open A Deck: Ability to open a specified card deck to show the remaining cards in the deck.
  • Draw a Card: Ability to draw a specified number of cards from a specified deck

Non-Functional Requirements

  • Provide proper error messages to the user such as: a. Deck does not exist b. Number of cards to be drawn exceeds the number of cards present in the deck.
  • Query Parameters to be presented through URL instead of request body.
  • Abstraction Layers in Services & Data Access Layer to make it extensible to implement different Databases in the future.
  • Proper Dependency Injection to each request context without initializing Objects for every request.

High-Level Design

This is overall a simple API, so we would build a REST API in MVC Architecture. Separating concerns and responsibilities in different modules. I have chosen to do a Monolithic System Architecture instead of Service Oriented or a Microservice Architecture since we would have services for only one model. This can be turned into SOA or microservices easily if we put routes, tests, and internal folders to a subfolder called deck.

Component Design

There's essentially only 1 component to be built which is Deck which consists of Cards. As mentioned earlier we would separate out concerns. Following submodules are built:

  • Routing
  • Services Abstraction
  • Dependency Injection of correct Dependencies
  • Settings separated out in another file to have different settings in different environments.

Database Design

Models

Deck

  • id : string
  • shuffled : boolean
  • cards : [] Card

Card

  • value
  • suit
  • code

Deck contains Card as a JSONB field.

We would not have a different table for Card but store it in a JSONB column in the Deck Table.

The pitfalls for having a different table for Card:

  • Suppose we have a table Cards with 52 cards.
  • For each deck, we would need to maintain a relationship table with FK in Cards & Deck.
  • This junction table would Linearly grow with each new Deck. 1 deck - 52 Relations 10 Decks - 520 Relations.

These relations would need to be maintained i.e., Indices, records need to be deleted when cards are drawn, the order of the cards needs to be maintained in the relationship table as well.

This would be expensive in terms of Database Size + Operational Execution Time for the APIs.

Database Choice

The database used in the project is SQLite because it's easier to run this on localhost. To productionize this, we can easily extend the DAL Layer for another Database.

API Signatures

Create a new deck

POST /deck/new?shuffled=true&cards=2C,10H,5H

Request Body None

Response

{
    "deck_id": "string",
    "remaininf": 52,
    "shuffled": true
}

Open Deck

Endpoint: GET /deck/:id

Parameters:

Name Type Description
id string The ID of the deck to open

Response:

{
    "deck_id": "string",
    "shuffled": boolean,
    "remaining": int,
    "cards": [
        {
            "value": string,
            "suit": string,
            "code": string
        }
    ]
}

Draw Card

Endpoint: POST /deck/:id/draw?count=1

Parameters:

Name Type Description
id string The ID of the deck to draw a card from

Request Body:

{}

Response:

{
  "cards": [
    {
      "value" : "string",
      "card" : "string",
      "suit": "string"
    }
  ]
}

card-dec-go's People

Contributors

xuryax avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.