Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xiety committed Mar 18, 2024
1 parent 2f32cf6 commit fd991a1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
15 changes: 9 additions & 6 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@
<button @click="redo" :disabled='!canRedo'>Redo</button>
{{ undoStack.length }} / {{ redoStack.length + undoStack.length }}
<div>
<input id="mode-interval" type="radio" value="interval" v-model="mode" />
<input id="mode-interval" type="radio" :value="nameof<Card>('interval')" v-model="mode" />
<label for="mode-interval">Interval</label>
</div>
<div>
<input id="mode-cumulativeInterval" type="radio" value="cumulativeInterval" v-model="mode" />
<input id="mode-cumulativeInterval" type="radio" :value="nameof<Card>('cumulativeInterval')" v-model="mode" />
<label for="mode-cumulativeInterval">Cumulative</label>
</div>
<div>
<input id="mode-realDifficulty" type="radio" value="realDifficulty" v-model="mode" />
<label for="mode-realDifficulty">Difficulty</label>
<input id="mode-displayDifficulty" type="radio" :value="nameof<Card>('displayDifficulty')" v-model="mode" />
<label for="mode-displayDifficulty">Difficulty</label>
</div>
<div>
<input id="animation" type="checkbox" v-model="animation" />
<label for="animation">Animation</label>
</div>
</div>
<div style="font-size: 75%; width: 100%;">
<Slider v-for="(slider, index) in additionalSliders" :info="slider" v-model="fsrs_params.m[index]" v-on:change="commit" />
<Slider v-for="(slider, index) in additionalSliders" :info="slider" v-model="fsrs_params.m[index]"
v-on:change="commit" />
<Slider v-for="(slider, index) in sliders" :info="slider" v-model="fsrs_params.w[index]" v-on:change="commit" />
</div>
<table class="table-dataset">
Expand Down Expand Up @@ -126,6 +127,8 @@ import Slider from './Slider.vue';
ChartJS.register(Title, Tooltip, Legend, PointElement, LineElement, CategoryScale, LinearScale, Colors, zoomPlugin, ChartDataLabels);
const nameof = <T>(name: keyof T) => name;
const mode = ref<keyof Card>("interval");
const animation = ref(true);
Expand All @@ -141,7 +144,7 @@ const options = createOptions();
function getDataLabel(card: Card) {
const names = ["", "Again", "Hard", "Good", "Easy"];
return `${names[card.score]} ${card.realDifficulty.toFixed(0)}%`;
return `${names[card.score]} ${card.displayDifficulty.toFixed(0)}%`;
}
function convertCardToMyData(card: Card): MyData {
Expand Down
61 changes: 32 additions & 29 deletions src/fsrsCalculator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ export class FsrsCalculator {
this.factor = m[2];
}

calcI(r: number, s: number): number {
calcInterval(r: number, s: number): number {
return (s / this.factor) * (Math.pow(r, 1.0 / this.decay) - 1.0);
}

calcS0(g: number): number {
calcStabilityStart(g: number): number {
return this.w[g - 1];
}

calcD0(g: number): number {
calcDifficultyStart(g: number): number {
let d = this.w[4] - this.w[5] * (g - 3.0);
return this.clamp(d, 1, 10);
}

calcDN(d: number, g: number): number {
calcDifficultyNormal(d: number, g: number): number {
let dn = d - this.w[6] * (g - 3.0);
let dn2 = this.w[7] * this.calcD0(3) + (1.0 - this.w[7]) * dn;
let dn2 = this.w[7] * this.calcDifficultyStart(3) + (1.0 - this.w[7]) * dn;
return this.clamp(dn2, 1, 10);
}

calcRevExp(w: number, r: number): number {
return Math.exp(w * (1.0 - r));
}

calcSN(d: number, s: number, r: number, g: number): number {
calcStabilityNormal(d: number, s: number, r: number, g: number): number {
let p = 1.0;
if (g == 2)
p = this.w[15];
Expand All @@ -44,11 +44,11 @@ export class FsrsCalculator {
return s * (1.0 + sinc);
}

calcSF(d: number, s: number, r: number): number {
calcStabilityFailed(d: number, s: number, r: number): number {
return this.w[11] * Math.pow(d, -this.w[12]) * (Math.pow(s + 1.0, this.w[13]) - 1.0) * this.calcRevExp(this.w[14], r);
}

calcRD(d: number) {
calcDisplayDifficulty(d: number) {
return (d - 1.0) / 9.0 * 100.0;
}

Expand All @@ -60,27 +60,30 @@ export class FsrsCalculator {
if (g < 1 || g > 4)
return card;

const difficulty = this.calcNextDifficulty(card, g);
const stability = this.calcNextStability(card, g);
const displayDifficulty = this.calcDisplayDifficulty(difficulty);
const interval = this.calcInterval(this.desiredR, stability);
const cumulativeInterval = card.cumulativeInterval + interval;

return new Card(false, difficulty, displayDifficulty, stability, interval, cumulativeInterval, g);
}

private calcNextDifficulty(card: Card, g: number): number {
if (card.new) {
return this.calcDifficultyStart(g);
} else {
return this.calcDifficultyNormal(card.difficulty, g);
}
}

private calcNextStability(card: Card, g: number): number {
if (card.new) {
const d = this.calcD0(g);
const rd = this.calcRD(d);
const s = this.calcS0(g);
const i = this.calcI(this.desiredR, s);
const ci = card.cumulativeInterval + i;
return new Card(false, d, rd, s, i, ci, g);
return this.calcStabilityStart(g);
} else if (g == 1) {
const d = this.calcDN(card.difficulty, g);
const rd = this.calcRD(d);
const s = this.calcSF(card.difficulty, card.stability, this.desiredR);
const i = this.calcI(this.desiredR, s);
const ci = card.cumulativeInterval + i;
return new Card(false, d, rd, s, i, ci, g);
return this.calcStabilityFailed(card.difficulty, card.stability, this.desiredR);
} else {
const d = this.calcDN(card.difficulty, g);
const rd = this.calcRD(d);
const s = this.calcSN(card.difficulty, card.stability, this.desiredR, g);
const i = this.calcI(this.desiredR, s);
const ci = card.cumulativeInterval + i;
return new Card(false, d, rd, s, i, ci, g);
return this.calcStabilityNormal(card.difficulty, card.stability, this.desiredR, g);
}
}

Expand All @@ -100,16 +103,16 @@ export class FsrsCalculator {
export class Card {
new: boolean;
difficulty: number;
realDifficulty: number;
displayDifficulty: number;
stability: number;
interval: number;
cumulativeInterval: number;
score: number;

public constructor(n: boolean, difficulty: number, realDifficulty: number, stability: number, interval: number, cumulativeInterval: number, score: number) {
public constructor(n: boolean, difficulty: number, displayDifficulty: number, stability: number, interval: number, cumulativeInterval: number, score: number) {
this.new = n;
this.difficulty = difficulty;
this.realDifficulty = realDifficulty;
this.displayDifficulty = displayDifficulty;
this.stability = stability;
this.interval = interval;
this.cumulativeInterval = cumulativeInterval;
Expand Down

0 comments on commit fd991a1

Please sign in to comment.