|
1 | | -import { State } from '@/store/state' |
| 1 | +import { BotPersistence, FlowerPrice, RoundTurn, State } from '@/store/state' |
2 | 2 | import { RouteLocation } from 'vue-router' |
3 | 3 | import getIntRouteParam from '@brdgm/brdgm-commons/src/util/router/getIntRouteParam' |
4 | | -import BotMode from '@/services/enum/BotMode' |
| 4 | +import Season from '@/services/enum/Season' |
| 5 | +import CardDeck from '@/services/CardDeck' |
| 6 | +import BotGarden from '@/services/BotGarden' |
| 7 | +import getAllEnumValues from '@brdgm/brdgm-commons/src/util/enum/getAllEnumValues' |
| 8 | +import Flower from '@/services/enum/Flower' |
| 9 | +import MarketPrices from '@/services/MarketPrices' |
5 | 10 |
|
6 | 11 | export default class NavigationState { |
7 | 12 |
|
8 | 13 | readonly round : number |
9 | 14 | readonly turn : number |
10 | | - readonly botMode : BotMode |
| 15 | + readonly season : Season |
| 16 | + |
| 17 | + readonly marketPrices : MarketPrices |
| 18 | + readonly cardDeck : CardDeck |
| 19 | + readonly botGarden : BotGarden |
11 | 20 |
|
12 | 21 | constructor(route: RouteLocation, state: State) { |
13 | 22 | this.round = getIntRouteParam(route, 'round') |
| 23 | + this.season = getSeason(this.round, state) |
14 | 24 | this.turn = getIntRouteParam(route, 'turn') |
15 | | - this.botMode = state.setup.botMode |
| 25 | + |
| 26 | + this.marketPrices = MarketPrices.fromPersistence(getFlowerPrices(this.round, this.turn, state)) |
| 27 | + this.cardDeck = CardDeck.fromPersistence(getBotPersistence(this.round, this.turn, state).cardDeck) |
| 28 | + this.botGarden = BotGarden.fromPersistence(getBotPersistence(this.round, this.turn, state).garden, this.marketPrices.flowerOrder) |
16 | 29 | } |
17 | 30 |
|
18 | 31 | } |
| 32 | + |
| 33 | +function getSeason(round: number, state: State) : Season { |
| 34 | + const roundData = state.rounds.find(r => r.round === round) |
| 35 | + return roundData ? roundData.season : Season.AUTUMN |
| 36 | +} |
| 37 | + |
| 38 | +function getFlowerPrices(round: number, turn: number, state: State) : FlowerPrice[] { |
| 39 | + const prices = getTurns(round, turn, state).find(t => t.botPersistence)?.marketPrices |
| 40 | + if (prices) { |
| 41 | + return prices |
| 42 | + } |
| 43 | + if (round > 1) { |
| 44 | + return getFlowerPrices(round - 1, 0, state) |
| 45 | + } |
| 46 | + else { |
| 47 | + return state.setup.initialMarketPrices ?? MarketPrices.new().toPersistence() |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +function getBotPersistence(round: number, turn: number, state: State) : BotPersistence { |
| 52 | + const botPersistence = getTurns(round, turn, state).find(t => t.botPersistence)?.botPersistence |
| 53 | + if (botPersistence) { |
| 54 | + return botPersistence |
| 55 | + } |
| 56 | + if (round > 1) { |
| 57 | + return getBotPersistence(round - 1, 0, state) |
| 58 | + } |
| 59 | + else { |
| 60 | + return state.setup.initialBotPersistence ?? { |
| 61 | + cardDeck: CardDeck.new().toPersistence(), |
| 62 | + garden: BotGarden.new([], getAllEnumValues(Flower)).toPersistence() |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * Get previous turns of round in reverse turn order. |
| 69 | + * If turn=0 gets all turns. |
| 70 | + */ |
| 71 | +function getTurns(round: number, turn: number, state: State) : RoundTurn[] { |
| 72 | + const roundData = state.rounds.find(r => r.round === round) |
| 73 | + if (!roundData) { |
| 74 | + return [] |
| 75 | + } |
| 76 | + return roundData.turns |
| 77 | + .filter(t => t.turn < turn || turn == 0) |
| 78 | + .toSorted((a, b) => b.turn - a.turn) |
| 79 | +} |
0 commit comments