Skip to content

Commit

Permalink
navigation state
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Oct 19, 2024
1 parent 8a3a9d6 commit a407946
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 12 deletions.
8 changes: 8 additions & 0 deletions src/services/enum/Player.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Player
*/
enum Player {
PLAYER = 'player',
BOT = 'bot'
}
export default Player
13 changes: 12 additions & 1 deletion src/store/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineStore } from 'pinia'
import { name } from '@/../package.json'
import DifficultyLevel from '@/services/enum/DifficultyLevel'
import ScoringCategory from '@/services/enum/ScoringCategory'
import Player from '@/services/enum/Player'

export const useStateStore = defineStore(`${name}.state`, {
state: () => {
Expand All @@ -20,6 +21,7 @@ export const useStateStore = defineStore(`${name}.state`, {
resetGame() {
this.setup.eraScoringTiles = []
this.setup.finalScoringTiles = []
this.setup.initialCardDeck = undefined
this.rounds = []
},
storeRound(round : Round) {
Expand Down Expand Up @@ -48,17 +50,26 @@ export interface Setup {
difficultyLevel: DifficultyLevel
eraScoringTiles: ScoringCategory[]
finalScoringTiles: ScoringCategory[]
initialCardDeck?: CardDeckPersistence
debugMode?: boolean
}

export interface Round {
round: number
startPlayer: Player
turns: Turn[]
}
export interface Turn {
round: number
turn: number
cardDeck?: CardDeckPersistence
player: Player
botPersistence?: BotPersistence
}
export interface BotPersistence {
cardDeck: CardDeckPersistence
actionRoll: number
territoryRoll: number
beaconRoll: number
}
export interface CardDeckPersistence {
pile: number[]
Expand Down
71 changes: 67 additions & 4 deletions src/util/BotNavigationState.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,78 @@
import { State } from '@/store/state'
import { BotPersistence, CardDeckPersistence, State } from '@/store/state'
import { RouteLocation } from 'vue-router'
import AbstractNavigationState from './AbstractNavigationState'
import getIntRouteParam from '@brdgm/brdgm-commons/src/util/router/getIntRouteParam'
import CardDeck from '@/services/CardDeck'
import Card from '@/services/Card'
import rollDice from '@brdgm/brdgm-commons/src/util/random/rollDice'
import Cards from '@/services/Cards'

export default class BotNavigationState extends AbstractNavigationState {

readonly bot : number
private readonly cardDeck : CardDeck
private readonly currentCard : Card
private readonly actionRoll : number
private readonly territoryRoll : number
private readonly beaconRoll : number

constructor(route : RouteLocation, state : State) {
super(route, state)
this.bot = getIntRouteParam(route, 'bot')

// try to load persistence with rolled die values for current turns
const botPersistence = getBotPersistence(state, this.round, this.turn)
if (botPersistence) {
this.cardDeck = CardDeck.fromPersistence(botPersistence.cardDeck)
this.actionRoll = botPersistence.actionRoll
this.territoryRoll = botPersistence.territoryRoll
this.beaconRoll = botPersistence.beaconRoll
}
// otherwise prepare new turn based on previous card deck
else {
this.cardDeck = CardDeck.fromPersistence(getCardDeckFromPreviousTurn(state, this.round, this.turn))
// draw next card
this.cardDeck.draw()
// roll dice for action selection
this.actionRoll = rollDice(6)
this.territoryRoll = rollDice(6)
this.beaconRoll = rollDice(6)
}
this.currentCard = this.cardDeck.currentCard ?? Cards.get(1)
}

}

function getBotPersistence(state: State, round: number, turn: number) : BotPersistence|undefined {
const roundData = state.rounds.find(item => item.round == round)
if (roundData) {
const turnData = roundData.turns.find(item => item.turn == turn)
if (turnData && turnData.botPersistence) {
return turnData.botPersistence
}
}
return undefined
}

function getCardDeckFromPreviousTurn(state: State, round: number, turn: number) : CardDeckPersistence {
const roundData = state.rounds.find(item => item.round == round)
if (roundData) {
const lastTurnCardDeck = roundData.turns
.filter(item => (item.turn < turn) || turn == 0)
.toSorted((a,b) => a.turn - b.turn)
.map(item => item.botPersistence?.cardDeck)
.find(item => item != undefined)
if (lastTurnCardDeck) {
return lastTurnCardDeck
}
}

// check previous round
if (round > 1) {
return getCardDeckFromPreviousTurn(state, round - 1, 0)
}

// get initial card deck
const initialCardDeck = state.setup.initialCardDeck
if (initialCardDeck) {
return initialCardDeck
}
return CardDeck.new().toPersistence() // should never happen
}
4 changes: 0 additions & 4 deletions src/util/PlayerNavigationState.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { State } from '@/store/state'
import { RouteLocation } from 'vue-router'
import AbstractNavigationState from './AbstractNavigationState'
import getIntRouteParam from '@brdgm/brdgm-commons/src/util/router/getIntRouteParam'

export default class PlayerNavigationState extends AbstractNavigationState {

readonly player : number

constructor(route : RouteLocation, state : State) {
super(route, state)
this.player = getIntRouteParam(route, 'player')
}

}
4 changes: 3 additions & 1 deletion src/views/AppHome.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<p v-html="t('home.play1')"></p>
<p v-html="t('home.play2')"></p>

<div class="row">
<div class="row mb-3">
<div class="col hello">
<div class="vici">
<img src="@/assets/vici-hello.webp" alt=""/>
Expand Down Expand Up @@ -73,6 +73,8 @@ export default defineComponent({
float: right;
width: 200px;
margin-left: 15px;
margin-top: 10px;
margin-bottom: 10px;
img {
width: 100%;
}
Expand Down
16 changes: 14 additions & 2 deletions src/views/SetupBot.vue
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
import { defineComponent } from 'vue'
import { useI18n } from 'vue-i18n'
import FooterButtons from '@/components/structure/FooterButtons.vue'
import { useStateStore } from '@/store/state'
import { Round } from '@/store/state'
import Player from '@/services/enum/Player'
import CardDeck from '@/services/CardDeck'
export default defineComponent({
name: 'SetupBot',
Expand All @@ -43,11 +47,19 @@ export default defineComponent({
},
setup() {
const { t } = useI18n()
return { t }
const state = useStateStore()
return { t, state }
},
methods: {
startGame() : void {
this.$router.push('/round/1')
this.state.setup.initialCardDeck = CardDeck.new().toPersistence()
const round : Round = {
round: 1,
startPlayer: Player.PLAYER,
turns: []
}
this.state.storeRound(round)
this.$router.push('/round/1/turn/1')
}
}
})
Expand Down

0 comments on commit a407946

Please sign in to comment.