Module: Lich::Common::GUI::FavoritesManager
- Defined in:
- documented/common/gui/favorites_manager.rb
Class Method Summary collapse
-
.add_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Adds a character to the favorites list.
-
.create_character_id(username, char_name, game_code, frontend = nil) ⇒ Hash
Creates a character ID hash from the provided parameters.
-
.extract_character_id(entry_data) ⇒ Hash
Extracts character ID information from the provided entry data.
-
.favorites_available?(data_dir) ⇒ Boolean
Checks if favorites are available in the specified data directory.
-
.favorites_count(data_dir) ⇒ Integer
Counts the number of favorites in the specified data directory.
-
.get_account_favorites(data_dir, username) ⇒ Array<Hash>
Retrieves all favorites for a specific account.
-
.get_all_favorites(data_dir) ⇒ Array<Hash>
Retrieves all favorites for a given data directory.
-
.get_game_favorites(data_dir, game_code) ⇒ Array<Hash>
Retrieves all favorites for a specific game.
-
.is_favorite?(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Checks if a character is in the favorites list.
-
.remove_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Removes a character from the favorites list.
-
.reorder_favorites(data_dir, ordered_favorites) ⇒ Boolean
Reorders the favorites list based on the provided order.
-
.toggle_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Toggles a character’s favorite status.
-
.validate_and_cleanup_favorites(data_dir) ⇒ Hash
Validates and cleans up orphaned favorites in the specified data directory.
Class Method Details
.add_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Adds a character to the favorites list.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'documented/common/gui/favorites_manager.rb', line 16 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 = YamlState.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.}" false end end |
.create_character_id(username, char_name, game_code, frontend = nil) ⇒ Hash
Creates a character ID hash from the provided parameters.
274 275 276 277 278 279 280 281 |
# File 'documented/common/gui/favorites_manager.rb', line 274 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.
288 289 290 291 292 293 294 295 296 297 |
# File 'documented/common/gui/favorites_manager.rb', line 288 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.
304 305 306 307 308 309 |
# File 'documented/common/gui/favorites_manager.rb', line 304 def self.favorites_available?(data_dir) return false if data_dir.nil? yaml_file = Lich::Common::GUI::YamlState.yaml_file_path(data_dir) File.exist?(yaml_file) end |
.favorites_count(data_dir) ⇒ Integer
Counts the number of favorites in the specified data directory.
165 166 167 168 169 170 171 172 173 174 |
# File 'documented/common/gui/favorites_manager.rb', line 165 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.}" 0 end end |
.get_account_favorites(data_dir, username) ⇒ Array<Hash>
Retrieves all favorites for a specific account.
183 184 185 186 187 188 189 190 191 192 193 |
# File 'documented/common/gui/favorites_manager.rb', line 183 def self.get_account_favorites(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.}" [] end end |
.get_all_favorites(data_dir) ⇒ Array<Hash>
Retrieves all favorites for a given data directory.
122 123 124 125 126 127 128 129 130 131 |
# File 'documented/common/gui/favorites_manager.rb', line 122 def self.get_all_favorites(data_dir) return [] if data_dir.nil? begin YamlState.get_favorites(data_dir) rescue StandardError => e Lich.log "error: Error in FavoritesManager.get_all_favorites: #{e.}" [] end end |
.get_game_favorites(data_dir, game_code) ⇒ Array<Hash>
Retrieves all favorites for a specific game.
202 203 204 205 206 207 208 209 210 211 212 |
# File 'documented/common/gui/favorites_manager.rb', line 202 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.}" [] end end |
.is_favorite?(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Checks if a character is in the favorites list.
105 106 107 108 109 110 111 112 113 114 |
# File 'documented/common/gui/favorites_manager.rb', line 105 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 YamlState.is_favorite?(data_dir, username, char_name, game_code, frontend) rescue StandardError => e Lich.log "error: Error in FavoritesManager.is_favorite?: #{e.}" false end end |
.remove_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Removes a character from the favorites list.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'documented/common/gui/favorites_manager.rb', line 47 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 = YamlState.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.}" false end end |
.reorder_favorites(data_dir, ordered_favorites) ⇒ Boolean
Reorders the favorites list based on the provided order.
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'documented/common/gui/favorites_manager.rb', line 140 def self.reorder_favorites(data_dir, ordered_favorites) return false if data_dir.nil? || ordered_favorites.nil? begin result = YamlState.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.}" false end end |
.toggle_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Toggles a character’s favorite status.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'documented/common/gui/favorites_manager.rb', line 78 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.}" false end end |
.validate_and_cleanup_favorites(data_dir) ⇒ Hash
Validates and cleans up orphaned favorites in the specified data directory.
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 258 259 260 261 262 263 264 |
# File 'documented/common/gui/favorites_manager.rb', line 220 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 = YamlState.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.}" { valid: false, cleaned: 0, errors: [e.] } end end |