38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
export const useVillageHelpers = () => {
|
|
/**
|
|
* Converts numeric coordinates to a chess-like format (e.g., 0,0 -> A7).
|
|
* The grid is 5 columns (A-E) and 7 rows (1-7).
|
|
* Rows are numbered from bottom to top, so y=6 is row '1'.
|
|
* @param x The column index (0-4).
|
|
* @param y The row index (0-6).
|
|
*/
|
|
const formatCoordinates = (x: number, y: number): string => {
|
|
const col = String.fromCharCode('A'.charCodeAt(0) + x);
|
|
const row = 7 - y;
|
|
return `${col}${row}`;
|
|
};
|
|
|
|
/**
|
|
* Finds and replaces all occurrences of numeric coordinates like (x, y)
|
|
* in a string with the chess-like format.
|
|
* @param message The message string.
|
|
*/
|
|
const formatMessageCoordinates = (message: string): string => {
|
|
if (!message) return '';
|
|
// Regex to find coordinates like (1, 2)
|
|
return message.replace(/\((\d+), (\d+)\)/g, (match, xStr, yStr) => {
|
|
const x = parseInt(xStr, 10);
|
|
const y = parseInt(yStr, 10);
|
|
if (!isNaN(x) && !isNaN(y)) {
|
|
return formatCoordinates(x, y);
|
|
}
|
|
return match; // Return original if parsing fails
|
|
});
|
|
};
|
|
|
|
return {
|
|
formatCoordinates,
|
|
formatMessageCoordinates,
|
|
};
|
|
};
|