Repository navigation
Expand file tree
/
Copy pathpractice_v2.html
More file actions
193 lines (179 loc) · 7.81 KB
/
Copy pathpractice_v2.html
File metadata and controls
193 lines (179 loc) · 7.81 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CodePilot - Practice Problem</title>
<link rel="stylesheet" href="css/style.css">
<style>
.split-layout {
display: grid;
grid-template-columns: 1fr 1fr;
height: calc(100vh - 80px); /* Minus nav */
}
.problem-desc {
padding: 2rem;
overflow-y: auto;
border-right: 1px solid var(--border-color);
}
.editor-container {
display: flex;
flex-direction: column;
background: #1e1e1e; /* Classic editor dark */
}
textarea.code-editor {
flex: 1;
background: transparent;
color: #d4d4d4;
border: none;
padding: 1.5rem;
font-family: 'Courier New', Courier, monospace;
font-size: 1.1rem;
resize: none;
outline: none;
}
.editor-footer {
padding: 1rem;
background: #252526;
display: flex;
justify-content: flex-end;
gap: 1rem;
border-top: 1px solid #333;
}
#resultMsg {
padding: 0.5rem 1rem;
border-radius: 4px;
font-weight: bold;
}
</style>
</head>
<body>
<nav>
<a href="index.html" class="logo">CodePilot</a>
<div class="nav-links">
<a href="problems_v2.html">Problems</a>
<a href="dashboard_v2.html">Dashboard</a>
</div>
</nav>
<div class="split-layout">
<div class="problem-desc" id="descPanel">
<h1 id="pTitle">Loading...</h1>
<div class="mt-2 mb-2">
<span id="pDiff" class="badge"></span>
<span id="pCat" class="text-secondary" style="margin-left:1rem;"></span>
</div>
<p id="pDesc" style="white-space: pre-wrap; margin-top: 2rem;" class="text-secondary"></p>
<div class="card mt-2">
<h4>Hints</h4>
<ul id="pHints" class="mt-2 text-secondary" style="margin-left: 1.5rem;"></ul>
</div>
</div>
<div class="editor-container">
<textarea id="codeEditor" class="code-editor" spellcheck="false" placeholder="Write your code here... /* Note: include 'fail' in your code to test Wrong Answer include 'error' to test Error handling. Otherwise it will pass. */ function solve() { // your logic }"></textarea>
<div class="editor-footer">
<div id="resultMsg" class="hidden"></div>
<button class="btn-secondary" onclick="runCode()">Run Code</button>
<button class="btn-primary" onclick="submitCode()">Submit</button>
</div>
</div>
</div>
<script src="js/app.js?v=3"></script>
<script src="js/problemsData.js?v=3"></script>
<script>
// Allow guest practice if offline or not logged in
if (!getToken()) {
localStorage.setItem('codepilot_token', 'guest_demo_token');
localStorage.setItem('codepilot_user', JSON.stringify({ id: 'guest', username: 'Guest Developer' }));
}
const urlParams = new URLSearchParams(window.location.search);
const problemId = urlParams.get('id');
const resultMsg = document.getElementById('resultMsg');
function renderProblemDetails(p) {
if (!p) return;
document.getElementById('pTitle').textContent = p.title;
const diffBadge = document.getElementById('pDiff');
diffBadge.textContent = p.difficulty;
diffBadge.className = `badge ${p.difficulty.toLowerCase()}`;
document.getElementById('pCat').textContent = p.category;
document.getElementById('pDesc').textContent = p.description;
if (Array.isArray(p.hints)) {
document.getElementById('pHints').innerHTML = p.hints.map(h => `<li>${h}</li>`).join('');
}
}
async function loadProblem() {
// Step 1: Instant load from PROBLEMS_DATA
if (typeof PROBLEMS_DATA !== 'undefined' && Array.isArray(PROBLEMS_DATA)) {
const localP = PROBLEMS_DATA.find(p => p._id === problemId || p.title.toLowerCase() === (problemId || '').toLowerCase()) || PROBLEMS_DATA[0];
if (localP) renderProblemDetails(localP);
}
// Step 2: Background revalidation
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 2500);
const res = await fetch(`${API_URL}/problems/${problemId}`, { signal: controller.signal });
clearTimeout(timeoutId);
if (res.ok) {
const p = await res.json();
renderProblemDetails(p);
}
} catch (e) {
console.log("Using static problem data.");
}
}
function runCode() {
resultMsg.textContent = 'Running test cases...';
resultMsg.style.background = 'rgba(255,255,255,0.1)';
resultMsg.style.color = 'white';
resultMsg.classList.remove('hidden');
setTimeout(() => {
resultMsg.textContent = 'Run successful! All sample cases passed. ✨';
resultMsg.style.background = 'rgba(16,185,129,0.2)';
resultMsg.style.color = 'var(--success)';
}, 800);
}
async function submitCode() {
const code = document.getElementById('codeEditor').value;
resultMsg.textContent = 'Submitting & verifying...';
resultMsg.style.background = 'rgba(255,255,255,0.1)';
resultMsg.style.color = 'white';
resultMsg.classList.remove('hidden');
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 2500);
const res = await fetch(`${API_URL}/submissions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${getToken()}`
},
body: JSON.stringify({ problemId, code }),
signal: controller.signal
});
clearTimeout(timeoutId);
const data = await res.json();
if (data.status === 'Accepted') {
resultMsg.textContent = 'Accepted! 🔥 Streak preserved.';
resultMsg.style.background = 'rgba(16,185,129,0.2)';
resultMsg.style.color = 'var(--success)';
} else if (data.status === 'Wrong Answer') {
resultMsg.textContent = 'Wrong Answer';
resultMsg.style.background = 'rgba(239,68,68,0.2)';
resultMsg.style.color = 'var(--danger)';
} else {
resultMsg.textContent = data.status || 'Verified';
resultMsg.style.background = 'rgba(245,158,11,0.2)';
resultMsg.style.color = 'var(--warning)';
}
} catch (e) {
// Offline demo verification
setTimeout(() => {
resultMsg.textContent = 'Accepted! 🚀 (Offline Mode: All hidden tests passed)';
resultMsg.style.background = 'rgba(16,185,129,0.2)';
resultMsg.style.color = 'var(--success)';
}, 800);
}
}
loadProblem();
</script>
</body>
</html>