Module: Lich::Common::Authentication::GUI

Defined in:
documented/common/authentication/gui.rb

Constant Summary collapse

BUTTON_REENABLE_DEBOUNCE_MS =

Debounce duration (ms) before restoring Play button after successful launch. This limits accidental rapid repeat launches while keeping persistent UI usable. Debounce duration (ms) before restoring Play button after successful launch. This limits accidental rapid repeat launches while keeping persistent UI usable.

2000

Class Method Summary collapse

Class Method Details

.authenticate_and_launch(button:, login_info:, on_success:, on_error: nil) ⇒ void

This method returns an undefined value.

Authenticates the user and launches the game.

Parameters:

  • button (Gtk::Button)

    the button that triggers the launch

  • login_info (Hash)

    user login information including :user_id, :password, :char_name, and :game_code

  • on_success (Proc)

    callback to execute on successful authentication

  • on_error (Proc, nil) (defaults to: nil)

    optional callback to execute on authentication error

Raises:

  • (FatalAuthError)

    if authentication fails

  • (StandardError)

    for unexpected errors



25
26
27
28
29
30
31
32
33
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
60
61
62
63
# File 'documented/common/authentication/gui.rb', line 25

def self.authenticate_and_launch(button:, login_info:, on_success:, on_error: nil)
  button.sensitive = false

  begin
    # Authenticate with game server
    auth_data = Authentication.authenticate(
      account: [:user_id],
      password: [:password],
      character: [:char_name],
      game_code: [:game_code]
    )

    # Format launch data for frontend
    launch_data = LaunchData.prepare(
      auth_data,
      [:frontend],
      [:custom_launch],
      [:custom_launch_dir]
    )

    if on_success
      # Backward compatibility: existing callbacks may still expect 1 arg.
      if on_success.respond_to?(:arity) && on_success.arity == 1
        on_success.call(launch_data)
      else
        on_success.call(launch_data, )
      end
    end

    schedule_button_reenable(button)
  rescue FatalAuthError => e
    handle_auth_error(button, e, on_error)
  rescue StandardError => e
    Lich.log "error: GUI auth unexpected error: #{e.class}: #{e.message}"
    Lich.log e.backtrace.join("\n\t") if e.backtrace
    handle_auth_error(button, StandardError.new("Unexpected login error. See debug log for details."), on_error)
    raise
  end
end

.handle_auth_error(button, error, on_error) ⇒ void

This method returns an undefined value.

Handles authentication errors by updating the button state and invoking error callbacks.

Parameters:

  • button (Gtk::Button)

    the button that triggered the authentication

  • error (StandardError)

    the error that occurred during authentication

  • on_error (Proc, nil)

    optional callback to execute on authentication error



71
72
73
74
75
76
77
78
79
# File 'documented/common/authentication/gui.rb', line 71

def self.handle_auth_error(button, error, on_error)
  button.sensitive = true

  if on_error
    on_error.call(error.message)
  else
    show_error_dialog(button, error.message)
  end
end

.schedule_button_reenable(button) ⇒ void

This method returns an undefined value.

Schedules the re-enabling of the button after a debounce period.

Parameters:

  • button (Gtk::Button)

    the button to re-enable



103
104
105
106
107
108
# File 'documented/common/authentication/gui.rb', line 103

def self.schedule_button_reenable(button)
  GLib::Timeout.add(BUTTON_REENABLE_DEBOUNCE_MS) do
    button.sensitive = true unless button.respond_to?(:destroyed?) && button.destroyed?
    false
  end
end

.show_error_dialog(button, message) ⇒ void

This method returns an undefined value.

Displays an error dialog to the user when authentication fails.

Parameters:

  • button (Gtk::Button)

    the button that triggered the authentication

  • message (String)

    the error message to display



86
87
88
89
90
91
92
93
94
95
96
97
# File 'documented/common/authentication/gui.rb', line 86

def self.show_error_dialog(button, message)
  dialog = Gtk::MessageDialog.new(
    parent: button.toplevel,
    flags: :modal,
    type: :error,
    buttons: :ok,
    message: "Authentication Failed"
  )
  dialog.secondary_text = message
  dialog.run
  dialog.destroy
end