Skip to content

Richard-inter/game

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

22 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Game Backend Services

A microservices-based game backend system built with Go, featuring claw machine and player management services with real-time communication capabilities.

πŸ—οΈ Architecture

This project implements a distributed microservices architecture with the following components:

  • API Service - REST API gateway (Port 8080)
  • WebSocket Service - Real-time WebSocket connections (Port 8081)
  • TCP Service - TCP socket connections (Port 8082)
  • RPC Services - gRPC microservices:
    • ClawMachine Service (Port 9091)
    • Player Service (Port 9094)
  • Infrastructure - MySQL database, etcd service discovery

πŸš€ Features

  • Microservices Architecture - Scalable, independent services
  • Real-time Communication - WebSocket and TCP support
  • Service Discovery - etcd-based service registration and discovery
  • Database Integration - MySQL with GORM ORM
  • Protocol Buffers - Efficient inter-service communication
  • Docker Support - Containerized deployment
  • Configuration Management - YAML-based configuration

πŸ“‹ Prerequisites

  • Go 1.24+
  • Docker & Docker Compose
  • MySQL 8.0+
  • etcd (optional, for service discovery)
  • Protocol Buffers compiler (protoc)
  • FlatBuffers compiler (flatc)

πŸ› οΈ Installation

  1. Clone the repository:
git clone https://github.com/Richard-inter/game.git
cd game
  1. Install dependencies:
make deps
  1. Start infrastructure:
make docker-up-infra

πŸƒβ€β™‚οΈ Quick Start

Development Mode

Start all services individually:

# Start each service in separate terminals
make run-game          # Game service (Port 9090)
make run-api           # API service (Port 8080)
make run-websocket     # WebSocket service (Port 8081)
make run-tcp           # TCP service (Port 8082)
make run-clawmachine   # ClawMachine RPC service (Port 9091)
make run-player        # Player RPC service (Port 9094)

Docker Mode

Start all services with Docker Compose:

make docker-up

Build

Build all services:

make build

Build individual services:

make build-api
make build-websocket
make build-tcp
make build-clawmachine
make build-player

πŸ“ Project Structure

game/
β”œβ”€β”€ cmd/                    # Service entry points
β”‚   β”œβ”€β”€ api-service/        # REST API gateway
β”‚   β”œβ”€β”€ websocket-service/  # WebSocket handler
β”‚   β”œβ”€β”€ tcp-service/        # TCP socket handler
β”‚   └── rpc/               # gRPC microservices
β”‚       β”œβ”€β”€ rpc-clawmachine-service/
β”‚       └── rpc-player-service/
β”œβ”€β”€ internal/              # Private application code
β”‚   β”œβ”€β”€ config/           # Configuration management
β”‚   β”œβ”€β”€ db/               # Database connections
β”‚   β”œβ”€β”€ discovery/        # Service discovery (etcd)
β”‚   β”œβ”€β”€ domain/           # Business logic models
β”‚   β”œβ”€β”€ repository/       # Data access layer
β”‚   β”œβ”€β”€ service/          # Business services
β”‚   └── transport/        # Transport layer (HTTP, gRPC, WebSocket, TCP)
β”œβ”€β”€ pkg/                  # Public library code
β”‚   β”œβ”€β”€ common/           # Common utilities
β”‚   β”œβ”€β”€ logger/           # Logging utilities
β”‚   └── protocol/         # Protocol definitions (Proto, FlatBuffers)
β”œβ”€β”€ config/               # Configuration files
β”œβ”€β”€ scripts/              # Development scripts

βš™οΈ Configuration

Services use YAML configuration files in the config/ directory:

  • api-service.yaml - API service configuration
  • websocket-service.yaml - WebSocket service configuration
  • game-service.yaml - Game service configuration
  • rpc-clawmachine-service.yaml - ClawMachine RPC service configuration
  • rpc-player-service.yaml - Player RPC service configuration

Environment Variables

  • CONFIG_PATH - Path to configuration file (optional, defaults to service-specific config)

πŸ”§ Development

Code Generation

Generate protocol buffer files:

make proto

Generate FlatBuffer files:

make flatbuffers

Generate all code:

make generate

Testing

Run tests:

make test

Run tests with coverage:

make test-coverage

Linting

Lint code:

make lint

Lint and fix:

make lint-fix

Formatting

Format code:

make fmt

🐳 Docker

Development

Build development image:

make docker-build-dev

Production

Build production image:

make docker-build

Start services:

make docker-up

Stop services:

make docker-down

View logs:

make docker-logs

πŸ“Š Services

API Service (Port 8080)

REST API gateway that handles HTTP requests and forwards them to appropriate microservices.

WebSocket Service (Port 8081)

Handles real-time WebSocket connections for live game updates.

TCP Service (Port 8082)

Manages TCP socket connections for low-latency communication.

ClawMachine Service (Port 9091)

gRPC service managing claw machine game logic and state.

Player Service (Port 9094)

gRPC service handling player data, authentication, and profiles.

πŸ”Œ API Endpoints

The API service exposes REST endpoints for:

  • Player Management

    • GET /players - List players
    • POST /players - Create player
    • GET /players/{id} - Get player details
    • PUT /players/{id} - Update player
    • DELETE /players/{id} - Delete player
  • Claw Machine Management

    • GET /clawmachines - List claw machines
    • POST /clawmachines - Create claw machine
    • GET /clawmachines/{id} - Get claw machine details
    • PUT /clawmachines/{id} - Update claw machine
    • DELETE /clawmachines/{id} - Delete claw machine

πŸ—„οΈ Database

The project uses MySQL 8.0 as the primary database. The database schema includes:

  • players - Player information and statistics
  • claw_machines - Claw machine configuration and state
  • games - Game sessions and results

Database connection is managed through GORM ORM with connection pooling.

πŸ” Service Discovery

Services can be discovered using etcd. When service discovery is enabled:

  1. Services register themselves with etcd on startup
  2. Client services discover service endpoints dynamically
  3. Load balancing and health checking are handled automatically

To disable service discovery, set discovery.enabled: false in configuration.

πŸ§ͺ Testing

The project includes unit tests for business logic and integration tests for API endpoints. Tests are located alongside the source code.

πŸ“ Logging

Structured logging using zap with high-performance JSON output:

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

Logs include service name, version, request correlation IDs, and structured fields for better observability. The logger supports both structured logging with zap.SugaredLogger and typed logging with zap.Logger for optimal performance.

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests for new functionality
  5. Run linting and tests
  6. Submit a pull request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Troubleshooting

Common Issues

  1. Port conflicts - Ensure ports 8080-8082 and 9090-9094 are available
  2. Database connection - Verify MySQL is running and credentials are correct
  3. Service discovery - Check etcd is accessible if enabled
  4. Docker issues - Ensure Docker daemon is running and ports are exposed

Health Checks

Each service exposes a health check endpoint:

  • GET /health - Service health status

Logs

View service logs for debugging:

# Docker logs
make docker-logs

# Individual service logs
docker-compose logs -f [service-name]

πŸ“ž Support

For support and questions:

  • Create an issue in the GitHub repository
  • Check the documentation in the docs/ directory
  • Review configuration examples in config/

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors