feat: add comprehensive game configuration system
Add a centralized game configuration system that allows customizable scoring mechanics and game rules. Users can now set default game configurations that persist across sessions, and individual quizzes can have their own configuration overrides. ## New Features ### Game Configuration Options - Shuffle Questions: Randomize question order when starting a game - Shuffle Answers: Randomize answer positions for each question - Host Participates: Toggle whether the host plays as a competitor or spectates (host now shows as 'Spectator' when not participating) - Streak Bonus: Multiplied points for consecutive correct answers, with configurable threshold and multiplier values - Comeback Bonus: Extra points for players ranked below top 3 - Wrong Answer Penalty: Deduct percentage of max points for incorrect answers (configurable percentage) - First Correct Bonus: Extra points for the first player to answer correctly on each question ### Default Settings Management - New Settings icon in landing page header (authenticated users only) - DefaultConfigModal for editing user-wide default game settings - Default configs are loaded when creating new quizzes - Defaults persist to database via new user API endpoints ### Reusable UI Components - GameConfigPanel: Comprehensive toggle-based settings panel with expandable sub-options, tooltips, and suggested values based on question count - DefaultConfigModal: Modal wrapper for editing default configurations ## Technical Changes ### Frontend - New useUserConfig hook for fetching/saving user default configurations - QuizEditor now uses GameConfigPanel instead of inline toggle checkboxes - GameScreen handles spectator mode with disabled answer buttons - Updated useGame hook with new scoring calculations and config state - Improved useAuthenticatedFetch with deduped silent refresh and redirect-once pattern to prevent multiple auth redirects ### Backend - Added game_config column to quizzes table (JSON storage) - Added default_game_config column to users table - New PATCH endpoint for quiz config updates: /api/quizzes/:id/config - New PUT endpoint for user defaults: /api/users/me/default-config - Auto-migration in connection.ts for existing databases ### Scoring System - New calculatePoints() function in constants.ts handles all scoring logic including streaks, comebacks, penalties, and first-correct bonus - New calculateBasePoints() for time-based point calculation - New getPlayerRank() helper for comeback bonus eligibility ### Tests - Added tests for DefaultConfigModal component - Added tests for GameConfigPanel component - Added tests for QuizEditor config integration - Added tests for useUserConfig hook - Updated API tests for new endpoints ## Type Changes - Added GameConfig interface with all configuration options - Added DEFAULT_GAME_CONFIG constant with sensible defaults - Quiz type now includes optional config property
This commit is contained in:
parent
90fba17a1e
commit
af21f2bcdc
23 changed files with 2925 additions and 133 deletions
|
|
@ -1257,6 +1257,271 @@ async function runTests() {
|
|||
await request('DELETE', `/api/quizzes/${quizId}`, undefined, 204);
|
||||
});
|
||||
|
||||
console.log('\nGame Config Tests:');
|
||||
let gameConfigQuizId: string | null = null;
|
||||
|
||||
await test('POST /api/quizzes with gameConfig saves config', async () => {
|
||||
const quizWithConfig = {
|
||||
title: 'Quiz With Game Config',
|
||||
source: 'manual',
|
||||
gameConfig: {
|
||||
shuffleQuestions: true,
|
||||
shuffleAnswers: true,
|
||||
hostParticipates: false,
|
||||
streakBonusEnabled: true,
|
||||
streakThreshold: 5,
|
||||
streakMultiplier: 1.5,
|
||||
comebackBonusEnabled: true,
|
||||
comebackBonusPoints: 100,
|
||||
penaltyForWrongAnswer: true,
|
||||
penaltyPercent: 30,
|
||||
firstCorrectBonusEnabled: true,
|
||||
firstCorrectBonusPoints: 75,
|
||||
},
|
||||
questions: [
|
||||
{
|
||||
text: 'Config test question?',
|
||||
timeLimit: 20,
|
||||
options: [
|
||||
{ text: 'A', isCorrect: true, shape: 'triangle', color: 'red' },
|
||||
{ text: 'B', isCorrect: false, shape: 'diamond', color: 'blue' },
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const { data } = await request('POST', '/api/quizzes', quizWithConfig, 201);
|
||||
gameConfigQuizId = (data as { id: string }).id;
|
||||
});
|
||||
|
||||
await test('GET /api/quizzes/:id returns gameConfig', async () => {
|
||||
if (!gameConfigQuizId) throw new Error('No game config quiz created');
|
||||
const { data } = await request('GET', `/api/quizzes/${gameConfigQuizId}`);
|
||||
const quiz = data as Record<string, unknown>;
|
||||
|
||||
if (!quiz.gameConfig) throw new Error('Missing gameConfig');
|
||||
const config = quiz.gameConfig as Record<string, unknown>;
|
||||
if (config.shuffleQuestions !== true) throw new Error('shuffleQuestions not preserved');
|
||||
if (config.shuffleAnswers !== true) throw new Error('shuffleAnswers not preserved');
|
||||
if (config.hostParticipates !== false) throw new Error('hostParticipates not preserved');
|
||||
if (config.streakBonusEnabled !== true) throw new Error('streakBonusEnabled not preserved');
|
||||
if (config.streakThreshold !== 5) throw new Error('streakThreshold not preserved');
|
||||
if (config.streakMultiplier !== 1.5) throw new Error('streakMultiplier not preserved');
|
||||
if (config.comebackBonusEnabled !== true) throw new Error('comebackBonusEnabled not preserved');
|
||||
if (config.comebackBonusPoints !== 100) throw new Error('comebackBonusPoints not preserved');
|
||||
if (config.penaltyForWrongAnswer !== true) throw new Error('penaltyForWrongAnswer not preserved');
|
||||
if (config.penaltyPercent !== 30) throw new Error('penaltyPercent not preserved');
|
||||
if (config.firstCorrectBonusEnabled !== true) throw new Error('firstCorrectBonusEnabled not preserved');
|
||||
if (config.firstCorrectBonusPoints !== 75) throw new Error('firstCorrectBonusPoints not preserved');
|
||||
});
|
||||
|
||||
await test('PUT /api/quizzes/:id updates gameConfig', async () => {
|
||||
if (!gameConfigQuizId) throw new Error('No game config quiz created');
|
||||
|
||||
const updatedQuiz = {
|
||||
title: 'Updated Config Quiz',
|
||||
gameConfig: {
|
||||
shuffleQuestions: false,
|
||||
shuffleAnswers: false,
|
||||
hostParticipates: true,
|
||||
streakBonusEnabled: false,
|
||||
streakThreshold: 3,
|
||||
streakMultiplier: 1.1,
|
||||
comebackBonusEnabled: false,
|
||||
comebackBonusPoints: 50,
|
||||
penaltyForWrongAnswer: false,
|
||||
penaltyPercent: 25,
|
||||
firstCorrectBonusEnabled: false,
|
||||
firstCorrectBonusPoints: 50,
|
||||
},
|
||||
questions: [
|
||||
{
|
||||
text: 'Updated question?',
|
||||
options: [
|
||||
{ text: 'X', isCorrect: true, shape: 'circle', color: 'yellow' },
|
||||
{ text: 'Y', isCorrect: false, shape: 'square', color: 'green' },
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
await request('PUT', `/api/quizzes/${gameConfigQuizId}`, updatedQuiz);
|
||||
|
||||
const { data } = await request('GET', `/api/quizzes/${gameConfigQuizId}`);
|
||||
const quiz = data as Record<string, unknown>;
|
||||
const config = quiz.gameConfig as Record<string, unknown>;
|
||||
|
||||
if (config.shuffleQuestions !== false) throw new Error('shuffleQuestions not updated');
|
||||
if (config.hostParticipates !== true) throw new Error('hostParticipates not updated');
|
||||
if (config.streakBonusEnabled !== false) throw new Error('streakBonusEnabled not updated');
|
||||
});
|
||||
|
||||
await test('PATCH /api/quizzes/:id/config updates only gameConfig', async () => {
|
||||
if (!gameConfigQuizId) throw new Error('No game config quiz created');
|
||||
|
||||
const newConfig = {
|
||||
gameConfig: {
|
||||
shuffleQuestions: true,
|
||||
shuffleAnswers: true,
|
||||
hostParticipates: true,
|
||||
streakBonusEnabled: true,
|
||||
streakThreshold: 4,
|
||||
streakMultiplier: 1.3,
|
||||
comebackBonusEnabled: true,
|
||||
comebackBonusPoints: 150,
|
||||
penaltyForWrongAnswer: true,
|
||||
penaltyPercent: 20,
|
||||
firstCorrectBonusEnabled: true,
|
||||
firstCorrectBonusPoints: 100,
|
||||
},
|
||||
};
|
||||
|
||||
await request('PATCH', `/api/quizzes/${gameConfigQuizId}/config`, newConfig);
|
||||
|
||||
const { data } = await request('GET', `/api/quizzes/${gameConfigQuizId}`);
|
||||
const quiz = data as Record<string, unknown>;
|
||||
const config = quiz.gameConfig as Record<string, unknown>;
|
||||
|
||||
if (config.shuffleQuestions !== true) throw new Error('PATCH did not update shuffleQuestions');
|
||||
if (config.streakThreshold !== 4) throw new Error('PATCH did not update streakThreshold');
|
||||
if (config.comebackBonusPoints !== 150) throw new Error('PATCH did not update comebackBonusPoints');
|
||||
if (quiz.title !== 'Updated Config Quiz') throw new Error('PATCH should not have changed title');
|
||||
});
|
||||
|
||||
await test('PATCH /api/quizzes/:id/config with non-existent ID returns 404', async () => {
|
||||
const config = {
|
||||
gameConfig: {
|
||||
shuffleQuestions: true,
|
||||
shuffleAnswers: false,
|
||||
hostParticipates: true,
|
||||
streakBonusEnabled: false,
|
||||
streakThreshold: 3,
|
||||
streakMultiplier: 1.1,
|
||||
comebackBonusEnabled: false,
|
||||
comebackBonusPoints: 50,
|
||||
penaltyForWrongAnswer: false,
|
||||
penaltyPercent: 25,
|
||||
firstCorrectBonusEnabled: false,
|
||||
firstCorrectBonusPoints: 50,
|
||||
},
|
||||
};
|
||||
await request('PATCH', '/api/quizzes/non-existent-id/config', config, 404);
|
||||
});
|
||||
|
||||
await test('PATCH /api/quizzes/:id/config with null gameConfig clears config', async () => {
|
||||
if (!gameConfigQuizId) throw new Error('No game config quiz created');
|
||||
|
||||
await request('PATCH', `/api/quizzes/${gameConfigQuizId}/config`, { gameConfig: null });
|
||||
|
||||
const { data } = await request('GET', `/api/quizzes/${gameConfigQuizId}`);
|
||||
const quiz = data as Record<string, unknown>;
|
||||
|
||||
if (quiz.gameConfig !== null) throw new Error('gameConfig should be null after clearing');
|
||||
});
|
||||
|
||||
await test('POST /api/quizzes without gameConfig sets null config', async () => {
|
||||
const quizNoConfig = {
|
||||
title: 'Quiz Without Config',
|
||||
source: 'manual',
|
||||
questions: [
|
||||
{
|
||||
text: 'No config question?',
|
||||
options: [
|
||||
{ text: 'A', isCorrect: true, shape: 'triangle', color: 'red' },
|
||||
{ text: 'B', isCorrect: false, shape: 'diamond', color: 'blue' },
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
const { data } = await request('POST', '/api/quizzes', quizNoConfig, 201);
|
||||
const quizId = (data as { id: string }).id;
|
||||
|
||||
const { data: getResult } = await request('GET', `/api/quizzes/${quizId}`);
|
||||
const quiz = getResult as Record<string, unknown>;
|
||||
|
||||
if (quiz.gameConfig !== null) throw new Error('Expected null gameConfig for quiz without config');
|
||||
|
||||
await request('DELETE', `/api/quizzes/${quizId}`, undefined, 204);
|
||||
});
|
||||
|
||||
await test('DELETE cleanup game config quiz', async () => {
|
||||
if (gameConfigQuizId) {
|
||||
await request('DELETE', `/api/quizzes/${gameConfigQuizId}`, undefined, 204);
|
||||
}
|
||||
});
|
||||
|
||||
console.log('\nUser Default Config Tests:');
|
||||
|
||||
await test('GET /api/users/me returns defaultGameConfig', async () => {
|
||||
const { data } = await request('GET', '/api/users/me');
|
||||
const user = data as Record<string, unknown>;
|
||||
|
||||
if (!('defaultGameConfig' in user)) throw new Error('Missing defaultGameConfig field');
|
||||
});
|
||||
|
||||
await test('PUT /api/users/me/default-config saves default config', async () => {
|
||||
const defaultConfig = {
|
||||
defaultGameConfig: {
|
||||
shuffleQuestions: true,
|
||||
shuffleAnswers: true,
|
||||
hostParticipates: false,
|
||||
streakBonusEnabled: true,
|
||||
streakThreshold: 4,
|
||||
streakMultiplier: 1.25,
|
||||
comebackBonusEnabled: true,
|
||||
comebackBonusPoints: 75,
|
||||
penaltyForWrongAnswer: true,
|
||||
penaltyPercent: 15,
|
||||
firstCorrectBonusEnabled: true,
|
||||
firstCorrectBonusPoints: 60,
|
||||
},
|
||||
};
|
||||
|
||||
await request('PUT', '/api/users/me/default-config', defaultConfig);
|
||||
|
||||
const { data } = await request('GET', '/api/users/me');
|
||||
const user = data as Record<string, unknown>;
|
||||
const config = user.defaultGameConfig as Record<string, unknown>;
|
||||
|
||||
if (!config) throw new Error('defaultGameConfig not saved');
|
||||
if (config.shuffleQuestions !== true) throw new Error('shuffleQuestions not saved');
|
||||
if (config.streakThreshold !== 4) throw new Error('streakThreshold not saved');
|
||||
if (config.comebackBonusPoints !== 75) throw new Error('comebackBonusPoints not saved');
|
||||
});
|
||||
|
||||
await test('PUT /api/users/me/default-config with null clears config', async () => {
|
||||
await request('PUT', '/api/users/me/default-config', { defaultGameConfig: null });
|
||||
|
||||
const { data } = await request('GET', '/api/users/me');
|
||||
const user = data as Record<string, unknown>;
|
||||
|
||||
if (user.defaultGameConfig !== null) throw new Error('defaultGameConfig should be null after clearing');
|
||||
});
|
||||
|
||||
await test('PUT /api/users/me/default-config with partial config saves as-is', async () => {
|
||||
const partialConfig = {
|
||||
defaultGameConfig: {
|
||||
shuffleQuestions: true,
|
||||
hostParticipates: false,
|
||||
},
|
||||
};
|
||||
|
||||
await request('PUT', '/api/users/me/default-config', partialConfig);
|
||||
|
||||
const { data } = await request('GET', '/api/users/me');
|
||||
const user = data as Record<string, unknown>;
|
||||
const config = user.defaultGameConfig as Record<string, unknown>;
|
||||
|
||||
if (!config) throw new Error('Partial config not saved');
|
||||
if (config.shuffleQuestions !== true) throw new Error('shuffleQuestions not in partial config');
|
||||
if (config.hostParticipates !== false) throw new Error('hostParticipates not in partial config');
|
||||
});
|
||||
|
||||
await test('PUT /api/users/me/default-config cleanup - reset to null', async () => {
|
||||
await request('PUT', '/api/users/me/default-config', { defaultGameConfig: null });
|
||||
});
|
||||
|
||||
console.log('\nPhase 6 - Duplicate/Idempotency Tests:');
|
||||
|
||||
await test('POST /api/quizzes with same data creates separate quizzes', async () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue