Class: MatchStartController

Inherits:
ApplicationController show all
Includes:
ApplicationDefs, ApplicationHelper, MatchStartHelper
Defined in:
app/controllers/match_start_controller.rb

Overview

Controller for the 'start a new game' view.

Constant Summary

Constants included from ApplicationDefs

ApplicationDefs::LOG_DIRECTORY, ApplicationDefs::MATCH_LOG_DIRECTORY

Instance Method Summary collapse

Methods included from MatchStartHelper

#hidden_begin_match, #hidden_begin_match_form, #hidden_game_def_file_name, #hidden_match_id, #hidden_match_name, #hidden_match_start_poll_form, #hidden_num_hands, #hidden_poll, #hidden_rand_seed, #hidden_seat, #hidden_start_opponents, #hidden_submit, #label_for_required, #matches_to_join, #matches_to_rejoin, #num_players, #seats_to_join, #seats_to_rejoin, #send_parameters_to_connect_to_dealer, #truncate_opponent_names_if_necessary, #wait_for_match_to_start

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 included from ApplicationDefs

bots, game_definitions, included, random_seat, random_seed

Methods inherited from ApplicationController

report_error_request_message

Instance Method Details

#indexObject

Presents the main 'start a new game' view.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/match_start_controller.rb', line 19

def index
  Match.delete_irrelevant_matches!

  unless user_initialized?
    @alert_message = "Unable to set default hotkeys for #{user.name}, #{self.class.report_error_request_message}"
  end

  respond_to do |format|
    format.html {} # Render the default partial
    format.js do
      replace_page_contents NEW_MATCH_PARTIAL
    end
  end
end

#joinObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/match_start_controller.rb', line 61

def join
  match_name = params[:match_name].strip
  seat = params[:seat].to_i

  return reset_to_match_entry_view(
    "Sorry, unable to join match \"#{match_name}\" in seat #{seat}."
  ) if (
    error? do
      opponent_users_match = Match.where(name_from_user: match_name).first
      raise unless opponent_users_match

      # Copy match information
      @match = opponent_users_match.dup
      underscore = '_'
      @match.name_from_user = underscore
      while !@match.save do
        @match.name_from_user << underscore
      end
      @match.user_name = user.name

      # Swap seat
      @match.seat = seat
      @match.opponent_names.insert(
        opponent_users_match.seat - 1,
        opponent_users_match.user_name
      )
      @match.opponent_names.delete_at(seat - 1)
      @match.save!(validate: false)

      TableManager.perform_async(
        ApplicationDefs::START_PROXY_REQUEST_CODE,
        @match.id.to_s
      )
    end
  )

  wait_for_match_to_start
end

#newObject



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

def new
  params[:match][:opponent_names] = truncate_opponent_names_if_necessary(
    params[:match]
  )
  return reset_to_match_entry_view(
    'Sorry, unable to finish creating a match instance, please try again or rejoin a match already in progress.'
  ) if (
    error? do
      @match = Match.new(params[:match].merge(user_name: user_name)).finish_starting!
    end
  )

  TableManager.perform_async(
    ApplicationDefs::START_MATCH_REQUEST_CODE,
    @match.id.to_s,
    options: [
      '-a', # Append logs with the same name rather than overwrite
      "--t_response #{DEALER_MILLISECOND_TIMEOUT}",
      '--t_hand -1',
      '--t_per_hand -1'
    ].join(' '),
    log_directory: MATCH_LOG_DIRECTORY
  )

  wait_for_match_to_start
end

#pollObject



116
117
118
119
# File 'app/controllers/match_start_controller.rb', line 116

def poll
  @match = Match.find params[:match_id]
  wait_for_match_to_start
end

#rejoinObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'app/controllers/match_start_controller.rb', line 100

def rejoin
  match_name = params[:match_name].strip
  seat = params[:seat].to_i

  return reset_to_match_entry_view(
    "Sorry, unable to find match \"#{match_name}\" in seat #{seat}."
  ) if (
    error? do
      @match = Match.where(name: match_name, seat: seat).first
      raise unless @match
    end
  )

  wait_for_match_to_start
end