forked from noxious/server
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
import fs from 'fs'
|
|
import sharp from 'sharp'
|
|
import { commandLogger } from '../utilities/logger'
|
|
import { Server } from 'socket.io'
|
|
import { getPublicPath } from '../utilities/utilities'
|
|
|
|
export default class TilesCommand {
|
|
constructor(private readonly io: Server) {}
|
|
|
|
public async execute(): Promise<void> {
|
|
// Get all tiles
|
|
const tilesDir = getPublicPath('tiles')
|
|
const tiles = fs.readdirSync(tilesDir).filter((file) => file.endsWith('.png'))
|
|
|
|
// Create output directory if it doesn't exist
|
|
if (!fs.existsSync(tilesDir)) {
|
|
fs.mkdirSync(tilesDir, { recursive: true })
|
|
}
|
|
|
|
for (const tile of tiles) {
|
|
// Check if tile is already 66x34
|
|
const metadata = await sharp(getPublicPath('tiles', tile)).metadata()
|
|
if (metadata.width === 66 && metadata.height === 34) {
|
|
commandLogger.info(`Tile ${tile} already processed`)
|
|
continue
|
|
}
|
|
|
|
const inputPath = getPublicPath('tiles', tile)
|
|
const outputPath = getPublicPath('tiles', tile)
|
|
|
|
try {
|
|
await sharp(inputPath)
|
|
.resize({
|
|
width: 66,
|
|
height: 34,
|
|
fit: 'fill',
|
|
kernel: 'nearest'
|
|
})
|
|
.toFile(outputPath)
|
|
|
|
commandLogger.info(`Processed: ${tile}`)
|
|
} catch (error) {
|
|
console.error(`Error processing ${tile}:`, error)
|
|
}
|
|
}
|
|
|
|
commandLogger.info('Tile processing completed.')
|
|
}
|
|
}
|