@@ -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