Skip to content

Commit

Permalink
Refactor for safe Promise usage
Browse files Browse the repository at this point in the history
  • Loading branch information
saertna committed Nov 24, 2023
1 parent 738e3af commit 40268cc
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 49 deletions.
61 changes: 38 additions & 23 deletions main.js

Large diffs are not rendered by default.

70 changes: 44 additions & 26 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ export default class gamification extends Plugin {
console.log(`Badge: ${badgeName}, Date: ${badgeInfo.date}, Level: ${badgeInfo.level}`);
}
}*/
this.openAvatarFile();
//this.openAvatarFile();
const badgeTemp = await this.whichLevelNextBadge(17)
new Notice(`${badgeTemp}`)
});
}

Expand Down Expand Up @@ -1184,37 +1186,53 @@ export default class gamification extends Plugin {
}


async whichLevelNextBadge(currentLevel: number): Promise<number>{
let nextBadgeLevel = 0
for (let i = currentLevel; i < 110; i++){
const badge : Badge = getBadgeForLevel(i, true)
// Regular expression to match the level number
const levelRegex = /level (\d+)/;
// Extract the level number using the regular expression
const match = badge.level.match(levelRegex);
if(match){
const levelNumber = parseInt(match[1], 10); // Convert the matched number to an integer
if (levelNumber > currentLevel && nextBadgeLevel == 0 ) {
nextBadgeLevel = levelNumber;
async whichLevelNextBadge(currentLevel: number): Promise<number | null> {
try {
let nextBadgeLevel = 0
for (let i = currentLevel; i < 110; i++){
const badge : Badge = getBadgeForLevel(i, true)
// Regular expression to match the level number
const levelRegex = /level (\d+)/;
// Extract the level number using the regular expression
const match = badge.level.match(levelRegex);
if(match){
const levelNumber = parseInt(match[1], 10); // Convert the matched number to an integer
if (levelNumber > currentLevel && nextBadgeLevel == 0 ) {
nextBadgeLevel = levelNumber;
}
}
}
return nextBadgeLevel
}
catch (e) {
console.log(e)
return null;
}
return nextBadgeLevel
}


async boosterForInit(): Promise<number> {
const nextBadgeAt = await this.whichLevelNextBadge(this.getSettingNumber('statusLevel'))
const statusPointsToReach = statusPointsForLevel(nextBadgeAt)
//console.log(`statusPointsToReach for next Badge: ${statusPointsToReach}`)
// 50 Notes from Level 1 to 5 to get the first badge.
// 300 Points in average for a Note.
const boosterFactor = Math.round((statusPointsToReach - this.getSettingNumber('statusPoints'))/50/300);
this.setSettingNumber('badgeBoosterFactor', boosterFactor)
this.setSettingBoolean('badgeBoosterState', true)
//await this.saveData(this.settings)
//console.log(`boosterFaktor: ${boosterFactor}`)
return boosterFactor
async boosterForInit(): Promise<number | null> {
try {
const nextBadgeAt = await this.whichLevelNextBadge(this.getSettingNumber('statusLevel'))
if (nextBadgeAt != null){
const statusPointsToReach = statusPointsForLevel(nextBadgeAt)
//console.log(`statusPointsToReach for next Badge: ${statusPointsToReach}`)
// 50 Notes from Level 1 to 5 to get the first badge.
// 300 Points in average for a Note.
const boosterFactor = Math.round((statusPointsToReach - this.getSettingNumber('statusPoints'))/50/300);
this.setSettingNumber('badgeBoosterFactor', boosterFactor)
this.setSettingBoolean('badgeBoosterState', true)
//await this.saveData(this.settings)
//console.log(`boosterFaktor: ${boosterFactor}`)
return boosterFactor
} else {
return 0
}
}
catch (e) {
console.log(e);
return null;
}
}


Expand Down

0 comments on commit 40268cc

Please sign in to comment.