robert-damoc / game-stats

0 stars 0 forks source link

Add transition rules to Game state #33

Closed robert-damoc closed 1 year ago

robert-damoc commented 1 year ago

We should not be able to edit the game state from the Game#edit page, so we can remove this functionality.

We want to add some transition rules for when the game state is edited. It is called a state_machine, and there are 2 popular gems in rails for that. 1. state_machine and 2. aasm. We will do that without the gems, so the following code needs to be added to the games model:

  # Define valid state transitions
  VALID_TRANSITIONS = {
    created: [:in_progress, :canceled],
    in_progress: [:completed, :canceled],
    completed: [],
    canceled: []
  }.freeze

  # Add validations for state transitions
  validate :valid_state_transition, on: :update

  private

  # Validate if the state transition is allowed
  def valid_state_transition
    unless VALID_TRANSITIONS[state_was.to_sym].include?(state.to_sym)
      errors.add(:state, "invalid state transition")
    end
  end

For the UI, we will add in the actions section the following icons

  1. Play (the icon for play from music).
    • This will update the game state to in_progress
    • Will only be visible if the game state is created
    • Can only click it if you have between 2 and 8 players
  2. Cancel (I suppose an X or the stop button from music here)
    • This will update the game state to canceled
    • Will only be visible if the game state is created or in_progress
  3. Complete/Finish (no idea on the icon, maybe a race flag, or an ok mark, or smt)
    • This will update the game state to completed
    • Will only be visible if the game state is in_progress