69 lines
2.0 KiB
TypeScript
69 lines
2.0 KiB
TypeScript
import { readFile } from 'node:fs/promises';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { dirname, resolve } from 'node:path';
|
|
|
|
type UpdateItem = {
|
|
name: string;
|
|
amount: number;
|
|
displayName?: string | null;
|
|
};
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
|
|
const TARGET_URL = process.env.UPDATE_URL ?? 'https://localhost:3000/api/me-system/update';
|
|
const INTERVAL_MS = Number(process.env.INTERVAL_MS ?? 10_000);
|
|
const JSON_PATH = process.env.JSON_PATH ?? resolve(__dirname, 'me_system_update.json');
|
|
const MAX_VARIATION = Number(process.env.MAX_VARIATION ?? 0.05);
|
|
|
|
if (TARGET_URL.startsWith('https://')) {
|
|
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
|
|
}
|
|
|
|
function mutate(items: UpdateItem[]): UpdateItem[] {
|
|
return items.map((item) => {
|
|
const delta = (Math.random() * 2 - 1) * MAX_VARIATION;
|
|
const next = Math.max(0, Math.round(item.amount * (1 + delta)));
|
|
return { ...item, amount: next };
|
|
});
|
|
}
|
|
|
|
async function sendUpdate(items: UpdateItem[]): Promise<void> {
|
|
const startedAt = Date.now();
|
|
try {
|
|
const res = await fetch(TARGET_URL, {
|
|
method: 'POST',
|
|
headers: { 'content-type': 'application/json' },
|
|
body: JSON.stringify(items),
|
|
});
|
|
const took = Date.now() - startedAt;
|
|
const text = await res.text();
|
|
console.log(
|
|
`[${new Date().toISOString()}] POST ${res.status} (${took} ms, ${items.length} items) -> ${text}`,
|
|
);
|
|
} catch (err) {
|
|
console.error(`[${new Date().toISOString()}] request failed:`, err);
|
|
}
|
|
}
|
|
|
|
async function main(): Promise<void> {
|
|
const raw = await readFile(JSON_PATH, 'utf8');
|
|
let current: UpdateItem[] = JSON.parse(raw);
|
|
|
|
console.log(
|
|
`Loaded ${current.length} items from ${JSON_PATH}\n` +
|
|
`Posting to ${TARGET_URL} every ${INTERVAL_MS} ms (max ±${MAX_VARIATION * 100}% per tick).`,
|
|
);
|
|
|
|
await sendUpdate(current);
|
|
|
|
setInterval(() => {
|
|
current = mutate(current);
|
|
void sendUpdate(current);
|
|
}, INTERVAL_MS);
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error('Fatal:', err);
|
|
process.exit(1);
|
|
});
|