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
Returns the count of favorites for 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 the specified 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 the favorites list and removes any orphaned entries.
Class Method Details
.add_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Adds a character to the favorites list.
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.}" false end end |
.create_character_id(username, char_name, game_code, frontend = nil) ⇒ Hash
Creates a character ID hash from the provided parameters.
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.
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.
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.
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.}" 0 end end |
.get_account_favorites(data_dir, username) ⇒ Array<Hash>
Retrieves all favorites for a specific account.
175 176 177 178 179 180 181 182 183 184 185 |
# File 'documented/common/gui/favorites_manager.rb', line 175 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 the specified data directory.
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.}" [] end end |
.get_game_favorites(data_dir, game_code) ⇒ Array<Hash>
Retrieves all favorites for a specific game.
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.}" [] end end |
.is_favorite?(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Checks if a character is in the favorites list.
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.}" false end end |
.remove_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Removes a character from the favorites list.
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.}" false end end |
.reorder_favorites(data_dir, ordered_favorites) ⇒ Boolean
Reorders the favorites list based on the provided order.
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.}" false end end |
.toggle_favorite(data_dir, username, char_name, game_code, frontend = nil) ⇒ Boolean
Toggles a character's favorite status.
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.}" false end end |
.validate_and_cleanup_favorites(data_dir) ⇒ Hash
Validates the favorites list and removes any orphaned entries.
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.}" { valid: false, cleaned: 0, errors: [e.] } end end |