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.
Files changed (58) hide show
  1. package/{build → dist}/lib/classes/Deck.d.ts +0 -0
  2. package/{build → dist}/lib/classes/Deck.js +0 -0
  3. package/{build → dist}/lib/classes/Hand.d.ts +0 -0
  4. package/{build → dist}/lib/classes/Hand.js +0 -0
  5. package/{build → dist}/lib/classes/Match.d.ts +0 -0
  6. package/{build → dist}/lib/classes/Match.js +0 -0
  7. package/{build → dist}/lib/classes/Play.d.ts +0 -0
  8. package/{build → dist}/lib/classes/Play.js +0 -0
  9. package/{build → dist}/lib/classes/Player.d.ts +0 -0
  10. package/{build → dist}/lib/classes/Player.js +0 -0
  11. package/{build → dist}/lib/classes/Round.d.ts +0 -0
  12. package/{build → dist}/lib/classes/Round.js +0 -0
  13. package/{build → dist}/lib/classes/Table.d.ts +0 -0
  14. package/{build → dist}/lib/classes/Table.js +0 -0
  15. package/{build → dist}/lib/classes/Team.d.ts +0 -0
  16. package/{build → dist}/lib/classes/Team.js +0 -0
  17. package/{build → dist}/lib/classes/Truco.d.ts +0 -0
  18. package/{build → dist}/lib/classes/Truco.js +0 -0
  19. package/{build → dist}/lib/constants.d.ts +0 -0
  20. package/{build → dist}/lib/constants.js +0 -0
  21. package/{build → dist}/lib/index.d.ts +0 -0
  22. package/{build → dist}/lib/index.js +0 -0
  23. package/{build → dist}/lib/types.d.ts +0 -0
  24. package/{build → dist}/lib/types.js +0 -0
  25. package/{build → dist}/lib/utils.d.ts +0 -0
  26. package/{build → dist}/lib/utils.js +0 -0
  27. package/{build → dist}/server/index.d.ts +0 -0
  28. package/{build → dist}/server/index.js +0 -0
  29. package/{build → dist}/server/match.d.ts +0 -0
  30. package/{build → dist}/server/match.js +0 -0
  31. package/{build → dist}/server/types.d.ts +0 -0
  32. package/{build → dist}/server/types.js +0 -0
  33. package/{build → dist}/test/autoplay.d.ts +0 -0
  34. package/{build → dist}/test/autoplay.js +0 -0
  35. package/{build → dist}/test/play.d.ts +0 -0
  36. package/{build → dist}/test/play.js +0 -0
  37. package/package.json +5 -3
  38. package/.prettierrc.json +0 -7
  39. package/nodemon.json +0 -6
  40. package/src/lib/classes/Deck.ts +0 -25
  41. package/src/lib/classes/Hand.ts +0 -207
  42. package/src/lib/classes/Match.ts +0 -79
  43. package/src/lib/classes/Play.ts +0 -40
  44. package/src/lib/classes/Player.ts +0 -37
  45. package/src/lib/classes/Round.ts +0 -30
  46. package/src/lib/classes/Table.ts +0 -32
  47. package/src/lib/classes/Team.ts +0 -41
  48. package/src/lib/classes/Truco.ts +0 -72
  49. package/src/lib/constants.ts +0 -108
  50. package/src/lib/index.ts +0 -135
  51. package/src/lib/types.ts +0 -216
  52. package/src/lib/utils.ts +0 -66
  53. package/src/server/index.ts +0 -34
  54. package/src/server/match.ts +0 -0
  55. package/src/server/types.ts +0 -12
  56. package/src/test/autoplay.ts +0 -57
  57. package/src/test/play.ts +0 -122
  58. package/tsconfig.json +0 -19
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "trucoshi",
3
- "version": "0.0.9",
3
+ "version": "0.0.11",
4
4
  "main": "build/lib/trucoshi.js",
5
5
  "license": "GPL-3.0",
6
6
  "scripts": {
@@ -9,13 +9,15 @@
9
9
  "test:autoplay": "yarn run ts-node src/test/autoplay",
10
10
  "test:play": "yarn run ts-node src/test/play",
11
11
  "test:legacy": "yarn run ts-node src/test/legacy",
12
- "build": "yarn run rimraf ./build && yarn run tsc --project ."
12
+ "build": "yarn run rimraf ./dist && yarn run tsc --project ."
13
13
  },
14
14
  "devDependencies": {
15
15
  "@types/node": "^18.11.18",
16
16
  "nodemon": "^2.0.20"
17
17
  },
18
-
18
+ "files": [
19
+ "dist"
20
+ ],
19
21
  "dependencies": {
20
22
  "rimraf": "^4.1.1",
21
23
  "socket.io": "^4.5.4",
package/.prettierrc.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "trailingComma": "es5",
3
- "tabWidth": 2,
4
- "semi": false,
5
- "singleQuote": false,
6
- "printWidth": 100
7
- }
package/nodemon.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "watch": ["src"],
3
- "ext": ".ts,.js",
4
- "ignore": [],
5
- "exec": "yarn run ts-node ./src/server/index.ts"
6
- }
@@ -1,25 +0,0 @@
1
- import { CARDS } from "../constants"
2
- import { ICard, IDeck } from "../types"
3
- import { shuffleArray } from "../utils"
4
-
5
- export function Deck(): IDeck {
6
- const deck: IDeck = {
7
- cards: Object.keys(CARDS) as Array<ICard>,
8
- usedCards: [],
9
- takeCard() {
10
- const card = deck.cards.shift() as ICard
11
- deck.usedCards.push(card)
12
- return card
13
- },
14
- shuffle() {
15
- deck.cards = deck.cards.concat(deck.usedCards)
16
- deck.usedCards = []
17
- deck.cards = shuffleArray(deck.cards)
18
- if (deck.cards.length !== 40) {
19
- throw new Error("This is not good")
20
- }
21
- return deck
22
- },
23
- }
24
- return deck
25
- }
@@ -1,207 +0,0 @@
1
- import {
2
- EEnvidoCommand,
3
- EHandState,
4
- ESayCommand,
5
- IDeck,
6
- IHand,
7
- IHandCommands,
8
- IMatch,
9
- } from "../types"
10
- import { checkHandWinner } from "../utils"
11
- import { PlayInstance } from "./Play"
12
- import { Round } from "./Round"
13
- import { Truco } from "./Truco"
14
-
15
- export function Hand(match: IMatch, deck: IDeck, idx: number) {
16
- match.teams.forEach((team) => {
17
- team.players.forEach((player) => {
18
- const playerHand = [deck.takeCard(), deck.takeCard(), deck.takeCard()]
19
- player.setHand(playerHand)
20
- player.enable()
21
- // player.setHand(["5c", "4c", "6c"])
22
- })
23
- })
24
-
25
- function* roundsGeneratorSequence() {
26
- let currentRoundIdx = 0
27
- let forehandTeamIdx = match.table.player(hand.turn).teamIdx as 0 | 1
28
-
29
- while (currentRoundIdx < 3 && !hand.finished()) {
30
- const round = Round(0)
31
- hand.setCurrentRound(round)
32
- hand.pushRound(round)
33
-
34
- let previousRound = hand.rounds[currentRoundIdx - 1]
35
-
36
- // Put previous round winner as forehand
37
- if (previousRound && previousRound.winner && !previousRound.tie) {
38
- const newTurn = match.table.getPlayerPosition(previousRound.winner.id)
39
- if (newTurn !== -1) {
40
- hand.setTurn(newTurn)
41
- }
42
- }
43
-
44
- while (round.turn < match.table.players.length) {
45
-
46
- while (hand.state === EHandState.WAITING_FOR_TRUCO_ANSWER) {
47
- const { value } = hand.truco.getNextPlayer()
48
- if (value && value.currentPlayer) {
49
- console.log({ value: value.currentPlayer })
50
- hand.setCurrentPlayer(value.currentPlayer)
51
- yield hand
52
- }
53
- }
54
-
55
- const player = match.table.player(hand.turn)
56
- hand.setCurrentPlayer(player)
57
- if (player.disabled) {
58
- hand.setCurrentPlayer(null)
59
- }
60
-
61
- yield hand
62
- }
63
-
64
- if (match.teams[0].isTeamDisabled() && match.teams[1].isTeamDisabled()) {
65
- hand.setState(EHandState.FINISHED)
66
- break
67
- }
68
-
69
- let winnerTeamIdx = checkHandWinner(hand.rounds, forehandTeamIdx)
70
-
71
- if (match.teams[0].isTeamDisabled()) {
72
- winnerTeamIdx = 1
73
- }
74
- if (match.teams[1].isTeamDisabled()) {
75
- winnerTeamIdx = 0
76
- }
77
-
78
- if (winnerTeamIdx !== null) {
79
- hand.addPoints(winnerTeamIdx, hand.truco.state)
80
- hand.setState(EHandState.FINISHED)
81
- }
82
-
83
- currentRoundIdx++
84
- }
85
- yield hand
86
- }
87
-
88
- const roundsGenerator = roundsGeneratorSequence()
89
-
90
- const commands: IHandCommands = {
91
- [ESayCommand.MAZO]: (player) => {
92
- hand.disablePlayer(player)
93
- },
94
- [ESayCommand.TRUCO]: (player) => {
95
- const { teamIdx } = hand.truco
96
- if (teamIdx === null || teamIdx !== player.teamIdx) {
97
- hand.setState(EHandState.WAITING_FOR_TRUCO_ANSWER)
98
- hand.truco.sayTruco(player.teamIdx as 0 | 1, match.teams[Number(!player.teamIdx)].players)
99
- }
100
- },
101
- [ESayCommand.QUIERO]: () => {
102
- if (hand.state === EHandState.WAITING_FOR_TRUCO_ANSWER) {
103
- hand.truco.setAnswer(true)
104
- hand.setState(EHandState.WAITING_PLAY)
105
- }
106
- },
107
- [ESayCommand.NO_QUIERO]: (player) => {
108
- if (hand.state === EHandState.WAITING_FOR_TRUCO_ANSWER) {
109
- hand.truco.setAnswer(false)
110
- hand.setState(EHandState.WAITING_PLAY)
111
- }
112
- },
113
- [ESayCommand.FLOR]: () => {},
114
- [ESayCommand.CONTRAFLOR]: () => {},
115
- [EEnvidoCommand.ENVIDO]: () => {},
116
- [EEnvidoCommand.ENVIDO_ENVIDO]: () => {},
117
- [EEnvidoCommand.REAL_ENVIDO]: () => {},
118
- [EEnvidoCommand.FALTA_ENVIDO]: () => {},
119
- }
120
-
121
- const hand: IHand = {
122
- idx,
123
- turn: Number(match.table.forehandIdx),
124
- state: EHandState.WAITING_PLAY,
125
- rounds: [],
126
- truco: Truco(),
127
- envido: {
128
- accept: 1,
129
- decline: 2,
130
- teamIdx: null,
131
- },
132
- points: [0, 0],
133
- currentRound: null,
134
- _currentPlayer: null,
135
- set currentPlayer(player) {
136
- hand._currentPlayer = player
137
- },
138
- get currentPlayer() {
139
- if (hand.state === EHandState.WAITING_FOR_TRUCO_ANSWER) {
140
- return hand.truco.currentPlayer
141
- }
142
- return hand._currentPlayer
143
- },
144
- commands,
145
- play() {
146
- return PlayInstance(hand, match.teams)
147
- },
148
- use(idx: number) {
149
- const player = hand.currentPlayer
150
- const round = hand.currentRound
151
- if (!player || !round) {
152
- return null
153
- }
154
-
155
- const card = player.useCard(idx)
156
- if (card) {
157
- hand.nextTurn()
158
- return round.use({ player, card })
159
- }
160
-
161
- return null
162
- },
163
- nextTurn() {
164
- if (hand.turn >= match.table.players.length - 1) {
165
- hand.setTurn(0)
166
- } else {
167
- hand.setTurn(hand.turn + 1)
168
- }
169
-
170
- hand.currentRound?.nextTurn()
171
- },
172
- getNextPlayer() {
173
- return roundsGenerator.next()
174
- },
175
- disablePlayer(player) {
176
- match.teams[player.teamIdx].disable(player)
177
- },
178
- addPoints(team, points) {
179
- hand.points[team] = hand.points[team] + points
180
- },
181
- pushRound(round) {
182
- hand.rounds.push(round)
183
- return round
184
- },
185
- setTurn(turn) {
186
- hand.turn = turn
187
- return match.table.player(hand.turn)
188
- },
189
- setCurrentRound(round) {
190
- hand.currentRound = round
191
- return hand.currentRound
192
- },
193
- setCurrentPlayer(player) {
194
- hand._currentPlayer = player
195
- return hand._currentPlayer
196
- },
197
- setState(state) {
198
- hand.state = state
199
- return hand.state
200
- },
201
- finished: () => {
202
- return hand.state === EHandState.FINISHED
203
- },
204
- }
205
-
206
- return hand
207
- }
@@ -1,79 +0,0 @@
1
- import { IHand, IMatch, ITable, ITeam } from "../types"
2
- import { Deck } from "./Deck"
3
- import { Hand } from "./Hand"
4
-
5
- export function Match(table: ITable, teams: Array<ITeam> = [], matchPoint: number = 9): IMatch {
6
- const deck = Deck().shuffle()
7
-
8
- const size = teams[0].players.length
9
-
10
- if (size !== teams[1].players.length) {
11
- throw new Error("Team size mismatch")
12
- }
13
-
14
- function* handsGeneratorSequence() {
15
- while (!match.winner) {
16
- deck.shuffle()
17
- const hand = match.setCurrentHand(Hand(match, deck, match.hands.length + 1)) as IHand
18
- match.pushHand(hand)
19
- while (!hand.finished()) {
20
- const { value } = hand.getNextPlayer()
21
- if (value && value.finished()) {
22
- continue
23
- }
24
- match.setCurrentHand(value as IHand)
25
- yield match
26
- }
27
-
28
- match.setCurrentHand(null)
29
-
30
- const teams = match.addPoints(hand.points)
31
- const winner = teams.find((team) => team.points.winner)
32
-
33
- if (winner) {
34
- match.setWinner(winner)
35
- match.setCurrentHand(null)
36
- break
37
- }
38
- match.table.nextTurn()
39
- }
40
- yield match
41
- }
42
-
43
- const handsGenerator = handsGeneratorSequence()
44
-
45
- const match: IMatch = {
46
- winner: null,
47
- teams: teams as [ITeam, ITeam],
48
- hands: [],
49
- table,
50
- currentHand: null,
51
- play() {
52
- match.getNextTurn()
53
- if (!match.currentHand) {
54
- return null
55
- }
56
- return match.currentHand.play()
57
- },
58
- addPoints(points) {
59
- match.teams[0].addPoints(matchPoint, points[0])
60
- match.teams[1].addPoints(matchPoint, points[1])
61
- return match.teams
62
- },
63
- pushHand(hand) {
64
- match.hands.push(hand)
65
- },
66
- setCurrentHand(hand) {
67
- match.currentHand = hand
68
- return match.currentHand
69
- },
70
- setWinner(winner) {
71
- match.winner = winner
72
- },
73
- getNextTurn() {
74
- return handsGenerator.next()
75
- },
76
- }
77
-
78
- return match
79
- }
@@ -1,40 +0,0 @@
1
- import { EEnvidoCommand, EHandState, ESayCommand, IHand, IPlayInstance, ITeam } from "../types"
2
-
3
- export function PlayInstance(hand: IHand, teams: [ITeam, ITeam]) {
4
- const instance: IPlayInstance = {
5
- state: hand.state,
6
- teams,
7
- truco: hand.truco,
8
- envido: hand.envido,
9
- handIdx: hand.idx,
10
- roundIdx: hand.rounds.length,
11
- player: hand.currentPlayer,
12
- commands: [],
13
- rounds: hand.rounds,
14
- use(idx) {
15
- return hand.use(idx)
16
- },
17
- say(command) {
18
- if (!hand._currentPlayer || !instance.commands?.includes(command)) {
19
- return null
20
- }
21
-
22
- hand.commands[command](hand._currentPlayer)
23
-
24
- return command
25
- },
26
- }
27
-
28
- instance.commands?.push(ESayCommand.MAZO)
29
- instance.commands?.push(ESayCommand.TRUCO)
30
-
31
- if (hand.rounds.length === 1) {
32
- instance.commands?.push(EEnvidoCommand.ENVIDO)
33
- }
34
-
35
- if (hand.state === EHandState.WAITING_FOR_TRUCO_ANSWER) {
36
- instance.commands = [ESayCommand.TRUCO, ESayCommand.QUIERO, ESayCommand.NO_QUIERO]
37
- }
38
-
39
- return instance
40
- }
@@ -1,37 +0,0 @@
1
- import { IPlayer } from "../types"
2
-
3
- export function Player(id: string, teamIdx: number) {
4
- const player: IPlayer = {
5
- id,
6
- teamIdx,
7
- hand: [],
8
- commands: [],
9
- usedHand: [],
10
- disabled: false,
11
- ready: false,
12
- enable() {
13
- player.disabled = false
14
- },
15
- disable() {
16
- player.disabled = true
17
- },
18
- setReady(ready) {
19
- player.ready = ready
20
- },
21
- setHand(hand) {
22
- player.hand = hand
23
- player.usedHand = []
24
- return hand
25
- },
26
- useCard(idx) {
27
- if (player.hand[idx]) {
28
- const card = player.hand.splice(idx, 1)[0]
29
- player.usedHand.push(card)
30
- return card
31
- }
32
- return null
33
- },
34
- }
35
-
36
- return player
37
- }
@@ -1,30 +0,0 @@
1
- import { IRound } from "../types"
2
- import { getCardValue } from "../utils"
3
-
4
- export function Round(turn: number): IRound {
5
- const round: IRound = {
6
- turn,
7
- highest: -1,
8
- winner: null,
9
- cards: [],
10
- tie: false,
11
- nextTurn() {
12
- round.turn++
13
- },
14
- use({ card, player }) {
15
- const value = getCardValue(card)
16
- if (value === round.highest && player.teamIdx !== round.winner?.teamIdx) {
17
- round.tie = true
18
- }
19
- if (value > round.highest) {
20
- round.tie = false
21
- round.highest = value
22
- round.winner = player
23
- }
24
- round.cards.push({ card, player })
25
- return card
26
- },
27
- }
28
-
29
- return round
30
- }
@@ -1,32 +0,0 @@
1
- import { GAME_ERROR, TEAM_SIZE_VALUES } from "../constants"
2
- import { IPlayer, ITable, ITeam } from "../types"
3
- import { Match } from "./Match"
4
- import { Player } from "./Player"
5
- import { Team } from "./Team"
6
-
7
- export function Table(players: Array<IPlayer>, teams: Array<ITeam>): ITable {
8
- const table: ITable = {
9
- players,
10
- cards: [],
11
- forehandIdx: 0,
12
- nextTurn() {
13
- if (table.forehandIdx < table.players.length - 1) {
14
- table.forehandIdx++
15
- } else {
16
- table.forehandIdx = 0
17
- }
18
- return table.player()
19
- },
20
- getPlayerPosition(id) {
21
- return table.players.findIndex((p) => p.id === id)
22
- },
23
- player(idx) {
24
- if (idx !== undefined) {
25
- return table.players[idx]
26
- }
27
- return table.players[table.forehandIdx]
28
- },
29
- }
30
-
31
- return table
32
- }
@@ -1,41 +0,0 @@
1
- import { IPlayer, ITeam } from "../types"
2
-
3
- export function Team(players: Array<IPlayer>) {
4
- const team: ITeam = {
5
- _players: new Map<string, IPlayer>(),
6
- get players() {
7
- return Array.from(team._players.values())
8
- },
9
- points: {
10
- buenas: 0,
11
- malas: 0,
12
- winner: false,
13
- },
14
- isTeamDisabled() {
15
- return team.players.reduce((prev, curr) => prev && curr.disabled, true)
16
- },
17
- disable(player) {
18
- team._players.get(player.id)?.disable()
19
- return team.isTeamDisabled()
20
- },
21
- addPoints(matchPoint, points) {
22
- const malas = team.points.malas + points
23
- const diff = malas - matchPoint
24
- if (diff > 0) {
25
- team.points.malas = matchPoint
26
- team.points.buenas += diff
27
- if (team.points.buenas >= matchPoint) {
28
- team.points.winner = true
29
- }
30
- } else {
31
- team.points.malas = malas
32
- }
33
-
34
- return team.points
35
- },
36
- }
37
-
38
- players.forEach((player) => team._players.set(player.id, player))
39
-
40
- return team
41
- }