summaryrefslogtreecommitdiff
path: root/blaquiz.php
blob: d276e4330212a831333c08150f7b65445f960a56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
	/*Bla Quiz - A Quiz program..*/

	//Orginal Author: Phil Burton <philbeansburton@gmail.com>

	// Version History
	// 0.0.1 Created blaquiz.php 
	
	$version = "BlaQuiz Version: 0.0.1";

	$questions = array (
		"How many bits in a byte?",
		"What does TTP mean?"
	);
	$answers = array (
		"8",
		"Time To Poo"
	);
	// get the input
	$stdin = read_stdin();
	
	$stdin = explode(" ", $stdin);
	$out = "";
	$token = "";
	if(isset($stdin[0]))
	{
		switch ($stdin[0])
		{
			case "-q":
				$id = rand(0,sizeof($questions) - 1);
				$out = "ID: $id $questions[$id]";
				break;
			case "-a":
				if(isset($stdin[1]) && isset($stdin[2]))
				{
					//Check question ID
					$id = $stdin[1];
					if($id < sizeof($questions))
					{
						//get answer
						$answer = "";
						for($i=2; $i<sizeof($stdin); $i++)
						{
							$answer .= $stdin[$i] . " ";
						}
						$answer = strtolower(substr($answer, 0 , -1));
						if(strtolower($answers[$id])  == $answer)
						{
							$out = "Correct! $answer is the right answer!"; 
						}
						else
						{
							$out = "Nope, $answer is the wrong answer!";
						}
					}
					else
					{
						$out = "Please give your answers like this: -a <answer>";
					}
				}
				else
				{
					$out = "This question doesn't exist";
				}
				break;
			default:
				$out = "Please either ask for a new question: -q. Or guess at an answer -a <qid> <answer>.";
				break;
		}	
	}
	else
	{
		$out = "Please either aska  new question: -q or guess at an answer: -a <qid> <answer>.";
	}
	echo $out . "\n";


	function read_stdin()
	{
        	$fr=fopen("php://stdin","r");   // open our file pointer to read from stdin
	        $input = fgets($fr,128);        // read a maximum of 128 characters
	        $input = rtrim($input);         // trim any trailing spaces.
        	fclose ($fr);                   // close the file handle
	        return $input;                  // return the text entered
	}
?>