Skip to content

Latest commit

Β 

History

History
266 lines (215 loc) Β· 7.04 KB

File metadata and controls

266 lines (215 loc) Β· 7.04 KB

🎴 Card Image Service Implementation Summary

βœ… Completed Implementation

The card image service has been successfully implemented as a standalone, encapsulated service that can be imported and used from the main project.

πŸ“ Project Structure

card-image-service/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ index.ts                 # Main exports
β”‚   β”œβ”€β”€ generateAndSendCard.ts   # Core service logic
β”‚   β”œβ”€β”€ config.ts                # Configuration loader
β”‚   β”œβ”€β”€ logger.ts                # Logging utilities
β”‚   β”œβ”€β”€ cache.ts                 # Image caching system
β”‚   β”œβ”€β”€ telegram.ts              # Telegram API service
β”‚   β”œβ”€β”€ types.ts                 # TypeScript interfaces
β”‚   β”œβ”€β”€ bot.ts                   # Standalone bot
β”‚   └── image/
β”‚       └── composer.ts          # Image generation logic
β”œβ”€β”€ assets/
β”‚   β”œβ”€β”€ card/                    # Card images by style
β”‚   β”‚   └── general/            # General style cards
β”‚   └── card_area/              # Background images by area
β”œβ”€β”€ package.json                 # Dependencies and scripts
β”œβ”€β”€ tsconfig.json               # TypeScript configuration
β”œβ”€β”€ env.example                 # Environment variables template
└── README.md                   # Comprehensive documentation

πŸ”§ Core Features

1. Image Generation

  • Uses Sharp library for high-quality image composition
  • Supports multiple card styles (general, blue, red, etc.)
  • Supports multiple background areas (general, club, etc.)
  • Automatic layout calculation based on card count
  • Debug tag support for image identification

2. Caching System

  • Intelligent caching with 24-hour expiry
  • Hash-based cache keys for identical requests
  • JSON file storage for persistence
  • Automatic cache cleanup of expired entries

3. Telegram Integration

  • Dedicated bot for image sending
  • Channel-based image delivery
  • File ID and message ID tracking
  • Error handling and retry logic

4. Standalone Operation

  • Independent bot with health check commands
  • Cache management commands (/cache, /clearcache)
  • Status monitoring (/status)

πŸš€ Usage Examples

From Main Project

import { generateAndSendCardImage } from '@/utils/cardImageService';

// Generate and send card image
const messageId = await generateAndSendCardImage(
  ['ace_of_hearts', 'king_of_spades', 'queen_of_diamonds'],
  'general',  // style
  'club',     // area
  'Player Hand' // debug tag
);

Standalone Usage

import { generateAndSendCard } from './card-image-service/src';

const messageId = await generateAndSendCard(
  ['2_of_clubs', '3_of_hearts'],
  'general',
  'general',
  'Test Hand'
);

πŸ“¦ Dependencies

Core Dependencies

  • grammy - Telegram bot framework
  • sharp - Image processing library
  • dotenv - Environment variable management
  • pino - Structured logging

Development Dependencies

  • typescript - Type checking and compilation
  • eslint - Code linting
  • vitest - Testing framework

πŸ”§ Configuration

Environment Variables

BOT_TOKEN=your_card_image_bot_token_here
TARGET_CHANNEL_ID=@your_channel_username_or_id
LOG_LEVEL=info

Image Structure

assets/
β”œβ”€β”€ card/
β”‚   └── general/                # Style name
β”‚       β”œβ”€β”€ 2_of_clubs.png
β”‚       β”œβ”€β”€ 3_of_clubs.png
β”‚       β”œβ”€β”€ ace_of_hearts.png
β”‚       └── ...
└── card_area/
    β”œβ”€β”€ general.png             # Area name
    β”œβ”€β”€ club.png
    └── ...

πŸ§ͺ Testing

Build and Test

cd card-image-service
pnpm install
pnpm build
pnpm test

Manual Testing

# Start the standalone bot
pnpm dev

# Test image generation
pnpm tsx src/test.ts

πŸ”— Integration with Main Project

1. Wrapper Service

Created src/utils/cardImageService.ts as a clean interface:

  • Dynamic loading to avoid build-time dependencies
  • Proper error handling and logging
  • Type-safe function signatures

2. Usage in Game Logic

Example integration in poker stake handler:

// Example usage (commented out for now)
// import { generateAndSendCardImage } from '@/utils/cardImageService';

// In game logic:
// const messageId = await generateAndSendCardImage(
//   playerCards,
//   'general',
//   'club',
//   `Player ${playerName} Hand`
// );

🎯 Key Benefits

1. Encapsulation

  • Completely independent service
  • No reverse dependencies
  • Self-contained configuration

2. Performance

  • Intelligent caching system
  • Avoids regenerating identical images
  • Efficient image composition

3. Flexibility

  • Multiple card styles and backgrounds
  • Debug tags for identification
  • Standalone or integrated usage

4. Maintainability

  • Clean separation of concerns
  • Comprehensive logging
  • Type-safe interfaces

πŸ”„ Next Steps

1. Add Card Images

Place actual card images in the assets structure:

card-image-service/assets/card/general/
β”œβ”€β”€ 2_of_clubs.png
β”œβ”€β”€ 3_of_clubs.png
β”œβ”€β”€ ace_of_hearts.png
└── ... (all 52 cards)

2. Add Background Images

Place background images in:

card-image-service/assets/card_area/
β”œβ”€β”€ general.png
β”œβ”€β”€ club.png
└── ... (other backgrounds)

3. Configure Environment

cd card-image-service
cp env.example .env
# Edit .env with your bot token and channel ID

4. Test Integration

# Build the service
cd card-image-service && pnpm build

# Test from main project
cd ..
pnpm type-check

🚨 Important Notes

1. Asset Requirements

  • Card images must be placed in the correct directory structure
  • Image names must match the card array parameters exactly
  • PNG format is recommended for transparency support

2. Bot Configuration

  • Requires a separate bot token for the card image service
  • Bot must have permission to send messages to the target channel
  • Channel ID must be correctly configured

3. Caching Behavior

  • Cache entries expire after 24 hours
  • Cache is stored in card-image-service/cache.json
  • Cache can be cleared via bot commands or programmatically

4. Error Handling

  • Missing images throw descriptive errors
  • Telegram API failures are handled gracefully
  • Cache failures don't prevent image generation

πŸ“š Documentation

  • README.md - Comprehensive usage guide
  • example-usage.ts - Practical examples
  • test.ts - Testing utilities
  • types.ts - Complete type definitions

βœ… Status

  • βœ… Service Implementation - Complete
  • βœ… TypeScript Compilation - Working
  • βœ… Dependencies - Installed
  • βœ… Documentation - Comprehensive
  • βœ… Integration Wrapper - Created
  • ⏳ Asset Setup - Requires card images
  • ⏳ Environment Configuration - Requires bot token
  • ⏳ Testing - Ready for testing with assets

The card image service is now ready for use! Just add the card images and configure the environment variables to start generating and sending card images to your Telegram channel.