trucoshi 0.0.9 → 0.0.11
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/{build → dist}/lib/classes/Deck.d.ts +0 -0
- package/{build → dist}/lib/classes/Deck.js +0 -0
- package/{build → dist}/lib/classes/Hand.d.ts +0 -0
- package/{build → dist}/lib/classes/Hand.js +0 -0
- package/{build → dist}/lib/classes/Match.d.ts +0 -0
- package/{build → dist}/lib/classes/Match.js +0 -0
- package/{build → dist}/lib/classes/Play.d.ts +0 -0
- package/{build → dist}/lib/classes/Play.js +0 -0
- package/{build → dist}/lib/classes/Player.d.ts +0 -0
- package/{build → dist}/lib/classes/Player.js +0 -0
- package/{build → dist}/lib/classes/Round.d.ts +0 -0
- package/{build → dist}/lib/classes/Round.js +0 -0
- package/{build → dist}/lib/classes/Table.d.ts +0 -0
- package/{build → dist}/lib/classes/Table.js +0 -0
- package/{build → dist}/lib/classes/Team.d.ts +0 -0
- package/{build → dist}/lib/classes/Team.js +0 -0
- package/{build → dist}/lib/classes/Truco.d.ts +0 -0
- package/{build → dist}/lib/classes/Truco.js +0 -0
- package/{build → dist}/lib/constants.d.ts +0 -0
- package/{build → dist}/lib/constants.js +0 -0
- package/{build → dist}/lib/index.d.ts +0 -0
- package/{build → dist}/lib/index.js +0 -0
- package/{build → dist}/lib/types.d.ts +0 -0
- package/{build → dist}/lib/types.js +0 -0
- package/{build → dist}/lib/utils.d.ts +0 -0
- package/{build → dist}/lib/utils.js +0 -0
- package/{build → dist}/server/index.d.ts +0 -0
- package/{build → dist}/server/index.js +0 -0
- package/{build → dist}/server/match.d.ts +0 -0
- package/{build → dist}/server/match.js +0 -0
- package/{build → dist}/server/types.d.ts +0 -0
- package/{build → dist}/server/types.js +0 -0
- package/{build → dist}/test/autoplay.d.ts +0 -0
- package/{build → dist}/test/autoplay.js +0 -0
- package/{build → dist}/test/play.d.ts +0 -0
- package/{build → dist}/test/play.js +0 -0
- package/package.json +5 -3
- package/.prettierrc.json +0 -7
- package/nodemon.json +0 -6
- package/src/lib/classes/Deck.ts +0 -25
- package/src/lib/classes/Hand.ts +0 -207
- package/src/lib/classes/Match.ts +0 -79
- package/src/lib/classes/Play.ts +0 -40
- package/src/lib/classes/Player.ts +0 -37
- package/src/lib/classes/Round.ts +0 -30
- package/src/lib/classes/Table.ts +0 -32
- package/src/lib/classes/Team.ts +0 -41
- package/src/lib/classes/Truco.ts +0 -72
- package/src/lib/constants.ts +0 -108
- package/src/lib/index.ts +0 -135
- package/src/lib/types.ts +0 -216
- package/src/lib/utils.ts +0 -66
- package/src/server/index.ts +0 -34
- package/src/server/match.ts +0 -0
- package/src/server/types.ts +0 -12
- package/src/test/autoplay.ts +0 -57
- package/src/test/play.ts +0 -122
- package/tsconfig.json +0 -19
package/src/lib/classes/Truco.ts
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { ITruco } from "../types"
|
|
2
|
-
|
|
3
|
-
export function Truco() {
|
|
4
|
-
function* trucoAnswerGeneratorSequence() {
|
|
5
|
-
let i = 0
|
|
6
|
-
while (i < truco.players.length && truco.answer === null) {
|
|
7
|
-
const player = truco.players[truco.turn]
|
|
8
|
-
truco.setCurrentPlayer(player)
|
|
9
|
-
if (player.disabled) {
|
|
10
|
-
truco.setCurrentPlayer(null)
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (truco.turn >= truco.players.length - 1) {
|
|
14
|
-
truco.setTurn(0)
|
|
15
|
-
} else {
|
|
16
|
-
truco.setTurn(truco.turn + 1)
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
i++
|
|
20
|
-
|
|
21
|
-
yield truco
|
|
22
|
-
}
|
|
23
|
-
yield truco
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const truco: ITruco = {
|
|
27
|
-
turn: 0,
|
|
28
|
-
state: 1,
|
|
29
|
-
teamIdx: null,
|
|
30
|
-
answer: null,
|
|
31
|
-
currentPlayer: null,
|
|
32
|
-
generator: trucoAnswerGeneratorSequence(),
|
|
33
|
-
players: [],
|
|
34
|
-
sayTruco(teamIdx, players) {
|
|
35
|
-
truco.teamIdx = teamIdx
|
|
36
|
-
truco.answer = null
|
|
37
|
-
truco.players = players
|
|
38
|
-
truco.generator = trucoAnswerGeneratorSequence()
|
|
39
|
-
return truco
|
|
40
|
-
},
|
|
41
|
-
setPlayers(players) {
|
|
42
|
-
truco.players = players
|
|
43
|
-
},
|
|
44
|
-
setAnswer(answer) {
|
|
45
|
-
if (answer) {
|
|
46
|
-
truco.state++
|
|
47
|
-
}
|
|
48
|
-
if (answer !== null) {
|
|
49
|
-
truco.teamIdx = null
|
|
50
|
-
}
|
|
51
|
-
truco.answer = answer
|
|
52
|
-
return truco
|
|
53
|
-
},
|
|
54
|
-
setTeam(idx: 0 | 1) {
|
|
55
|
-
truco.teamIdx = idx
|
|
56
|
-
return truco.teamIdx
|
|
57
|
-
},
|
|
58
|
-
setTurn(turn) {
|
|
59
|
-
truco.turn = turn
|
|
60
|
-
return truco.turn
|
|
61
|
-
},
|
|
62
|
-
setCurrentPlayer(player) {
|
|
63
|
-
truco.currentPlayer = player
|
|
64
|
-
return truco.currentPlayer
|
|
65
|
-
},
|
|
66
|
-
getNextPlayer() {
|
|
67
|
-
return truco.generator.next()
|
|
68
|
-
},
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return truco
|
|
72
|
-
}
|
package/src/lib/constants.ts
DELETED
|
@@ -1,108 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EEnvidoCommand,
|
|
3
|
-
ESayCommand,
|
|
4
|
-
IEnvidoCalculator,
|
|
5
|
-
IEnvidoCalculatorArgs,
|
|
6
|
-
ITeam,
|
|
7
|
-
TeamPoints,
|
|
8
|
-
} from "./types"
|
|
9
|
-
import { getMaxNumberIndex } from "./utils"
|
|
10
|
-
|
|
11
|
-
export const CARDS = {
|
|
12
|
-
"1e": 14,
|
|
13
|
-
"1b": 13,
|
|
14
|
-
"7e": 12,
|
|
15
|
-
"7o": 11,
|
|
16
|
-
"3e": 10,
|
|
17
|
-
"3o": 10,
|
|
18
|
-
"3b": 10,
|
|
19
|
-
"3c": 10,
|
|
20
|
-
"2e": 9,
|
|
21
|
-
"2o": 9,
|
|
22
|
-
"2b": 8,
|
|
23
|
-
"2c": 8,
|
|
24
|
-
"1o": 7,
|
|
25
|
-
"1c": 7,
|
|
26
|
-
re: 6,
|
|
27
|
-
ro: 6,
|
|
28
|
-
rb: 6,
|
|
29
|
-
rc: 6,
|
|
30
|
-
ce: 5,
|
|
31
|
-
co: 5,
|
|
32
|
-
cb: 5,
|
|
33
|
-
cc: 5,
|
|
34
|
-
pe: 4,
|
|
35
|
-
po: 4,
|
|
36
|
-
pb: 4,
|
|
37
|
-
pc: 4,
|
|
38
|
-
"7b": 3,
|
|
39
|
-
"7c": 3,
|
|
40
|
-
"6e": 2,
|
|
41
|
-
"6o": 2,
|
|
42
|
-
"6b": 2,
|
|
43
|
-
"6c": 2,
|
|
44
|
-
"5e": 1,
|
|
45
|
-
"5o": 1,
|
|
46
|
-
"5b": 1,
|
|
47
|
-
"5c": 1,
|
|
48
|
-
"4e": 0,
|
|
49
|
-
"4o": 0,
|
|
50
|
-
"4b": 0,
|
|
51
|
-
"4c": 0,
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export const COLORS = [
|
|
55
|
-
"#9b111",
|
|
56
|
-
"#17c6c6",
|
|
57
|
-
"#8c1d1d",
|
|
58
|
-
"#9f9b9b",
|
|
59
|
-
"#a5a5a5",
|
|
60
|
-
"#f5a623",
|
|
61
|
-
"#f44336",
|
|
62
|
-
"#c2185b",
|
|
63
|
-
]
|
|
64
|
-
|
|
65
|
-
export const TEAM_SIZE_VALUES = [1, 2, 3]
|
|
66
|
-
|
|
67
|
-
export enum GAME_ERROR {
|
|
68
|
-
UNEXPECTED_TEAM_SIZE = 'UNEXPECTED_TEAM_SIZE',
|
|
69
|
-
TEAM_NOT_READY = 'TEAM_NOT_READY',
|
|
70
|
-
TEAM_IS_FULL = 'TEAM_IS_FULL',
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export const EnvidoCalculator: IEnvidoCalculator = {
|
|
74
|
-
[EEnvidoCommand.ENVIDO]: () => ({
|
|
75
|
-
accept: 2,
|
|
76
|
-
decline: 1,
|
|
77
|
-
next: [EEnvidoCommand.ENVIDO_ENVIDO, EEnvidoCommand.REAL_ENVIDO, EEnvidoCommand.FALTA_ENVIDO],
|
|
78
|
-
}),
|
|
79
|
-
[EEnvidoCommand.ENVIDO_ENVIDO]: () => ({
|
|
80
|
-
accept: 4,
|
|
81
|
-
decline: 2,
|
|
82
|
-
next: [EEnvidoCommand.REAL_ENVIDO, EEnvidoCommand.FALTA_ENVIDO],
|
|
83
|
-
}),
|
|
84
|
-
[EEnvidoCommand.REAL_ENVIDO]: () => ({
|
|
85
|
-
accept: 3,
|
|
86
|
-
decline: 1,
|
|
87
|
-
next: [EEnvidoCommand.FALTA_ENVIDO],
|
|
88
|
-
}),
|
|
89
|
-
[EEnvidoCommand.FALTA_ENVIDO]: (args) => {
|
|
90
|
-
if (!args || !args.teams || !args.matchPoint) {
|
|
91
|
-
return {
|
|
92
|
-
accept: 1,
|
|
93
|
-
decline: 1,
|
|
94
|
-
next: [],
|
|
95
|
-
}
|
|
96
|
-
}
|
|
97
|
-
const { teams, matchPoint } = args
|
|
98
|
-
const totals = teams.map((team) => team.points.malas + team.points.buenas)
|
|
99
|
-
const higher = getMaxNumberIndex(totals)
|
|
100
|
-
const points = teams[higher].points
|
|
101
|
-
const accept = points.buenas > 0 ? matchPoint - points.buenas : matchPoint - points.malas
|
|
102
|
-
return {
|
|
103
|
-
accept,
|
|
104
|
-
decline: 2,
|
|
105
|
-
next: [],
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
}
|
package/src/lib/index.ts
DELETED
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
import { Match } from "./classes/Match"
|
|
2
|
-
import { Player } from "./classes/Player"
|
|
3
|
-
import { Table } from "./classes/Table"
|
|
4
|
-
import { Team } from "./classes/Team"
|
|
5
|
-
import { GAME_ERROR, TEAM_SIZE_VALUES } from "./constants"
|
|
6
|
-
import { EHandState, IMatch, IPlayInstance, IPrivateTrucoshi, ITeam, ITrucoshi } from "./types"
|
|
7
|
-
|
|
8
|
-
export type IWinnerCallback = (winner: ITeam, teams: [ITeam, ITeam]) => Promise<void>
|
|
9
|
-
export type ITurnCallback = (play: IPlayInstance) => Promise<void>
|
|
10
|
-
export type ITrucoCallback = (play: IPlayInstance) => Promise<void>
|
|
11
|
-
|
|
12
|
-
export interface IGameLoop {
|
|
13
|
-
_onTruco: ITrucoCallback
|
|
14
|
-
_onTurn: ITurnCallback
|
|
15
|
-
_onWinner: IWinnerCallback
|
|
16
|
-
onTurn: (callback: ITurnCallback) => IGameLoop
|
|
17
|
-
onWinner: (callback: IWinnerCallback) => IGameLoop
|
|
18
|
-
onTruco: (callback: ITrucoCallback) => IGameLoop
|
|
19
|
-
begin: () => void
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const GameLoop = (match: IMatch) => {
|
|
23
|
-
let gameloop: IGameLoop = {
|
|
24
|
-
_onTruco: () => Promise.resolve(),
|
|
25
|
-
_onTurn: () => Promise.resolve(),
|
|
26
|
-
_onWinner: () => Promise.resolve(),
|
|
27
|
-
onTruco: (callback: ITrucoCallback) => {
|
|
28
|
-
gameloop._onTruco = callback
|
|
29
|
-
return gameloop
|
|
30
|
-
},
|
|
31
|
-
onTurn: (callback: ITurnCallback) => {
|
|
32
|
-
gameloop._onTurn = callback
|
|
33
|
-
return gameloop
|
|
34
|
-
},
|
|
35
|
-
onWinner: (callback: IWinnerCallback) => {
|
|
36
|
-
gameloop._onWinner = callback
|
|
37
|
-
return gameloop
|
|
38
|
-
},
|
|
39
|
-
async begin() {
|
|
40
|
-
while (!match.winner) {
|
|
41
|
-
const play = match.play()
|
|
42
|
-
|
|
43
|
-
if (!play || !play.player) {
|
|
44
|
-
continue
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
if (play.state === EHandState.WAITING_FOR_TRUCO_ANSWER) {
|
|
48
|
-
await gameloop._onTruco(play)
|
|
49
|
-
continue
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (play.state === EHandState.WAITING_PLAY) {
|
|
53
|
-
await gameloop._onTurn(play)
|
|
54
|
-
continue
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
await gameloop._onWinner(match.winner, match.teams)
|
|
59
|
-
},
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
return gameloop
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
export function Trucoshi(teamSize?: 1 | 2 | 3) {
|
|
66
|
-
const trucoshi: IPrivateTrucoshi = {
|
|
67
|
-
lastTeamIdx: 1,
|
|
68
|
-
_players: new Map(),
|
|
69
|
-
get players() {
|
|
70
|
-
return Array.from(trucoshi._players.values())
|
|
71
|
-
},
|
|
72
|
-
teams: [],
|
|
73
|
-
table: null,
|
|
74
|
-
maxPlayers: teamSize ? teamSize * 2 : 6,
|
|
75
|
-
full: false,
|
|
76
|
-
ready: false,
|
|
77
|
-
calculateReady() {
|
|
78
|
-
trucoshi.ready = trucoshi.players.reduce((prev, curr) => prev && curr.ready, true)
|
|
79
|
-
return trucoshi.ready
|
|
80
|
-
},
|
|
81
|
-
calculateFull() {
|
|
82
|
-
trucoshi.full = trucoshi._players.size >= trucoshi.maxPlayers
|
|
83
|
-
return trucoshi.full
|
|
84
|
-
},
|
|
85
|
-
addPlayer(id, teamIdx) {
|
|
86
|
-
const maxSize = teamSize ? teamSize : 3
|
|
87
|
-
if (trucoshi.full || trucoshi.players.filter((p) => p.teamIdx === teamIdx).length > maxSize) {
|
|
88
|
-
throw new Error(GAME_ERROR.TEAM_IS_FULL)
|
|
89
|
-
}
|
|
90
|
-
const player = Player(id, teamIdx !== undefined ? teamIdx : Number(!trucoshi.lastTeamIdx))
|
|
91
|
-
trucoshi.lastTeamIdx = Number(!trucoshi.lastTeamIdx) as 0 | 1
|
|
92
|
-
trucoshi._players.set(id, player)
|
|
93
|
-
trucoshi.calculateFull()
|
|
94
|
-
trucoshi.calculateReady()
|
|
95
|
-
return player
|
|
96
|
-
},
|
|
97
|
-
removePlayer(id) {
|
|
98
|
-
trucoshi._players.delete(id)
|
|
99
|
-
trucoshi.calculateFull()
|
|
100
|
-
trucoshi.calculateReady()
|
|
101
|
-
return trucoshi
|
|
102
|
-
},
|
|
103
|
-
startMatch(matchPoint = 9) {
|
|
104
|
-
trucoshi.calculateReady()
|
|
105
|
-
const teamSize = trucoshi._players.size / 2
|
|
106
|
-
|
|
107
|
-
if (!TEAM_SIZE_VALUES.includes(teamSize)) {
|
|
108
|
-
throw new Error(GAME_ERROR.UNEXPECTED_TEAM_SIZE)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (!trucoshi.ready) {
|
|
112
|
-
throw new Error(GAME_ERROR.TEAM_NOT_READY)
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
trucoshi.teams.push(Team(trucoshi.players.filter((p) => p.teamIdx === 0)))
|
|
116
|
-
trucoshi.teams.push(Team(trucoshi.players.filter((p) => p.teamIdx === 1)))
|
|
117
|
-
|
|
118
|
-
if (
|
|
119
|
-
trucoshi.teams[0].players.length !== teamSize ||
|
|
120
|
-
trucoshi.teams[1].players.length !== teamSize
|
|
121
|
-
) {
|
|
122
|
-
throw new Error(GAME_ERROR.UNEXPECTED_TEAM_SIZE)
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
trucoshi.table = Table(trucoshi.players, trucoshi.teams)
|
|
126
|
-
return GameLoop(Match(trucoshi.table, trucoshi.teams, matchPoint))
|
|
127
|
-
},
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return {
|
|
131
|
-
addPlayer: trucoshi.addPlayer,
|
|
132
|
-
removePlayer: trucoshi.removePlayer,
|
|
133
|
-
startMatch: trucoshi.startMatch,
|
|
134
|
-
}
|
|
135
|
-
}
|
package/src/lib/types.ts
DELETED
|
@@ -1,216 +0,0 @@
|
|
|
1
|
-
import { IGameLoop } from "."
|
|
2
|
-
import { CARDS } from "./constants"
|
|
3
|
-
|
|
4
|
-
export type ICard = keyof typeof CARDS
|
|
5
|
-
|
|
6
|
-
export interface IDeck {
|
|
7
|
-
cards: Array<ICard>
|
|
8
|
-
usedCards: Array<ICard>
|
|
9
|
-
takeCard(): ICard
|
|
10
|
-
shuffle(): IDeck
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface IPlayedCard {
|
|
14
|
-
player: IPlayer
|
|
15
|
-
card: ICard
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export interface IPlayer {
|
|
19
|
-
teamIdx: number
|
|
20
|
-
id: string
|
|
21
|
-
hand: Array<ICard>
|
|
22
|
-
commands: Array<ECommand>
|
|
23
|
-
usedHand: Array<ICard>
|
|
24
|
-
disabled: boolean
|
|
25
|
-
ready: boolean
|
|
26
|
-
enable(): void
|
|
27
|
-
disable(): void
|
|
28
|
-
setReady(ready: boolean): void
|
|
29
|
-
setHand(hand: Array<ICard>): Array<ICard>
|
|
30
|
-
useCard(idx: number): ICard | null
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
export interface ITeam {
|
|
34
|
-
_players: Map<string, IPlayer>
|
|
35
|
-
players: Array<IPlayer>
|
|
36
|
-
points: TeamPoints
|
|
37
|
-
isTeamDisabled(): boolean
|
|
38
|
-
disable(player: IPlayer): boolean
|
|
39
|
-
addPoints(matchPoint: number, points: number): TeamPoints
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export interface IMatch {
|
|
43
|
-
teams: [ITeam, ITeam]
|
|
44
|
-
hands: Array<IHand>
|
|
45
|
-
winner: ITeam | null
|
|
46
|
-
currentHand: IHand | null
|
|
47
|
-
table: ITable
|
|
48
|
-
play(): IPlayInstance | null
|
|
49
|
-
addPoints(points: HandPoints): [ITeam, ITeam]
|
|
50
|
-
pushHand(hand: IHand): void
|
|
51
|
-
setCurrentHand(hand: IHand | null): IHand | null
|
|
52
|
-
setWinner(winner: ITeam): void
|
|
53
|
-
getNextTurn(): IteratorResult<IMatch | null, IMatch | null | void>
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export interface TeamPoints {
|
|
57
|
-
buenas: number
|
|
58
|
-
malas: number
|
|
59
|
-
winner: boolean
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export interface HandPoints {
|
|
63
|
-
0: number
|
|
64
|
-
1: number
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export interface RoundPoints {
|
|
68
|
-
0: number
|
|
69
|
-
1: number
|
|
70
|
-
ties: number
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
export enum ESayCommand {
|
|
74
|
-
QUIERO = "QUIERO",
|
|
75
|
-
NO_QUIERO = "NO_QUIERO",
|
|
76
|
-
TRUCO = "TRUCO",
|
|
77
|
-
MAZO = "MAZO",
|
|
78
|
-
FLOR = "FLOR",
|
|
79
|
-
CONTRAFLOR = "CONTRAFLOR",
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export enum EEnvidoCommand {
|
|
83
|
-
ENVIDO = "ENVIDO",
|
|
84
|
-
ENVIDO_ENVIDO = "ENVIDO_ENVIDO",
|
|
85
|
-
REAL_ENVIDO = "REAL_ENVIDO",
|
|
86
|
-
FALTA_ENVIDO = "FALTA_ENVIDO",
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export type ECommand = ESayCommand | EEnvidoCommand
|
|
90
|
-
|
|
91
|
-
export interface ITruco {
|
|
92
|
-
state: 1 | 2 | 3 | 4
|
|
93
|
-
teamIdx: 0 | 1 | null
|
|
94
|
-
answer: boolean | null
|
|
95
|
-
turn: number
|
|
96
|
-
players: Array<IPlayer>
|
|
97
|
-
currentPlayer: IPlayer | null
|
|
98
|
-
generator: Generator<ITruco, void, unknown>
|
|
99
|
-
sayTruco(teamIdx: 0 | 1, players: Array<IPlayer>): ITruco
|
|
100
|
-
setPlayers(players: Array<IPlayer>): void
|
|
101
|
-
setAnswer(answer: boolean | null): ITruco
|
|
102
|
-
setTurn(turn: number): number
|
|
103
|
-
setTeam(idx: 0 | 1): 0 | 1
|
|
104
|
-
setCurrentPlayer(player: IPlayer | null): IPlayer | null
|
|
105
|
-
getNextPlayer(): IteratorResult<ITruco, ITruco | void>
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
export interface EnvidoState {
|
|
109
|
-
accept: number
|
|
110
|
-
decline: number
|
|
111
|
-
teamIdx: 0 | 1 | null
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
export interface IPlayInstance {
|
|
115
|
-
teams: [ITeam, ITeam]
|
|
116
|
-
handIdx: number
|
|
117
|
-
roundIdx: number
|
|
118
|
-
state: EHandState
|
|
119
|
-
truco: ITruco
|
|
120
|
-
envido: EnvidoState
|
|
121
|
-
player: IPlayer | null
|
|
122
|
-
commands: Array<ECommand> | null
|
|
123
|
-
rounds: Array<IRound> | null
|
|
124
|
-
use(idx: number): ICard | null
|
|
125
|
-
say(command: ECommand): ECommand | null
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export enum EHandState {
|
|
129
|
-
WAITING_PLAY = "WAITING_PLAY",
|
|
130
|
-
WAITING_FOR_TRUCO_ANSWER = "WAITING_FOR_TRUCO_ANSWER",
|
|
131
|
-
WAITING_ENVIDO_ANSWER = "WAITING_ENVIDO_ANSWER",
|
|
132
|
-
FINISHED = "FINISHED",
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
export type IHandCommands = {
|
|
136
|
-
[key in ECommand]: (player: IPlayer) => void
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export interface IHand {
|
|
140
|
-
idx: number
|
|
141
|
-
state: EHandState
|
|
142
|
-
turn: number
|
|
143
|
-
points: HandPoints
|
|
144
|
-
truco: ITruco
|
|
145
|
-
envido: EnvidoState
|
|
146
|
-
rounds: Array<IRound>
|
|
147
|
-
_currentPlayer: IPlayer | null
|
|
148
|
-
get currentPlayer(): IPlayer | null
|
|
149
|
-
set currentPlayer(player: IPlayer | null)
|
|
150
|
-
currentRound: IRound | null
|
|
151
|
-
commands: IHandCommands
|
|
152
|
-
finished: () => boolean
|
|
153
|
-
play(): IPlayInstance | null
|
|
154
|
-
nextTurn(): void
|
|
155
|
-
use(idx: number): ICard | null
|
|
156
|
-
pushRound(round: IRound): IRound
|
|
157
|
-
setTurn(turn: number): IPlayer
|
|
158
|
-
addPoints(team: 0 | 1, points: number): void
|
|
159
|
-
disablePlayer(player: IPlayer): void
|
|
160
|
-
setCurrentRound(round: IRound | null): IRound | null
|
|
161
|
-
setCurrentPlayer(player: IPlayer | null): IPlayer | null
|
|
162
|
-
setState(state: EHandState): EHandState
|
|
163
|
-
getNextPlayer(): IteratorResult<IHand, IHand | void>
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
export interface IPrivateTrucoshi {
|
|
167
|
-
lastTeamIdx: 0 | 1
|
|
168
|
-
_players: Map<string, IPlayer>
|
|
169
|
-
get players(): Array<IPlayer>
|
|
170
|
-
teams: Array<ITeam>
|
|
171
|
-
maxPlayers: number
|
|
172
|
-
table: ITable | null
|
|
173
|
-
ready: boolean
|
|
174
|
-
full: boolean
|
|
175
|
-
addPlayer(id: string, teamIdx?: 0 | 1): IPlayer
|
|
176
|
-
removePlayer(id: string): ITrucoshi
|
|
177
|
-
calculateReady(): boolean
|
|
178
|
-
calculateFull(): boolean
|
|
179
|
-
startMatch(matchPoint?: 9 | 12 | 15): IGameLoop
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export interface ITrucoshi extends Pick<IPrivateTrucoshi, 'addPlayer' | 'removePlayer' | 'startMatch'> {}
|
|
183
|
-
|
|
184
|
-
export interface ITable {
|
|
185
|
-
forehandIdx: number
|
|
186
|
-
cards: Array<Array<IPlayedCard>>
|
|
187
|
-
players: Array<IPlayer>
|
|
188
|
-
nextTurn(): IPlayer
|
|
189
|
-
player(idx?: number): IPlayer
|
|
190
|
-
getPlayerPosition(id: string): number
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
export interface IRound {
|
|
194
|
-
tie: boolean
|
|
195
|
-
winner: IPlayer | null
|
|
196
|
-
highest: number
|
|
197
|
-
cards: Array<IPlayedCard>
|
|
198
|
-
turn: number,
|
|
199
|
-
nextTurn(): void
|
|
200
|
-
use(playedCard: IPlayedCard): ICard
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
export type IEnvidoCalculatorResult = {
|
|
204
|
-
accept: number
|
|
205
|
-
decline: number
|
|
206
|
-
next: Array<ECommand>
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export type IEnvidoCalculatorArgs = {
|
|
210
|
-
teams: [ITeam, ITeam]
|
|
211
|
-
matchPoint: number
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export type IEnvidoCalculator = {
|
|
215
|
-
[key in EEnvidoCommand]: (args?: IEnvidoCalculatorArgs) => IEnvidoCalculatorResult
|
|
216
|
-
}
|
package/src/lib/utils.ts
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
import { CARDS } from "./constants"
|
|
2
|
-
import { ICard, IRound, ITeam, RoundPoints } from "./types"
|
|
3
|
-
|
|
4
|
-
export function getMaxNumberIndex<T = number>(array: Array<T>) {
|
|
5
|
-
return array.reduce((accumulator, current, index) => {
|
|
6
|
-
return current > array[accumulator] ? index : accumulator
|
|
7
|
-
}, 0)
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export function getCardValue(card: ICard) {
|
|
11
|
-
return CARDS[card] || -1
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export function shuffleArray<T = unknown>(array: Array<T>) {
|
|
15
|
-
let currentIndex = array.length,
|
|
16
|
-
randomIndex
|
|
17
|
-
|
|
18
|
-
while (currentIndex != 0) {
|
|
19
|
-
randomIndex = Math.floor(Math.random() * currentIndex)
|
|
20
|
-
currentIndex--
|
|
21
|
-
;[array[currentIndex], array[randomIndex]] = [array[randomIndex], array[currentIndex]]
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
return array as Array<T>
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function checkHandWinner(
|
|
28
|
-
rounds: Array<IRound>,
|
|
29
|
-
forehandTeamIdx: 0 | 1
|
|
30
|
-
): null | 0 | 1 {
|
|
31
|
-
const roundsWon: RoundPoints = {
|
|
32
|
-
0: 0,
|
|
33
|
-
1: 0,
|
|
34
|
-
ties: 0,
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
for (let i = 0; i < rounds.length; i++) {
|
|
38
|
-
const round = rounds[i]
|
|
39
|
-
if (round.tie) {
|
|
40
|
-
roundsWon[0] += 1
|
|
41
|
-
roundsWon[1] += 1
|
|
42
|
-
roundsWon.ties = roundsWon.ties + 1
|
|
43
|
-
continue
|
|
44
|
-
}
|
|
45
|
-
if (round.winner?.teamIdx === 0) {
|
|
46
|
-
roundsWon[0] += 1
|
|
47
|
-
}
|
|
48
|
-
if (round.winner?.teamIdx === 1) {
|
|
49
|
-
roundsWon[1] += 1
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if ((roundsWon[0] > 2 && roundsWon[1] > 2) || (rounds.length > 2 && roundsWon.ties > 0)) {
|
|
54
|
-
return forehandTeamIdx
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (roundsWon[0] >= 2 && roundsWon[1] < 2) {
|
|
58
|
-
return 0
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (roundsWon[1] >= 2 && roundsWon[0] < 2) {
|
|
62
|
-
return 1
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
return null
|
|
66
|
-
}
|
package/src/server/index.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { createServer } from "http"
|
|
2
|
-
import { Server } from "socket.io"
|
|
3
|
-
import { EClientEvent, EServerEvent } from "./types"
|
|
4
|
-
|
|
5
|
-
const PORT = 4001
|
|
6
|
-
|
|
7
|
-
const httpServer = createServer()
|
|
8
|
-
const io = new Server(httpServer, {
|
|
9
|
-
cors: {
|
|
10
|
-
origin: "http://localhost:3000",
|
|
11
|
-
methods: ["GET", "POST"],
|
|
12
|
-
},
|
|
13
|
-
})
|
|
14
|
-
|
|
15
|
-
const sessions = new Map<string, string>()
|
|
16
|
-
|
|
17
|
-
io.on("connection", (socket) => {
|
|
18
|
-
socket.on(EClientEvent.PING, (msg) => {
|
|
19
|
-
io.emit(EServerEvent.PONG, msg)
|
|
20
|
-
})
|
|
21
|
-
|
|
22
|
-
socket.on(EClientEvent.CREATE_MATCH, (msg) => {
|
|
23
|
-
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
socket.on(EClientEvent.SET_PLAYER_ID, (msg) => {
|
|
27
|
-
if (typeof msg === 'string' && msg.length < 32) {
|
|
28
|
-
}
|
|
29
|
-
})
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
httpServer.listen(PORT)
|
|
33
|
-
|
|
34
|
-
console.log("Listening on port", PORT)
|
package/src/server/match.ts
DELETED
|
File without changes
|
package/src/server/types.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export enum EClientEvent {
|
|
2
|
-
PING = "PING",
|
|
3
|
-
CREATE_MATCH = "CREATE_MATCH",
|
|
4
|
-
JOIN_MATCH = "JOIN_MATCH",
|
|
5
|
-
START_MATCH = "START_MATCH",
|
|
6
|
-
SET_PLAYER_ID = "SET_PLAYER_ID",
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
export enum EServerEvent {
|
|
10
|
-
SET_SESSION_ID = "SET_SESSION_ID",
|
|
11
|
-
PONG = "PONG",
|
|
12
|
-
}
|
package/src/test/autoplay.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
import { Trucoshi } from "../lib"
|
|
2
|
-
import { IRound } from "../lib/types"
|
|
3
|
-
;(async () => {
|
|
4
|
-
const trucoshi = Trucoshi()
|
|
5
|
-
|
|
6
|
-
trucoshi.addPlayer("lukini").setReady(true)
|
|
7
|
-
trucoshi.addPlayer("denoph").setReady(true)
|
|
8
|
-
trucoshi.addPlayer("guada").setReady(true)
|
|
9
|
-
trucoshi.addPlayer("juli").setReady(true)
|
|
10
|
-
trucoshi.addPlayer("day").setReady(true)
|
|
11
|
-
trucoshi.addPlayer("fran").setReady(true)
|
|
12
|
-
|
|
13
|
-
trucoshi
|
|
14
|
-
.startMatch()
|
|
15
|
-
.onTurn(async (play) => {
|
|
16
|
-
if (!play.player) {
|
|
17
|
-
return
|
|
18
|
-
}
|
|
19
|
-
const name = play.player?.id.toUpperCase()
|
|
20
|
-
console.log(`=== Mano ${play.handIdx} === Ronda ${play.roundIdx} === Turno de ${name} ===`)
|
|
21
|
-
play.teams.map((team, id) =>
|
|
22
|
-
console.log(`=== Team ${id} = ${team.points.malas} malas ${team.points.buenas} buenas ===`)
|
|
23
|
-
)
|
|
24
|
-
console.log(
|
|
25
|
-
play.rounds && play.rounds.length
|
|
26
|
-
? play.rounds.map((round: IRound) =>
|
|
27
|
-
round.cards.length ? round.cards.map((c) => [c.player.id, c.card]) : ""
|
|
28
|
-
)
|
|
29
|
-
: ""
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
const randomIdx = Math.round(Math.random() * (play.player.hand.length - 1))
|
|
33
|
-
const handString = JSON.stringify(play.player.hand)
|
|
34
|
-
const card = play.use(randomIdx)
|
|
35
|
-
|
|
36
|
-
console.log(`\n${handString}\nUsing ${card}`)
|
|
37
|
-
console.log(
|
|
38
|
-
play.rounds && play.rounds.length
|
|
39
|
-
? play.rounds.map((round: IRound) =>
|
|
40
|
-
round.cards.length ? round.cards.map((c) => [c.player.id, c.card]) : ""
|
|
41
|
-
)
|
|
42
|
-
: ""
|
|
43
|
-
)
|
|
44
|
-
})
|
|
45
|
-
.onWinner(async (winner, teams) => {
|
|
46
|
-
console.log("\n")
|
|
47
|
-
teams.map((t, i) =>
|
|
48
|
-
console.log(
|
|
49
|
-
`Equipo ${i}: ${t.players.map((p) => ` ${p.id}`)} === ${t.points.malas} malas ${
|
|
50
|
-
t.points.buenas
|
|
51
|
-
} buenas`
|
|
52
|
-
)
|
|
53
|
-
)
|
|
54
|
-
console.log(`\nEquipo Ganador:${winner.players.map((p) => ` ${p.id}`)}`)
|
|
55
|
-
})
|
|
56
|
-
.begin()
|
|
57
|
-
})()
|