Skip to content

Commit

Permalink
add random to schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
linanwx committed Jun 28, 2023
1 parent 4b0ca3c commit 963bb80
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 48 deletions.
1 change: 1 addition & 0 deletions Pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export abstract class Pattern {
this.pcard = card
this.TagID = id
}
Pronounce() {}
abstract SubmitOpt(opt: Operation): Promise<void>;
abstract Component(props:PatternProps): JSX.Element;
abstract insertPatternID(): void;
Expand Down
35 changes: 14 additions & 21 deletions arrangement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,33 +157,26 @@ export class Arrangement implements ArrangementBase {
return
}
async *PatternSequence(name: string) {
let patterns = null;

if (name == REVIEWTAG) {
for (let i = 0; i < this.needReviewPattern.length; i++) {
let p = this.needReviewPattern[i]
let cardp = await this.findLivePattern(p)
if (cardp) {
yield new PatternIter(cardp, i, this.needReviewPattern.length)
}
}
}
if (name == NEWTAG) {
for (let i = 0; i < this.newPattern.length; i++) {
let p = this.newPattern[i]
let cardp = await this.findLivePattern(p)
if (cardp) {
yield new PatternIter(cardp, i, this.newPattern.length)
}
}
patterns = this.needReviewPattern;
} else if (name == NEWTAG) {
patterns = this.newPattern;
} else if (name == LEARNTAG) {
patterns = this.needLearn;
}
if (name == LEARNTAG) {
for (let i = 0; i < this.needLearn.length; i++) {
let p = this.needLearn[i]

if (patterns) {
for (let i = 0; i < patterns.length; i++) {
let p = patterns[i]
let cardp = await this.findLivePattern(p)
if (cardp) {
yield new PatternIter(cardp, i, this.needLearn.length)
yield new PatternIter(cardp, i, patterns.length)
}
}
}
return true

return true;
}
}
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "aosr",
"name": "Aosr",
"version": "1.0.34",
"version": "1.0.35",
"minAppVersion": "0.12.0",
"description": "Another obsidian spaced repetition",
"author": "linanwx",
Expand Down
44 changes: 23 additions & 21 deletions patternLine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@ abstract class linePattern extends Pattern {
Component = (props: PatternProps): JSX.Element => {
return <LinePatternComponent reverse={this.reverse} front={this.front} back={this.back} path={this.card.note.path} patternProps={props}></LinePatternComponent>
}
Pronounce(): void {
// 如果是单词 则尝试调用有道发音
let ttstext = ""
if (this.reverse == false) {
ttstext = this.front
} else {
ttstext = this.back
}
if (/^[a-zA-Z\s-]+$/.test(ttstext)) {
setTimeout(() => {
this.playTTS(ttstext)
}, 100);
}
}
playTTS = async (text: string) => {
if (GlobalSettings.WordTTSURL.length > 0) {
let url = GlobalSettings.WordTTSURL.replace('%s', text)
const audio = new Audio(url)
await audio.play()
}
}
}

class singleLinePattern extends linePattern {
Expand All @@ -52,7 +73,7 @@ class singleLinePattern extends linePattern {
this.card.updateFile({
updateFunc: (content): string => {
let newContent = this.keyText + " " + this.TagID;
return content.replace(this.keyText, ()=>{return newContent});
return content.replace(this.keyText, () => { return newContent });
}
})
}
Expand All @@ -66,7 +87,7 @@ class multiLinePattern extends linePattern {
this.card.updateFile({
updateFunc: (content): string => {
let newContent = `${this.front}? ${this.TagID}\n${this.back}`
return content.replace(this.keyText, ()=>{return newContent})
return content.replace(this.keyText, () => { return newContent })
}
})
}
Expand All @@ -86,13 +107,6 @@ type singleLinePatternComponentState = {
}

class LinePatternComponent extends React.Component<singleLinePatternComponentProps, singleLinePatternComponentState> {
playTTS = async (text: string) => {
if (GlobalSettings.WordTTSURL.length > 0) {
let url = GlobalSettings.WordTTSURL.replace('%s', text)
const audio = new Audio(url)
await audio.play()
}
}
async componentDidMount() {
let markdownDivFront = this.state.markdownDivFront
markdownDivFront.empty()
Expand All @@ -109,18 +123,6 @@ class LinePatternComponent extends React.Component<singleLinePatternComponentPro
markdownDivFront: markdownDivFront,
markdownDivBack: markdownDivBack,
})
// 如果是单词 则尝试调用有道发音
let ttstext = ""
if (this.props.reverse == false) {
ttstext = this.props.front
} else {
ttstext = this.props.back
}
if (/^[a-zA-Z\s-]+$/.test(ttstext)) {
setTimeout(() => {
this.playTTS(ttstext)
}, 100);
}
}
constructor(props: singleLinePatternComponentProps) {
super(props)
Expand Down
25 changes: 20 additions & 5 deletions schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export function NewSchedule(id: string) {
return new defaultSchedule(id)
}

function sigmod(x:number):number {
function sigmod(x: number): number {
return (GlobalSettings.DefaultEase - 100) * 2 / (1 + Math.exp(-0.0125 * (x - 250))) + 100;
}

Expand Down Expand Up @@ -355,12 +355,24 @@ abstract class scheduler {
abstract calculate(): moment.Duration
}

function randomIncrease(value: number) {
// 生成一个0.95到1.05之间的随机数
var randomFactor = Math.random() * 0.1 + 0.95;

// 计算增加后的值
var increasedValue = value * randomFactor;

// 返回增加后的值,保留整数
return parseFloat(increasedValue.toFixed(0));
}

// 简单难度计算
class easeSchedule extends scheduler {
calculate(): moment.Duration {
let basesecond = this.schedule.Gap.asSeconds() * this.schedule.Ease / 100;
let addsecond = Number(GlobalSettings.EasyBonus) * 24 * 60 * 60
let newdiff = window.moment.duration(basesecond + addsecond, "seconds")
let newrand = randomIncrease(basesecond + addsecond)
let newdiff = window.moment.duration(newrand, "seconds")
return newdiff
}
}
Expand All @@ -369,7 +381,8 @@ class easeSchedule extends scheduler {
class fairSchedule extends scheduler {
calculate(): moment.Duration {
let basesecond = this.schedule.Gap.asSeconds() * this.schedule.Ease / 100;
let newdiff = window.moment.duration(basesecond, "seconds")
let newrand = randomIncrease(basesecond)
let newdiff = window.moment.duration(newrand, "seconds")
return newdiff
}
}
Expand All @@ -378,7 +391,8 @@ class fairSchedule extends scheduler {
class hardSchedule extends scheduler {
calculate(): moment.Duration {
let basesecond = this.schedule.Gap.asSeconds() * 100 / this.schedule.Ease;
let newdiff = window.moment.duration(basesecond, "seconds")
let newrand = randomIncrease(basesecond)
let newdiff = window.moment.duration(newrand, "seconds")
return newdiff
}
}
Expand All @@ -389,7 +403,8 @@ class unknowSchedule extends scheduler {
let basesecond = this.schedule.Gap.asSeconds() * 100 / this.schedule.Ease;
let addsecond = Number(GlobalSettings.HardBonus) * 24 * 60 * 60;
let diffsecond = basesecond - addsecond
let newdiff = window.moment.duration(diffsecond, "seconds")
let newrand = randomIncrease(diffsecond)
let newdiff = window.moment.duration(newrand, "seconds")
return newdiff
}
}
1 change: 1 addition & 0 deletions view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ class Reviewing extends React.Component<ReviewingProps, ReviewingState> {
fileName: replaceSlashWithArrow(removeMdExtension(result.value.pattern.card.note.path)),
showAns: false,
})
result.value.pattern.Pronounce()
}
openPatternFile = async (pattern: Pattern | undefined) => {
if (!pattern) {
Expand Down

0 comments on commit 963bb80

Please sign in to comment.