import path from 'node:path';
import measure from './datas/measure.json';
import predict from './result.json';
import { writeFile, writeFileSync } from 'node:fs';

const ob: any[] = [];

let max = 0;
let min = Infinity;
predict.forEach((p) => {
    const songno = path.basename(p.filename, '.tja')
    const m = measure.find((m) => m.songno.toString() === songno && (
        p.difficulty_type === "oni" ? m.diff === "oni" :
        m.diff === "ura"
    ));

    if(m){
        if(max < p.predicted_measure){
            max = p.predicted_measure
        }
        if(min > p.predicted_measure){
            min = p.predicted_measure;
        }
        ob.push({
            songno,
            diff: m.diff,
            predicted: p.predicted_measure,
            measure: m.상수,
            error: p.predicted_measure - m.상수,
            errorAbs: Math.abs(p.predicted_measure - m.상수)
        })
    }
});

ob.forEach((v) => v.calibrated = ((v.predicted - min) * 11 / (max - min) + 1))

writeFileSync('./compare.json', JSON.stringify(ob, null, 2))