texas-poker-core 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +32 -0
- package/.commitlintrc +12 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +1 -0
- package/.prettierignore +4 -0
- package/.prettierrc +8 -0
- package/eslint.config.mjs +55 -0
- package/global.env.d.ts +5 -0
- package/jest.config.js +10 -0
- package/jest.setup.js +2 -0
- package/package.json +3 -6
- package/src/Controller/index.test.ts +66 -0
- package/src/Controller/index.ts +131 -0
- package/src/Dealer/index.test.ts +32 -0
- package/src/Dealer/index.ts +359 -0
- package/src/Deck/constant.ts +122 -0
- package/src/Deck/core.test.ts +98 -0
- package/src/Deck/core.ts +245 -0
- package/src/Deck/index.test.ts +100 -0
- package/src/Deck/index.ts +120 -0
- package/src/Player/constant.ts +55 -0
- package/src/Player/index.test.ts +61 -0
- package/src/Player/index.ts +533 -0
- package/src/Pool/index.test.ts +117 -0
- package/src/Pool/index.ts +208 -0
- package/src/Room/index.test.ts +109 -0
- package/src/Room/index.ts +159 -0
- package/src/index.ts +3 -0
- package/src/main.test.ts +22 -0
- package/src/main.ts +58 -0
- package/src/utils/index.ts +38 -0
- package/tsconfig.json +26 -0
package/.babelrc
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"ignore": ["**/*.test.ts"],
|
|
3
|
+
"presets": [
|
|
4
|
+
"@babel/preset-typescript",
|
|
5
|
+
[
|
|
6
|
+
"@babel/preset-env",
|
|
7
|
+
{
|
|
8
|
+
"targets": {
|
|
9
|
+
// "node": "current"
|
|
10
|
+
"browsers": "> 0.25%"
|
|
11
|
+
},
|
|
12
|
+
// false标识es module
|
|
13
|
+
"modules": "commonjs",
|
|
14
|
+
// 根据使用的特性引入 polyfills
|
|
15
|
+
"useBuiltIns": "usage",
|
|
16
|
+
// 指定 core-js 版本
|
|
17
|
+
"corejs": { "version": 3, "proposals": true }
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
],
|
|
21
|
+
"plugins": [
|
|
22
|
+
[
|
|
23
|
+
"module-resolver",
|
|
24
|
+
{
|
|
25
|
+
"root": ["./src"],
|
|
26
|
+
"alias": {
|
|
27
|
+
"@": "./src"
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
]
|
|
32
|
+
}
|
package/.commitlintrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx --no -- commitlint --edit $1
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx lint-staged
|
package/.prettierignore
ADDED
package/.prettierrc
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import globals from 'globals'
|
|
2
|
+
import pluginJs from '@eslint/js'
|
|
3
|
+
import tseslint from 'typescript-eslint'
|
|
4
|
+
|
|
5
|
+
/** @type {import('eslint').Linter.Config[]} */
|
|
6
|
+
export default [
|
|
7
|
+
{ files: ['**/*.{ts}'], ignores: ['dist', 'types'] },
|
|
8
|
+
{
|
|
9
|
+
languageOptions: { globals: globals.node }
|
|
10
|
+
},
|
|
11
|
+
pluginJs.configs.recommended,
|
|
12
|
+
...tseslint.configs.recommended,
|
|
13
|
+
{
|
|
14
|
+
rules: {
|
|
15
|
+
'no-unused-private-class-members': 'warn',
|
|
16
|
+
'no-unused-vars': 'off',
|
|
17
|
+
'@typescript-eslint/no-unused-vars': 'error',
|
|
18
|
+
'@typescript-eslint/no-explicit-any': 'error',
|
|
19
|
+
'no-undef': 'off',
|
|
20
|
+
'linebreak-style': ['error', 'unix'],
|
|
21
|
+
quotes: ['error', 'single'],
|
|
22
|
+
semi: ['error', 'never'],
|
|
23
|
+
'no-else-return': 'error',
|
|
24
|
+
'comma-spacing': 'error',
|
|
25
|
+
'object-curly-spacing': ['error', 'always'],
|
|
26
|
+
'@typescript-eslint/no-non-null-assertion': 'off',
|
|
27
|
+
'no-console': 'off',
|
|
28
|
+
'no-const-assign': 'error',
|
|
29
|
+
'no-constant-condition': 'error',
|
|
30
|
+
'no-empty': 'warn',
|
|
31
|
+
'no-func-assign': 'error',
|
|
32
|
+
'no-inline-comments': 'error',
|
|
33
|
+
'no-lonely-if': 'error',
|
|
34
|
+
'no-multiple-empty-lines': ['error', { max: 1 }],
|
|
35
|
+
'no-trailing-spaces': 'error',
|
|
36
|
+
camelcase: 'error',
|
|
37
|
+
'no-dupe-keys': 'error',
|
|
38
|
+
'no-nested-ternary': 'error',
|
|
39
|
+
'no-param-reassign': 'error',
|
|
40
|
+
'no-self-compare': 'error',
|
|
41
|
+
'no-unneeded-ternary': 'error',
|
|
42
|
+
'comma-dangle': ['error', 'never'],
|
|
43
|
+
'arrow-spacing': 'error',
|
|
44
|
+
'arrow-parens': 'error',
|
|
45
|
+
// 立即执行函数风格
|
|
46
|
+
'wrap-iife': ['error', 'inside'],
|
|
47
|
+
'key-spacing': [
|
|
48
|
+
'error',
|
|
49
|
+
{
|
|
50
|
+
afterColon: true
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
]
|
package/global.env.d.ts
ADDED
package/jest.config.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
preset: 'ts-jest',
|
|
3
|
+
roots: ['<rootDir>/src'],
|
|
4
|
+
setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
|
|
5
|
+
transformIgnorePatterns: ['/node_modules/'],
|
|
6
|
+
moduleNameMapper: {
|
|
7
|
+
// <rootDir>代表jest.config文件所在的根目录
|
|
8
|
+
'@/(.*)$': '<rootDir>/src/$1'
|
|
9
|
+
}
|
|
10
|
+
}
|
package/jest.setup.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "texas-poker-core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "德州扑克核心功能",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "types/index.d.ts",
|
|
@@ -14,9 +14,6 @@
|
|
|
14
14
|
"prepublishOnly": "tsc --noEmit && pnpm run test",
|
|
15
15
|
"build": "cross-env PROJECT_ENV=prd tsc && babel src --out-dir dist --extensions \".ts\""
|
|
16
16
|
},
|
|
17
|
-
"files": [
|
|
18
|
-
"dist/**/*"
|
|
19
|
-
],
|
|
20
17
|
"keywords": [
|
|
21
18
|
"德州",
|
|
22
19
|
"扑克"
|
|
@@ -24,12 +21,12 @@
|
|
|
24
21
|
"author": "三分",
|
|
25
22
|
"license": "ISC",
|
|
26
23
|
"dependencies": {
|
|
27
|
-
"cross-env": "^7.0.3",
|
|
28
|
-
"jest": "^29.7.0",
|
|
29
24
|
"ramda": "^0.30.1",
|
|
30
25
|
"core-js": "3"
|
|
31
26
|
},
|
|
32
27
|
"devDependencies": {
|
|
28
|
+
"jest": "^29.7.0",
|
|
29
|
+
"cross-env": "^7.0.3",
|
|
33
30
|
"@babel/cli": "^7.26.4",
|
|
34
31
|
"@babel/core": "^7.26.10",
|
|
35
32
|
"@babel/preset-env": "^7.26.9",
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import Room from '@/Room'
|
|
2
|
+
import Controller from '.'
|
|
3
|
+
import Dealer from '@/Dealer'
|
|
4
|
+
import { Player } from '@/Player'
|
|
5
|
+
|
|
6
|
+
const dealer = new Dealer(1000)
|
|
7
|
+
const controller = new Controller(dealer)
|
|
8
|
+
const p1 = new Player({
|
|
9
|
+
user: { id: 1, balance: 5000 },
|
|
10
|
+
lowestBetAmount: dealer.getLowestBetAmount(),
|
|
11
|
+
controller
|
|
12
|
+
})
|
|
13
|
+
const room = new Room(dealer)
|
|
14
|
+
|
|
15
|
+
const p2 = new Player({
|
|
16
|
+
lowestBetAmount: 1000,
|
|
17
|
+
user: { id: 2, balance: 30000 },
|
|
18
|
+
controller
|
|
19
|
+
})
|
|
20
|
+
const p3 = new Player({
|
|
21
|
+
lowestBetAmount: 1000,
|
|
22
|
+
user: { id: 3, balance: 10000 },
|
|
23
|
+
controller
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
const p4 = new Player({
|
|
27
|
+
lowestBetAmount: 1000,
|
|
28
|
+
user: { id: 4, balance: 20000 },
|
|
29
|
+
controller
|
|
30
|
+
})
|
|
31
|
+
describe('class Controller', () => {
|
|
32
|
+
test('function transferControl', () => {
|
|
33
|
+
room.addPlayer(p1)
|
|
34
|
+
room.addPlayer(p2)
|
|
35
|
+
room.addPlayer(p3)
|
|
36
|
+
room.addPlayer(p4)
|
|
37
|
+
room.getDealer().setButton(p3)
|
|
38
|
+
// 发牌, 分配角色
|
|
39
|
+
room.ready()
|
|
40
|
+
// room.getDealer().log()
|
|
41
|
+
|
|
42
|
+
controller.start()
|
|
43
|
+
room.getDealer().log()
|
|
44
|
+
expect(controller.activePlayer === p1).toBe(true)
|
|
45
|
+
// p1.log()
|
|
46
|
+
p1.bet(4000)
|
|
47
|
+
// p1.log()
|
|
48
|
+
|
|
49
|
+
expect(controller.activePlayer === p2).toBe(true)
|
|
50
|
+
// p2.log()
|
|
51
|
+
p2.allIn(dealer)
|
|
52
|
+
// p2.log()
|
|
53
|
+
|
|
54
|
+
expect(controller.activePlayer === p3).toBe(true)
|
|
55
|
+
// p3.log()
|
|
56
|
+
p3.allIn(dealer)
|
|
57
|
+
|
|
58
|
+
expect(controller.activePlayer === p4).toBe(true)
|
|
59
|
+
p4.allIn(dealer)
|
|
60
|
+
controller.end()
|
|
61
|
+
expect(p1.getBalance()).toEqual(1000)
|
|
62
|
+
expect(p2.getBalance()).toEqual(10_000)
|
|
63
|
+
expect(p3.getBalance()).toEqual(0)
|
|
64
|
+
expect(p4.getBalance()).toEqual(0)
|
|
65
|
+
})
|
|
66
|
+
})
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// 控制游戏的进程
|
|
2
|
+
import Dealer from '../Dealer'
|
|
3
|
+
import { Player } from '../Player'
|
|
4
|
+
|
|
5
|
+
export type Stage = 'pre-flop' | 'flop' | 'turn' | 'river' | 'showdown'
|
|
6
|
+
const stages: Stage[] = ['pre-flop', 'flop', 'turn', 'river', 'showdown']
|
|
7
|
+
|
|
8
|
+
class Controller {
|
|
9
|
+
#status: 'on' | 'pause' | 'abort' | 'waiting' = 'waiting'
|
|
10
|
+
#stage: Stage = 'pre-flop'
|
|
11
|
+
#activePlayer: Player | null = null
|
|
12
|
+
#timer: NodeJS.Timeout | null = null
|
|
13
|
+
// 记录游戏的进行时间,单位 second
|
|
14
|
+
#count = 0
|
|
15
|
+
#dealer: Dealer
|
|
16
|
+
constructor(dealer: Dealer) {
|
|
17
|
+
this.#dealer = dealer
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
get stage() {
|
|
21
|
+
return this.#stage
|
|
22
|
+
}
|
|
23
|
+
setControl(player: Player | null) {
|
|
24
|
+
this.#activePlayer = player
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
transferControlToNext(nextPlayer: Player | null) {
|
|
28
|
+
this.setControl(nextPlayer)
|
|
29
|
+
nextPlayer?.getControl()
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
get activePlayer() {
|
|
33
|
+
return this.#activePlayer
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// 每个玩家行动之后, 都要调用此方法
|
|
37
|
+
// 推进到新的阶段后, 将控制权交给小盲位
|
|
38
|
+
// 如果小盲位已经出局或者无法行动(all-in), 依次将控制权交给下一个可以行动的玩家
|
|
39
|
+
tryToAdvanceGameToNextStage() {
|
|
40
|
+
if (this.#stage === 'showdown') throw new Error('游戏已经结束')
|
|
41
|
+
|
|
42
|
+
const maxBetAmount = this.#dealer.getCurrentStageMaxBetAmount()
|
|
43
|
+
// this.#dealer.forEach((p) => p.log('ss,'))
|
|
44
|
+
const players = this.#dealer
|
|
45
|
+
// 场上正常下注的玩家, 下注金额需要都等于最大下注金额
|
|
46
|
+
.filter((p) => p.getStatus() === 'waiting')
|
|
47
|
+
|
|
48
|
+
const allPlayersBetThSameAmount = players
|
|
49
|
+
.map((p) => p.getCurrentStageTotalAmount())
|
|
50
|
+
.every((amount) => amount === maxBetAmount)
|
|
51
|
+
|
|
52
|
+
if (allPlayersBetThSameAmount) {
|
|
53
|
+
const index = stages.findIndex((stage) => stage === this.#stage)
|
|
54
|
+
this.#stage = stages[index + 1]
|
|
55
|
+
this.setControl(this.#dealer.getTheFirstPlayerToAct())
|
|
56
|
+
this.#dealer.resetCurrentStageTotalAmount()
|
|
57
|
+
this.#dealer.resetActionsOfPlayers()
|
|
58
|
+
console.log('游戏进入下一个阶段 => ', this.#stage)
|
|
59
|
+
return true
|
|
60
|
+
}
|
|
61
|
+
return false
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 创建一个迭代器控制游戏进行
|
|
65
|
+
*gameIterator(): Generator<void> {
|
|
66
|
+
while (true) {
|
|
67
|
+
// this.transferControlToNext(this.#dealer.);
|
|
68
|
+
// 暂停,等待玩家行动
|
|
69
|
+
yield
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @description 开始计时器, 将控制权移交给第一个可以行动的玩家
|
|
75
|
+
*/
|
|
76
|
+
start() {
|
|
77
|
+
// 将控制权给第一个可以行动的玩家
|
|
78
|
+
this.transferControlToNext(this.#dealer.getTheFirstPlayerToAct())
|
|
79
|
+
|
|
80
|
+
this.#status = 'on'
|
|
81
|
+
this.#stage = 'pre-flop'
|
|
82
|
+
this.startTimer()
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
startTimer() {
|
|
86
|
+
// 避免重复开启计时器
|
|
87
|
+
if (this.#timer) return
|
|
88
|
+
|
|
89
|
+
this.#timer = setInterval(() => {
|
|
90
|
+
this.#count++
|
|
91
|
+
}, 1000)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @description 继续游戏
|
|
96
|
+
*/
|
|
97
|
+
continue() {
|
|
98
|
+
this.#status = 'on'
|
|
99
|
+
this.#activePlayer?.continue()
|
|
100
|
+
this.startTimer()
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
clearTimer() {
|
|
104
|
+
if (this.#timer) {
|
|
105
|
+
clearInterval(this.#timer)
|
|
106
|
+
this.#timer = null
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* @description 结束游戏, 回收控制权, 清除玩家的计时器
|
|
111
|
+
*/
|
|
112
|
+
end() {
|
|
113
|
+
this.clearTimer()
|
|
114
|
+
this.#stage = 'showdown'
|
|
115
|
+
|
|
116
|
+
this.#activePlayer?.removeControl()
|
|
117
|
+
this.#activePlayer?.clearTimer()
|
|
118
|
+
this.#activePlayer = null
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @description 暂停游戏
|
|
123
|
+
*/
|
|
124
|
+
pause() {
|
|
125
|
+
this.#status = 'pause'
|
|
126
|
+
|
|
127
|
+
this.clearTimer()
|
|
128
|
+
this.activePlayer?.pause()
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
export default Controller
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import Dealer from '.'
|
|
2
|
+
import { Player } from '@/Player'
|
|
3
|
+
import Controller from '@/Controller'
|
|
4
|
+
|
|
5
|
+
describe('dealer', () => {
|
|
6
|
+
test('Game init successfully', () => {
|
|
7
|
+
const dealer = new Dealer(200)
|
|
8
|
+
const lowestBetAmount = dealer.getLowestBetAmount()
|
|
9
|
+
const controller = new Controller(dealer)
|
|
10
|
+
dealer.join(
|
|
11
|
+
new Player({
|
|
12
|
+
user: { id: 2, balance: 40000 },
|
|
13
|
+
lowestBetAmount,
|
|
14
|
+
controller
|
|
15
|
+
})
|
|
16
|
+
)
|
|
17
|
+
dealer.join(
|
|
18
|
+
new Player({
|
|
19
|
+
user: { id: 3, balance: 40000 },
|
|
20
|
+
lowestBetAmount,
|
|
21
|
+
controller
|
|
22
|
+
})
|
|
23
|
+
)
|
|
24
|
+
dealer.start()
|
|
25
|
+
|
|
26
|
+
expect(dealer.getDeck().getCards().length).toEqual(52)
|
|
27
|
+
expect(dealer.getDeck().getPokes().commonPokes.length).toEqual(5)
|
|
28
|
+
expect(dealer.getDeck().getPokes().handPokes.length).toEqual(2)
|
|
29
|
+
expect(dealer.getPlayersCount()).toEqual(2)
|
|
30
|
+
expect(dealer.getLowestBetAmount()).toEqual(200)
|
|
31
|
+
})
|
|
32
|
+
})
|