Beispiel #1
0
if ($structureId !== null || !$player->current_quizz) {
    try {
        $structureId = RequestTool::getParameter('structureId', RequestTool::PARAM_TYPE_UNSIGNED_INT);
        $structure = OsteoFactory::getElement('Structure', $structureId);
    } catch (Exception $e) {
        // Incorrect parameter or invalid structure: do nothing
        RequestTool::redirect('/');
    }
    $isCraft = RequestTool::getParameter('craft', RequestTool::PARAM_TYPE_BOOLEAN, false, false);
    $questionList = OsteoFactory::getElementList('Question', ($isCraft ? 'question_is_in_craft_quizz=1 AND ' : '') . 'structure_id=' . $structureId, 'RAND() LIMIT ' . $structure->question_number);
    $quizz = array('structureId' => $structureId, 'questionList' => array());
    foreach ($questionList as $question) {
        $quizz['questionList'][] = array('questionId' => $question->id, 'answer' => null);
    }
    $quizz['isCraft'] = $isCraft;
    $quizz['startDate'] = DateTool::getTimestamp();
    $player->current_quizz = json_encode($quizz);
    $player->update();
    $currentQuestionIndex = 0;
    $currentQuestion = reset($questionList);
} else {
    if ($player->current_quizz) {
        $quizz = json_decode($player->current_quizz, true);
        foreach ($quizz['questionList'] as $questionNumber => $quizzQuestion) {
            if ($quizzQuestion['answer'] === null) {
                $currentQuestion = OsteoFactory::getElement('Question', $quizzQuestion['questionId']);
                $currentQuestionIndex = $questionNumber;
                break;
            }
        }
        // No questionwith no answer: test is over
Beispiel #2
0
if (!$isPlayerConnected) {
    RequestTool::redirect('/');
}
// Checks if quizz is initialized
$player = $authenticationInstance->getConnectedPlayer();
if (!$player->current_quizz) {
    RequestTool::redirect('/');
}
$quizz = json_decode($player->current_quizz, true);
$correctAnswerNumber = 0;
foreach ($quizz['questionList'] as $quizzQuestion) {
    if ($quizzQuestion['answer'] === true) {
        ++$correctAnswerNumber;
    }
}
$time = DateTool::getTimestamp() - $quizz['startDate'];
$player->current_quizz = null;
$player->update();
OsteoFactory::getElementList('Structure');
$currentStructure = OsteoFactory::getElement('Structure', $quizz['structureId']);
$percentCompleted = $correctAnswerNumber / count($quizz['questionList']);
$isSuccess = false;
$isCrafted = false;
if ($percentCompleted >= 0.6) {
    $isSuccess = true;
    if (!$quizz['isCraft']) {
        // Gives player some loot
        $gainQuantity = ceil($percentCompleted * $currentStructure->component_quantity * 0.5 * rand(40, 100) / 100);
        $component = $currentStructure->getParentStructureCategory()->getParentComponent();
        try {
            $playerComponent = OsteoFactory::getElement('PlayerHasComponent', null, 'player_id=' . $player->id . ' AND component_id=' . $component->id);