From 7e73e2e7f027300f34fef9bbf60b1a0acbc2f389 Mon Sep 17 00:00:00 2001 From: saertna <83655354+saertna@users.noreply.github.com> Date: Fri, 1 Dec 2023 22:10:51 +0100 Subject: [PATCH] Check if badge was given already --- .../obsidian-gamified-pkm.zip | Bin 388512 -> 390144 bytes src/constants.ts | 2 +- src/main.ts | 118 ++++++++++++------ 3 files changed, 80 insertions(+), 40 deletions(-) diff --git a/obsidian-gamified-pkm/obsidian-gamified-pkm.zip b/obsidian-gamified-pkm/obsidian-gamified-pkm.zip index 9ae09f4aa3b42f501a109307007ae6ca989a0825..5a9bdc14d684603c8eca1da49335877985ae5031 100644 GIT binary patch delta 695 zcmbu7Ur19?9LIOB`!1U~jaIvv(Fvi9AhVuSB3Mx1qO2?<46}9#X$`q;khZKn45T2J zExvCND7Nk1vNdaO67|qa^oJhv-JmWFdM)T|MqTM8=)gIg^Zoq}zu)J}@BYWY^-lq< zCsgD1yDIBYA=!8G=Js2r4VP&rwUnl$G&QBwq%`+a4O52an10UCV?2BbLH;ms)FIrU zWuUI3w# zbpqq;?^J6!KY=h0rm>RtL)3GB8ZACO8Wwv*G#-g`d4=Fu7ELRJi0Cn)Q@M?GEM&2U zFS6Li+DTksPYzA&$e@9pMFe;zgF2on%ID`vwD4sPyOyGR@8AFj-eVQ779qGWht(X- zV&z)mbJi?EXJH{J*IG_TyjZuVm27k)m|JZ5Cb!>2E$ou+7AA2jL&2;m7cE zwCwg(kxC;LlrA+?C?ytQz_)z5`27pEijbVRDbM;Nj}(`yvg@mJxO6;AzO%W{Mu(dF N$}tW@+trT delta 404 zcmY+A-z!4_6vy}Des4p5#q84L#sjtowkJaJqx@LLY9T3B)>5-+?mT#ql@$;2B1cNm zLUxT}cOy@pqzDgENb)j5sr>`)?8&L~?OW%Z@2St+yKeJI$Msp%OrauGyOF@a;o`-o zB>{yc5#WNHGRUcdoI1!c5{HnF&V5)(Nh@tkp_A4pP*&mgx~fJ!UcuSs^?L-qi8s~N z^22_!sol(%w>rjLPM&(I)g{bmbweiI8lg=mGz#cXyGH@`KNp|&quUhEJw~sRu1}z& zp(qR*w^QJqo-s2yq9~$;J><}Q6hE{UMhh*SBAedB;K(W=gR(^A(bc$8Lp35;nzb_Q z>LA0=tb}~J*^=*NN+~SLaw(#W?j#sVyN_{t*q2|7#L$s)Hrk27K=ud< z==BU5YMucbzdr&RRNImG^EZ6R`Hzl " + badge.description + " - *" + window.moment().format('D.M.YY') + "*\n" + const newLines = [...lines.slice(0, start), badgeString, ...lines.slice(end)]; + const newLines2 = [...newLines.slice(0, start2), ...newLines.slice(end2)] + await this.app.vault.modify(file, newLines2.join("\n")); + //if(debugLogs) console.debug(`badgeString: ${badgeString}`) + } } - if (reference != null && reference2 != null){ - end = reference + 1; - start = reference + 1; + } - end2 = reference2 + 2; - start2 = reference2 + 1; + async giveSecretBadgeInProfile(avatarPageName: string, badge: Badge){ + const badgeDict = parseBadgeCSV2Dict(this.getSettingString('receivedBadges')); + if (!badgeDict[badge.name]) { + await this.writeBadgeCSV(badge, window.moment().format('YYYY-MM-DD'), 'level ' + (this.getSettingNumber('statusLevel')).toString()) + const existingFile = this.app.vault.getAbstractFileByPath(`${avatarPageName}.md`); + if (existingFile == null) { + if (debugLogs) console.debug(`File ${avatarPageName}.md does not exist`); + return; + } + const file = existingFile as TFile; - const badgeString = "**" + badge.name + "** " + badge.level + "\n> " + badge.description + " - *" + window.moment().format('D.M.YY') + "*\n" - const newLines = [...lines.slice(0, start), badgeString, ...lines.slice(end)]; - const newLines2 = [...newLines.slice(0, start2), ...newLines.slice(end2)] - await this.app.vault.modify(file, newLines2.join("\n")); - //if(debugLogs) console.debug(`badgeString: ${badgeString}`) + const content = await this.app.vault.read(file); + let reference: number | null = null; + let end: number | null = null; + let start: number | null = null; + + const lines = content.split("\n"); + for (let i = 0; i < lines.length; i++) { + const line = lines[i].trim(); + if (line === "#### achieved") { + if (reference === null) { + reference = i; + } + } + } + if (reference != null) { + end = reference + 1; + start = reference + 1; + + const badgeString = "**" + badge.name + "** " + "\n> " + badge.description + " - *" + window.moment().format('D.M.YY') + "*\n" + const newLines = [...lines.slice(0, start), badgeString, ...lines.slice(end)]; + await this.app.vault.modify(file, newLines.join("\n")); + //if(debugLogs) console.debug(`badgeString: ${badgeString}`) + } } }