diff --git a/src/lib/ActionInputStateMachine.svelte.ts b/src/lib/ActionInputStateMachine.svelte.ts index 6875221..bf65756 100644 --- a/src/lib/ActionInputStateMachine.svelte.ts +++ b/src/lib/ActionInputStateMachine.svelte.ts @@ -1,14 +1,13 @@ -import type { AutoAction, AutoInputState } from '$lib/types'; +import type { AutoAction, AutoActionData, AutoInputState } from '$lib/types'; export class ActionInputStateMachine { actionState: AutoInputState = $state('None') as AutoInputState; held_bunnies: number = 0; - held_balloons: number = $state(0); - held_totes: number = $state(0); - held_scorables: number = $derived(this.held_balloons + this.held_bunnies); - held_ejectables: number = $derived(this.held_scorables + this.held_totes); + held_balloons: number = 0; + held_totes: number = 0; + /// Methods for adjusting the state (checked by user) intake_piece() { this.actionState = this.actionState === 'None' ? 'Intake' : this.actionState; } @@ -27,20 +26,39 @@ export class ActionInputStateMachine { } complete_action(success: boolean): AutoAction { + const action_data: AutoActionData = { + action: this.actionState as AutoAction, + success, + ok: true + }; + + if (this.new_action(action_data)) { + const ret = this.actionState.slice(); + this.actionState = 'None'; + return ret as AutoAction; + } else { + console.error('Illegal action, this is a bug'); + // UB aqfter contract of state machine has been broken + return {} as unknown as AutoAction; + } + } + + // Takes an action and returns if it's a legal one + public new_action(action_data: AutoActionData): boolean { + const success = action_data.success; + const action = action_data.action; if (success) { - if (this.actionState.includes('IntakeBalloon')) this.held_balloons++; - else if (this.actionState.includes('IntakeBunny')) this.held_bunnies++; - else if (this.actionState.includes('IntakeTote')) this.held_totes++; - else if (this.actionState.includes('EjectBalloon')) this.held_balloons--; - else if (this.actionState.includes('EjectBunny')) this.held_bunnies--; - else if (this.actionState.includes('EjectTote')) this.held_totes--; + if (action.includes('IntakeBalloon')) this.held_balloons++; + else if (action.includes('IntakeBunny')) this.held_bunnies++; + else if (action.includes('IntakeTote')) this.held_totes++; + else if (action.includes('EjectBalloon')) this.held_balloons--; + else if (action.includes('EjectBunny')) this.held_bunnies--; + else if (action.includes('EjectTote')) this.held_totes--; } - // Assume failed scoring is still ejecting - if (this.actionState.includes('ScoreBalloon')) this.held_balloons--; - else if (this.actionState.includes('ScoreBunny')) this.held_bunnies--; + if (action.includes('ScoreBalloon')) this.held_balloons--; + else if (action.includes('ScoreBunny')) this.held_bunnies--; - const ret = this.actionState.slice(); - this.actionState = 'None'; - return ret as AutoAction; + if (action.includes('Intake')) return true; + return this.held_balloons >= 0 && this.held_bunnies >= 0 && this.held_totes >= 0; } } diff --git a/src/lib/components/Action.svelte b/src/lib/components/Action.svelte index 103f0fb..ea61e37 100644 --- a/src/lib/components/Action.svelte +++ b/src/lib/components/Action.svelte @@ -26,15 +26,18 @@ > {action_data.action}