Skip to content

Commit c2e8813

Browse files
committed
flower order, next season
1 parent 7183119 commit c2e8813

6 files changed

Lines changed: 131 additions & 3 deletions

File tree

src/services/BotGarden.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { cloneDeep, shuffle } from 'lodash'
2+
import { GardenSeason } from '@/store/state'
3+
import { ref } from 'vue'
4+
import Flower from './enum/Flower'
5+
import getAllEnumValues from '@brdgm/brdgm-commons/src/util/enum/getAllEnumValues'
6+
import Season from './enum/Season'
7+
8+
/**
9+
* Manages Lady Pei's garden.
10+
*/
11+
export default class BotGarden {
12+
13+
private readonly _seasons
14+
private readonly _flowerOrder: Flower[]
15+
16+
private constructor(seasons : GardenSeason[], flowerOrder: Flower[]) {
17+
this._seasons = ref(seasons)
18+
this._flowerOrder = flowerOrder
19+
}
20+
21+
public get seasons() : readonly GardenSeason[] {
22+
return this._seasons.value
23+
}
24+
25+
/**
26+
* Plants the given flower.
27+
* @param flower Flower to plan
28+
*/
29+
public plant(flower: Flower) : void {
30+
31+
}
32+
33+
/**
34+
* Gets persistence view of garden seasons.
35+
*/
36+
public toPersistence() : GardenSeason[] {
37+
return cloneDeep(this._seasons.value)
38+
}
39+
40+
/**
41+
* Initialize Lady Pei's garden based on the player's selected flowers.
42+
* Take two flowers from each family not in the player's garden, one from each family that is.
43+
* Randomly assign one flower per season.
44+
* @param playerFlowers Flowers the player has in their garden
45+
* @param flowerOrder The order of flowers in the market (for Lady Pei's color priority)
46+
* @returns BotGarden
47+
*/
48+
public static new(playerFlowers : Flower[], flowerOrder: Flower[]) : BotGarden {
49+
const allFlowers = getAllEnumValues(Flower)
50+
const pool : Flower[] = []
51+
for (const flower of allFlowers) {
52+
if (playerFlowers.includes(flower)) {
53+
pool.push(flower)
54+
}
55+
else {
56+
pool.push(flower, flower)
57+
}
58+
}
59+
const shuffled = shuffle(pool)
60+
const allSeasons = getAllEnumValues(Season)
61+
const seasons : GardenSeason[] = allSeasons.map((season, index) => ({
62+
season,
63+
flowers: [shuffled[index]],
64+
bigExtension: 0,
65+
smallExtensions: 0
66+
}))
67+
return new BotGarden(seasons, flowerOrder)
68+
}
69+
70+
/**
71+
* Re-creates garden seasons from persistence.
72+
* Note that flower order must also be passed in, as it is not stored in the garden persistence (but rather market prices persistence).
73+
*/
74+
public static fromPersistence(persistence : GardenSeason[], flowerOrder: Flower[]) : BotGarden {
75+
return new BotGarden(cloneDeep(persistence), flowerOrder)
76+
}
77+
78+
}

src/services/MarketPrices.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ export default class MarketPrices {
2222
return this._prices.value
2323
}
2424

25+
public get flowerOrder() : Flower[] {
26+
return this._prices.value.map(item => item.flower)
27+
}
28+
2529
public getPrice(flower : Flower) : number {
2630
const price = this._prices.value.find(item => item.flower === flower)
2731
if (!price) {

src/util/getNextSeason.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Season from '@/services/enum/Season'
2+
3+
const SEASON_ORDER = [Season.SPRING, Season.SUMMER, Season.AUTUMN, Season.WINTER]
4+
5+
/**
6+
* Gets the next season in season order.
7+
*/
8+
export default function getNextSeason(season: Season) : Season {
9+
const index = SEASON_ORDER.indexOf(season)
10+
return SEASON_ORDER[(index + 1) % SEASON_ORDER.length]
11+
}

src/views/SetupBot.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default defineComponent({
5454
},
5555
flowerOrder() : Flower[] {
5656
const marketPrices = this.state.setup.initialMarketPrices ? MarketPrices.fromPersistence(this.state.setup.initialMarketPrices) : MarketPrices.new()
57-
return marketPrices.prices.map(item => item.flower)
57+
return marketPrices.flowerOrder
5858
}
5959
},
6060
methods: {

tests/unit/services/MarketPrices.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,24 @@ describe('services/MarketPrices', () => {
1515
it('new - contains all flowers', () => {
1616
const marketPrices = MarketPrices.new()
1717

18-
const flowers = marketPrices.prices.map(item => item.flower)
18+
const flowers = marketPrices.flowerOrder
1919
expect(flowers).to.include.members([Flower.RED, Flower.PURPLE, Flower.YELLOW, Flower.BLUE, Flower.ORANGE])
2020
})
2121

22+
it('flowerOrder', () => {
23+
const marketPrices = MarketPrices.fromPersistence([
24+
{ flower: Flower.ORANGE, price: 4 },
25+
{ flower: Flower.BLUE, price: 4 },
26+
{ flower: Flower.RED, price: 4 },
27+
{ flower: Flower.YELLOW, price: 4 },
28+
{ flower: Flower.PURPLE, price: 4 },
29+
])
30+
31+
expect(marketPrices.flowerOrder).to.eql([
32+
Flower.ORANGE, Flower.BLUE, Flower.RED, Flower.YELLOW, Flower.PURPLE
33+
])
34+
})
35+
2236
it('getPrice', () => {
2337
const marketPrices = MarketPrices.fromPersistence([
2438
{ flower: Flower.RED, price: 5 },
@@ -93,6 +107,6 @@ describe('services/MarketPrices', () => {
93107
const restored = MarketPrices.fromPersistence(marketPrices.toPersistence())
94108
expect(restored.getPrice(Flower.RED)).to.eq(marketPrices.getPrice(Flower.RED))
95109
expect(restored.getPrice(Flower.BLUE)).to.eq(marketPrices.getPrice(Flower.BLUE))
96-
expect(restored.prices.map(p => p.flower)).to.eql(marketPrices.prices.map(p => p.flower))
110+
expect(restored.flowerOrder).to.eql(marketPrices.flowerOrder)
97111
})
98112
})
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import Season from '@/services/enum/Season'
2+
import getNextSeason from '@/util/getNextSeason'
3+
import { expect } from 'chai'
4+
5+
describe('util/getNextSeason', () => {
6+
it('SPRING -> SUMMER', () => {
7+
expect(getNextSeason(Season.SPRING)).to.eq(Season.SUMMER)
8+
})
9+
10+
it('SUMMER -> AUTUMN', () => {
11+
expect(getNextSeason(Season.SUMMER)).to.eq(Season.AUTUMN)
12+
})
13+
14+
it('AUTUMN -> WINTER', () => {
15+
expect(getNextSeason(Season.AUTUMN)).to.eq(Season.WINTER)
16+
})
17+
18+
it('WINTER -> SPRING', () => {
19+
expect(getNextSeason(Season.WINTER)).to.eq(Season.SPRING)
20+
})
21+
})

0 commit comments

Comments
 (0)