Skip to content

Commit b3a6e91

Browse files
committed
improve question generation process
1 parent a91b975 commit b3a6e91

File tree

1 file changed

+42
-20
lines changed

1 file changed

+42
-20
lines changed

examples/corporate-conundrum/game.sql

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
1-
select 'shell' as component, * FROM shell;
2-
1+
select 'shell' as component,
2+
*
3+
FROM shell;
4+
-- Display the list of players with a link for each one to start playing
35
INSERT INTO players(name, game_id)
46
SELECT $Player as name,
57
$id::integer as game_id
68
WHERE $Player IS NOT NULL;
7-
8-
SELECT 'list' as component, 'Players' as title;
9-
SELECT
10-
name as title,
9+
SELECT 'list' as component,
10+
'Players' as title;
11+
SELECT name as title,
1112
'next-question.sql?game_id=' || game_id || '&player=' || name as link
12-
FROM players WHERE game_id = $id::integer;
13-
14-
SELECT 'form' as component, 'Add a new player' as title, 'Add to game' as validate;
15-
SELECT 'Player' as name, 'Player name' as placeholder, TRUE as autofocus;
16-
13+
FROM players
14+
WHERE game_id = $id::integer;
15+
---------------------------
16+
-- Player insertion form --
17+
---------------------------
18+
SELECT 'form' as component,
19+
'Add a new player' as title,
20+
'Add to game' as validate;
21+
SELECT 'Player' as name,
22+
'Player name' as placeholder,
23+
TRUE as autofocus;
1724
-- Insert a new question into the game_questions table for each new player
18-
INSERT INTO game_questions(game_id, question_id, wrong_answer, impostor, game_order)
19-
SELECT
20-
$id::integer as game_id,
21-
questions.id as question_id,
22-
questions.true_answer * (abs(random() %2) + 0.5) as wrong_answer, -- wrong answer = true answer +/- 50% TODO: better wrong answer generation
23-
$Player as impostor,
24-
random() as game_order
25+
INSERT INTO game_questions(
26+
game_id,
27+
question_id,
28+
wrong_answer,
29+
impostor,
30+
game_order
31+
)
32+
SELECT $id::integer as game_id,
33+
questions.id as question_id,
34+
-- When the true answer is small, set the wrong answer to just +/- 1, otherwise -25%/+75%.
35+
-- When it is a date between 1200 and 2100, make it -25 % or +75 % of the distance to today
36+
CAST(CASE
37+
WHEN questions.true_answer < 10 THEN questions.true_answer + 1 - 2 * abs(random() %2) -- wrong answer = true answer +/- 1
38+
WHEN questions.true_answer > 1200
39+
AND questions.true_answer < 2100 THEN 2023 - (2023 - questions.true_answer) * (abs(random() %2) + 0.75) -- wrong answer = true answer -25% or +75% of the distance to today
40+
ELSE questions.true_answer * (abs(random() %2) + 0.75)
41+
END AS INTEGER) as wrong_answer,
42+
-- wrong answer = true answer +/- 50% TODO: better wrong answer generation
43+
$Player as impostor,
44+
random() as game_order
2545
FROM questions
26-
LEFT JOIN game_questions ON questions.id = game_questions.question_id AND game_questions.game_id = $id::integer
27-
WHERE game_questions.question_id IS NULL AND $Player IS NOT NULL
46+
LEFT JOIN game_questions ON questions.id = game_questions.question_id
47+
AND game_questions.game_id = $id::integer
48+
WHERE game_questions.question_id IS NULL
49+
AND $Player IS NOT NULL
2850
ORDER BY random()
2951
LIMIT 1;

0 commit comments

Comments
 (0)