class PreferenceRank {
constructor() {
this.state = {
items: [],
scores: {},
currentPair: [],
remainingPairs: [],
totalPairs: 0,
currentProgress: 0,
[Redacted Person]: false,
language: 'en',
history: []
};
this.config = {
INITIAL_RATING: 1000,
K_FACTOR: 32,
translations: { /* ... (tidak diubah) */ }
};
this.init();
}
$ = document.getElementById.bind(document);
init() {
["languageSelect", "allowTies", "startButton", "leftItem", "rightItem", "likeBothButton", "dislikeBothButton", "restartBattleButton", "restartNewItemsButton", "undoButton"].forEach(id => {
const el = this.$(id);
if (!el) return;
el.addEventListener(id.includes("Select") || id.includes("Ties") ? "change" : "click", () => {
const action = ({
languageSelect: "updateLanguage",
allowTies: "toggleTieOptions",
startButton: "startRank",
leftItem: "chooseLeft",
rightItem: "chooseRight",
likeBothButton: "chooseTie",
dislikeBothButton: "chooseTie",
restartBattleButton: "restartBattle",
restartNewItemsButton: "restartNewItems",
undoButton: "undo"
})[id];
this[action]();
});
});
this.updateLanguage();
}
updateLanguage() {
const lang = this.$("languageSelect").value;
this.state.language = lang;
const t = this.config.translations[lang];
for (const key in t) {
if (this.$(key)) this.$(key).textContent = t[key];
}
this.updateProgress();
if (this.$("results").style.display !== "none") this.showResults();
}
startRank() {
const items = this.$("itemInput").value.split("\n").map(i => i.trim()).filter(Boolean);
if (items.length < 2) return alert(this.config.translations[this.state.language].alert);
this.state = {
...this.state,
items,
allowTies: this.$("allowTies").checked,
currentProgress: 0,
scores: Object.fromEntries(items.map(i => [i, this.config.INITIAL_RATING])),
remainingPairs: this.shuffleArray(items.flatMap((v, i) => items.slice(i + 1).map(w => [v, w]))),
totalPairs: items.length * (items.length - 1) / 2,
history: [] // Reset history when starting a new rank
};
this.showSection("battleSection");
this.$("tieButtons").style.display = this.state.allowTies ? "flex" : "none";
this.nextBattle();
}
chooseLeft() { this.choose(1); }
chooseRight() { this.choose(0); }
chooseTie() { this.choose(0.5); }
choose(result) {
const [left, right] = this.state.currentPair;
this.state.history.push({ ...this.state }); // Simplify history saving
const expected = 1 / (1 + 10 ** ((this.state.scores[right] - this.state.scores[left]) / 400));
this.state.scores[left] += this.config.K_FACTOR * (result - expected);
this.state.scores[right] += this.config.K_FACTOR * ((1 - result) - (1 - expected));
this.state.currentProgress++;
this.updateProgress();
this.$("undoButton").disabled = false;
this.clearSelections();
// Optimized selection highlighting
let selectedButton = result === 1 ? this.$("leftItem") : result === 0 ? this.$("rightItem") : event.target;
selectedButton.classList.add("selected");
setTimeout(() => this.nextBattle(), 300);
}
showResults() {
const t = this.config.translations[this.state.language];
const sortedScores = Object.entries(this.state.scores).sort(([, a], [, b]) => b - a);
const resultsHTML = `<h2>${t.results}</h2><table>
<thead><tr><th>${t.rank}</th><th>${t.item}</th><th>${t.score}</th></tr></thead><tbody>
${sortedScores.map(([item, score], i) => `<tr><td>${i + 1}</td><td>${item}</td><td>${Math.round(score)}</td></tr>`).join('')}
</tbody></table>`;
this.$("results").innerHTML = resultsHTML;
this.showSection("results");
this.$("restartButtons").style.display = "flex";
}
shuffleArray = (arr) => arr.sort(() => Math.random() - 0.5);
nextBattle() {
if (!this.state.remainingPairs.length) return this.showResults();
this.state.currentPair = this.state.remainingPairs.pop();
this.$("leftItem").textContent = this.state.currentPair[0];
this.$("rightItem").textContent = this.state.currentPair[1];
this.updateProgress();
this.clearSelections();
}
updateProgress = () => {
const t = this.config.translations[this.state.language];
this.$("progress").textContent = `${t.progress}: ${this.state.currentProgress}/${this.state.totalPairs}`;
}
showSection = (id) => {
["inputSection", "battleSection", "results", "restartButtons"].forEach(sec => this.$(sec).style.display = sec === id ? "block" : "none");
}
clearSelections = () => ["leftItem", "rightItem", "likeBothButton", "dislikeBothButton"].forEach(id => this.$(id).classList.remove("selected"));
undo() {
if (!this.state.history.length) return;
this.state = this.state.history.pop(); // Restore previous state directly
this.$("leftItem").textContent = this.state.currentPair[0];
this.$("rightItem").textContent = this.state.currentPair[1];
this.updateProgress();
this.$("undoButton").disabled = !this.state.history.length;
}
// ... (restartBattle and restartNewItems functions remain unchanged)
}
const ranker = new PreferenceRank();
if (navigator.language?.startsWith("id")) {
ranker.$("languageSelect").value = "id";
ranker.updateLanguage();
}