2025-12-29 10:54:00 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @zh @esengine/transaction 事务系统
|
|
|
|
|
|
* @en @esengine/transaction Transaction System
|
|
|
|
|
|
*
|
|
|
|
|
|
* @zh 提供游戏事务处理能力,支持商店购买、玩家交易、分布式事务
|
|
|
|
|
|
* @en Provides game transaction capabilities, supporting shop purchases, player trading, and distributed transactions
|
|
|
|
|
|
*
|
|
|
|
|
|
* @example
|
|
|
|
|
|
* ```typescript
|
|
|
|
|
|
* import {
|
|
|
|
|
|
* TransactionManager,
|
|
|
|
|
|
* MemoryStorage,
|
|
|
|
|
|
* CurrencyOperation,
|
|
|
|
|
|
* InventoryOperation,
|
|
|
|
|
|
* } from '@esengine/transaction'
|
|
|
|
|
|
*
|
|
|
|
|
|
* // 创建事务管理器
|
|
|
|
|
|
* const manager = new TransactionManager({
|
|
|
|
|
|
* storage: new MemoryStorage(),
|
|
|
|
|
|
* })
|
|
|
|
|
|
*
|
|
|
|
|
|
* // 执行事务
|
|
|
|
|
|
* const result = await manager.run((tx) => {
|
|
|
|
|
|
* tx.addOperation(new CurrencyOperation({
|
|
|
|
|
|
* type: 'deduct',
|
|
|
|
|
|
* playerId: 'player1',
|
|
|
|
|
|
* currency: 'gold',
|
|
|
|
|
|
* amount: 100,
|
|
|
|
|
|
* }))
|
|
|
|
|
|
* tx.addOperation(new InventoryOperation({
|
|
|
|
|
|
* type: 'add',
|
|
|
|
|
|
* playerId: 'player1',
|
|
|
|
|
|
* itemId: 'sword',
|
|
|
|
|
|
* quantity: 1,
|
|
|
|
|
|
* }))
|
|
|
|
|
|
* })
|
|
|
|
|
|
*
|
|
|
|
|
|
* if (result.success) {
|
|
|
|
|
|
* console.log('Transaction completed!')
|
|
|
|
|
|
* }
|
|
|
|
|
|
* ```
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
// Core | 核心
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
export type {
|
|
|
|
|
|
TransactionState,
|
|
|
|
|
|
OperationResult,
|
|
|
|
|
|
TransactionResult,
|
|
|
|
|
|
OperationLog,
|
|
|
|
|
|
TransactionLog,
|
|
|
|
|
|
TransactionOptions,
|
|
|
|
|
|
TransactionManagerConfig,
|
|
|
|
|
|
ITransactionStorage,
|
|
|
|
|
|
ITransactionOperation,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
ITransactionContext
|
|
|
|
|
|
} from './core/types.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
TransactionContext,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
createTransactionContext
|
|
|
|
|
|
} from './core/TransactionContext.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
TransactionManager,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
createTransactionManager
|
|
|
|
|
|
} from './core/TransactionManager.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
// Storage | 存储
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
MemoryStorage,
|
|
|
|
|
|
createMemoryStorage,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
type MemoryStorageConfig
|
|
|
|
|
|
} from './storage/MemoryStorage.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
RedisStorage,
|
|
|
|
|
|
createRedisStorage,
|
|
|
|
|
|
type RedisStorageConfig,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
type RedisClient
|
|
|
|
|
|
} from './storage/RedisStorage.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
MongoStorage,
|
|
|
|
|
|
createMongoStorage,
|
|
|
|
|
|
type MongoStorageConfig,
|
|
|
|
|
|
type MongoDb,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
type MongoCollection
|
|
|
|
|
|
} from './storage/MongoStorage.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
// Operations | 操作
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
2025-12-29 15:02:13 +08:00
|
|
|
|
export { BaseOperation } from './operations/BaseOperation.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
CurrencyOperation,
|
|
|
|
|
|
createCurrencyOperation,
|
|
|
|
|
|
type CurrencyOperationType,
|
|
|
|
|
|
type CurrencyOperationData,
|
|
|
|
|
|
type CurrencyOperationResult,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
type ICurrencyProvider
|
|
|
|
|
|
} from './operations/CurrencyOperation.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
InventoryOperation,
|
|
|
|
|
|
createInventoryOperation,
|
|
|
|
|
|
type InventoryOperationType,
|
|
|
|
|
|
type InventoryOperationData,
|
|
|
|
|
|
type InventoryOperationResult,
|
|
|
|
|
|
type IInventoryProvider,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
type ItemData
|
|
|
|
|
|
} from './operations/InventoryOperation.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
TradeOperation,
|
|
|
|
|
|
createTradeOperation,
|
|
|
|
|
|
type TradeOperationData,
|
|
|
|
|
|
type TradeOperationResult,
|
|
|
|
|
|
type TradeItem,
|
|
|
|
|
|
type TradeCurrency,
|
|
|
|
|
|
type TradeParty,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
type ITradeProvider
|
|
|
|
|
|
} from './operations/TradeOperation.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
// Distributed | 分布式
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
SagaOrchestrator,
|
|
|
|
|
|
createSagaOrchestrator,
|
|
|
|
|
|
type SagaOrchestratorConfig,
|
|
|
|
|
|
type SagaStep,
|
|
|
|
|
|
type SagaStepState,
|
|
|
|
|
|
type SagaStepLog,
|
|
|
|
|
|
type SagaLog,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
type SagaResult
|
|
|
|
|
|
} from './distributed/SagaOrchestrator.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
// Integration | 集成
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
withTransactions,
|
|
|
|
|
|
TransactionRoom,
|
|
|
|
|
|
type TransactionRoomConfig,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
type ITransactionRoom
|
|
|
|
|
|
} from './integration/RoomTransactionMixin.js';
|
2025-12-29 10:54:00 +08:00
|
|
|
|
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
// Tokens | 令牌
|
|
|
|
|
|
// =============================================================================
|
|
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
|
TransactionManagerToken,
|
2025-12-29 15:02:13 +08:00
|
|
|
|
TransactionStorageToken
|
|
|
|
|
|
} from './tokens.js';
|