Skip to content

Commit 54b5534

Browse files
committed
refactor logic
1 parent 2fac8b9 commit 54b5534

2 files changed

Lines changed: 67 additions & 49 deletions

File tree

src/components/round/botAction/ActionPrice.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</template>
1616

1717
<script lang="ts">
18-
import { defineComponent, PropType, ref } from 'vue'
18+
import { defineComponent, PropType } from 'vue'
1919
import { useI18n } from 'vue-i18n'
2020
import NavigationState from '@/util/NavigationState'
2121
import Card from '@/services/Card'

src/services/BotActions.ts

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -59,57 +59,75 @@ export default class BotActions {
5959
* Process card actions to bot actions (executing the automatic managed ones).
6060
*/
6161
private process(actions: CardAction[]): BotAction[] {
62-
const botActions: BotAction[] = []
63-
for (const action of actions) {
64-
const botAction: BotAction = action
65-
66-
switch (action.action) {
67-
68-
case Action.PLANT:
69-
// plant most expensive flower in the garden, reduce price by 1
70-
botAction.flower = this._marketPrices.getMostExpensiveFlower()
71-
this._botGarden.plant(botAction.flower, this._season)
72-
this._marketPrices.decrease(botAction.flower)
73-
break
74-
75-
case Action.SOLD:
76-
// get distinct list of flowers in current season, reduce price by 1 for each
77-
const distinctFlowers = [...new Set(this.getFlowersOfCurrentSeason())]
78-
for (const flower of distinctFlowers) {
79-
this._marketPrices.decrease(flower)
80-
}
81-
break
82-
83-
case Action.PAID:
84-
// identify the most expensive flower in this season, reduce price by 1 and gain that number of VP
85-
const mostExpensiveFlower = this.getMostExpensiveFlowerOfCurrentSeason()
86-
if (mostExpensiveFlower) {
87-
this._marketPrices.decrease(mostExpensiveFlower)
88-
botAction.vp = this._marketPrices.getPrice(mostExpensiveFlower)
89-
}
90-
break
62+
return actions.map(action => this.processAction(action))
63+
}
9164

92-
case Action.PRICE:
93-
// if windows is already defined: increase price of flowers in that window by 1, otherwise increase 1 least/most expensive flower
94-
const windowState = this._windowStates.getWindowState(action.floor ?? 1, action.windowSelection ?? WindowSelection.LEFT)
95-
if (windowState) {
96-
botAction.flowers = windowState.flowers
97-
}
98-
else if (action.priceSelection == PriceSelection.MOST_EXPENSIVE) {
99-
botAction.flowers = [this._marketPrices.getMostExpensiveFlower()]
100-
}
101-
else {
102-
botAction.flowers = [this._marketPrices.getCheapestFlower()]
103-
}
104-
for (const flower of botAction.flowers) {
105-
this._marketPrices.increase(flower)
106-
}
107-
break
108-
}
65+
private processAction(action: CardAction): BotAction {
66+
const botAction: BotAction = action
67+
switch (action.action) {
68+
case Action.PLANT:
69+
this.processPlant(botAction)
70+
break
71+
case Action.SOLD:
72+
this.processSold()
73+
break
74+
case Action.PAID:
75+
this.processPaid(botAction)
76+
break
77+
case Action.PRICE:
78+
this.processPrice(botAction)
79+
break
80+
}
81+
return botAction
82+
}
83+
84+
/**
85+
* Plant most expensive flower in the garden, reduce price by 1
86+
*/
87+
private processPlant(botAction: BotAction): void {
88+
botAction.flower = this._marketPrices.getMostExpensiveFlower()
89+
this._botGarden.plant(botAction.flower, this._season)
90+
this._marketPrices.decrease(botAction.flower)
91+
}
10992

110-
botActions.push(botAction)
93+
/**
94+
* Get distinct list of flowers in current season, reduce price by 1 for each
95+
*/
96+
private processSold(): void {
97+
const distinctFlowers = [...new Set(this.getFlowersOfCurrentSeason())]
98+
for (const flower of distinctFlowers) {
99+
this._marketPrices.decrease(flower)
100+
}
101+
}
102+
103+
/**
104+
* Identify the most expensive flower in this season, reduce price by 1 and gain that number of VP
105+
*/
106+
private processPaid(botAction: BotAction): void {
107+
const mostExpensiveFlower = this.getMostExpensiveFlowerOfCurrentSeason()
108+
if (mostExpensiveFlower) {
109+
this._marketPrices.decrease(mostExpensiveFlower)
110+
botAction.vp = this._marketPrices.getPrice(mostExpensiveFlower)
111+
}
112+
}
113+
114+
/**
115+
* If window is already defined: increase price of flowers in that window by 1, otherwise increase 1 least/most expensive flower
116+
*/
117+
private processPrice(botAction: BotAction): void {
118+
const windowState = this._windowStates.getWindowState(botAction.floor ?? 1, botAction.windowSelection ?? WindowSelection.LEFT)
119+
if (windowState) {
120+
botAction.flowers = windowState.flowers
121+
}
122+
else if (botAction.priceSelection == PriceSelection.MOST_EXPENSIVE) {
123+
botAction.flowers = [this._marketPrices.getMostExpensiveFlower()]
124+
}
125+
else {
126+
botAction.flowers = [this._marketPrices.getCheapestFlower()]
127+
}
128+
for (const flower of botAction.flowers) {
129+
this._marketPrices.increase(flower)
111130
}
112-
return botActions
113131
}
114132

115133
private getFlowersOfCurrentSeason(): Flower[] {

0 commit comments

Comments
 (0)