@ block52/ poker-vm- sdk (英语)
TypeScript SDK for Block52 Poker VM - 一个以Cosmos为主的以块链为主的扑克平台.
特征
- 🎮 完成扑克游戏逻辑 - 卡片交易、手评价、游戏状态管理
- 🔐 Cosmos 区块链集成 - 交易签名、查询和钱包管理
- 🎲 舱面管理 - 摇摆、交易和追踪卡状态 以区块链验证随机性
- 🤝 类型安全 - 完整类型脚本支持,并用原生生物定义生成的类型
- 🔄 实时更新 - 对游戏状态同步的WebSocket支持
- 💰 调试操作 - 安装在USDC信号处理和转换中
安装
npm install @block52/poker-vm-sdk
或带线:
yarn add @block52/poker-vm-sdk
快速启动
创建签名客户端
import { createSigningClientFromMnemonic, getDefaultCosmosConfig } from '@block52/poker-vm-sdk';
// Connect to the blockchain
const config = getDefaultCosmosConfig("mainnet"); // or "testnet", "localhost"
const mnemonic = "your twelve word mnemonic phrase here...";
const client = await createSigningClientFromMnemonic(config, mnemonic);
const walletAddress = await client.getWalletAddress();
console.log("Wallet Address:", walletAddress);
创建扑克游戏
// Define game parameters
const gameParams = {
gameFormat: "cash", // or "tournament"
gameVariant: "texas-holdem",
minPlayers: 2,
maxPlayers: 6,
minBuyIn: client.usdcToB52usdc(10), // 10 USDC minimum
maxBuyIn: client.usdcToB52usdc(100), // 100 USDC maximum
smallBlind: client.usdcToB52usdc(0.5), // 0.5 USDC small blind
bigBlind: client.usdcToB52usdc(1), // 1 USDC big blind
timeout: 60 // 60 seconds per action
};
// Create the game on-chain
const txHash = await client.createGame(
gameParams.gameFormat,
gameParams.gameVariant,
gameParams.minPlayers,
gameParams.maxPlayers,
gameParams.minBuyIn,
gameParams.maxBuyIn,
gameParams.smallBlind,
gameParams.bigBlind,
gameParams.timeout
);
console.log("Game created! Transaction:", txHash);
与牌和德克牌合作
import { Deck, PokerSolver } from '@block52/poker-vm-sdk';
// Create and shuffle a deck
const deck = new Deck();
deck.shuffle([1, 2, 3, 4, 5]); // Seed for deterministic shuffle
// Deal cards
const playerHand = deck.deal(2); // Deal 2 cards to player
const communityCards = deck.deal(5); // Deal 5 community cards
// Evaluate hand strength
const allCards = [...playerHand, ...communityCards];
const evaluation = PokerSolver.evaluateSevenCardHand(allCards);
console.log("Hand type:", evaluation.type);
console.log("Hand rank:", evaluation.rank);
// Compare two hands
const player1Evaluation = PokerSolver.evaluateSevenCardHand(player1Cards);
const player2Evaluation = PokerSolver.evaluateSevenCardHand(player2Cards);
const result = PokerSolver.compareHands(player1Evaluation, player2Evaluation);
// result > 0: player1 wins, < 0: player2 wins, 0: tie
查询游戏状态
import { CosmosClient, getDefaultCosmosConfig } from '@block52/poker-vm-sdk';
// Create a query-only client (no signing required)
const config = getDefaultCosmosConfig("mainnet");
const queryClient = new CosmosClient(config);
// Query a game by ID
const game = await queryClient.getGame(gameId);
console.log("Game options:", game);
// Get game state
const gameState = await queryClient.getGameState(gameId);
console.log("Game state:", gameState);
// List all games
const allGames = await queryClient.listGames();
console.log("Active games:", allGames.length);
// Get games for a specific player
const playerGames = await queryClient.getPlayerGames(playerAddress);
console.log("Player games:", playerGames.length);
调试操作
// Convert between USDC and b52usdc (blockchain representation)
const usdcAmount = 100; // 100 USDC
const b52usdcAmount = client.usdcToB52usdc(usdcAmount);
// Convert back
const usdcValue = client.b52usdcToUsdc(b52usdcAmount);
// Check balance
const balance = await client.getB52USDCBalance(walletAddress);
console.log("Balance:", client.b52usdcToUsdc(balance), "USDC");
API 参考
签名空间
扩展 CosmosClient 具有交易签名能力。 包括所有查询方法 CosmosClient 加:
交易方法
createGame(gameFormat, gameVariant, minPlayers, maxPlayers, minBuyIn, maxBuyIn, smallBlind, bigBlind, timeout, rakeConfig?, sngConfig?)- 创建新的扑克游戏joinGame(gameId, seat, buyInAmount)- 在一个特定位置加入已有的游戏leaveGame(gameId)- 留下游戏和现金performAction(gameId, action, amount?, data?)- 实施扑克动作(倍数、呼叫、提升、检查、全入、交易)topUp(gameId, amount)- 将芯片添加到桌子上sendTokens(fromAddress, toAddress, amount, denom?, memo?)- 向另一个地址发送签名sendB52USDC(fromAddress, toAddress, amount, memo?)- 发送b52USDC令牌
桥梁方法
processDeposit(depositIndex)- 按指数处理伊特鲁姆的桥梁存款initiateWithdrawal(baseAddress, amount)- 开始撤回基地getWithdrawalRequest(nonce)- 询问非ce提出的具体退出请求listWithdrawalRequests(cosmosAddress?)- 除名请求
钱包方法
getWalletAddress()- 获取当前钱包地址setWallet(wallet)- 为签名设置一个新的钱包getWallet()- 获取当前钱包实例disconnect()- 关闭客户端连接
宇宙线
通过 REST API 读取块链状态的查询客户端 .
块链查询方法
getAccount(address)- 获取账户信息getAllBalances(address)- 获得所有象征性余额的地址getBalance(address, denom?)- 获得具体符号余额(默认:b52USDC)getHeight()- 获取当前块高getTx(txHash)-通过散列交易getBlock(height)- 按高度逐个街区getLatestBlock()- 获取最新块getBlocks(startHeight, count?)- 从高处开始多块块getLatestBlocks(count?)- 获取最新的N区块
游戏查询方法
getGame(gameId)- 通过ID获取游戏选项/配置getGameState(gameId)- 通过 ID 获取当前游戏状态listGames()- 获得所有活跃的游戏findGames(min?, max?)- 按玩家容量查找游戏(minPlayers/maxPlayers)getPlayerGames(playerAddress)- 为特定玩家获得所有游戏getLegalActions(gameId, playerAddress?)- 为游戏中的玩家争取法律行动
调试方法( U)
getB52USDCBalance(address)- 获得B52USDC的地址余额b52usdcToUsdc(amount)- 将b52USDC(微单位)转换为USDC显示格式usdcToB52usdc(amount)- 将USDC转换为b52USDC(微单位)calculateEquity(hands, board?, dead?, simulations?)- 通过Monte Carlo计算手股calculateEquityFull(hands, board?, dead?, simulations?)- 用完整的元数据计算公平listWithdrawalRequests(cosmosAddress?)- 除名请求(全部或具体地址)
地标
采用区块链集成的牌甲板管理.
方法
constructor(deck?: string)- 从元音字符串或新订购的甲板创建甲板getNext()- 绘制下一张牌deal(amount)- 发多张牌toString()- 将甲板串起shuffle(seed)- Shuffle甲板(仅测试,生产使用区块链随机性)
常数
Deck.DECK_SIZE- 标准甲板上的牌数(52)Deck.RANK_MAP- 排序值图到字符串(A、T、J、Q、K)Deck.SUIT_MAP- 服饰值图到字符串(C、D、H、S)
扑克索尔
手评和扑克逻辑.
静态方法
evaluatePartialHand(cards)- 评估扑克手力(部分或完整的手)evaluateSevenCardHand(cards)- 从7张牌上评估最好的5张牌手compareHands(hand1, hand2)- 用两只手来决定赢家
钱包公用事业
钱包管理费.
generateWallet(prefix?, wordCount?)- 生成一个带有随机元件的新钱包( 12或24个字)createWalletFromMnemonic(mnemonic, prefix?)- 从现有元件创建钱包getAddressFromMnemonic(mnemonic, prefix?)- 来自元音的衍生地址isValidMnemonic(mnemonic)- 验证元音短语BLOCK52_HD_PATH- Block52的标准Cosmos HD衍生路径
配置
网络终点
import { getDefaultCosmosConfig } from '@block52/poker-vm-sdk';
// Mainnet
const mainnetConfig = getDefaultCosmosConfig("mainnet");
// Testnet
const testnetConfig = getDefaultCosmosConfig("testnet");
// Local development
const localConfig = getDefaultCosmosConfig("localhost");
// Custom configuration
const customConfig = {
rpcEndpoint: "https://your-node.example.com:26657",
restEndpoint: "https://your-node.example.com:1317",
chainId: "pokerchain-1",
prefix: "poker"
};
实例
看看这个 实例 完整工作示例目录 :
- 创建游戏实例( t) - 制作扑克游戏的完整实例
- 钱包示例.ts - 钱包的建立和管理
举例来说:
git clone https://github.com/block52/poker-vm.git
cd poker-vm/sdk
yarn install
yarn build
npx ts-node examples/create-game-example.ts
发展
来源建筑
# Clone the repository
git clone https://github.com/block52/poker-vm.git
cd poker-vm/sdk
# Install dependencies
yarn install
# Build the SDK
yarn build
# Run tests
yarn test
# Lint code
yarn lint
发布到 npm
SDK在创建发行时通过GitHub Actions自动发布到npm.
自动出版程序
-
更新版本 内
package.json数字 :cd sdk # Edit package.json to update version (e.g., "1.1.9") -
提交和推 版本更改 :
git add package.json git commit -m "chore: update SDK version to 1.1.9" git push -
创建 GitHub 发布 带有标签匹配
sdk-v*数字 :gh release create sdk-v1.1.9 --title "SDK v1.1.9" --notes "Release notes here"
GitHub 动作工作流程将自动:
- 安装依赖关系
- 运行测试
- 构建 SDK
- 校验软件包。 json 版本匹配发行标记
- 向 npm 公开发布
重要注释 :
- 释放标签必须从
sdk-v(例如,sdk-v1.1.9(中文(简体) ) - 版本在
package.json必须匹配标签版本(例如,1.1.9标记sdk-v1.1.9(中文(简体) ) - 这个
NPM_TOKEN密钥必须在 GitHub 仓库设置中配置 - 工作流程使用
--ignore-scripts跳过prepublishOnly脚本已构建 SDK
项目结构
sdk/
├── src/
│ ├── index.ts # Main entry point
│ ├── client.ts # Base client implementation
│ ├── cosmosClient.ts # Query client
│ ├── signingClient.ts # Signing client
│ ├── deck.ts # Deck class
│ ├── pokerSolver.ts # Hand evaluation
│ ├── walletUtils.ts # Wallet utilities
│ ├── types/ # TypeScript type definitions
│ ├── utils/ # Utility functions
│ └── pokerchain.poker.v1/ # Generated protobuf types
├── examples/ # Usage examples
├── tests/ # Test files
└── dist/ # Built output (after yarn build)
类型脚本支持
这个SDK被用TypeScript来写,包括了完整的类型定义. 无额外 @types 需要软件包.
import type { GameStateResponseDTO, PlayerDTO, ActionDTO } from '@block52/poker-vm-sdk';
光圈类型
SDK定义了 单一真相来源 所有游戏类型:
GameStateResponseDTO- 线上游戏数据的根类型TexasHoldemStateDTO- 游戏状态( 包含)players, (中文(简体) ),communityCards页:1GameOptionsDTO- 游戏配置( 无)format或者说owner- 使用根字段)PlayerDTO- 玩家资讯
见 克劳德·姆德 为完整的类型规则.
浏览器支持
SDK同时在Node.js和浏览器环境中工作. 对于浏览器的使用,可能需要为节点.js内置配置多填充:
// Example webpack config
module.exports = {
resolve: {
fallback: {
"stream": require.resolve("stream-browserify"),
"crypto": require.resolve("crypto-browserify"),
"buffer": require.resolve("buffer/")
}
}
};
捐款
欢迎捐款! 请看 主要仓库 捐款准则.
许可证
麻省理工学院执照 - 看 自由主义 详细文件.
支助
相关项目
更改日志
见 释放 用于版本历史和更改.