generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
118 lines (101 loc) · 3.21 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { App, Notice, Plugin, PluginSettingTab, Setting } from 'obsidian';
interface RPGSettings {
level: number;
xp: number;
xpPerSecond: number;
xpPerFile: number;
currentSessionDuration: number;
displayStaringTimeAsHumanString: boolean;
}
const SETTINGS: RPGSettings = {
level: 1,
xp: 0,
xpPerSecond: 1,
xpPerFile: 50,
currentSessionDuration: 0,
displayStaringTimeAsHumanString: true
};
export default class RPG extends Plugin {
settings: RPGSettings;
statsStatusBar: HTMLElement;
xpStatusBar: HTMLElement;
levelStatusBar: HTMLElement;
fileCount: number = 0;
async onload() {
await this.loadSettings();
this.settings.currentSessionDuration = 0;
this.statsStatusBar = this.addStatusBarItem();
this.registerInterval(window.setInterval(() => {
this.settings.currentSessionDuration += 1000;
this.settings.xp += this.settings.xpPerSecond;
this.updateStats();
this.saveSettings();
}, 1000));
this.app.workspace.onLayoutReady(() => {
this.registerEvent(this.app.vault.on('create', () => {
new Notice(`You gained ${this.settings.xpPerFile} XP for creating a file!`);
this.updateStats();
}));
});
this.registerInterval(window.setInterval(() => {
this.saveSettings();
}, 600000));
this.addSettingTab(new RPGSettingsTab(this.app, this));
}
onunload() {
this.saveSettings();
}
async loadSettings() {
this.settings = Object.assign(SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
updateStats() {
this.fileCount = this.app.vault.getFiles().length;
const totalXP = this.settings.xp + (this.fileCount * this.settings.xpPerFile);
const currentLevel = Math.floor(Math.sqrt(Math.max(totalXP, 0) / 100)) + 1;
if (currentLevel > this.settings.level) {
this.settings.level = currentLevel;
new Notice(`You leveled up to level ${currentLevel}!`);
}
if (currentLevel < this.settings.level) {
this.settings.level = currentLevel;
new Notice(`You leveled down to level ${currentLevel}!`);
}
this.statsStatusBar.setText(`[${this.settings.level}] ${totalXP} XP gained`);
}
}
class RPGSettingsTab extends PluginSettingTab {
plugin: RPG;
constructor(app: App, plugin: RPG) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
let { containerEl } = this;
containerEl.empty();
new Setting(containerEl)
.setName('XP gained per second')
.setDesc('Set the amount of XP gained for each second spent in the vault.')
.addText((text) =>
text
.setValue(this.plugin.settings.xpPerSecond.toString())
.onChange((value) => {
this.plugin.settings.xpPerSecond = parseInt(value);
this.plugin.saveSettings();
})
);
new Setting(containerEl)
.setName('XP gained per file')
.setDesc('Set the amount of XP gained for each file in the vault.')
.addText((text) =>
text
.setValue(this.plugin.settings.xpPerFile.toString())
.onChange((value) => {
this.plugin.settings.xpPerFile = parseInt(value);
this.plugin.saveSettings();
})
);
}
}