Files
dwroller/database/scripts/apply_normalize.js
Alex dde3abda55 feat: Add validation and normalization utilities for player records
- Implemented `validatePlayer` and `normalizeTabInfo` functions in `database/validate.js` for player data validation and normalization.
- Added error handling for required fields and enforced data types and constraints.
- Introduced standardization for characteristics and skills.

feat: Create script for extracting rules from PDF files

- Developed `extract-rules.js` to extract text from PDF rulebooks and categorize rules.
- Integrated `pdftotext` for PDF processing and created a searchable rules database.
- Implemented rule categorization and indexing for efficient searching.

feat: Implement RulesTab component for rule searching

- Created `RulesTab.jsx` for searching and displaying rules with filtering options.
- Added recent searches and quick reference buttons for user convenience.
- Integrated API calls for fetching rules and displaying results dynamically.

feat: Add test script for Rules API

- Created `test-rules.js` to test the functionality of the Rules API endpoints.
- Included tests for search and stats endpoints to ensure proper response handling.
2025-08-16 12:15:32 +02:00

42 lines
1.5 KiB
JavaScript

const { playerHelpers } = require('../sqlite-db');
const { validatePlayer } = require('../validate');
function backupDb() {
const fs = require('fs');
const path = require('path');
const ts = new Date().toISOString().replace(/[:.]/g,'');
const src = path.join(__dirname, '..', 'sqlite', 'deathwatch.db');
const dst = path.join(__dirname, '..', 'sqlite', `deathwatch.db.pre_normalize.${ts}.bak`);
if (fs.existsSync(src)) {
fs.copyFileSync(src, dst);
console.log('Backup created:', dst);
} else {
console.log('No sqlite DB found at', src);
}
}
function apply() {
backupDb();
const players = playerHelpers.getAll();
let applied = 0;
const details = [];
for (const p of players) {
const { valid, errors, normalized } = validatePlayer(p);
// Compare normalized to existing for keys we change (rollerInfo, shopInfo, tabInfo)
const changed = JSON.stringify({rollerInfo: p.rollerInfo, shopInfo: p.shopInfo, tabInfo: p.tabInfo}) !== JSON.stringify({rollerInfo: normalized.rollerInfo, shopInfo: normalized.shopInfo, tabInfo: normalized.tabInfo});
if (changed) {
try {
playerHelpers.update(p.name, normalized);
applied++;
details.push({ name: p.name, errors, appliedChanges: true });
} catch (err) {
details.push({ name: p.name, error: String(err) });
}
}
}
console.log(`Applied normalization to ${applied} players`);
if (details.length) console.log('Details:', JSON.stringify(details, null, 2));
}
apply();