-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathplay.php
153 lines (141 loc) · 6.58 KB
/
play.php
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
require 'local/config.php';
require 'sources/autoload.php';
$nq = new \NineteenQ\Engine(empty($_GET['q']) ? '' : $_GET['q']);
$numQuestions = count($nq->askedQuestions);
?>
<!doctype html>
<html lang="en">
<head>
<meta name="robots" content="noindex" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>19 Questions</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
<script>var q=<?= json_encode($nq->state) ?>;</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link href="//cdnjs.cloudflare.com/ajax/libs/x-editable/1.5.0/inputs-ext/typeaheadjs/lib/typeahead.js-bootstrap.css" rel="stylesheet" media="screen">
<script src="//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.10.2/typeahead.bundle.min.js"></script>
</head>
<body class="bg-light lead">
<div class="container-md my-5">
<main class="bg-info my-5 p-5 text-center rounded">
<h1>19 Questions <small class="text-muted">you think of something, we guess it</small></h1>
<?php if (isset($_GET['wrapup']) || $numQuestions >= 40): ?>
<p class="display-2 text-success">You win this round!</p>
<h3>Were you thinking of one of these:</h3>
<?php
foreach ($nq->getTopHunches() as $hunch) {
$nameHtml = htmlspecialchars($hunch->name);
if (!empty($hunch->subname)) $nameHtml .= '<small>(' . htmlspecialchars($hunch->subname) . ')</small>';
echo "<a class=\"btn btn-secondary\" href=\"save.php?obj={$hunch->objectId}&q=".$nq->state."\">{$nameHtml}</a>\n";
}
?>
<hr>
<h3>If not, please type the thing here:</h3>
<form class="form form-inline mx-auto" action="save.php" method="get">
<input name="q" type="hidden" value="<?= $nq->state ?>">
<input name="objectname" id="theobjectname" class="form-control">
<input type="submit" value="Submit" class="btn btn-primary">
</form>
<script>
var bestPictures = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('name'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: 'api/v1/objects.php?q=%QUERY'
});
bestPictures.initialize();
$('#theobjectname').typeahead(null, {
name: 'best-pictures',
displayKey: 'name',
source: bestPictures.ttAdapter(),
templates: {
suggestion: function(datum){return datum['name']}
}
});
</script>
</div>
</body>
</html>
<?php exit; ?>
<?php endif; ?>
<?php
if ($numQuestions >= 19) {
echo "<div class=\"alert alert-info\"><strong>You've won this round!</strong> You can continue answering a few more questions or <a href=\"".basename($_SERVER['PHP_SELF'])."?wrapup=yes&q=".$nq->state."\" class=\"btn btn-info\">tell me what you were thinking of</a> so I can learn.</div>\n";
}
echo "<p class=\"lead\"><b>#". ($numQuestions+1) ."</b> ";
list($text, $subtext, $choices) = $nq->getNextQuestion();
if (strlen($subtext)) $text .= " ($subtext)";
echo "$text? ";
foreach ($choices as $choice) {
if (preg_match('/w([0-9]+)/',$choice[1],$regs))
$prefix = "save.php?obj={$regs[1]}&q=";
else
$prefix = basename($_SERVER['PHP_SELF']).'?'.(isset($_GET['debug'])?'debug&':'')."q=";
echo "<a class=\"btn btn-outline-primary\" rel=\"nofollow\" href=\"$prefix".$choice[1]."\">".$choice[0]."</a> ";
}
echo "</p><hr />";
foreach (array_reverse($nq->askedQuestions) as $pastQuestion) {
list($name, $subtext, $answer) = $pastQuestion;
if (strlen($subtext)) $name .= " ($subtext)";
echo "<p>".htmlentities($name)." — $answer</p>";
}
?>
</main>
<section>
<button id="show-brain-dump" class="btn btn-info">Show Brain Dump</button>
<div id="brain-dump" class="row row-cols-1 row-cols-md-2 d-none">
<div class="col">
<h2>Hunches</h2>
<table class="table table-condensed">
<?php
foreach ($nq->getTopHunches() as $hunch) {
$htmlName = htmlspecialchars($hunch->name);
if (!empty($hunch->subname)) {
$htmlName .= ' <small><em>(' . htmlspecialchars($hunch->subname) . ')</em></small>';
}
echo '<tr><td>' . $htmlName . '<td>' . number_format($hunch->likelihood*100 / $nq->objectSumLikelihood, 3) . '%';
echo '<tr><td colspan=2>';
echo '<div class="progress">';
echo '<div class="progress-bar bg-info" title="' . number_format($hunch->likelihood*100 / $nq->objectSumLikelihood, 3) . '% likelihood" role="progressbar" style="width: ' . number_format($hunch->likelihood*100 / $nq->objectSumLikelihood, 3) . '%"></div>';
echo '</div>';
}
echo '<tr><td><em>Total entropy:<td>' . number_format($nq->objectEntropy, 2) . ' bits';
?>
</table>
</div>
<div class="col">
<h2>Top questions</h2>
<table class="table table-condensed">
<?php
foreach ($nq->getBestQuestions(10) as $question) {
list($score, $id, $name, $sub, $yes, $no) = $question;
if (strlen($sub)) $name .= " <small><em>($sub)</em></small>";
echo "<tr><td>$name<td>" . number_format($score, 3) . " bits";
echo "<tr><td colspan=2>";
echo '<div class="progress">';
echo '<div class="progress-bar bg-success" title="' . number_format($yes*100, 2) . '% yes" role="progressbar" style="width: ' . number_format($yes*100, 2) . '%"></div>';
echo '<div class="progress-bar bg-info" title="' . number_format((1-$yes-$no)*100, 2) . '% skip" role="progressbar" style="width: ' . number_format((1-$yes-$no)*100, 2) . '%"></div>';
echo '<div class="progress-bar bg-danger" title="' . number_format($no*100, 2) . '% no" role="progressbar" style="width: ' . number_format($no*100, 2) . '%"></div>';
echo '</div>';
}
?>
</table>
</div>
</div>
</section>
</div>
<footer>
<div class="container-md text-muted my-5">
No cookies. No analytics.
</div>
</footer>
<script>
document.getElementById("show-brain-dump").addEventListener("click", function() {
document.getElementById("brain-dump").classList.toggle("d-none");
document.getElementById("show-brain-dump").classList.toggle("d-none");
});
</script>
</body>
</html>