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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 22x 22x 17x 21x 21x 21x 21x 21x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 21x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 19x 21x 21x 21x 21x 21x 19x 21x 21x 21x 21x 5x 5x 5x 5x 5x 5x 19x 19x 19x 19x 19x 19x 21x 17x 5x 5x 5x 5x 2x 2x 2x 3x 5x 8x 8x 8x 8x 8x 3x 3x 5x 5x 5x 5x 5x 17x 7x 7x 17x 1x 1x 17x 3x 3x 17x 3x 3x 3x 4x 4x 3x 3x 4x 3x 3x 17x 1x 1x 1x 1x 1x 1x 1x 17x 2x 2x 17x 2x 2x 17x 17x 1x 1x 1x 17x 1x 1x 17x | import { CommandLibrary, GameCommand } from './commandLibrary';
import { SynonymResolver } from './SynonymResolver';
/**
* Result object returned when adding a command with synonym fetching.
* Provides developers with information about what synonyms were registered.
*/
export interface CommandAddResult {
/** Whether the command was successfully added */
success: boolean;
/** The command name that was added */
commandName: string;
/** Array of synonyms that were successfully registered */
synonymsMapped: string[];
/** Total number of synonyms mapped */
synonymCount: number;
/** Error message if command addition failed */
error?: string;
}
/**
* 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.
* - Automatically fetch and register synonyms for commands
* - 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.
* Synonyms are automatically fetched from DataMuse API unless disabled.
*/
export class CommandMapping {
/** Reference to the CommandLibrary singleton */
private library: CommandLibrary;
private synonymResolver: SynonymResolver;
/**
* Creates a new CommandMapping instance and connects to the CommandLibrary.
*/
constructor() {
this.library = CommandLibrary.getInstance();
this.synonymResolver = SynonymResolver.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)
* @param {boolean} options.fetchSynonyms - Whether to auto-fetch synonyms (default: true)
* @param {number} options.numberOfSynonyms - Number of synonyms to fetch (default: 3)
* @returns {Promise<CommandAddResult>} Result object with command and synonym information
*/
public async addCommand(
name: string,
action: () => void,
options?: {
description?: string;
active?: boolean;
fetchSynonyms?: boolean;
numberOfSynonyms?: number;
}
): Promise<CommandAddResult> {
const normalized = this.normalize(name);
if (!normalized) {
console.error('Command name cannot be empty');
return {
success: false,
commandName: name,
synonymsMapped: [],
synonymCount: 0,
error: 'Command name cannot be empty'
};
}
if (this.library.has(normalized)) {
console.warn(`Command "${normalized}" already exists`);
return {
success: false,
commandName: normalized,
synonymsMapped: [],
synonymCount: 0,
error: `Command "${normalized}" already exists`
};
}
const cmd: GameCommand = {
name: normalized,
action,
description: options?.description ?? '',
active: options?.active ?? true,
};
const ok = this.library.add(cmd);
if (!ok) {
console.warn(`Failed to add command "${normalized}"`);
return {
success: false,
commandName: normalized,
synonymsMapped: [],
synonymCount: 0,
error: `Failed to add command "${normalized}"`
};
}
console.log(`Command "${normalized}" added successfully`);
// Fetch and register synonyms if enabled (default: true)
const fetchSynonyms = options?.fetchSynonyms ?? true;
let numOfSynonyms = options?.numberOfSynonyms ?? 3;
let synonymsMapped: string[] = [];
if (fetchSynonyms) {
try {
if(numOfSynonyms<1){
numOfSynonyms = 1;
}
this.synonymResolver.setMAX_RESULTS(numOfSynonyms);//set the number of synonyms to be added
synonymsMapped = await this.fetchAndRegisterSynonyms(normalized);
} catch (error) {
console.error(`Error fetching synonyms for "${normalized}":`, error);
}
}
return {
success: true,
commandName: normalized,
synonymsMapped: synonymsMapped,
synonymCount: synonymsMapped.length
};
}
/**
* Fetches synonyms from the API and registers them in the CommandLibrary.
* This is called automatically by addCommand() unless fetchSynonyms is disabled.
*
* @private
* @param {string} commandName - The command name to fetch synonyms for
* @returns {Promise<string[]>}
*/
private async fetchAndRegisterSynonyms(commandName: string): Promise<string[]>{
console.log(`Fetching synonyms for "${commandName}"...`);
try {
// Get synonyms from the API (cached if already fetched)
const synonyms = await this.synonymResolver.getSynonyms(commandName);
if (synonyms.length === 0) {
console.log(`No synonyms found for "${commandName}"`);
return [];
}
// Register all synonyms in the library
const successfullyAdded: string[] = [];
for (const synonym of synonyms) {
const added = this.library.addSynonym(synonym, commandName);
if (added) {
successfullyAdded.push(synonym);
}
}
console.log(
`Registered ${successfullyAdded.length} synonym(s) for "${commandName}": ` +
`${successfullyAdded.slice(0, 5).join(', ')}${successfullyAdded.length > 5 ? '...' : ''}`
);
return successfullyAdded;
} catch (error) {
console.error(`Failed to fetch synonyms for "${commandName}":`, error);
return [];
}
}
/**
* Manually adds a synonym for an existing command.
* Use this to add custom synonyms that aren't in the API.
*
* @param {string} synonym - The synonym word
* @param {string} commandName - The command it should trigger
* @returns {boolean} True if synonym was added successfully
*
* @example
* ```ts
* mapper.addSynonym('hop', 'jump'); // Now "hop" triggers "jump" command
* ```
*/
public addSynonym(synonym: string, commandName: string): boolean {
return this.library.addSynonym(synonym, commandName);
}
/**
* Manually adds multiple synonyms for an existing command.
*
* @param {string[]} synonyms - Array of synonym words
* @param {string} commandName - The command they should trigger
* @returns {number} Number of synonyms successfully added
*
* @example
* ```ts
* mapper.addSynonyms(['hop', 'leap', 'spring'], 'jump');
* ```
*/
public addSynonyms(synonyms: string[], commandName: string): number {
return this.library.addSynonyms(synonyms, commandName);
}
/**
* Gets all synonyms for a specific command.
*
* @param {string} commandName - The command name
* @returns {string[]} Array of synonyms
*
* @example
* ```ts
* const synonyms = mapper.getSynonymsForCommand('jump');
* console.log(synonyms); // ['leap', 'hop', 'bound', ...]
* ```
*/
public getSynonymsForCommand(commandName: string): string[] {
return this.library.getSynonymsForCommand(commandName);
}
/**
* Gets a detailed mapping of all commands and their synonyms.
* Useful for debugging or displaying to developers.
*
* @returns {Map<string, string[]>} Map of command names to their synonym arrays
*
* @example
* ```ts
* const mapping = mapper.getAllSynonymMappings();
* for (const [command, synonyms] of mapping.entries()) {
* console.log(`${command}: ${synonyms.join(', ')}`);
* }
* // Output:
* // jump: leap, hop, spring, bound
* // run: sprint, jog, dash
* ```
*/
public getAllSynonymMappings(): Map<string, string[]> {
const mappings = new Map<string, string[]>();
const commands = this.library.list();
for (const command of commands) {
const synonyms = this.library.getSynonymsForCommand(command.name);
if (synonyms.length > 0) {
mappings.set(command.name, synonyms);
}
}
return mappings;
}
/**
* 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 and synonyms from the CommandLibrary.
*
* @returns {void}
*/
public clearAllCommands(): void {
this.library.clear();
console.log('All commands and synonyms cleared');
}
/**
* Gets the total number of synonym mappings registered.
*
* @returns {number} Number of synonyms
*/
public getSynonymCount(): number {
return this.library.getSynonymCount();
}
}
|