36 lines
1.6 KiB
TypeScript
36 lines
1.6 KiB
TypeScript
import { db } from '../db/index.js';
|
|
import { zombieMapBySlug } from './map-aliases.js';
|
|
|
|
export function validServerId(value: unknown): value is string {
|
|
if (typeof value !== 'string') return false;
|
|
const match = /^(t5|t6)_zombie_([a-z0-9]+)_([1-9]|1[0-9]|20)$/.exec(value);
|
|
return !!match && !!zombieMapBySlug(match[1], match[2]);
|
|
}
|
|
|
|
export function listServerKeys() {
|
|
return db().prepare(`SELECT k.server_id, k.updated_at,
|
|
EXISTS(SELECT 1 FROM servers s WHERE s.name = k.server_id) AS deployed
|
|
FROM server_keys k ORDER BY k.server_id`).all() as Array<{
|
|
server_id: string; updated_at: string; deployed: number;
|
|
}>;
|
|
}
|
|
|
|
export function getServerKey(serverId: string): string | undefined {
|
|
return (db().prepare('SELECT pluto_key FROM server_keys WHERE server_id = ?')
|
|
.get(serverId) as { pluto_key: string } | undefined)?.pluto_key;
|
|
}
|
|
|
|
export function saveServerKey(serverId: unknown, key: unknown): void {
|
|
if (!validServerId(serverId)) throw new Error('Choose a valid T5 or T6 zombie server ID with a lobby number from 1 to 20');
|
|
if (typeof key !== 'string' || !key.trim() || key.trim().length > 512 || /\s/.test(key.trim())) {
|
|
throw new Error('Enter a server key without whitespace (maximum 512 characters)');
|
|
}
|
|
db().prepare(`INSERT INTO server_keys (server_id, pluto_key) VALUES (?, ?)
|
|
ON CONFLICT(server_id) DO UPDATE SET pluto_key = excluded.pluto_key, updated_at = datetime('now')`)
|
|
.run(serverId, key.trim());
|
|
}
|
|
|
|
export function removeServerKey(serverId: string): boolean {
|
|
return db().prepare('DELETE FROM server_keys WHERE server_id = ?').run(serverId).changes > 0;
|
|
}
|