Class: PlayerActionsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper, PlayerActionsHelper
Defined in:
app/controllers/player_actions_controller.rb

Overview

Controller for the main game view where the table and actions are presented to the player. Implements the actions in the main match view.

Instance Method Summary collapse

Methods included from PlayerActionsHelper

#acting_player_id, #custom_hotkey_amount_field_tag, #custom_hotkey_key_field_tag, #custom_hotkeys_amount_param_key, #custom_hotkeys_keys_param_key, #customize_hotkeys_html_id, #fold_disabled_when, #fold_html_class, #hotkey_field_tag, #hotkeys_param_key, #html_character, #leave_match_button_html_class, #leave_match_confirmation_message, #leave_match_label, #make_wager_button_label, #match_view, #nav_leave_html_class, #next_hand_button_visible?, #next_hand_id, #no_change?, #pass_action_button_label, #pass_html_class, #replace_page_contents_with_updated_game_view, #update_hotkeys_html_class, #update_id, #update_state_html_class, #user_must_act?, #wager_disabled_when, #wager_html_class, #waiting_for_response

Methods included from ApplicationHelper

#error?, #help_link, #link_with_glyph, #log_error, #match, #replace_page_contents, #reset_to_match_entry_view, #user, #user_initialized?, #user_name

Methods inherited from ApplicationController

report_error_request_message

Instance Method Details

#indexObject



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/controllers/player_actions_controller.rb', line 23

def index
  return reset_to_match_entry_view(
    "Sorry, there was a problem starting the match, #{self.class.report_error_request_message}."
  ) if (
    error? do
      session['waiting_for_response'] = false
      Rails.logger.ap waiting_for_response: session['waiting_for_response']

      replace_page_contents_with_updated_game_view params[:match_id]
    end
  )
end

#leave_matchObject



192
193
194
# File 'app/controllers/player_actions_controller.rb', line 192

def leave_match
  redirect_to root_path, remote: true
end

#log_sessionObject



19
20
21
# File 'app/controllers/player_actions_controller.rb', line 19

def log_session
  Rails.logger.ap session: session
end

#reset_hotkeysObject



187
188
189
190
# File 'app/controllers/player_actions_controller.rb', line 187

def reset_hotkeys
  user.reset_hotkeys!
  return replace_page_contents_with_updated_game_view(params[:match_id])
end

#take_actionObject



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

def take_action
  return reset_to_match_entry_view(
    "Sorry, there was a problem taking action #{params[:poker_action]}, #{self.class.report_error_request_message}."
  ) if (
    error? do
      # Initialize the match view so that the app is guaranteed to not update before showing the
      # table.
      @match_view = MatchView.new params[:match_id]
      TableManager.perform_async(
        ApplicationDefs::PLAY_ACTION_REQUEST_CODE,
        params[:match_id],
        action: params[:poker_action]
      )
      session['waiting_for_response'] = true

      replace_page_contents_with_updated_game_view(params[:match_id])
    end
  )
end

#updateObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'app/controllers/player_actions_controller.rb', line 77

def update
  deleted_slice = nil

  return reset_to_match_entry_view(
    "Sorry, there was a problem cleaning up the previous match slice of #{params[:match_id]}, #{self.class.report_error_request_message}."
  ) if (
    error? do
      @match_view = MatchView.new params[:match_id]

      # Abort the update if there's only one slice
      if @match_view.match.slices.length < 2

        if @match_view.hand_ended?
          # To ensure that we can't try to click 'Next Hand' again.
          session['waiting_for_response'] = true
          return replace_page_contents_with_updated_game_view(params[:match_id])
        end

        Rails.logger.ap action: 'update', param_ms: params[:match_state], view_ms: @match_view.state.to_s, equal_ms: params[:match_state] == @match_view.state.to_s

        if params[:match_state] == @match_view.state.to_s
          @update_state_periodically = true;
          return replace_page_contents_with_updated_game_view(params[:match_id])
        end
        return replace_page_contents_with_updated_game_view(params[:match_id])
      end

      # Delete the first slice in the list since it's no longer needed
      deleted_slice = @match_view.match.slices.first
      deleted_slice.delete
    end
  )

  if (
    error? do
      session['waiting_for_response'] = false
      replace_page_contents_with_updated_game_view(params[:match_id])
    end
  )
    begin
      @match_view.match.slices << deleted_slice if @match_view.match.slices.empty?
      @match_view.match.save!
    rescue => e
      # If the match can't be retrieved or saved then
      # it can't be resumed anyway, so nothing
      # special to do here.
      log_error e
    end
    return(
      reset_to_match_entry_view(
        "Sorry, there was a problem continuing the match, #{self.class.report_error_request_message}."
      )
    )
  end
end

#update_hotkeysObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/player_actions_controller.rb', line 133

def update_hotkeys
  return reset_to_match_entry_view(
    "Sorry, there was a problem saving hotkeys, #{self.class.report_error_request_message}."
  ) if (
    error? do
      conflicting_hotkeys = []

      hotkey_hash = params[hotkeys_param_key]
      params[custom_hotkeys_amount_param_key].zip(
        params[custom_hotkeys_keys_param_key]
      ).each do |amount, key|
        hotkey_hash[Hotkey.wager_hotkey_label(amount.to_f)] = key
      end
      hotkey_hash.each do |action_label, new_key|
        if new_key.blank?
          # Delete custom hotkeys that have been left blank
          user.hotkeys.where(action: action_label).delete unless Hotkey::DEFAULT_HOTKEYS.include?(action_label)
          next
        end
        next if action_label.blank?
        new_key = new_key.strip.capitalize
        next if no_change?(action_label, new_key)

        conflicted_hotkey = user.hotkeys.select { |hotkey| hotkey.key == new_key }.first
        if conflicted_hotkey
          conflicted_label = conflicted_hotkey.action
          if conflicted_label
            conflicting_hotkeys << { key: new_key, current_label: conflicted_label, new_label: action_label }
            next
          end
        end

        previous_hotkey = user.hotkeys.where(action: action_label).first
        if previous_hotkey
          previous_hotkey.key = new_key
          previous_hotkey.save!
        else
          user.hotkeys.create! action: action_label, key: new_key
        end
      end
      user.save!

      unless conflicting_hotkeys.empty?
        @alert_message = "Sorry, the following hotkeys conflicted and were not saved: \n" <<
          conflicting_hotkeys.map do |conflict|
            "    - You tried to set '#{conflict[:new_label]}' to '#{conflict[:key]}' when it was already mapped to '#{conflict[:current_label]}'\n"
          end.join
      end
      return replace_page_contents_with_updated_game_view(params[:match_id])
    end
  )
  render nothing: true
end

#update_stateObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/player_actions_controller.rb', line 56

def update_state
  return reset_to_match_entry_view(
    "Sorry, there was a problem retrieving match #{params[:match_id]}, #{self.class.report_error_request_message}."
  ) if (
    error? do
      @match_view = MatchView.new params[:match_id]

      Rails.logger.ap hand_ended: @match_view.hand_ended?

      return update unless @match_view.hand_ended?
    end
  )
  Rails.logger.ap action: 'update_state', waiting_for_response: session['waiting_for_response']

  if params[:match_state] == @match_view.state.to_s
    @update_state_periodically = true;
    return replace_page_contents_with_updated_game_view(params[:match_id])
  end
  replace_page_contents_with_updated_game_view(params[:match_id])
end