Module: Lich::Common::GUI::FavoritesManager

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

Class Method Summary collapse

Class Method Details

.add_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean

Adds a character to the favorites list.

Parameters:

  • data_dir (String)

    the directory containing data files

  • username (String)

    the user's account name

  • char_name (String)

    the name of the character to add

  • game_code (String)

    the game code associated with the character

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

    optional frontend identifier

Returns:

  • (Boolean)

    true if the character was added successfully, false otherwise

Raises:

  • (StandardError)

    if an error occurs during the operation



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'documented/common/gui/favorites_manager.rb', line 15

def self.add_favorite(data_dir, username, char_name, game_code, frontend = nil)
  return false if data_dir.nil? || username.nil? || char_name.nil? || game_code.nil?

  begin
    result = Lich::Common::Authentication::EntryStore.add_favorite(data_dir, username, char_name, game_code, frontend)

    if result
      frontend_info = frontend ? " (#{frontend})" : ""
      Lich.log "info: Added character '#{char_name}' (#{game_code})#{frontend_info} from account '#{username}' to favorites"
    else
      frontend_info = frontend ? " (#{frontend})" : ""
      Lich.log "warning: Failed to add character '#{char_name}' (#{game_code})#{frontend_info} from account '#{username}' to favorites"
    end

    result
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.add_favorite: #{e.message}"
    false
  end
end

.create_character_id(username, char_name, game_code, frontend = nil) ⇒ Hash

Creates a character ID hash from the provided parameters.

Parameters:

  • username (String)

    the user's account name

  • char_name (String)

    the name of the character

  • game_code (String)

    the game code associated with the character

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

    optional frontend identifier

Returns:

  • (Hash)

    a hash representing the character ID



266
267
268
269
270
271
272
273
# File 'documented/common/gui/favorites_manager.rb', line 266

def self.create_character_id(username, char_name, game_code, frontend = nil)
  {
    username: username,
    char_name: char_name,
    game_code: game_code,
    frontend: frontend
  }
end

.extract_character_id(entry_data) ⇒ Hash

Extracts character ID information from the provided entry data.

Parameters:

  • entry_data (Hash)

    the entry data containing character information

Returns:

  • (Hash)

    a hash containing the extracted character ID information, or an empty hash if input is invalid



279
280
281
282
283
284
285
286
287
288
# File 'documented/common/gui/favorites_manager.rb', line 279

def self.extract_character_id(entry_data)
  return {} unless entry_data.is_a?(Hash)

  {
    username: entry_data[:user_id],
    char_name: entry_data[:char_name],
    game_code: entry_data[:game_code],
    frontend: entry_data[:frontend]
  }
end

.favorites_available?(data_dir) ⇒ Boolean

Checks if favorites are available in the specified data directory.

Parameters:

  • data_dir (String)

    the directory containing data files

Returns:

  • (Boolean)

    true if favorites are available, false otherwise



294
295
296
297
298
299
# File 'documented/common/gui/favorites_manager.rb', line 294

def self.favorites_available?(data_dir)
  return false if data_dir.nil?

  yaml_file = Lich::Common::Authentication::EntryStore.yaml_file_path(data_dir)
  File.exist?(yaml_file)
end

.favorites_count(data_dir) ⇒ Integer

Returns the count of favorites for the specified data directory.

Parameters:

  • data_dir (String)

    the directory containing data files

Returns:

  • (Integer)

    the number of favorites

Raises:

  • (StandardError)

    if an error occurs during the operation



158
159
160
161
162
163
164
165
166
167
# File 'documented/common/gui/favorites_manager.rb', line 158

def self.favorites_count(data_dir)
  return 0 if data_dir.nil?

  begin
    get_all_favorites(data_dir).length
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.favorites_count: #{e.message}"
    0
  end
end

.get_account_favorites(data_dir, username) ⇒ Array<Hash>

Retrieves all favorites for a specific account.

Parameters:

  • data_dir (String)

    the directory containing data files

  • username (String)

    the user's account name

Returns:

  • (Array<Hash>)

    an array of favorites associated with the account

Raises:

  • (StandardError)

    if an error occurs during the operation



175
176
177
178
179
180
181
182
183
184
185
# File 'documented/common/gui/favorites_manager.rb', line 175

def self.(data_dir, username)
  return [] if data_dir.nil? || username.nil?

  begin
    all_favorites = get_all_favorites(data_dir)
    all_favorites.select { |fav| fav[:user_id] == username }
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.get_account_favorites: #{e.message}"
    []
  end
end

.get_all_favorites(data_dir) ⇒ Array<Hash>

Retrieves all favorites for the specified data directory.

Parameters:

  • data_dir (String)

    the directory containing data files

Returns:

  • (Array<Hash>)

    an array of favorite characters, each represented as a hash

Raises:

  • (StandardError)

    if an error occurs during the operation



117
118
119
120
121
122
123
124
125
126
# File 'documented/common/gui/favorites_manager.rb', line 117

def self.get_all_favorites(data_dir)
  return [] if data_dir.nil?

  begin
    Lich::Common::Authentication::EntryStore.get_favorites(data_dir)
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.get_all_favorites: #{e.message}"
    []
  end
end

.get_game_favorites(data_dir, game_code) ⇒ Array<Hash>

Retrieves all favorites for a specific game.

Parameters:

  • data_dir (String)

    the directory containing data files

  • game_code (String)

    the game code to filter favorites

Returns:

  • (Array<Hash>)

    an array of favorites associated with the game

Raises:

  • (StandardError)

    if an error occurs during the operation



193
194
195
196
197
198
199
200
201
202
203
# File 'documented/common/gui/favorites_manager.rb', line 193

def self.get_game_favorites(data_dir, game_code)
  return [] if data_dir.nil? || game_code.nil?

  begin
    all_favorites = get_all_favorites(data_dir)
    all_favorites.select { |fav| fav[:game_code] == game_code }
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.get_game_favorites: #{e.message}"
    []
  end
end

.is_favorite?(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean

Checks if a character is in the favorites list.

Parameters:

  • data_dir (String)

    the directory containing data files

  • username (String)

    the user's account name

  • char_name (String)

    the name of the character to check

  • game_code (String)

    the game code associated with the character

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

    optional frontend identifier

Returns:

  • (Boolean)

    true if the character is a favorite, false otherwise

Raises:

  • (StandardError)

    if an error occurs during the operation



101
102
103
104
105
106
107
108
109
110
# File 'documented/common/gui/favorites_manager.rb', line 101

def self.is_favorite?(data_dir, username, char_name, game_code, frontend = nil)
  return false if data_dir.nil? || username.nil? || char_name.nil? || game_code.nil?

  begin
    Lich::Common::Authentication::EntryStore.is_favorite?(data_dir, username, char_name, game_code, frontend)
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.is_favorite?: #{e.message}"
    false
  end
end

.remove_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean

Removes a character from the favorites list.

Parameters:

  • data_dir (String)

    the directory containing data files

  • username (String)

    the user's account name

  • char_name (String)

    the name of the character to remove

  • game_code (String)

    the game code associated with the character

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

    optional frontend identifier

Returns:

  • (Boolean)

    true if the character was removed successfully, false otherwise

Raises:

  • (StandardError)

    if an error occurs during the operation



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'documented/common/gui/favorites_manager.rb', line 45

def self.remove_favorite(data_dir, username, char_name, game_code, frontend = nil)
  return false if data_dir.nil? || username.nil? || char_name.nil? || game_code.nil?

  begin
    result = Lich::Common::Authentication::EntryStore.remove_favorite(data_dir, username, char_name, game_code, frontend)

    if result
      frontend_info = frontend ? " (#{frontend})" : ""
      Lich.log "info: Removed character '#{char_name}' (#{game_code})#{frontend_info} from account '#{username}' from favorites"
    else
      frontend_info = frontend ? " (#{frontend})" : ""
      Lich.log "warning: Failed to remove character '#{char_name}' (#{game_code})#{frontend_info} from account '#{username}' from favorites"
    end

    result
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.remove_favorite: #{e.message}"
    false
  end
end

.reorder_favorites(data_dir, ordered_favorites) ⇒ Boolean

Reorders the favorites list based on the provided order.

Parameters:

  • data_dir (String)

    the directory containing data files

  • ordered_favorites (Array<Hash>)

    the new order of favorites

Returns:

  • (Boolean)

    true if the reorder was successful, false otherwise

Raises:

  • (StandardError)

    if an error occurs during the operation



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'documented/common/gui/favorites_manager.rb', line 134

def self.reorder_favorites(data_dir, ordered_favorites)
  return false if data_dir.nil? || ordered_favorites.nil?

  begin
    result = Lich::Common::Authentication::EntryStore.reorder_favorites(data_dir, ordered_favorites)

    if result
      Lich.log "info: Successfully reordered #{ordered_favorites.length} favorites"
    else
      Lich.log "warning: Failed to reorder favorites"
    end

    result
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.reorder_favorites: #{e.message}"
    false
  end
end

.toggle_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean

Toggles a character's favorite status.

Parameters:

  • data_dir (String)

    the directory containing data files

  • username (String)

    the user's account name

  • char_name (String)

    the name of the character to toggle

  • game_code (String)

    the game code associated with the character

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

    optional frontend identifier

Returns:

  • (Boolean)

    true if the character is now a favorite, false if it was removed

Raises:

  • (StandardError)

    if an error occurs during the operation



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'documented/common/gui/favorites_manager.rb', line 75

def self.toggle_favorite(data_dir, username, char_name, game_code, frontend = nil)
  return false if data_dir.nil? || username.nil? || char_name.nil? || game_code.nil?

  begin
    if is_favorite?(data_dir, username, char_name, game_code, frontend)
      remove_favorite(data_dir, username, char_name, game_code, frontend)
      false
    else
      add_favorite(data_dir, username, char_name, game_code, frontend)
      true
    end
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.toggle_favorite: #{e.message}"
    false
  end
end

.validate_and_cleanup_favorites(data_dir) ⇒ Hash

Validates the favorites list and removes any orphaned entries.

Parameters:

  • data_dir (String)

    the directory containing data files

Returns:

  • (Hash)

    a hash containing validation results, including:

    • valid [Boolean] indicates if the validation was successful
    • cleaned [Integer] number of cleaned favorites
    • errors [Array] list of errors encountered during cleanup

Raises:

  • (StandardError)

    if an error occurs during the operation



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'documented/common/gui/favorites_manager.rb', line 213

def self.validate_and_cleanup_favorites(data_dir)
  return { valid: false, cleaned: 0, errors: ['Invalid data directory'] } if data_dir.nil?

  begin
    # Load all entry data to validate against
    entry_data = Lich::Common::Authentication::EntryStore.load_saved_entries(data_dir, false)
    favorites = get_all_favorites(data_dir)

    cleaned_count = 0
    errors = []

    favorites.each do |favorite|
      # Check if the character still exists in the entry data
      character_exists = entry_data.any? do |entry|
        entry[:user_id] == favorite[:user_id] &&
          entry[:char_name] == favorite[:char_name] &&
          entry[:game_code] == favorite[:game_code] &&
          (favorite[:frontend].nil? || entry[:frontend] == favorite[:frontend])
      end

      unless character_exists
        # Remove orphaned favorite
        if remove_favorite(data_dir, favorite[:user_id], favorite[:char_name], favorite[:game_code], favorite[:frontend])
          cleaned_count += 1
          frontend_info = favorite[:frontend] ? " (#{favorite[:frontend]})" : ""
          Lich.log "info: Removed orphaned favorite: #{favorite[:char_name]} (#{favorite[:game_code]})#{frontend_info} from #{favorite[:user_id]}"
        else
          frontend_info = favorite[:frontend] ? " (#{favorite[:frontend]})" : ""
          errors << "Failed to remove orphaned favorite: #{favorite[:char_name]} (#{favorite[:game_code]})#{frontend_info} from #{favorite[:user_id]}"
        end
      end
    end

    {
      valid: true,
      total_favorites: favorites.length,
      cleaned: cleaned_count,
      remaining: favorites.length - cleaned_count,
      errors: errors
    }
  rescue StandardError => e
    Lich.log "error: Error in FavoritesManager.validate_and_cleanup_favorites: #{e.message}"
    { valid: false, cleaned: 0, errors: [e.message] }
  end
end