Skip to content

Commit 9e3030f

Browse files
committed
implement back button
1 parent 42c77fc commit 9e3030f

16 files changed

Lines changed: 108 additions & 86 deletions

File tree

.vscode/settings.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1-
{}
1+
{
2+
"i18n-ally.localesPaths": [
3+
"src/locales"
4+
]
5+
}

src/components/round/DebugInfo.vue

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<b>draftingPriority</b>: <span v-html="getCardDeckInfo(draftingPriority)"></span><br/>
77
<b>construction</b>: <span v-html="getCardDeckInfo(construction)"></span><br/>
88
<b>war</b>: <span v-html="getCardDeckInfo(war)"></span><br/>
9+
<b>lastDraftStep</b>: {{navigationState.lastDraftStep}}<br/>
910
</p>
1011
</div>
1112
</template>

src/components/round/TechCardDraft.vue

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
<button class="btn btn-primary btn-lg me-3 mt-2" @click="next()">
3333
{{t('action.next')}}
3434
</button>
35-
<button class="btn btn-outline-secondary btn-sm mt-2" @click="reset()">
36-
{{t('action.reset')}}
37-
</button>
3835

3936
<div class="draftedCards">
4037
<div v-if="playerTechs.length > 0" class="mt-3">
@@ -50,13 +47,12 @@
5047
</div>
5148
</div>
5249
</div>
53-
5450
</template>
5551

5652
<script lang="ts">
5753
import { defineComponent, ref } from 'vue'
5854
import { useI18n } from 'vue-i18n'
59-
import { useStateStore } from '@/store/state'
55+
import { TechDraftStep, useStateStore } from '@/store/state'
6056
import NavigationState from '@/util/NavigationState'
6157
import TechCardSelection from '@/services/TechCardSelection'
6258
import Tech from '@/services/enum/Tech'
@@ -67,6 +63,7 @@ import TechCard from './TechCard.vue'
6763
import AppIcon from '../structure/AppIcon.vue'
6864
import TechCardsPlayerDraft from './TechCardsPlayerDraft.vue'
6965
import { useRouter } from 'vue-router'
66+
import { cloneDeep } from 'lodash'
7067
7168
export default defineComponent({
7269
name: 'TechCardDraft',
@@ -81,11 +78,21 @@ export default defineComponent({
8178
const router = useRouter()
8279
8380
const roundData = state.rounds.find(item => item.round == props.navigationState.round)!
84-
const botTechs = ref(roundData.botTechs ?? [])
85-
const playerTechs = ref(roundData.playerTechs ?? [])
86-
const playerSpecialActions = ref(roundData.playerSpecialActions ?? 0)
81+
const lastTechDraftStep = roundData.techDraftSteps?.find(item => item.step == props.navigationState.draftingStep - 1)
82+
const botTechs = ref(lastTechDraftStep?.botTechs ?? [])
83+
const playerTechs = ref(lastTechDraftStep?.playerTechs ?? [])
84+
const playerSpecialActions = ref(lastTechDraftStep?.playerSpecialActions ?? 0)
85+
const techDraftStep = ref({
86+
round: props.navigationState.round,
87+
step: props.navigationState.draftingStep,
88+
nextStartPlayer: lastTechDraftStep?.nextStartPlayer,
89+
nextArchitectPlayer: lastTechDraftStep?.nextArchitectPlayer,
90+
botTechs: cloneDeep(lastTechDraftStep?.botTechs ?? []),
91+
playerTechs: cloneDeep(lastTechDraftStep?.playerTechs ?? []),
92+
playerSpecialActions: lastTechDraftStep?.playerSpecialActions ?? 0
93+
} as TechDraftStep)
8794
88-
return { t, state, router, roundData, botTechs, playerTechs, playerSpecialActions }
95+
return { t, state, router, roundData, techDraftStep, botTechs, playerTechs, playerSpecialActions }
8996
},
9097
props: {
9198
navigationState: {
@@ -145,21 +152,22 @@ export default defineComponent({
145152
await new Promise(resolve => setTimeout(resolve, 400))
146153
this.removeAnimation = false
147154
this.navigationState.techCardSelection.remove(tech)
148-
this.persist()
149155
},
150156
async nextTurn() {
151157
if (this.draftingCompleted) {
152158
return
153159
}
154160
if (this.botMarkerPlaced < this.playerMarkerPlaced) {
155161
await this.nextTurnBot()
162+
await this.nextTurn()
156163
}
157164
else if (this.playerMarkerPlaced < this.botMarkerPlaced) {
158165
await this.nextTurnPlayer()
159166
}
160167
else if (this.roundData.startPlayer == Player.BOT) {
161168
await this.nextTurnBot()
162-
}
169+
await this.nextTurn()
170+
}
163171
else {
164172
await this.nextTurnPlayer()
165173
}
@@ -171,13 +179,12 @@ export default defineComponent({
171179
const tech = techCardSelection.determineTech(draftingRowCard, draftingPriorityCard, prosperityCards.current.flat())
172180
this.botTechs.push(tech)
173181
if (tech == Tech.ARMY) {
174-
this.roundData.nextStartPlayer = Player.BOT
182+
this.techDraftStep.nextStartPlayer = Player.BOT
175183
}
176184
if (tech == Tech.ENGINEERING) {
177-
this.roundData.nextArchitectPlayer = Player.BOT
185+
this.techDraftStep.nextArchitectPlayer = Player.BOT
178186
}
179187
await this.remove(tech)
180-
await this.nextTurn()
181188
},
182189
async nextTurnPlayer() {
183190
this.playerTurn = true
@@ -192,30 +199,15 @@ export default defineComponent({
192199
}
193200
this.playerTurn = false
194201
this.playerTechs.push(t)
195-
this.roundData.playerTechs = this.playerTechs
202+
this.techDraftStep.playerTechs = this.playerTechs
196203
if (t == Tech.ARMY) {
197-
this.roundData.nextStartPlayer = Player.PLAYER
204+
this.techDraftStep.nextStartPlayer = Player.PLAYER
198205
}
199206
if (t == Tech.ENGINEERING) {
200-
this.roundData.nextArchitectPlayer = Player.PLAYER
207+
this.techDraftStep.nextArchitectPlayer = Player.PLAYER
201208
}
202209
await this.remove(t)
203-
await this.nextTurn()
204-
},
205-
async reset() {
206-
this.techCardSelection.reset()
207-
this.navigationState.botCards.draftingRow.reset()
208-
this.navigationState.botCards.draftingPriority.reset()
209-
this.roundData.nextStartPlayer = undefined
210-
this.roundData.nextArchitectPlayer = undefined
211-
this.roundData.playerTechs = undefined
212-
213-
this.botTechs = []
214-
this.playerTechs = []
215-
this.playerSpecialActions = 0
216-
217-
this.persist()
218-
await this.nextTurn()
210+
await this.playerTurnCompleted()
219211
},
220212
async next() {
221213
if (this.draftingCompleted) {
@@ -224,15 +216,23 @@ export default defineComponent({
224216
else {
225217
this.playerSpecialActions++
226218
this.playerTurn = false
227-
this.persist()
228-
await this.nextTurn()
219+
await this.playerTurnCompleted()
220+
}
221+
},
222+
async playerTurnCompleted() {
223+
// if player was first player, execute bot turn before going to next step
224+
if (this.roundData.startPlayer == Player.PLAYER) {
225+
await this.nextTurnBot()
229226
}
227+
this.persistAndNextStep()
230228
},
231-
persist() : void {
232-
this.roundData.techCardSelection = this.techCardSelection.toPersistence()
233-
this.roundData.botTechs = this.botTechs
234-
this.roundData.playerTechs = this.playerTechs
235-
this.roundData.playerSpecialActions = this.playerSpecialActions
229+
persistAndNextStep() : void {
230+
this.techDraftStep.techCardSelection = this.techCardSelection.toPersistence()
231+
this.techDraftStep.botTechs = this.botTechs
232+
this.techDraftStep.playerTechs = this.playerTechs
233+
this.techDraftStep.playerSpecialActions = this.playerSpecialActions
234+
this.state.storeTechDraftStep(this.techDraftStep)
235+
this.router.push(`/round/${this.navigationState.round}/drafting/${this.techDraftStep.step+1}`)
236236
}
237237
},
238238
mounted() {

src/locales/de.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@
130130
"backToHome": "Zurück zum Anfang",
131131
"back": "Zurück",
132132
"close": "Schließen",
133-
"ok": "OK",
134-
"reset": "Zurücksetzen"
133+
"ok": "OK"
135134
},
136135
"footer": {
137136
"credits": "Credits"

src/locales/en.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@
130130
"backToHome": "Back to Home",
131131
"back": "Back",
132132
"close": "Close",
133-
"ok": "OK",
134-
"reset": "Reset"
133+
"ok": "OK"
135134
},
136135
"footer": {
137136
"credits": "Credits"

src/router/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ const routes: Array<RouteRecordRaw> = [
3535
},
3636
{
3737
path: '/round/:round/drafting',
38-
name: 'PhaseADrafting',
39-
component: PhaseADrafting
38+
// redirect to step 1 of drafting phase (route is kept for backward compatibility)
39+
redirect: (to) => ({ name: 'PhaseADrafting', params: { round: to.params.round, step: '1' } })
4040
},
4141
{
4242
path: '/round/:round/drafting/:step',
43-
name: 'PhaseADraftingStep',
43+
name: 'PhaseADrafting',
4444
component: PhaseADrafting
4545
},
4646
{

src/services/TechCardSelection.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,6 @@ export default class TechCardSelection {
105105
throw new Error('Tech card not found.')
106106
}
107107

108-
/**
109-
* Resets the removed tech cards.
110-
*/
111-
public reset() : void {
112-
this._removedTechs.value = []
113-
}
114-
115108
/**
116109
* Creates a selection of techs in 4 ros respecting the placeholder rows.
117110
*/

src/store/state.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export const useStateStore = defineStore(`${name}.state`, {
2525
storeRound(round : Round) {
2626
this.rounds = this.rounds.filter(item => item.round < round.round)
2727
this.rounds.push(round)
28+
},
29+
storeTechDraftStep(techDraftStep : TechDraftStep) {
30+
const round = this.rounds.find(item => item.round == techDraftStep.round)
31+
if (round) {
32+
round.techDraftSteps = (round.techDraftSteps ?? []).filter(item => item.step < techDraftStep.step)
33+
round.techDraftSteps.push(techDraftStep)
34+
}
2835
}
2936
},
3037
persist: true
@@ -51,24 +58,26 @@ export interface Round {
5158
prosperityCards: ProsperityCardsPersistence
5259
botCards: BotCardsPersistence
5360
rowPlaceholders: RowPlaceholdersPersistence
54-
techCardSelection: TechCardSelectionPersistence
61+
initialTechCardSelection?: TechCardSelectionPersistence
5562
techDraftSteps?: TechDraftStep[]
56-
// the following fields are deprecated, latest implementation uses the techDraftSteps field instead
63+
// the following fields are deprecated, latest implementation uses the initialTechCardSelection and techDraftSteps fields instead
5764
nextStartPlayer?: Player
5865
nextArchitectPlayer?: Player
5966
botTechs?: Tech[]
6067
playerTechs?: Tech[]
6168
playerSpecialActions?: number
69+
techCardSelection?: TechCardSelectionPersistence
6270
}
6371

6472
export interface TechDraftStep {
73+
round: number
6574
step: number
66-
player: Player
75+
techCardSelection: TechCardSelectionPersistence
6776
nextStartPlayer?: Player
6877
nextArchitectPlayer?: Player
69-
botTechs?: Tech[]
70-
playerTechs?: Tech[]
71-
playerSpecialActions?: number
78+
botTechs: Tech[]
79+
playerTechs: Tech[]
80+
playerSpecialActions: number
7281
}
7382

7483
export interface BotCardsPersistence {

src/util/NavigationState.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Round, State, TechDraftStep } from '@/store/state'
1+
import { Round, State, TechCardSelectionPersistence, TechDraftStep } from '@/store/state'
22
import { RouteLocation } from 'vue-router'
33
import getIntRouteParam from '@brdgm/brdgm-commons/src/util/router/getIntRouteParam'
44
import Player from '@/services/enum/Player'
@@ -17,6 +17,7 @@ export default class NavigationState {
1717
readonly techCardSelection : TechCardSelection
1818

1919
private readonly roundData : Round
20+
readonly lastDraftStep? : TechDraftStep
2021

2122
constructor(route: RouteLocation, state: State) {
2223
this.round = getIntRouteParam(route, 'round')
@@ -33,14 +34,14 @@ export default class NavigationState {
3334
prosperityCards: ProsperityCards.new().toPersistence(),
3435
botCards: BotCards.new(state.setup.difficultyLevel).toPersistence(),
3536
rowPlaceholders: rowPlaceholders.toPersistence(),
36-
techCardSelection: TechCardSelection.new(rowPlaceholders.rows, this.round).toPersistence()
3737
}
3838
}
3939
this.roundData = roundData
40+
this.lastDraftStep = this.getLastDraftStep()
4041
this.prosperityCards = ProsperityCards.fromPersistence(roundData.prosperityCards)
4142
this.botCards = BotCards.fromPersistence(roundData.botCards)
4243
this.rowPlaceholders = RowPlaceholders.fromPersistence(roundData.rowPlaceholders)
43-
this.techCardSelection = TechCardSelection.fromPersistence(roundData.techCardSelection, this.round)
44+
this.techCardSelection = TechCardSelection.fromPersistence(this.getTechCardSelectionPersistence(), this.round)
4445
}
4546

4647
public get startPlayer() : Player {
@@ -51,10 +52,25 @@ export default class NavigationState {
5152
return this.lastDraftStep?.nextArchitectPlayer ?? this.roundData.nextArchitectPlayer ?? this.roundData.architectPlayer
5253
}
5354

54-
private get lastDraftStep() : TechDraftStep|undefined {
55-
if (this.roundData.techDraftSteps) {
56-
return this.roundData.techDraftSteps.toSorted((a, b) => a.step - b.step)[this.roundData.techDraftSteps.length - 1]
55+
private getLastDraftStep() : TechDraftStep | undefined {
56+
const techDraftSteps = (this.roundData.techDraftSteps?.toSorted((a, b) => a.step - b.step) ?? [])
57+
.filter(item => item.step < this.draftingStep || this.draftingStep == 0)
58+
return techDraftSteps[techDraftSteps.length - 1]
59+
}
60+
61+
private getTechCardSelectionPersistence() : TechCardSelectionPersistence {
62+
if (this.lastDraftStep?.techCardSelection) {
63+
return this.lastDraftStep?.techCardSelection
64+
}
65+
if (this.roundData.initialTechCardSelection) {
66+
return this.roundData.initialTechCardSelection
67+
}
68+
if (this.roundData.techCardSelection) {
69+
// backward compatibility with old implementation; reset selection to restart draft
70+
return { techs: this.roundData.techCardSelection.techs, removedTechs: [] }
5771
}
72+
// should never happen
73+
return TechCardSelection.new(this.rowPlaceholders.rows, this.round).toPersistence()
5874
}
5975

6076
}

src/views/PhaseADrafting.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ export default defineComponent({
3535
const state = useStateStore()
3636
3737
const navigationState = new NavigationState(route, state)
38-
const { round } = navigationState
38+
const { round, draftingStep } = navigationState
3939
40-
return { t, state, navigationState, round }
40+
return { t, state, navigationState, round, draftingStep, route }
4141
},
4242
computed: {
4343
nextButtonRouteTo() : string {
4444
return `/round/${this.round}/prosperity`
4545
},
4646
backButtonRouteTo() : string {
47+
if (this.draftingStep > 1) {
48+
return `/round/${this.round}/drafting/${this.draftingStep-1}`
49+
}
4750
if (this.round > 1) {
4851
return `/round/${this.round-1}/upkeep`
4952
}

0 commit comments

Comments
 (0)