tourze / lottery-bundle
一个功能完整的 Symfony 抽奖系统包,支持多种抽奖活动配置、奖池管理、概率控制等功能
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
pkg:composer/tourze/lottery-bundle
Requires
- php: ^8.1
- ext-mbstring: *
- cloned/luckybox: ^0.9
- doctrine/collections: ^2.3
- doctrine/common: ^3.5
- doctrine/data-fixtures: ^2.0
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/doctrine-fixtures-bundle: ^4.0
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- easycorp/easyadmin-bundle: ^4
- fakerphp/faker: ^1.23
- knplabs/knp-menu: ^3.7
- nesbot/carbon: ^2.72 || ^3
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/doctrine-bridge: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/event-dispatcher-contracts: ^2.5 | ^3
- symfony/expression-language: ^6.4
- symfony/framework-bundle: ^6.4
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/messenger: ^6.4
- symfony/routing: ^6.4
- symfony/security-bundle: ^6.4
- symfony/security-core: ^6.4
- symfony/security-http: ^6.4
- symfony/serializer: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/arrayable: 0.0.*
- tourze/async-contracts: 0.0.*
- tourze/backtrace-helper: 0.1.*
- tourze/benifit-bundle: 0.0.*
- tourze/bundle-dependency: 0.0.*
- tourze/doctrine-helper: 0.0.*
- tourze/doctrine-indexed-bundle: 0.0.*
- tourze/doctrine-ip-bundle: 0.0.*
- tourze/doctrine-precision-bundle: 0.1.*
- tourze/doctrine-snowflake-bundle: 0.1.*
- tourze/doctrine-timestamp-bundle: 0.0.*
- tourze/doctrine-track-bundle: 0.1.*
- tourze/doctrine-user-bundle: 0.0.*
- tourze/easy-admin-menu-bundle: 0.1.*
- tourze/enum-extra: 0.1.*
- tourze/json-rpc-cache-bundle: 0.1.*
- tourze/json-rpc-core: 0.0.*
- tourze/json-rpc-http-endpoint-bundle: 0.1.*
- tourze/json-rpc-lock-bundle: 0.1.*
- tourze/json-rpc-log-bundle: 0.1.*
- tourze/lock-service-bundle: 0.1.*
- tourze/resource-manage-bundle: 0.1.*
- tourze/symfony-cron-job-bundle: 0.1.*
- tourze/symfony-ecol-bundle: 0.1.*
- tourze/symfony-routing-auto-loader-bundle: 0.0.*
- tourze/text-manage-bundle: 0.0.*
- tourze/user-event-bundle: 0.0.*
- tourze/user-id-bundle: 0.1.*
Requires (Dev)
- maglnet/composer-require-checker: ^4
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-10-31 19:59:53 UTC
README
A comprehensive lottery system bundle for Symfony applications featuring multiple lottery types, advanced prize pool management, chance control, and automated prize distribution.
Table of Contents
- Features
- Installation
- Quick Start
- Console Commands
- JSON-RPC API
- System Architecture
- Event System
- Advanced Usage
- Contributing
- License
Features
- Multiple Lottery Types - Support for wheel, grid, slot machine, and on-site lotteries
- Advanced Prize Pool Management - Multi-pool system with configurable probabilities and constraints
- Intelligent Chance Management - User chances with expiration tracking and status control
- Automated Prize Distribution - Automatic prize sending with consignee management
- Admin Interface - Complete EasyAdmin integration for lottery management
- Responsive H5 Frontend - Mobile-optimized lottery interface with multiple themes
- Event-Driven Architecture - Customizable event system for business logic extension
- JSON-RPC API - RESTful API endpoints for external system integration
- Cron Job Integration - Automated background tasks for maintenance and processing
- Expression Language - Flexible configuration with dynamic rule evaluation
- Concurrent Safety - Database-level locking for high-concurrency scenarios
Installation
Requirements
- PHP 8.1 or higher
- Symfony 7.3 or higher
- Doctrine ORM 3.0 or higher
- MySQL 5.7+ or PostgreSQL 10+
- Redis (recommended for caching)
Install via Composer
composer require tourze/lottery-bundle
Quick Start
<?php use LotteryBundle\Service\LotteryService; use LotteryBundle\Service\PrizeService; use LotteryBundle\Entity\Chance; // Execute lottery draw $lotteryService = $container->get(LotteryService::class); $result = $lotteryService->doLottery($chance); // Count valid chances for user $validChances = $lotteryService->countValidChance($user, $activity); // Award chances to user $lotteryService->giveChance($user, $chance); // Prize management $prizeService = $container->get(PrizeService::class); $prizeService->sendPrize($chance); // Check if activity is currently active $isActive = $activity->isActive(); // Get available lottery templates $templates = $lotteryService->getAvailableTemplates();
Bundle Registration
Add to config/bundles.php:
LotteryBundle\LotteryBundle::class => ['all' => true],
Database Setup
# Create database tables php bin/console doctrine:migrations:migrate # Load sample data (optional) php bin/console doctrine:fixtures:load --group=lottery
Configuration
The bundle provides flexible configuration through:
- Activity Settings - Configure lottery activities with time constraints and participation rules
- Prize Pool Management - Set up multiple pools with different probability algorithms
- Prize Configuration - Define virtual rewards, physical prizes, and distribution methods
- Chance Control - Monitor user participation limits and success tracking
Console Commands
lottery:check-expire-chance
Checks and processes expired lottery chances. This command runs every minute via cron job.
php bin/console lottery:check-expire-chance
Purpose: Automatically marks expired chances as invalid and triggers expire events for cleanup.
lottery:check-review-chance-send-prize
Processes reviewed chances and sends prizes to winners. This command runs every hour at 43 minutes.
php bin/console lottery:check-review-chance-send-prize
Purpose: Automatically sends prizes to users whose chances have been reviewed and approved.
JSON-RPC API
The bundle exposes JSON-RPC procedures for external integrations:
Lottery Operations
JoinLottery- Execute lottery participation with concurrent safetyGetAllLotteryChance- Retrieve comprehensive lottery chance dataGetLotteryDetail- Get detailed activity information and rulesServerSendLotteryChance- Administrative chance distribution
User Management
GetUserLotteryChanceList- User's participation history with paginationGetUserValidLotteryChanceCounts- Real-time valid chance countsGetLotteryConsignee- User delivery address informationSaveOrUpdateLotteryConsignee- Manage shipping addresses
Prize Information
GetLotteryPrizeList- Available prizes with stock information
System Architecture
The lottery system implements a robust workflow with concurrent safety:
- Activity Validation - Verify lottery timing, status, and participation eligibility
- Chance Authentication - Validate user's available chances with database locking
- Chance Pre-deduction - Atomically reserve chances to prevent overselling
- Pool Selection - Dynamic pool determination based on configurable rules
- Prize Algorithm - Probability-based prize selection with stock management
- Result Confirmation - Transactional commit with event dispatching
- Prize Distribution - Automated or manual prize fulfillment process
Event System
Extensive event system for business logic customization:
Core Events
UserJoinSuccessEvent- Dispatched after successful lottery participationAfterChanceExpireEvent- Triggered when user chances expireChanceEvent- General chance lifecycle event
Customization Events
DecidePoolEvent- Override default pool selection algorithmDecidePrizeProbabilityEvent- Implement custom probability calculationsAllLotteryChanceEvent- Global event for all chance operations
Advanced Usage
Custom Event Subscribers
Extend lottery functionality with event subscribers:
use LotteryBundle\Event\UserJoinSuccessEvent; use LotteryBundle\Event\DecidePoolEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class CustomLotterySubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ UserJoinSuccessEvent::class => 'onUserJoinSuccess', DecidePoolEvent::class => 'onDecidePool', ]; } public function onUserJoinSuccess(UserJoinSuccessEvent $event): void { // Send notification, award points, etc. $chance = $event->getChance(); $this->notificationService->sendLotteryResult($chance); } public function onDecidePool(DecidePoolEvent $event): void { // Custom pool selection based on user VIP level $user = $event->getUser(); if ($user->isVip()) { $event->setPool($this->getVipPool()); } } }
Expression Language
Implement complex business rules with expression language:
# config/packages/lottery.yaml lottery: pools: vip_pool: # Only VIP users with 5+ chances expression: 'user.isVip() and chance.count() > 5' weekend_pool: # Special weekend prizes expression: 'date("N") >= 6' high_value_pool: # High-value prizes for frequent players expression: 'user.getLotteryCount() > 100'
External Integration
Integrate with external systems via JSON-RPC:
// Join lottery from external application $result = $client->call('JoinLottery', [ 'activityId' => 1, 'count' => 3, // Draw 3 times ]); // Get user's lottery history $history = $client->call('GetUserLotteryChanceList', [ 'page' => 1, 'limit' => 20, ]); // Check available chances $chances = $client->call('GetUserValidLotteryChanceCounts', [ 'activityId' => 1, ]);
Contributing
Please see CONTRIBUTING.md for details.
License
The MIT License (MIT). Please see License File for more information.