generated from Game-as-a-Service/Gaas-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: 賴鴻德 <[email protected]>
Co-authored-by: Pikacnu <[email protected]> Co-authored-by: Tuhacrt <[email protected]>
- Loading branch information
1 parent
f21033b
commit 2723a3f
Showing
11 changed files
with
221 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ export enum Status { | |
} | ||
|
||
export type Card = { | ||
id?: number; | ||
id?: string; | ||
status: Status; | ||
creature: Creature; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './createGame'; | ||
export * from './type'; | ||
export * as deck from './deck'; | ||
export * from './dealCards'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import A from 'fp-ts/Array'; | ||
import { Option } from 'fp-ts/Option'; | ||
|
||
export const lookupWithNegativeIndex = | ||
(index: number) => | ||
<T>(as: Array<T>): Option<T> => | ||
index >= 0 ? A.lookup(index, as) : A.lookup(as.length + index, as); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
packages/domain_layer/src/player/currentPlayerSelectUnrevealedCard.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import O from 'fp-ts/Option'; | ||
import { Game } from 'game'; | ||
import { Round } from 'round'; | ||
import { Card, Creature, Status } from 'card'; | ||
|
||
import { Player } from './type'; | ||
import { currentPlayerSelectUnrevealedCard } from './currentPlayerSelectUnrevealedCard'; | ||
|
||
describe('CurrentPlayerSelectUnrevealedCard', () => { | ||
it(`given a game | ||
contain player A B | ||
and | ||
player A hand [ | ||
{ id: 9999, status: 'UNREVEALED', creature: 'BAT' } | ||
] | ||
when player A selected { id: 9999, status: 'UNREVEALED', creature: 'BAT' } | ||
then return round | ||
`, () => { | ||
// Arrange | ||
const playerA: Player = { | ||
id: '1', | ||
uid: '1', | ||
name: 'A', | ||
hands: [], | ||
pastReceivedCards: [], | ||
}; | ||
const playerB: Player = { | ||
id: '2', | ||
uid: '2', | ||
name: 'B', | ||
hands: [], | ||
pastReceivedCards: [], | ||
}; | ||
|
||
const game: Game = { | ||
id: '1', | ||
players: [playerA, playerB], | ||
rounds: [], | ||
deck: [], | ||
}; | ||
|
||
const card: Card = { | ||
id: '9999', | ||
status: Status.Unrevealed, | ||
creature: Creature.Bat, | ||
}; | ||
|
||
playerA.hands = [card]; | ||
game.deck = [card]; | ||
|
||
const round: Round = { | ||
id: '1', | ||
currentPlayer: playerA, | ||
selectedCard: undefined, | ||
state: undefined, | ||
}; | ||
|
||
game.rounds = [round]; | ||
|
||
// Act | ||
const result = currentPlayerSelectUnrevealedCard(game)(playerA)(card); | ||
|
||
// Assert | ||
expect(result).toMatchObject(O.some({ ...round, selectedCard: card })); | ||
}); | ||
}); |
113 changes: 113 additions & 0 deletions
113
packages/domain_layer/src/player/currentPlayerSelectUnrevealedCard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { flow, pipe } from 'fp-ts/function'; | ||
import A from 'fp-ts/Array'; | ||
import O from 'fp-ts/Option'; | ||
import L from 'monocle-ts/Lens'; | ||
import { Card } from 'card'; | ||
import { Game } from 'game'; | ||
import { allPass, anyPass, prop } from 'remeda'; | ||
import { Round, SelectedPlayerState } from 'round'; | ||
import { P, match } from 'ts-pattern'; | ||
import { always } from 'utils/fp'; | ||
import { lookupWithNegativeIndex } from 'lib/lookupWithNegativeNumber'; | ||
|
||
import { Player } from './type'; | ||
|
||
interface CurrentPlayerSelectUnrevealedCard { | ||
(game: Game): (player: Player) => (selectedCard: Card) => O.Option<Round>; | ||
} | ||
|
||
const checkPlayerIsCurrentPlayer = (player: Player) => (game: Game) => | ||
pipe( | ||
game, | ||
prop('rounds'), | ||
A.last, | ||
O.map(prop('currentPlayer')), | ||
O.map(prop('id')), | ||
O.map((id) => id === player.id), | ||
O.getOrElse(always(false)), | ||
); | ||
|
||
const isFirstRound = (game: Game) => | ||
pipe( | ||
// | ||
game, | ||
prop('rounds'), | ||
A.size, | ||
(size) => size === 1, | ||
); | ||
|
||
const checkPlayerHasTheCard = (player: Player) => (selectedCard: Card) => | ||
pipe( | ||
// | ||
player, | ||
prop('hands'), | ||
A.some((card) => card.id === selectedCard.id), | ||
); | ||
|
||
const checkSelectCardIsAllowed = (selectedCard: Card) => (game: Game) => | ||
pipe( | ||
// | ||
game, | ||
prop('rounds'), | ||
lookupWithNegativeIndex(-2), | ||
O.map((round) => | ||
match(round) | ||
.with( | ||
P.union( | ||
{ | ||
state: SelectedPlayerState.GUESS_CARD_WIN, | ||
}, | ||
{ | ||
state: SelectedPlayerState.GUESS_CARD_LOSS, | ||
}, | ||
{ | ||
state: SelectedPlayerState.EVADE_NOT_LOOK_CARD, | ||
selectedCard: { | ||
id: selectedCard.id, | ||
}, | ||
}, | ||
{ | ||
state: SelectedPlayerState.EVADE_LOOK_CARD, | ||
selectedCard: { | ||
id: selectedCard.id, | ||
}, | ||
}, | ||
), | ||
always(true), | ||
) | ||
.otherwise(always(false)), | ||
), | ||
O.getOrElse(always(false)), | ||
); | ||
|
||
const setCardToRound = (selectedCard: Card) => | ||
pipe( | ||
// | ||
L.id<Round>(), | ||
L.prop('selectedCard'), | ||
L.modify(always(selectedCard)), | ||
); | ||
|
||
export const currentPlayerSelectUnrevealedCard: CurrentPlayerSelectUnrevealedCard = | ||
(game) => (player) => (selectedCard) => | ||
pipe( | ||
// | ||
game, | ||
O.fromPredicate( | ||
flow( | ||
allPass([ | ||
checkPlayerIsCurrentPlayer(player), | ||
always(checkPlayerHasTheCard(player)(selectedCard)), | ||
anyPass([ | ||
// 第一回合可以選任何牌 | ||
isFirstRound, | ||
// 第二回合之後只能視規則選牌 | ||
checkSelectCardIsAllowed(selectedCard), | ||
]), | ||
]), | ||
), | ||
), | ||
O.map(prop('rounds')), | ||
O.chain(A.last), | ||
O.map(setCardToRound(selectedCard)), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './type'; | ||
export * from './createPlayer'; | ||
export * from './toPlayers'; | ||
export * from './checkPlayerHasUnrevealedCardOfHands'; | ||
export * from './currentPlayerSelectUnrevealedCard'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './type'; | ||
export * from './createRound'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters