aboutsummaryrefslogtreecommitdiff
path: root/modules/ext/tutorial.js
blob: af4f4fab55ecbc496af58d60fa7b855fcee6b11d (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
class TutorialModule {
    constructor() {
        this.commands = {};
        this.terminal = null;
        this.STORAGE_KEY = 'webx11_tutorial_state';
        this.COMPLETE_KEY = 'webx11_tutorial_complete';
        this.SKIP_KEY = 'webx11_tutorial_skipped';
        this._maxTries = 3;
        this._initState();
    }

    _initState() {
        // Restore saved state or start fresh
        const saved = localStorage.getItem(this.STORAGE_KEY);
        if (saved) {
            try {
                const s = JSON.parse(saved);
                this._completed = new Set(s.completed || []);
                this._activeIndex = s.activeIndex || 0;
                this._tries = s.tries || {};
                this._started = s.started || false;
                return;
            } catch(e) { /* corrupt, reset */ }
        }
        this._completed = new Set();
        this._activeIndex = 0;
        this._tries = {};
        this._started = false;
    }

    _saveState() {
        try {
            localStorage.setItem(this.STORAGE_KEY, JSON.stringify({
                completed: Array.from(this._completed),
                activeIndex: this._activeIndex,
                tries: this._tries,
                started: this._started
            }));
        } catch(e) { /* quota */ }
    }

    _getQuests() {
        return [
            { id: 1, title: 'Your First Command', desc: 'Type `help` to see every command available in newOS4.',
              check: (cmd) => cmd === 'help', hint: 'Just type: help' },
            { id: 2, title: 'Explore the Filesystem', desc: 'Type `ls` to list files in the current directory.',
              check: (cmd) => cmd === 'ls', hint: 'Type: ls' },
            { id: 3, title: 'Create Something', desc: 'Use `touch hello.txt` to create your first file.',
              check: (cmd) => cmd === 'touch',
              hint: 'Type: touch hello.txt' },
            { id: 4, title: 'Write to a File', desc: 'Open the editor with `edit hello.txt`, type something, and click Save.',
              check: (cmd, term) => { const c = term && term.fs && term.fs.readFile('hello.txt'); return c && c.length > 0; },
              hint: 'Type: edit hello.txt, then write something and click 💾 Save & Test' },
            { id: 5, title: 'Talk to the AI', desc: 'Type `ai "hello"` to greet the AI assistant.',
              check: (cmd) => cmd === 'ai' || cmd === 'ask',
              hint: 'Type: ai "hello"' },
            { id: 6, title: 'Check Your Progress', desc: 'Type `status` to see how the system is doing.',
              check: (cmd) => cmd === 'status',
              hint: 'Type: status' },
        ];
    }

    requirements() {
        return { met: true, missing: [] };
    }

    getHelpInfo() {
        return {
            'tutorial': 'tutorial [start|skip|status] — Interactive guided tour of newOS4 (6 quests)',
        };
    }

    registerCommands() {
        this.commands['tutorial'] = (args) => this._handleTutorial(args);
    }

    // ── Public API called by terminal.js ──

    /** Called after every command execution to check quest progress */
    onCommandExecuted(cmd, args) {
        if (!this._started || this.isComplete()) return;
        const quests = this._getQuests();
        const current = quests[this._activeIndex];
        if (!current || this._completed.has(current.id)) return;

        // Special handling: quest 4 (edit) — don't count as wrong, wait for save
        if (current.id === 4 && cmd === 'edit') {
            this.terminal.print('  ✅ Editor opened! Write something and click Save.', 'success');
            return; // Don't fail, don't complete — wait for onFileSaved
        }

        // Check if this command completes the current quest
        if (current.check(cmd, this.terminal)) {
            this._completeCurrentQuest();
        } else {
            // Wrong command — increment try count
            const count = (this._tries[current.id] || 0) + 1;
            this._tries[current.id] = count;
            this._saveState();
            if (count >= this._maxTries) {
                this.terminal.print(`💡 Hint: ${current.hint}`, 'warning');
                this._tries[current.id] = 0; // reset so hint repeats
            }
        }
    }

    /** Called by editor module after a file is saved via the modal */
    onFileSaved(path) {
        if (!this._started || this.isComplete()) return;
        const quests = this._getQuests();
        const current = quests[this._activeIndex];
        if (!current || current.id !== 4 || this._completed.has(current.id)) return;

        if (current.check('__file_saved__', this.terminal)) {
            this._completeCurrentQuest();
        }
    }

    _completeCurrentQuest() {
        const quests = this._getQuests();
        const current = quests[this._activeIndex];
        this._completed.add(current.id);
        const nextIdx = this._activeIndex + 1;
        const next = quests[nextIdx];
        if (next) {
            this._activeIndex = nextIdx;
            this._saveState();
            this._showQuestNext(next);
        } else {
            this._activeIndex = nextIdx;
            this._saveState();
            this._showCompletion();
        }
    }

    isComplete() {
        return localStorage.getItem(this.COMPLETE_KEY) !== null;
    }

    isSkipped() {
        return localStorage.getItem(this.SKIP_KEY) !== null;
    }

    isStarted() {
        return this._started;
    }

    /** Called by welcome modal to begin the tutorial */
    startTutorial() {
        // Close welcome modal if it's open
        const modal = document.getElementById('welcome-modal');
        if (modal) modal.style.display = 'none';
        
        this._started = true;
        this._completed = new Set();
        this._activeIndex = 0;
        this._tries = {};
        this._saveState();
        localStorage.removeItem(this.COMPLETE_KEY);
        localStorage.removeItem(this.SKIP_KEY);
        this.terminal.print('╔══════════════════════════════════════╗', 'success');
        this.terminal.print('║   🎮 newOS4 Tutorial Started!        ║', 'success');
        this.terminal.print('╚══════════════════════════════════════╝', 'success');
        this.terminal.print('Follow the quests below. Type the correct command to advance.', 'info');
        this._showQuestNext(this._getQuests()[0]);
    }

    skipTutorial() {
        localStorage.setItem(this.SKIP_KEY, '1');
        this._started = false;
        this._completed = new Set();
        this._activeIndex = 0;
        this._tries = {};
        localStorage.removeItem(this.STORAGE_KEY);
        this.terminal.print('Tutorial skipped. Type `tutorial start` to begin anytime, or `help` to see all commands.', 'info');
    }

    /** Show status of tutorial progress */
    showStatus() {
        const quests = this._getQuests();
        const total = quests.length;
        const done = this._completed.size;

        if (!this._started && !this.isComplete()) {
            this.terminal.print('📋 Tutorial has not been started. Type `tutorial start` to begin!', 'info');
            return;
        }
        if (this.isComplete()) {
            this.terminal.print('🎉 Tutorial complete! All ' + total + ' quests finished.', 'success');
            return;
        }

        this.terminal.print('📋 Tutorial Progress: ' + done + '/' + total + ' quests', 'info');
        quests.forEach((q, i) => {
            const status = this._completed.has(q.id) ? '✅' : (i === this._activeIndex ? '👉' : '⬜');
            this.terminal.print('  ' + status + ' Quest ' + q.id + ': ' + q.title);
        });
        if (this._activeIndex < total) {
            this.terminal.print('');
            this._showQuestNext(quests[this._activeIndex]);
        }
    }

    // ── Internal ──

    _handleTutorial(args) {
        if (!args || args.length === 0) {
            // No args — show status or start if first time
            if (!this._started && !this.isComplete()) {
                this.terminal.print('╔══════════════════════════════════════╗', 'info');
                this.terminal.print('║   🎮 newOS4 Interactive Tutorial     ║', 'info');
                this.terminal.print('╚══════════════════════════════════════╝', 'info');
                this.terminal.print('Learn newOS4 by completing 6 short quests.', 'info');
                this.terminal.print('');
                this.terminal.print('Commands:', 'info');
                this.terminal.print('  tutorial start   — Begin or restart the tutorial');
                this.terminal.print('  tutorial status  — Show your progress');
                this.terminal.print('  tutorial skip    — Dismiss the tutorial');
                return;
            }
            this.showStatus();
            return;
        }

        const sub = args[0].toLowerCase();
        if (sub === 'start') this.startTutorial();
        else if (sub === 'skip') this.skipTutorial();
        else if (sub === 'status') this.showStatus();
        else this.terminal.print('Usage: tutorial [start|skip|status]', 'warning');
    }

    _showQuestNext(quest) {
        this.terminal.print('── Quest ' + quest.id + '/' + this._getQuests().length + ': ' + quest.title + ' ──', 'info');
        this.terminal.print('  ' + quest.desc, 'text-muted');
        this.terminal.print('  💡 ' + quest.hint, 'warning');
    }

    _showCompletion() {
        // Guard: only fire once
        if (this._completionFired) return;
        this._completionFired = true;
        
        this.terminal.print('');
        this.terminal.print('🎉🎉🎉 CONGRATULATIONS! 🎉🎉🎉', 'success');
        this.terminal.print('═'.repeat(40), 'success');
        this.terminal.print('  All 6 quests complete!', 'success');
        this.terminal.print('  You now know the basics of newOS4.', 'success');
        this.terminal.print('═'.repeat(40), 'success');
        this.terminal.print('');
        this.terminal.print('What next?', 'info');
        this.terminal.print('  • Explore commands:  help', 'info');
        this.terminal.print('  • Full docs:         help.html', 'info');
        this.terminal.print('  • AI assistant:      ai \"your question\"', 'info');
        this.terminal.print('  • Schedule tasks:    cron-every 60 echo hello', 'info');
        this.terminal.print('  • Install packages:  wpm list', 'info');
        this.terminal.print('  • Manage files:      edit, find, grep, files', 'info');

        localStorage.setItem(this.COMPLETE_KEY, Date.now().toString());
        localStorage.removeItem(this.STORAGE_KEY);

        // Fire confetti
        this._fireConfetti();
    }

    _fireConfetti() {
        const emojis = ['🎉', '✨', '⭐', '🌟', '💫'];
        const container = document.body;
        if (!container) return;
        // Remove any previous confetti first
        document.querySelectorAll('.confetti-piece').forEach(el => el.remove());
        for (let i = 0; i < 15; i++) {
            const el = document.createElement('div');
            el.className = 'confetti-piece';
            el.textContent = emojis[i % emojis.length];
            el.style.left = (Math.random() * 100) + 'vw';
            el.style.animationDuration = (2 + Math.random() * 2) + 's';
            el.style.animationDelay = (Math.random() * 1.5) + 's';
            el.style.fontSize = (16 + Math.random() * 20) + 'px';
            container.appendChild(el);
            setTimeout(() => el.remove(), 5000);
        }
    }
}

if (typeof window.webx11 !== 'undefined' && window.webx11) {
    webx11.moduleManager.registerModule(new TutorialModule(), 'https://webx11.duckdns.org/spaces/newos4/modules/ext/tutorial.js');
}