Skip to content

Commit 4a22101

Browse files
committed
navigation state
1 parent e6c687f commit 4a22101

15 files changed

Lines changed: 482 additions & 107 deletions

File tree

src/components/round/SideBar.vue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<div class="sidebar">
33
<div>
44
<span>{{t('sideBar.round', {round})}}</span><br/>
5+
<span class="fw-bold">{{t(`season.${season}`)}}</span><br/>
56
<span>{{t('sideBar.turn', {turn})}}</span>
67
</div>
78
</div>
@@ -12,6 +13,7 @@ import { defineComponent } from 'vue'
1213
import { useI18n } from 'vue-i18n'
1314
import { useStateStore } from '@/store/state'
1415
import NavigationState from '@/util/NavigationState'
16+
import Season from '@/services/enum/Season'
1517
1618
export default defineComponent({
1719
name: 'SideBar',
@@ -30,6 +32,9 @@ export default defineComponent({
3032
round() : number {
3133
return this.navigationState.round
3234
},
35+
season() : Season {
36+
return this.navigationState.season
37+
},
3338
turn() : number {
3439
return this.navigationState.turn
3540
}

src/locales/en.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@
3232
"validationHint": "Please select the flower colors you planted in your garden."
3333
}
3434
},
35+
"sideBar": {
36+
"round": "Year {round}",
37+
"turn": "Turn {turn}"
38+
},
39+
"season": {
40+
"autumn": "Autumn",
41+
"winter": "Winter",
42+
"spring": "Spring",
43+
"summer": "Summer"
44+
},
3545
"notfound": {
3646
"title": "Not Found"
3747
},

src/services/enum/Season.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
* Season
33
*/
44
enum Season {
5-
SPRING = 'spring',
6-
SUMMER = 'summer',
75
AUTUMN = 'autumn',
8-
WINTER = 'winter'
6+
WINTER = 'winter',
7+
SPRING = 'spring',
8+
SUMMER = 'summer'
99
}
1010
export default Season

src/store/state.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const useStateStore = defineStore(`${name}.state`, {
2020
resetGame() {
2121
this.rounds = []
2222
this.setup.initialMarketPrices = undefined
23-
this.setup.initialBotGarden = undefined
23+
this.setup.initialBotPersistence = undefined
2424
this.gameStatsSend = false
2525
},
2626
storeRound(round : Round) {
@@ -49,7 +49,7 @@ export interface State {
4949
export interface Setup {
5050
botMode: BotMode,
5151
initialMarketPrices?: FlowerPrice[]
52-
initialBotGarden?: GardenSeason[]
52+
initialBotPersistence?: BotPersistence
5353
debugMode?: boolean
5454
}
5555

src/util/NavigationState.ts

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,79 @@
1-
import { State } from '@/store/state'
1+
import { BotPersistence, FlowerPrice, RoundTurn, State } from '@/store/state'
22
import { RouteLocation } from 'vue-router'
33
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'
510

611
export default class NavigationState {
712

813
readonly round : number
914
readonly turn : number
10-
readonly botMode : BotMode
15+
readonly season : Season
16+
17+
readonly marketPrices : MarketPrices
18+
readonly cardDeck : CardDeck
19+
readonly botGarden : BotGarden
1120

1221
constructor(route: RouteLocation, state: State) {
1322
this.round = getIntRouteParam(route, 'round')
23+
this.season = getSeason(this.round, state)
1424
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)
1629
}
1730

1831
}
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+
}

src/views/SetupBot.vue

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import Flower from '@/services/enum/Flower'
4040
import Season from '@/services/enum/Season'
4141
import PlayerFlowerSelection from '@/components/setup/PlayerFlowerSelection.vue'
4242
import BotGarden from '@/services/BotGarden'
43+
import CardDeck from '@/services/CardDeck'
4344
4445
export default defineComponent({
4546
name: 'SetupBot',
@@ -75,7 +76,10 @@ export default defineComponent({
7576
methods: {
7677
startGame() : void {
7778
const botGarden = BotGarden.new(this.playerFlowers, this.flowerOrder)
78-
this.state.setup.initialBotGarden = botGarden.toPersistence()
79+
this.state.setup.initialBotPersistence = {
80+
cardDeck: CardDeck.new().toPersistence(),
81+
garden: botGarden.toPersistence()
82+
}
7983
this.state.storeRound({round:1, year:1, season:Season.AUTUMN, turns:[]})
8084
this.$router.push('/round/1/turn/1/player')
8185
}

tests/unit/helper/mockBotGarden.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import BotGarden from '@/services/BotGarden'
2+
import Flower from '@/services/enum/Flower'
3+
import Season from '@/services/enum/Season'
4+
import getAllEnumValues from '@brdgm/brdgm-commons/src/util/enum/getAllEnumValues'
5+
6+
export default function mockBotGarden(params?: BotGardenParams) : BotGarden {
7+
const seasons = getAllEnumValues(Season).map(season => {
8+
const item = params?.seasons?.find(p => p.season === season)
9+
return {
10+
season,
11+
flowers: item?.flowers ?? [],
12+
bigExtension: item?.bigExtension ?? 0,
13+
smallExtensions: item?.smallExtensions ?? 0
14+
}
15+
})
16+
return BotGarden.fromPersistence(seasons, params?.flowerOrder ?? getAllEnumValues(Flower))
17+
}
18+
19+
export interface BotGardenParams {
20+
seasons?: GardenSeasonParams[]
21+
flowerOrder?: Flower[]
22+
}
23+
24+
export interface GardenSeasonParams {
25+
season?: Season
26+
flowers?: Flower[]
27+
bigExtension?: number
28+
smallExtensions?: number
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { BotPersistence } from '@/store/state'
2+
import CardDeck from '@/services/CardDeck'
3+
import BotGarden from '@/services/BotGarden'
4+
import mockCardDeck from './mockCardDeck'
5+
import mockBotGarden from './mockBotGarden'
6+
7+
export default function mockBotPersistence(params?: MockBotPersistenceParams) : BotPersistence {
8+
return {
9+
cardDeck: (params?.cardDeck ?? mockCardDeck()).toPersistence(),
10+
garden: (params?.garden ?? mockBotGarden()).toPersistence()
11+
}
12+
}
13+
14+
export interface MockBotPersistenceParams {
15+
cardDeck?: CardDeck,
16+
garden?: BotGarden
17+
}

tests/unit/helper/mockCardDeck.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import CardDeck from '@/services/CardDeck'
2+
3+
export default function mockCardDeck(params?: MockCardDeckParams) : CardDeck {
4+
return CardDeck.fromPersistence({
5+
pile: params?.pile ?? [],
6+
played: params?.played ?? [],
7+
discard: params?.discard ?? []
8+
})
9+
}
10+
11+
export interface MockCardDeckParams {
12+
pile?: string[]
13+
played?: string[]
14+
discard?: string[]
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import Flower from '@/services/enum/Flower'
2+
import { FlowerPrice } from '@/store/state'
3+
import getAllEnumValues from '@brdgm/brdgm-commons/src/util/enum/getAllEnumValues'
4+
5+
export default function mockMarketPrices(params?: FlowerPrice[]) : FlowerPrice[] {
6+
return getAllEnumValues(Flower).map(flower => {
7+
return {
8+
flower,
9+
price: params?.find(p => p.flower === flower)?.price ?? 4
10+
}
11+
})
12+
}

0 commit comments

Comments
 (0)