-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
884 lines (770 loc) · 37.9 KB
/
script.js
File metadata and controls
884 lines (770 loc) · 37.9 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
document.addEventListener('DOMContentLoaded', () => {
// Challenge Database
const CHALLENGES = [
{
id: 'string-reversal',
title: 'String Reversal',
difficulty: 'easy',
category: 'Strings',
xp: 300,
description: `
<p>Write a function that takes a string as input and returns the string reversed.</p>
<h4>Constraints:</h4>
<ul>
<li>Length of string ≤ 10<sup>5</sup></li>
<li>String contains printable ASCII characters.</li>
</ul>
<h4>Example 1:</h4>
<div class="code-block">Input: s = "hello" Output: "olleh"</div>
<h4>Example 2:</h4>
<div class="code-block">Input: s = "CodeChallengePro" Output: "orPegnellahCedoC"</div>
`,
templates: {
javascript: `function reverseString(s) {\n // Write your JavaScript code here\n \n}`,
python: `def reverse_string(s: str) -> str:\n # Write your Python code here\n pass`,
cpp: `#include <string>\n\nclass Solution {\npublic:\n std::string reverseString(std::string s) {\n // Write your C++ code here\n \n }\n};`
},
testCases: [
{ input: '"hello"', expected: '"olleh"' },
{ input: '"CodeChallengePro"', expected: '"orPegnellahCedoC"' }
]
},
{
id: 'two-sum',
title: 'Two Sum',
difficulty: 'easy',
category: 'Algorithms',
xp: 400,
description: `
<p>Given an array of integers <code>nums</code> and an integer <code>target</code>, return indices of the two numbers such that they add up to <code>target</code>.</p>
<p>You may assume that each input would have exactly one solution, and you may not use the same element twice.</p>
<h4>Constraints:</h4>
<ul>
<li>2 ≤ nums.length ≤ 10<sup>4</sup></li>
<li>-10<sup>9</sup> ≤ nums[i] ≤ 10<sup>9</sup></li>
<li>-10<sup>9</sup> ≤ target ≤ 10<sup>9</sup></li>
</ul>
<h4>Example 1:</h4>
<div class="code-block">Input: nums = [2,7,11,15], target = 9 Output: [0,1]</div>
<h4>Example 2:</h4>
<div class="code-block">Input: nums = [3,2,4], target = 6 Output: [1,2]</div>
`,
templates: {
javascript: `function twoSum(nums, target) {\n // Write your JavaScript code here\n \n}`,
python: `def two_sum(nums: List[int], target: int) -> List[int]:\n # Write your Python code here\n pass`,
cpp: `#include <vector>\n\nclass Solution {\npublic:\n std::vector<int> twoSum(std::vector<int>& nums, int target) {\n // Write your C++ code here\n \n }\n};`
},
testCases: [
{ input: 'nums = [2,7,11,15], target = 9', expected: '[0,1]' },
{ input: 'nums = [3,2,4], target = 6', expected: '[1,2]' }
]
},
{
id: 'array-manipulation',
title: 'Array Manipulation',
difficulty: 'medium',
category: 'Arrays',
xp: 600,
description: `
<p>Starting with a 1-indexed array of zeros of size <code>n</code> and a list of operations, for each operation add a value to each of the array elements between two given indices, inclusive. Once all operations have been performed, return the maximum value in the array.</p>
<h4>Constraints:</h4>
<ul>
<li>3 ≤ n ≤ 10<sup>7</sup></li>
<li>1 ≤ operations.length ≤ 2 * 10<sup>5</sup></li>
</ul>
<h4>Example 1:</h4>
<div class="code-block">Input: n = 5, queries = [[1,2,100],[2,5,100],[3,4,100]] Output: 200</div>
`,
templates: {
javascript: `function arrayManipulation(n, queries) {\n // Write your JavaScript code here\n \n}`,
python: `def array_manipulation(n: int, queries: List[List[int]]) -> int:\n # Write your Python code here\n pass`,
cpp: `#include <vector>\n\nclass Solution {\npublic:\n long arrayManipulation(int n, std::vector<std::vector<int>>& queries) {\n // Write your C++ code here\n \n }\n};`
},
testCases: [
{ input: 'n = 5, queries = [[1,2,100],[2,5,100],[3,4,100]]', expected: '200' }
]
},
{
id: 'data-structures',
title: 'Merge K Sorted Lists',
difficulty: 'hard',
category: 'Trees/Graphs',
xp: 800,
description: `
<p>You are given an array of <code>k</code> linked-lists <code>lists</code>, each linked-list is sorted in ascending order.</p>
<p>Merge all the linked-lists into one sorted linked-list and return it.</p>
<h4>Constraints:</h4>
<ul>
<li>k == lists.length</li>
<li>0 ≤ k ≤ 10<sup>4</sup></li>
<li>0 ≤ lists[i].length ≤ 500</li>
</ul>
<h4>Example 1:</h4>
<div class="code-block">Input: lists = [[1,4,5],[1,3,4],[2,6]] Output: [1,1,2,3,4,4,5,6]</div>
`,
templates: {
javascript: `/*\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\nfunction mergeKLists(lists) {\n // Write your JavaScript code here\n \n}`,
python: `# Definition for singly-linked list.\n# class ListNode:\n# def __init__(self, val=0, next=None):\n# self.val = val\n# self.next = next\ndef merge_k_lists(lists: List[Optional[ListNode]]) -> Optional[ListNode]:\n # Write your Python code here\n pass`,
cpp: `/**\n * Definition for singly-linked list.\n * struct ListNode {\n * int val;\n * ListNode *next;\n * ListNode() : val(0), next(nullptr) {}\n * ListNode(int x) : val(x), next(nullptr) {}\n * };\n */\nclass Solution {\npublic:\n ListNode* mergeKLists(std::vector<ListNode*>& lists) {\n // Write your C++ code here\n \n }\n};`
},
testCases: [
{ input: 'lists = [[1,4,5],[1,3,4],[2,6]]', expected: '[1,1,2,3,4,4,5,6]' }
]
}
];
// Local Storage / State Management
let state = {
username: 'Guest Coder',
isLoggedIn: false,
xp: 320,
solvedChallenges: [],
streak: 1,
lastActive: new Date().toISOString().split('T')[0]
};
// Load State from LocalStorage if exists
if (localStorage.getItem('codechallenge_state')) {
try {
state = JSON.parse(localStorage.getItem('codechallenge_state'));
} catch (e) {
console.error('Failed to parse user state', e);
}
}
// Save State Function
function saveState() {
localStorage.setItem('codechallenge_state', JSON.stringify(state));
updateUIFromState();
}
// Leaderboard Mock Database
const LEADERBOARD_MOCK = [
{ name: 'AlphaCoder', xp: 3850, isMock: true },
{ name: 'ByteKnight', xp: 3120, isMock: true },
{ name: 'DevDynamo', xp: 2800, isMock: true },
{ name: 'GODFREY T R', xp: 2450, isMock: true },
{ name: 'StackMaster', xp: 1950, isMock: true }
];
// Forum Posts Initial Data
let forumPosts = [
{
id: 1,
title: 'Tips for solving Array Manipulation efficiently!',
author: 'AlphaCoder',
body: 'When working on the Array Manipulation challenge, avoid using a nested loop for updates. Instead, use a difference array approach (Prefix Sums). Add value to index start and subtract from index end+1. This turns a O(N*M) runtime complexity into O(N+M)!',
votes: 42,
commentsCount: 8,
upvoted: false,
timestamp: '3 hours ago'
},
{
id: 2,
title: 'How is the recursion depth limit handled in Python templates?',
author: 'DevDynamo',
body: 'If you are implementing trees or DFS algorithms on large inputs, Python will throw a RecursionError. Remember you can set `sys.setrecursionlimit(20000)` or write your search query iteratively with an explicit stack stack structure!',
votes: 18,
commentsCount: 3,
upvoted: false,
timestamp: '5 hours ago'
}
];
// Current Playground State
let activeChallenge = CHALLENGES[0];
let activeLanguage = 'javascript';
let challengeDrafts = {}; // Keeps user code edits during session
// Core DOM Elements
const navItems = document.querySelectorAll('.nav-item');
const sections = document.querySelectorAll('.app-section');
const sidebarUsername = document.getElementById('sidebar-username');
const sidebarAvatar = document.getElementById('sidebar-avatar');
const sidebarLevel = document.getElementById('sidebar-level');
const sidebarXpValue = document.getElementById('sidebar-xp-value');
const sidebarXpFill = document.getElementById('sidebar-xp-fill');
const userProfileSidebar = document.getElementById('user-profile-sidebar');
// Stats elements
const statRank = document.getElementById('stat-rank');
const statXp = document.getElementById('stat-xp');
const statSolved = document.getElementById('stat-solved');
const statStreak = document.getElementById('stat-streak');
// Dashboard actions
const btnStartRecommended = document.getElementById('btn-start-recommended');
const recommendDifficulty = document.getElementById('recommend-difficulty');
const recommendTitle = document.getElementById('recommend-title');
const recommendDesc = document.getElementById('recommend-desc');
// UI Updates based on state
function updateUIFromState() {
// Sidebar updates
sidebarUsername.textContent = state.username;
sidebarAvatar.textContent = state.username.charAt(0).toUpperCase();
// XP and Level Calculation
// Level increases every 1000 XP
const level = Math.floor(state.xp / 1000) + 1;
const currentLevelXp = state.xp % 1000;
sidebarLevel.textContent = `Level ${level} ${getLevelTitle(level)}`;
sidebarXpValue.textContent = `${currentLevelXp}/1000`;
sidebarXpFill.style.width = `${(currentLevelXp / 1000) * 100}%`;
// Stats boxes updates
statXp.textContent = `${state.xp} XP`;
statSolved.textContent = `${state.solvedChallenges.length} / ${CHALLENGES.length}`;
statStreak.textContent = `${state.streak} Day${state.streak !== 1 ? 's' : ''}`;
// Profile widget layout changes
const profileName = document.getElementById('profile-card-name');
const profileLevel = document.getElementById('profile-card-level');
const profileXp = document.getElementById('profile-card-xp');
const profileCompleted = document.getElementById('profile-card-completed');
const profileStreak = document.getElementById('profile-card-streak');
const profileAvatar = document.getElementById('profile-card-avatar');
if (profileName) {
profileName.textContent = state.username;
profileLevel.textContent = `Level ${level} ${getLevelTitle(level)}`;
profileXp.textContent = `${state.xp} XP`;
profileCompleted.textContent = `${state.solvedChallenges.length} / ${CHALLENGES.length}`;
profileStreak.textContent = `${state.streak} Day${state.streak !== 1 ? 's' : ''}`;
profileAvatar.textContent = state.username.charAt(0).toUpperCase();
}
// Leaderboard rendering
renderLeaderboard();
// Update recommended card
updateRecommendedChallenge();
// Nav authentication element update
const authNavText = document.getElementById('nav-auth-text');
const authNavItem = document.getElementById('nav-auth-item');
if (authNavText) {
authNavText.textContent = state.isLoggedIn ? 'Profile' : 'Sign In';
}
}
function getLevelTitle(level) {
if (level === 1) return 'Novice';
if (level === 2) return 'Scripter';
if (level === 3) return 'Craftsman';
if (level === 4) return 'Master';
return 'Grandmaster';
}
function updateRecommendedChallenge() {
// Pick first unsolved challenge
const unsolved = CHALLENGES.find(c => !state.solvedChallenges.includes(c.id));
if (unsolved) {
recommendDifficulty.textContent = unsolved.difficulty;
recommendDifficulty.className = `difficulty-badge ${unsolved.difficulty}`;
recommendTitle.textContent = unsolved.title;
recommendDesc.innerHTML = unsolved.description.split('</p>')[0] + '</p>';
recommendDesc.querySelector('p').style.margin = '0';
btnStartRecommended.style.display = 'inline-flex';
} else {
recommendDifficulty.textContent = 'Expert';
recommendDifficulty.className = 'difficulty-badge hard';
recommendTitle.textContent = 'All Completed!';
recommendDesc.textContent = 'Congratulations! You have completed all active challenges on the platform.';
btnStartRecommended.style.display = 'none';
}
}
// Leaderboard rendering (inserts user dynamically sorted)
function renderLeaderboard() {
const listContainer = document.getElementById('leaderboard-list');
if (!listContainer) return;
// Combine mocks and user
let leaderboard = [...LEADERBOARD_MOCK];
const userInList = leaderboard.find(item => item.name.toLowerCase() === state.username.toLowerCase());
if (userInList) {
// Update score
userInList.xp = state.xp;
userInList.isMock = false;
} else {
// Add user
leaderboard.push({ name: state.username, xp: state.xp, isMock: false });
}
// Sort desc
leaderboard.sort((a, b) => b.xp - a.xp);
// Find user rank and update stat rank
const userRankIndex = leaderboard.findIndex(item => item.name === state.username) + 1;
statRank.textContent = `#${userRankIndex}`;
listContainer.innerHTML = '';
leaderboard.forEach((user, index) => {
const rank = index + 1;
let rankClass = 'rank-other';
if (rank === 1) rankClass = 'rank-1';
else if (rank === 2) rankClass = 'rank-2';
else if (rank === 3) rankClass = 'rank-3';
const itemDiv = document.createElement('div');
itemDiv.className = `leaderboard-item ${!user.isMock ? 'highlight' : ''}`;
itemDiv.innerHTML = `
<div class="rank-badge ${rankClass}">${rank}</div>
<div class="leaderboard-name">${user.name} ${!user.isMock ? '(You)' : ''}</div>
<div class="leaderboard-xp">${user.xp} XP</div>
`;
listContainer.appendChild(itemDiv);
});
}
// Activity Heatmap Generation
function generateHeatmap() {
const heatmapGrid = document.getElementById('activity-heatmap');
if (!heatmapGrid) return;
heatmapGrid.innerHTML = '';
// Generate 120 cells
for (let i = 0; i < 120; i++) {
const cell = document.createElement('div');
cell.className = 'heatmap-cell';
// Assign some random mock level values to simulate history
let level = 0;
const r = Math.random();
if (r > 0.85) level = 1;
if (r > 0.92) level = 2;
if (r > 0.96) level = 3;
if (r > 0.99) level = 4;
if (level > 0) {
cell.classList.add(`level-${level}`);
}
// Set tooltip details
const date = new Date();
date.setDate(date.getDate() - (120 - i));
cell.title = `${date.toDateString()}: ${level > 0 ? level + ' challenge(s) solved' : 'No activity'}`;
heatmapGrid.appendChild(cell);
}
}
// SPA Tab Navigation Action
function switchTab(targetId) {
sections.forEach(sec => {
sec.classList.remove('active');
if (sec.id === targetId) sec.classList.add('active');
});
navItems.forEach(item => {
item.classList.remove('active');
if (item.getAttribute('data-target') === targetId) {
item.classList.add('active');
}
});
// Specific panel controls on tab shift
const authCardLogin = document.getElementById('auth-card-login');
const authCardProfile = document.getElementById('auth-card-profile');
if (targetId === 'auth') {
if (state.isLoggedIn) {
authCardLogin.style.display = 'none';
authCardProfile.style.display = 'flex';
} else {
authCardLogin.style.display = 'flex';
authCardProfile.style.display = 'none';
}
}
}
// Attach click listeners to sidebar links
navItems.forEach(item => {
item.addEventListener('click', () => {
const target = item.getAttribute('data-target');
switchTab(target);
});
});
// Start recommended challenge click
if (btnStartRecommended) {
btnStartRecommended.addEventListener('click', () => {
const unsolved = CHALLENGES.find(c => !state.solvedChallenges.includes(c.id));
if (unsolved) {
openChallengeInWorkspace(unsolved);
} else {
openChallengeInWorkspace(CHALLENGES[0]);
}
});
}
// Challenges Search and Grid Rendering
const searchInput = document.getElementById('challenge-search');
const filterTags = document.querySelectorAll('.filter-tag');
let activeFilter = 'all';
let searchQuery = '';
function renderChallengesGrid() {
const grid = document.getElementById('challenges-grid-container');
if (!grid) return;
grid.innerHTML = '';
const filtered = CHALLENGES.filter(c => {
const matchesFilter = activeFilter === 'all' || c.difficulty === activeFilter;
const matchesSearch = c.title.toLowerCase().includes(searchQuery.toLowerCase()) ||
c.category.toLowerCase().includes(searchQuery.toLowerCase());
return matchesFilter && matchesSearch;
});
if (filtered.length === 0) {
grid.innerHTML = `<div style="grid-column: 1/-1; text-align: center; color: var(--text-muted); padding: 40px;">No challenges found matching filters.</div>`;
return;
}
filtered.forEach(c => {
const isSolved = state.solvedChallenges.includes(c.id);
const card = document.createElement('div');
card.className = 'challenge-card glass-panel';
card.innerHTML = `
<div>
<div class="challenge-header">
<span class="difficulty-badge ${c.difficulty}">${c.difficulty}</span>
${isSolved ? `
<span style="display: flex; align-items: center; gap: 4px; font-size: 0.8rem; color: var(--accent-emerald); font-weight: 600;">
<svg viewBox="0 0 24 24" width="16" height="16" stroke="currentColor" stroke-width="2.5" fill="none" style="margin-right: 2px;"><polyline points="20 6 9 17 4 12"/></svg>
Solved
</span>` : ''}
</div>
<h3 class="challenge-card-title" style="margin-top: 12px;">${c.title}</h3>
<p class="challenge-desc">${c.description.replace(/<[^>]*>/g, '').substring(0, 120)}...</p>
</div>
<div style="display: flex; justify-content: space-between; align-items: center; border-top: 1px solid var(--border-color); padding-top: 16px;">
<div class="challenge-meta">
<span>${c.category}</span>
<span>•</span>
<span style="color: var(--accent-blue); font-weight: 500;">+${c.xp} XP</span>
</div>
<button class="btn btn-secondary start-challenge-btn" style="padding: 6px 14px; font-size: 0.8rem;" data-id="${c.id}">
${isSolved ? 'Review' : 'Solve'}
</button>
</div>
`;
// Start challenge event trigger
card.querySelector('.start-challenge-btn').addEventListener('click', () => {
openChallengeInWorkspace(c);
});
grid.appendChild(card);
});
}
// Search and filters listeners
if (searchInput) {
searchInput.addEventListener('input', (e) => {
searchQuery = e.target.value;
renderChallengesGrid();
});
}
filterTags.forEach(tag => {
tag.addEventListener('click', () => {
filterTags.forEach(t => t.classList.remove('active'));
tag.classList.add('active');
activeFilter = tag.getAttribute('data-filter');
renderChallengesGrid();
});
});
// Workspace Controller
const workspaceTitle = document.getElementById('workspace-title');
const workspaceDifficulty = document.getElementById('workspace-difficulty');
const workspaceBodyContent = document.getElementById('workspace-body-content');
const workspaceFilename = document.getElementById('workspace-filename');
const editorLanguageSelect = document.getElementById('editor-language');
const codeTextarea = document.getElementById('code-textarea');
const lineNumbersDiv = document.getElementById('line-numbers');
const terminalOutput = document.getElementById('terminal-output');
const btnRunCode = document.getElementById('btn-run-code');
const btnSubmitCode = document.getElementById('btn-submit-code');
const workspaceBackBtn = document.getElementById('workspace-back-btn');
function openChallengeInWorkspace(challenge) {
activeChallenge = challenge;
switchTab('workspace');
// Render instruction info
workspaceTitle.textContent = challenge.title;
workspaceDifficulty.textContent = challenge.difficulty;
workspaceDifficulty.className = `difficulty-badge ${challenge.difficulty}`;
workspaceBodyContent.innerHTML = challenge.description;
// Reset workspace editor draft / template logic
const savedLanguage = activeLanguage;
editorLanguageSelect.value = savedLanguage;
loadCodeTemplate(challenge, savedLanguage);
clearTerminal();
}
function loadCodeTemplate(challenge, language) {
const fileExtensions = { javascript: 'js', python: 'py', cpp: 'cpp' };
workspaceFilename.textContent = `solution.${fileExtensions[language]}`;
// Fetch draft if edited in session, else use challenge template default
const draftKey = `${challenge.id}_${language}`;
if (challengeDrafts[draftKey]) {
codeTextarea.value = challengeDrafts[draftKey];
} else {
codeTextarea.value = challenge.templates[language] || '';
}
syncLineNumbers();
}
function syncLineNumbers() {
if (!codeTextarea || !lineNumbersDiv) return;
const linesCount = codeTextarea.value.split('\n').length;
let lineNumbersHTML = '';
for (let i = 1; i <= linesCount; i++) {
lineNumbersHTML += `${i}<br>`;
}
lineNumbersDiv.innerHTML = lineNumbersHTML;
}
// Line numbers syncing event listener
if (codeTextarea) {
codeTextarea.addEventListener('input', () => {
const draftKey = `${activeChallenge.id}_${activeLanguage}`;
challengeDrafts[draftKey] = codeTextarea.value;
syncLineNumbers();
});
codeTextarea.addEventListener('scroll', () => {
lineNumbersDiv.scrollTop = codeTextarea.scrollTop;
});
// Intercept Tab inside textarea
codeTextarea.addEventListener('keydown', (e) => {
if (e.key === 'Tab') {
e.preventDefault();
const start = codeTextarea.selectionStart;
const end = codeTextarea.selectionEnd;
codeTextarea.value = codeTextarea.value.substring(0, start) + ' ' + codeTextarea.value.substring(end);
codeTextarea.selectionStart = codeTextarea.selectionEnd = start + 4;
const draftKey = `${activeChallenge.id}_${activeLanguage}`;
challengeDrafts[draftKey] = codeTextarea.value;
syncLineNumbers();
}
});
}
// Language dropdown selection change listener
if (editorLanguageSelect) {
editorLanguageSelect.addEventListener('change', (e) => {
activeLanguage = e.target.value;
loadCodeTemplate(activeChallenge, activeLanguage);
});
}
// Back to challenges button
if (workspaceBackBtn) {
workspaceBackBtn.addEventListener('click', () => {
switchTab('challenges');
});
}
// Terminal Logging Functions
function clearTerminal() {
if (!terminalOutput) return;
terminalOutput.innerHTML = '';
}
function printTerminalLine(text, type = 'stdout') {
if (!terminalOutput) return;
const line = document.createElement('div');
line.className = `terminal-line terminal-${type}`;
const timestamp = new Date().toLocaleTimeString();
line.textContent = `[${timestamp}] ${text}`;
terminalOutput.appendChild(line);
terminalOutput.scrollTop = terminalOutput.scrollHeight;
}
// Toast Alerts
function showToast(message, type = 'info') {
const toastContainer = document.getElementById('toast-container');
if (!toastContainer) return;
const toast = document.createElement('div');
toast.className = `toast ${type}`;
// Icon matching toast type
let iconMarkup = '';
if (type === 'success') {
iconMarkup = `<svg viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2.5" fill="none"><polyline points="20 6 9 17 4 12"/></svg>`;
} else if (type === 'error') {
iconMarkup = `<svg viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2.5" fill="none"><circle cx="12" cy="12" r="10"/><line x1="12" y1="8" x2="12" y2="12"/><line x1="12" y1="16" x2="12.01" y2="16"/></svg>`;
} else {
iconMarkup = `<svg viewBox="0 0 24 24" width="18" height="18" stroke="currentColor" stroke-width="2.5" fill="none"><circle cx="12" cy="12" r="10"/><line x1="12" y1="16" x2="12" y2="12"/><line x1="12" y1="8" x2="12.01" y2="8"/></svg>`;
}
toast.innerHTML = `
${iconMarkup}
<span>${message}</span>
`;
toastContainer.appendChild(toast);
// Slide out and destroy after 3.5 seconds
setTimeout(() => {
toast.style.animation = 'fadeIn 0.3s cubic-bezier(0.4, 0, 0.2, 1) reverse';
setTimeout(() => {
toast.remove();
}, 300);
}, 3200);
}
// Code execution simulation click handlers
if (btnRunCode) {
btnRunCode.addEventListener('click', () => {
const userCode = codeTextarea.value.trim();
const template = activeChallenge.templates[activeLanguage].trim();
// Check if unmodified
if (userCode === template || userCode === '') {
clearTerminal();
printTerminalLine('Compilation Error: Function body is empty or unaltered.', 'error');
printTerminalLine('Please write some custom logic inside the editor to proceed.', 'error');
return;
}
clearTerminal();
printTerminalLine(`Compiling solution.cpp/py/js...`, 'info');
setTimeout(() => {
printTerminalLine(`Running compiler verification...`, 'info');
setTimeout(() => {
printTerminalLine(`Executing ${activeChallenge.testCases.length} Test Cases...`, 'info');
setTimeout(() => {
// Print mock execution details
activeChallenge.testCases.forEach((tc, index) => {
printTerminalLine(`Test Case ${index + 1}: Input: ${tc.input}`, 'stdout');
printTerminalLine(`Test Case ${index + 1}: Output: ${tc.expected}`, 'stdout');
printTerminalLine(`Test Case ${index + 1}: Success`, 'success');
});
printTerminalLine(`Status: All local test cases passed!`, 'success');
showToast('Local test cases passed!', 'info');
}, 600);
}, 500);
}, 400);
});
}
if (btnSubmitCode) {
btnSubmitCode.addEventListener('click', () => {
const userCode = codeTextarea.value.trim();
const template = activeChallenge.templates[activeLanguage].trim();
// Check if unmodified
if (userCode === template || userCode === '') {
clearTerminal();
printTerminalLine('Compilation Error: Code submission rejected.', 'error');
printTerminalLine('You must implement the challenge requirements before submitting.', 'error');
return;
}
clearTerminal();
printTerminalLine(`Submitting code to sandbox server...`, 'info');
setTimeout(() => {
printTerminalLine(`Running rigorous system tests...`, 'info');
setTimeout(() => {
printTerminalLine(`Result: 12/12 Test Cases Passed!`, 'success');
printTerminalLine(`Complexity Analysis:`, 'info');
printTerminalLine(` Runtime: 42 ms (beats 88.5% of submissions)`, 'success');
printTerminalLine(` Memory: 38.4 MB (beats 92.1% of submissions)`, 'success');
printTerminalLine(`Submission Successful!`, 'success');
// If already solved, don't re-grant XP
const alreadySolved = state.solvedChallenges.includes(activeChallenge.id);
if (!alreadySolved) {
state.solvedChallenges.push(activeChallenge.id);
state.xp += activeChallenge.xp;
// Increment heatmap day score
generateHeatmap();
showToast(`Correct Answer! +${activeChallenge.xp} XP Earned.`, 'success');
} else {
showToast(`Correct Answer! Solution reviewed successfully.`, 'success');
}
// Save state updates
saveState();
renderChallengesGrid();
}, 800);
}, 500);
});
}
// Community / Forum Logic
const newPostTitle = document.getElementById('new-post-title');
const newPostBody = document.getElementById('new-post-body');
const btnCreatePost = document.getElementById('btn-create-post');
const forumContainer = document.getElementById('forum-posts-container');
function renderForumPosts() {
if (!forumContainer) return;
forumContainer.innerHTML = '';
forumPosts.forEach(post => {
const postCard = document.createElement('div');
postCard.className = 'forum-post-card glass-panel';
postCard.innerHTML = `
<div class="vote-widget">
<button class="vote-btn ${post.upvoted ? 'active' : ''}" data-id="${post.id}">
<svg viewBox="0 0 24 24"><polyline points="18 15 12 9 6 15"/></svg>
</button>
<span class="vote-count">${post.votes}</span>
</div>
<div class="post-main">
<div class="post-header">
<span class="post-author">${post.author}</span>
<span>•</span>
<span>${post.timestamp}</span>
</div>
<h3 class="post-title">${post.title}</h3>
<p class="post-body">${post.body}</p>
<div class="post-footer">
<a class="post-comment-btn">
<svg viewBox="0 0 24 24"><path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/></svg>
<span>${post.commentsCount} Comment${post.commentsCount !== 1 ? 's' : ''}</span>
</a>
</div>
</div>
`;
// Upvote hook
postCard.querySelector('.vote-btn').addEventListener('click', (e) => {
const btn = e.currentTarget;
const postId = parseInt(btn.getAttribute('data-id'));
const matched = forumPosts.find(p => p.id === postId);
if (matched) {
if (matched.upvoted) {
matched.votes--;
matched.upvoted = false;
} else {
matched.votes++;
matched.upvoted = true;
}
renderForumPosts();
}
});
forumContainer.appendChild(postCard);
});
}
if (btnCreatePost) {
btnCreatePost.addEventListener('click', () => {
const title = newPostTitle.value.trim();
const body = newPostBody.value.trim();
if (!title || !body) {
showToast('Please enter both a title and details for your post.', 'error');
return;
}
// Push post to stack
const newPost = {
id: Date.now(),
title: title,
author: state.username,
body: body,
votes: 1,
commentsCount: 0,
upvoted: true,
timestamp: 'Just now'
};
forumPosts.unshift(newPost);
// Reset input values
newPostTitle.value = '';
newPostBody.value = '';
renderForumPosts();
showToast('Post published successfully!', 'success');
});
}
// Support Form Submission
const supportForm = document.getElementById('support-form');
const btnSendSupport = document.getElementById('btn-send-support');
if (supportForm) {
supportForm.addEventListener('submit', (e) => {
e.preventDefault();
const name = document.getElementById('support-name').value.trim();
const email = document.getElementById('support-email').value.trim();
const msg = document.getElementById('support-msg').value.trim();
if (!name || !email || !msg) return;
showToast('Sending message...', 'info');
btnSendSupport.disabled = true;
btnSendSupport.textContent = 'Sending...';
setTimeout(() => {
showToast('Message sent! Support agent GODFREY T R will respond soon.', 'success');
supportForm.reset();
btnSendSupport.disabled = false;
btnSendSupport.textContent = 'Send Message';
}, 1200);
});
}
// Authentication (Login/Logout) Logic
const loginForm = document.getElementById('login-form');
const btnLogout = document.getElementById('btn-logout');
if (loginForm) {
loginForm.addEventListener('submit', (e) => {
e.preventDefault();
const usernameInput = document.getElementById('username').value.trim();
const passwordInput = document.getElementById('password').value.trim();
if (!usernameInput || !passwordInput) return;
// Simple mock authentication simulation
showToast('Authenticating...', 'info');
setTimeout(() => {
state.username = usernameInput;
state.isLoggedIn = true;
// Grant streak or solve defaults if new login, or just keep progress
saveState();
switchTab('dashboard');
showToast(`Welcome back, ${state.username}!`, 'success');
loginForm.reset();
}, 800);
});
}
if (btnLogout) {
btnLogout.addEventListener('click', () => {
state.username = 'Guest Coder';
state.isLoggedIn = false;
state.xp = 320;
state.solvedChallenges = [];
state.streak = 1;
saveState();
switchTab('dashboard');
showToast('You have signed out.', 'info');
});
}
// App Initialization
updateUIFromState();
generateHeatmap();
renderChallengesGrid();
renderForumPosts();
});