How make a quiz that asks a series of questions, waiting for user input?

Discussion in 'CommandHelper' started by malon, Sep 5, 2017.

  1. malon

    malon Member

    My grand scheme goal:
    Make a quiz that pulls from a pool of questions, generating unique quiz each time.

    My problem:
    I actually don't know how to wait for user input, then call the next question.


    This is unfinished, unworking code, but it's where I got stuck because I didn't know how to structure my code properly

    Code (Text):

    register_command('staffquiz',

        array(
            description: 'Start a staff quiz!',
            usage: '/staffquiz',
            permission: 'staff.quiz',
            noPermMsg: 'Sorry you don\'t have permission to use this command.',

            executor: closure(@alias, @sender, @args) {
                if (array_size(@args) != 0) {

                    msg(''.color(RED).color(BOLD).'Improper Usage!');
                    msg(''.color(RED).'Usage: /staffquiz');
                    die();
                }
                @questions = array('Q1','Q2','Q3','Q4','Q5','Q6','Q7','Q8','Q9','Q10')
                @numQuestions = array_size(@questions)
                @quizStarted = get_value('staffquiz.'[email protected]'started')
                if(@quizStarted == true){ #I am aware of (@quizStarted),  i do this for readability because i'm shite at commenting
                    msg('Sorry, you already took (or are taking) this quiz!')
                    die()
                }
                for(@i = 0, @i < 5, @i++){
                    @r = rand(0,(@numQuestions))
                    array_remove(@questions,@r)
                    @numQuestions--
                    store_value('staffquiz.'[email protected]'.question.'[email protected])
                    bind(player_chat,associative_array('id':'staffquizanswer'),null,@e){

                    }
                }
            }
        )
    )

     
  2. PseudoKnight

    PseudoKnight Well-Known Member

    Here's some ideas:
    PHP:
    register_command('staffquiz',

        array(
            description: 'Start a staff quiz!',
            usage: '/staffquiz',
            permission: 'staff.quiz',
            noPermMsg: 'Sorry you don\'t have permission to use this command.',

            executor: closure(@alias, @sender, @args) {
                if(array_size(@args) != 0) {
                    return(false); // this sends the player the usage text you specify in the register_command array
                }
                if(has_value('staffquiz', player())) {
                    die(color(RED).'You already took this quiz!');
                }
                if(has_bind(player().'staffquiz')){
                    die(color(RED).'You are already taking this quiz!');
                }
               
                @questions = array(
                    'Q1',
                    'Q2',
                    'Q3',
                    'Q4',
                    'Q5',
                    'Q6',
                    'Q7',
                    'Q8',
                    'Q9',
                    'Q10'
                );
               
                @randomQuestions = array_rand(@questions, 5, false);
               
                msg(color(YELLOW).color(BOLD).'Staff Quiz:');
                msg(color(YELLOW).'Please answer the following questions in chat. Other players will not see it.');
                msg(color(GREEN)[email protected]randomQuestions[0]);
               
                bind(player_chat, array('id':player().'staffquiz'), array('player': player()), @e, @randomQuestions, @answers = array()){
                    cancel();
                    msg(@e['message']);
                    @answers[] = array(@randomQuestions[0], @e['message']);
                    array_remove(@randomQuestions, 0);
                    if(array_size(@randomQuestions > 0)) {
                        msg(color(GREEN)[email protected]randomQuestions[0]);
                    } else {
                        unbind();
                        store_value('staffquiz', player(), @answers);
                        msg('Thank you for taking this quiz! Any chat messages you now send will be broadcasted as per usual.');
                    }
                }
            }
        )
    )
    Hope that's easy to follow.
  3. malon

    malon Member

    PseudoKnight, that is some fucking sexy ass code. I absolutely love the array_rand() followed by repeatedly asking then removing the 0th index repeatedly until there's no questions left.

    It's elegant as fuck, thank you, I failed at conceptualizing this.
  4. PseudoKnight

    PseudoKnight Well-Known Member

    :)

    You might also want to accept a keyword in player_chat that exits the staff quiz without submitting it.