Module: Lich::Common::ClientCapabilities

Defined in:
documented/common/client_capabilities.rb

Overview

Constant Summary collapse

DECLARABLE =

The capabilities a client can declare, in canonical order.

%i[room_element game_links double_bold].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.launch_capabilities ⇒ Array<Symbol>?

The session's set from --frontend-caps=, or nil when not given.

Returns:

  • (Array<Symbol>, nil)


91
92
93
# File 'documented/common/client_capabilities.rb', line 91

def launch_capabilities
  @launch_capabilities
end

Class Method Details

.declaration?(line) ⇒ Boolean

Whether a line from a client is a capability declaration.

Parameters:

  • line (String, nil) —

    one line read from a client

Returns:

  • (Boolean)


51
52
53
# File 'documented/common/client_capabilities.rb', line 51

def declaration?(line)
  line.is_a?(String) && DECLARATION.match?(line)
end

.declare(client, line) ⇒ Boolean

Applies a capability declaration to the client that sent it.

The declared set replaces any earlier declaration from that client. Names Lich does not know are ignored, so a newer client can declare capabilities an older Lich lacks; a declaration with no known name (or none at all) declares the empty set, the full legacy rewrite.

Parameters:

  • client (#declared_capabilities=, nil) —

    the client connection

  • line (String, nil) —

    one line read from that client

Returns:

  • (Boolean) —

    true when the line was a declaration, which the caller must then not forward to the game



66
67
68
69
70
71
72
73
74
75
# File 'documented/common/client_capabilities.rb', line 66

def declare(client, line)
  match = line.is_a?(String) && DECLARATION.match(line)
  return false unless match

  capabilities, unknown = parse(match[:names])
  client.declared_capabilities = capabilities if client.respond_to?(:declared_capabilities=)
  log("info: client declared capabilities [#{capabilities.join(' ')}]" \
      "#{" (ignored unknown: #{unknown.join(' ')})" unless unknown.empty?}")
  true
end

.group(clients) ⇒ Hash{Array<Symbol> => Array}

Groups clients by the capability set each one gets, so a server line is rewritten once per distinct set, not once per client.

Parameters:

  • clients (Array) —

    the clients about to receive one line

Returns:

  • (Hash{Array<Symbol> => Array}) —

    each set and its clients; with no clients, the session's set and an empty list, so the line still runs through the client pipeline (whose state it may update)



116
117
118
119
120
121
# File 'documented/common/client_capabilities.rb', line 116

def group(clients)
  session = session_default
  return { session => clients.dup } if clients.empty? || Frontend.supports_gsl?

  clients.group_by { |client| declared(client) || session }
end

.parse(names) ⇒ Array(Array<Symbol>, Array<String>)

Splits a list of capability names into the known set and the rest.

Parameters:

  • names (String, nil) —

    names separated by spaces, tabs or commas

Returns:

  • (Array(Array<Symbol>, Array<String>)) —

    the frozen known set in DECLARABLE order, and the unknown names as given



82
83
84
85
86
87
# File 'documented/common/client_capabilities.rb', line 82

def parse(names)
  tokens = names.to_s.split(SEPARATOR).reject(&:empty?)
  known, unknown = tokens.partition { |token| DECLARABLE.include?(token.downcase.to_sym) }
  wanted = known.map { |token| token.downcase.to_sym }
  [DECLARABLE.select { |capability| wanted.include?(capability) }.freeze, unknown]
end

.session_default(frontend = $frontend) ⇒ Array<Symbol>

The set a client gets when it has not declared one: the launch flag's, else the catalog's for the session's front end.

Parameters:

  • frontend (String, nil) (defaults to: $frontend) —

    the session's front end

Returns:

  • (Array<Symbol>) —

    frozen, in DECLARABLE order



105
106
107
# File 'documented/common/client_capabilities.rb', line 105

def session_default(frontend = $frontend)
  launch_capabilities || DECLARABLE.select { |capability| Frontend.has_capability?(frontend, capability) }.freeze
end