Add rules and patterns for working with Challenge Platform. Introduce guidelines for Chakra UI usage, localStorage management, context updates, code formatting, and multi-step forms. Enhance user experience with a dynamic queue checking system and progress bar. Include a checklist for commits to ensure code quality and consistency.
This commit is contained in:
@@ -2,7 +2,7 @@ const fs = require('fs')
|
||||
const path = require('path')
|
||||
const router = require('express').Router()
|
||||
|
||||
const timer = (time = 300) => (req, res, next) => setTimeout(next, time)
|
||||
const timer = (time = 100) => (req, res, next) => setTimeout(next, time)
|
||||
|
||||
const dataDir = path.join(__dirname, 'data')
|
||||
|
||||
@@ -57,18 +57,97 @@ router.get('/challenge/task/:id', (req, res) => {
|
||||
|
||||
router.post('/challenge/submit', (req, res) => {
|
||||
const response = readJson('submit.json')
|
||||
const queueId = response.body?.queueId
|
||||
|
||||
if (queueId) {
|
||||
queueBehaviors[queueId] = queueBehaviors[queueId] ?? { nextFailure: false, attemptNumber: 0 }
|
||||
queueStates[queueId] = {
|
||||
position: 3,
|
||||
initialPosition: 3,
|
||||
pollCount: 0,
|
||||
startTime: Date.now(),
|
||||
}
|
||||
}
|
||||
|
||||
res.json(response)
|
||||
})
|
||||
|
||||
// Храним состояние очереди для каждого queueId
|
||||
const queueStates = {}
|
||||
const queueBehaviors = {}
|
||||
|
||||
router.get('/challenge/check-status/:queueId', (req, res) => {
|
||||
const data = readJson('queue-status.json')
|
||||
const statuses = data.body || data
|
||||
const status = statuses[req.params.queueId]
|
||||
|
||||
if (!status) {
|
||||
return sendNotFound(res, `Статус очереди ${req.params.queueId} не найден`)
|
||||
const queueId = req.params.queueId
|
||||
|
||||
// Инициализируем состояние очереди, если его нет
|
||||
if (!queueStates[queueId]) {
|
||||
queueStates[queueId] = {
|
||||
position: 3,
|
||||
initialPosition: 3,
|
||||
pollCount: 0,
|
||||
startTime: Date.now()
|
||||
}
|
||||
}
|
||||
|
||||
const state = queueStates[queueId]
|
||||
state.pollCount++
|
||||
|
||||
// Симулируем движение в очереди
|
||||
// Каждый запрос уменьшаем позицию (быстрее)
|
||||
if (state.pollCount >= 1 && state.position > 0) {
|
||||
state.position--
|
||||
state.pollCount = 0
|
||||
}
|
||||
|
||||
// Если позиция 0, переходим к проверке
|
||||
if (state.position === 0 && state.pollCount >= 1) {
|
||||
const behavior = queueBehaviors[queueId] ?? { nextFailure: false, attemptNumber: 0 }
|
||||
const attemptNumber = behavior.attemptNumber + 1
|
||||
behavior.attemptNumber = attemptNumber
|
||||
const shouldFail = behavior.nextFailure
|
||||
behavior.nextFailure = !shouldFail
|
||||
|
||||
const baseSubmission = {
|
||||
_id: `submission-${queueId}-${attemptNumber}`,
|
||||
id: `submission-${queueId}-${attemptNumber}`,
|
||||
user: 'user-frontend-001',
|
||||
task: 'task-html-intro',
|
||||
result: '<html><head></head><body><h1>Hello</h1></body></html>',
|
||||
queueId,
|
||||
submittedAt: new Date(state.startTime).toISOString(),
|
||||
checkedAt: new Date().toISOString(),
|
||||
attemptNumber,
|
||||
}
|
||||
|
||||
const submission = shouldFail
|
||||
? {
|
||||
...baseSubmission,
|
||||
status: 'needs_revision',
|
||||
feedback: 'Добавьте описание внутри <section> и поясните, зачем нужен заголовок.',
|
||||
}
|
||||
: {
|
||||
...baseSubmission,
|
||||
status: 'accepted',
|
||||
feedback: 'Отличная работа! Теперь можно двигаться дальше.',
|
||||
}
|
||||
|
||||
delete queueStates[queueId]
|
||||
|
||||
return res.json({
|
||||
success: true,
|
||||
body: {
|
||||
status: 'completed',
|
||||
position: 0,
|
||||
submission,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Возвращаем текущее состояние очереди с начальной позицией для расчёта прогресса
|
||||
const status = state.position > 0
|
||||
? { status: 'waiting', position: state.position, initialPosition: state.initialPosition }
|
||||
: { status: 'in_progress', position: 0, initialPosition: state.initialPosition }
|
||||
|
||||
return res.json({ success: true, body: status })
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user