-
Notifications
You must be signed in to change notification settings - Fork 9
/
cardShop.ts
51 lines (40 loc) · 1.78 KB
/
cardShop.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { takeEvery, select, put } from "@redux-saga/core/effects";
import { BuyCardAction, cardsUpdated } from "../../../cardShop/cardActions";
import { BUY_CARD } from "../../../cardShop/cardActionTypes";
import { AppState } from "../../state";
import { GamePhase } from "@common";
import { createPieceFromCard } from "@common/piece-utils";
import { getFirstEmptyBenchSlot, BenchActions } from "@common/board";
import { moneyUpdateAction } from "../../actions/gameActions";
import { DefinitionProvider } from "@common/game/definitionProvider";
const definitionProvider = new DefinitionProvider();
export const cardShop = function*() {
yield takeEvery<BuyCardAction>(
BUY_CARD,
function*(action) {
const state: AppState = yield select();
const gamePhase = state.game.phase;
// not in correct phase
if (gamePhase === GamePhase.WAITING || gamePhase === GamePhase.DEAD) {
return;
}
const card = state.cards[action.payload.index];
const money = state.game.money;
// card doesn't exist or player can't afford
if (!card || money < card.cost) {
return;
}
const slot = getFirstEmptyBenchSlot(state.bench);
// no valid slots
if (slot === null) {
return;
}
const localPlayerId = state.localPlayer.id;
const piece = createPieceFromCard(definitionProvider, localPlayerId, card, slot);
const remainingCards = state.cards.map(c => c === card ? null : c);
yield put(BenchActions.benchPieceAdded(piece));
yield put(moneyUpdateAction(money - card.cost));
yield put(cardsUpdated(remainingCards));
}
);
};