Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | 1x 1x 8x 8x 8x 8x 8x 12x 12x 8x 11x 11x 11x 11x 11x 11x 1x 1x 1x 11x 1x 1x 1x 9x 9x 9x 11x 11x 11x 11x 11x 9x 11x 9x 11x 8x 1x 1x 1x 1x 1x 1x 1x 8x 1x 1x 8x 4x 4x 8x 8x 1x 1x 1x 8x | import { CommandLibrary, GameCommand } from './commandLibrary';
 
/**
 * CommandMapping provides a simple interface for developers to add and remove
 * commands from the CommandLibrary.
 *
 * This class allows you to:
 * - Create and add new commands to the library.
 * - Remove commands by name.
 * - Retrieve all commands or check if a command exists.
 * - Clear all commands from the library.
 *
 * Command names are case-insensitive and stored in lowercase.
 * Commands are stored in the CommandLibrary's HashMap.
 */
export class CommandMapping {
  /** Reference to the CommandLibrary singleton */
  private library: CommandLibrary;
 
  /**
   * Creates a new CommandMapping instance and connects to the CommandLibrary.
   */
  constructor() {
    this.library = CommandLibrary.getInstance();
  }
 
  /**
   * Normalizes a command name to lowercase and trims whitespace.
   *
   * @param {string} name - The command name to normalize
   * @returns {string} The normalized command name
   */
  private normalize(name: string): string {
    return name.toLowerCase().trim();
  }
 
  /**
   * Creates and adds a new command to the CommandLibrary.
   *
   * If a command with the same name already exists, it will not be added.
   * The command name is case-insensitive and will be stored in lowercase.
   *
   * @param {string} name - Command name (case-insensitive)
   * @param {() => void} action - Callback function to execute for this command
   * @param {Object} options - Optional configuration object
   * @param {string} options.description - Description of what the command does
   * @param {boolean} options.active - Whether the command is active (default: true)
   * @returns {boolean} Returns true if command was added successfully, false if duplicate or invalid
   */
  public addCommand(
    name: string,
    action: () => void,
    options?: {
      description?: string;
      active?: boolean;
      // icon?: unknown; // Uncomment if you reintroduce `icon` in the interface
    }
  ): boolean {
    const normalized = this.normalize(name);
 
    if (!normalized) {
      console.error('Command name cannot be empty');
      return false;
    }
 
    if (this.library.has(normalized)) {
      console.warn(`Command "${normalized}" already exists`);
      return false;
    }
 
    const cmd: GameCommand = {
      name: normalized,
      action,
      description: options?.description ?? '',
      active: options?.active ?? true,
    };
 
    const ok = this.library.add(cmd);
    if (ok) {
      console.log(`Command "${normalized}" added successfully`);
    } else {
      console.warn(`Failed to add command "${normalized}"`);
    }
    return ok;
  }
 
  /**
   * Removes a command from the CommandLibrary by name.
   *
   * The command name is case-insensitive.
   *
   * @param {string} name - The name of the command to remove
   * @returns {boolean} Returns true if command was removed, false if not found
   */
  public removeCommand(name: string): boolean {
    const normalized = this.normalize(name);
    const ok = this.library.remove(normalized);
 
    if (ok) {
      console.log(`Command "${normalized}" removed successfully`);
    } else {
      console.warn(`Command "${normalized}" not found`);
    }
    return ok;
  }
 
  /**
   * Retrieves all command names from the CommandLibrary.
   *
   * @returns {string[]} Array of all command names
   */
  public getAllCommands(): string[] {
    return this.library.list().map(c => c.name);
  }
 
  /**
   * Checks if a command exists in the CommandLibrary.
   *
   * @param {string} name - The name of the command to check
   * @returns {boolean} True if command exists, false otherwise
   */
  public hasCommand(name: string): boolean {
    return this.library.has(name);
  }
 
  /**
   * Retrieves a specific command from the CommandLibrary by name.
   *
   * @param {string} name - The name of the command to retrieve
   * @returns {GameCommand | undefined} The GameCommand object if found, undefined otherwise
   */
  public getCommand(name: string): GameCommand | undefined {
    return this.library.get(name);
  }
 
  /**
   * Clears all commands from the CommandLibrary.
   *
   * @returns {void}
   */
  public clearAllCommands(): void {
    this.library.clear();
    console.log('All commands cleared');
  }
}
 
  |