Download components/test-solver.js from Maqsadov/testmaster-pro-bot-edition: direct link, hf CLI and curl.
- Browser
- Download file 8.21 kB
-
https://huggingface.co/spaces/Maqsadov/testmaster-pro-bot-edition/resolve/main/components/test-solver.js
- Command line
-
hf download hf://spaces/Maqsadov/testmaster-pro-bot-edition/components/test-solver.js
-
curl -L -o test-solver.js https://huggingface.co/spaces/Maqsadov/testmaster-pro-bot-edition/resolve/main/components/test-solver.js
8.21 kB
| class CustomTestSolver extends HTMLElement { | |
| connectedCallback() { | |
| this.attachShadow({ mode: 'open' }); | |
| this.shadowRoot.innerHTML = ` | |
| <style> | |
| :host { | |
| display: block; | |
| animation: fadeIn 0.4s ease-out; | |
| } | |
| .card { | |
| background: white; | |
| padding: 2.5rem; | |
| border-radius: 1.5rem; | |
| box-shadow: var(--shadow-md); | |
| border: 1px solid rgba(0,0,0,0.05); | |
| text-align: center; | |
| max-width: 32rem; | |
| margin: 0 auto; | |
| } | |
| .emoji { | |
| font-size: 4rem; | |
| margin-bottom: 1.5rem; | |
| display: inline-block; | |
| animation: bounceIn 0.6s ease-out; | |
| } | |
| .title { | |
| font-size: 1.75rem; | |
| font-weight: 800; | |
| margin-bottom: 0.75rem; | |
| color: var(--text-primary); | |
| } | |
| .subtitle { | |
| color: var(--text-secondary); | |
| font-size: 1rem; | |
| margin-bottom: 2rem; | |
| max-width: 24rem; | |
| margin-left: auto; | |
| margin-right: auto; | |
| } | |
| .code-input { | |
| width: 100%; | |
| padding: 1.25rem; | |
| background: #f8fafc; | |
| border-radius: 1.5rem; | |
| margin-bottom: 1rem; | |
| outline: none; | |
| border: 2px solid transparent; | |
| font-size: 2.5rem; | |
| font-weight: 900; | |
| text-align: center; | |
| text-transform: uppercase; | |
| } | |
| .code-input:focus { | |
| border-color: var(--primary); | |
| } | |
| .answers-input { | |
| width: 100%; | |
| padding: 1.25rem; | |
| background: #f8fafc; | |
| border-radius: 1.5rem; | |
| margin-bottom: 1.5rem; | |
| outline: none; | |
| border: 1px solid #e2e8f0; | |
| font-size: 1rem; | |
| font-weight: 700; | |
| text-align: center; | |
| height: 8rem; | |
| } | |
| .submit-btn { | |
| width: 100%; | |
| background: var(--secondary); | |
| color: white; | |
| padding: 1.25rem; | |
| border-radius: 1.5rem; | |
| font-weight: 900; | |
| box-shadow: 0 4px 6px -1px rgba(16, 185, 129, 0.2); | |
| cursor: pointer; | |
| transition: all 0.2s; | |
| text-transform: uppercase; | |
| letter-spacing: 0.1em; | |
| } | |
| .submit-btn:active { | |
| transform: scale(0.98); | |
| } | |
| </style> | |
| <div class="card"> | |
| <div class="emoji">π</div> | |
| <h2 class="title">Testni Boshlash</h2> | |
| <p class="subtitle">Test kodini kiriting va javoblarni yo'llang</p> | |
| <input id="solve-code" type="text" placeholder="KOD" class="code-input"> | |
| <textarea id="solve-answers" placeholder="Javoblar (masalan: abcd...)" class="answers-input"></textarea> | |
| <button id="submit-test" class="submit-btn">Natijani Tekshirish</button> | |
| </div> | |
| `; | |
| // Auto-uppercase for test code | |
| this.shadowRoot.getElementById('solve-code').addEventListener('input', (e) => { | |
| e.target.value = e.target.value.toUpperCase(); | |
| }); | |
| // Submit test functionality | |
| this.shadowRoot.getElementById('submit-test').addEventListener('click', async () => { | |
| const code = this.shadowRoot.getElementById('solve-code').value.trim().toUpperCase(); | |
| let rawAns = this.shadowRoot.getElementById('solve-answers').value.trim().toLowerCase().replace(/\s/g, ''); | |
| if (!code || !rawAns) { | |
| tg.showAlert("Kod va javoblarni kiriting!"); | |
| return; | |
| } | |
| // Show loading | |
| const submitBtn = this.shadowRoot.getElementById('submit-test'); | |
| const originalText = submitBtn.textContent; | |
| submitBtn.textContent = "TEKSHIRILMOQDA..."; | |
| submitBtn.disabled = true; | |
| try { | |
| const { data: test, error: testError } = await sb.from('tests') | |
| .select('*') | |
| .eq('test_code', code) | |
| .single(); | |
| if (testError || !test) { | |
| tg.showAlert("Bunday kodli test topilmadi!"); | |
| return; | |
| } | |
| // Validate answer format | |
| if (test.test_type === 'simple' && !/^[a-dA-D]+$/.test(rawAns)) { | |
| tg.showAlert("Javoblar faqat a,b,c,d harflaridan iborat bo'lishi kerak!"); | |
| return; | |
| } | |
| let correct = 0; | |
| let total = 0; | |
| if (test.test_type === 'simple') { | |
| total = test.answer_keys.length; | |
| for(let i=0; i<total; i++) { | |
| if(rawAns[i] === test.answer_keys[i]) correct++; | |
| } | |
| } else { | |
| const parts = test.answer_keys.split('|'); | |
| total = (parts[0]?.length || 0) + (parts[1]?.length || 0) + (parts[2]?.split(',').length || 0); | |
| for(let i=0; i<parts[0].length; i++) { | |
| if(rawAns[i] === parts[0][i]) correct++; | |
| } | |
| } | |
| const logit = correct === 0 ? -4.0 : | |
| (correct === total ? 4.0 : | |
| Math.log(correct / (total - correct))).toFixed(2); | |
| try { | |
| document.getElementById('student-view').classList.add('hidden'); | |
| document.getElementById('result-view').classList.remove('hidden'); | |
| const resultView = document.querySelector('custom-test-results'); | |
| if (resultView) { | |
| resultView.setResults(correct, total, logit, test.title); | |
| } | |
| } catch (e) { | |
| console.error("Error showing results:", e); | |
| tg.showAlert("Natijalarni ko'rsatishda xatolik yuz berdi"); | |
| } | |
| const msg = `<b>π NATIJA:</b> ${currentUser.full_name}\n<b>Test:</b> ${test.title}\n<b>Ball:</b> ${correct}/${total}\n<b>Rasch:</b> ${logit}`; | |
| // Send to teacher | |
| sendBotMsg(test.teacher_tg_id, | |
| `π <b>YANGI TEST TOPSHIRILDI!</b>\n\n` + | |
| msg + `\n\n<b>O'quvchi:</b> @${user.username || 'foydalanuvchi'}`); | |
| // Send to student | |
| sendBotMsg(user.id.toString(), | |
| `β <b>SIZNING NATIJANGIZ!</b>\n\n` + | |
| msg + `\n\n<a href="https://t.me/share/url?url=${encodeURIComponent('Men bu testdan ' + correct + ' ball to\'pladim!')}">Do'stlarga ulashish</a>`); | |
| // Save result to database | |
| await sb.from('results').insert([{ | |
| student_tg_id: user.id.toString(), | |
| teacher_tg_id: test.teacher_tg_id, | |
| test_id: test.id, | |
| score: correct, | |
| total: total, | |
| logit: logit, | |
| answers: rawAns | |
| }]); | |
| } catch (err) { | |
| tg.showAlert("Xatolik yuz berdi: " + err.message); | |
| } finally { | |
| submitBtn.textContent = originalText; | |
| submitBtn.disabled = false; | |
| } | |
| }); | |
| } | |
| } | |
| customElements.define('custom-test-solver', CustomTestSolver); |