Class: BoxesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/boxes_controller.rb

Instance Method Summary collapse

Instance Method Details

#editObject

This method sets the object to be edited



9
10
11
# File 'app/controllers/boxes_controller.rb', line 9

def edit
  @box = LetterBox.find(params[:id])
end

#indexObject

This method displays the puzzles set for the coming week



3
4
5
6
# File 'app/controllers/boxes_controller.rb', line 3

def index
  BoxesService.set_week_boxes()
  @boxes = LetterBox.where(play_date: Date.tomorrow..Date.tomorrow + 6).order(:play_date)
end

#pathsArray<Array<String>>

This method generates and returns the shortest three paths to complete the current puzzle

Returns:

  • (Array<Array<String>>)


15
16
17
18
19
20
# File 'app/controllers/boxes_controller.rb', line 15

def paths
  paths = BoxesService.iterative_path_search(params[:letters].chars)
  respond_to do |format|
    format.json { render json: { 'paths': paths } }
  end
end

#playObject

This method sets up a game so its ready to be played



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/boxes_controller.rb', line 34

def play
  @aesthetic = Aesthetic.find_by(game_id: Game.find_by(name: "Letter Boxed").id)

  @letter_box = LetterBox.find_by(play_date: Date.today)
  while @letter_box.nil?
    @letter_box = LetterBox.create(
        letters: "clueiostjxnk",
        play_date: Date.today
    )
  end

  session[:lbscore] ||= 0
  session[:lbwords] ||= []
  session[:used_letters] ||= []

  @game_won = all_letters_used?
  @used_letters = session[:used_letters]
  @available_letters = get_available_letters
  @words = session[:lbwords]
  render "letterboxed"
end

#resetObject

This method resets today’s game for the current user



57
58
59
60
61
62
63
# File 'app/controllers/boxes_controller.rb', line 57

def reset
  session[:lbscore] = 0
  session[:lbwords] = []
  session[:used_letters] = []
  flash[:notice] = "Game has been reset!"
  redirect_to boxes_play_path
end

#submit_wordObject

This method checks whether a guess is valid and, if so, updates the user’s score



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/boxes_controller.rb', line 65

def submit_word
  return if all_letters_used?
  word = params[:lbword].downcase
  @letter_box = LetterBox.find_by(play_date: Date.today)

  if valid_word?(word, session[:lbwords].last)
    session[:lbwords] << word
    session[:lbscore] += 1
    session[:used_letters] |= word.chars
    update_stats(1)

    if all_letters_used?
      flash[:notice] = "Congratulations! You've used all letters in #{session[:lbscore]} words!"
      @game_won = true
    else
      remaining = get_available_letters.join(", ")
      flash[:notice] = "Valid word! Remaining letters: #{remaining}"
    end
  else
    redirect_to boxes_play_path, alert: "Invalid word!"; return
  end

  redirect_to boxes_play_path
end

#updateObject

This method updates a Letter Boxed object



23
24
25
26
27
28
29
30
31
# File 'app/controllers/boxes_controller.rb', line 23

def update
  @box = LetterBox.find(params[:id])

  if @box.update(params.require(:letter_box).permit(:letters))
    redirect_to edit_box_path(@box), notice: "Letter Boxed for #{@box.play_date.strftime("%B %d")} updated successfully!"
  else
    redirect_to edit_box_path(@box), alert: "Invalid update"
  end
end