"WHERE `idduel` IN (SELECT `duel` FROM `duel_player_score` WHERE `user` = ?)"+
"ORDER BY `idduel`"
// queryQuestionsForNewRound randomly queries questions for a new duel round. Questions which
// weren't played often by both users are preferred. Thus, the score for each question is
// calculated using the formula (#u1)^2 + (#u2) with some random factors for
// variance. #u1 and #u2 are the numbers of times the question has been played by the
// corresponding user.
constqueryQuestionsForNewRound="SELECT `pool_question`.`question` FROM `pool_question`"+
"JOIN `question` ON (`pool_question`.`question` = `question`.`idquestion`)"+
"LEFT JOIN `roundquestion` ON (`roundquestion`.`question`=`pool_question`.`question`)"+
"LEFT JOIN `useranswer` ON (`roundquestion`.`idroundquestion`=`useranswer`.`roundquestion`)"+
"WHERE"+
"`pool_question`.`pool`=? "+
"AND `pool_question`.`question` NOT IN (SELECT `question` FROM `roundquestion` INNER JOIN `round` ON (`round`.`idround` = `roundquestion`.`round`) WHERE `duel`=?)"+
"AND `question`.`published`=1 "+
"GROUP BY `pool_question`.`question`"+
"ORDER BY POW(COUNT(IF(`useranswer`.`user`=? AND RAND()<.9, 1, NULL)), 2) + POW(COUNT(IF(`useranswer`.`user`=? AND RAND()<.9, 1, NULL)), 2)"+
"LIMIT ?"
// DuelQuery provides functions to create, get and update duels in datastores.
// There can be various implementations for different datastores.
questions,errQuestions:=tx.Query("SELECT `pool_question`.`question` FROM `pool_question` JOIN `question` ON (`pool_question`.`question` = `question`.`idquestion`) WHERE `pool_question`.`pool`=? AND `pool_question`.`question` NOT IN (SELECT `question` FROM `roundquestion` INNER JOIN `round` ON (`round`.`idround` = `roundquestion`.`round`) WHERE `duel`=?) AND `question`.`published`=1 ORDER BY RAND() LIMIT ?",poolID,duelID,QuestionsPerRound)