-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
190 lines (166 loc) · 8.49 KB
/
index.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="Play Qwixx online! An unofficial fan site.">
<title>Play Qwixx online</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.2/css/all.css">
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>🎲</text></svg>">
<script src="https://kit.fontawesome.com/7d59c9faf1.js" crossorigin="anonymous"></script>
<script src="script.js" defer></script>
</head>
<body>
<nav>
<ul>
<li><a href="#playQwixx">Play Qwixx</a></li>
<li><a href="#bestScoreZone">Best scores</a></li>
<li><a href="#rules">Rules</a></li>
</ul>
</nav>
<div id="playQwixx">
<div id="diceZone">
<button type="button" id="rollDiceButton" onclick="newTurn()">Roll</button>
<div id="displayDice"></div>
</div>
<div id="messageZone">
</div>
<?php
define("SCORE_MAX", 288);
// Handling Best score form
if (!empty($_POST) && $_SERVER["REQUEST_METHOD"] == "POST") {
$nameToAdd = test_input($_POST["name"]);
$nameToAdd = substr($nameToAdd, 0, 15); // avoid too long names
$scoreToAdd = test_input($_POST["score"]);
intval($scoreToAdd);
$scoreToAdd = $scoreToAdd > SCORE_MAX ? 0 : $scoreToAdd;
// Update JSON file
$arrayToAdd = array("name" => $nameToAdd, "score" => $scoreToAdd);
// File names (BSF means BestScoreFile)
$alltime_bsf = "scores.json";
$today_bsf = "todayscores.json";
function updateFile($bsf) {
global $arrayToAdd;
// Get file
$filename = $bsf;
$jsonString = file_get_contents($filename);
// Change it to an array
$jsonData = json_decode($jsonString, true);
// Add data to the array
array_push($jsonData, $arrayToAdd);
// Sort the array
$score = array_column($jsonData, 'score');
array_multisort($score, SORT_DESC, $jsonData);
// Get rid of last element
array_pop($jsonData);
// Transform back into a string
$jsonData = json_encode($jsonData);
$myFile = fopen($filename, "w") or die("Cannot open file! Check permissions on json file");
fwrite($myFile, $jsonData);
fclose($myFile);
}
// Call function
updateFile($alltime_bsf);
updateFile($today_bsf);
// Prevent resubmission
header("Location: " . $_SERVER["REQUEST_URI"]);
exit;
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<div id="congrats" class="hide">
<p>🎉 Congrats! This is one of the best scores.<br>
Would you like to appear in the list?</p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" id="sendBestScore">
<input type="text" name="name" value="" hidden>
<input type="text" name="score" value="" hidden>
<button type="submit" name="button">Yes!</button>
</form>
</div>
</div>
<div id="bestScoreZone">
<h2>Best scores</h2>
<h3>All time</h3>
<ol id="bestScoreList"></ol>
<h3>Today</h3>
<ol id="todayBestScoreList"></ol>
</div>
<hr>
<section id="rules">
<h2>Rules</h2>
<p>Source: <cite><a href="https://www.fgbradleys.com/rules/rules4/Qwixx%20-%20rules.pdf" title="external PDF file containing Qwixx rules">Rules (PDF file)</a></cite></p>
<h3>Goal</h3>
<p>Score the most points by crossing out as many numbers in the four color-rows as possible while avoiding penalty points.</p>
<h3>How to play</h3>
<p>In this solo version, you're always the "main player" and roll all six dice every turn. You have to cross at least one number (into the four color-rows or a penalty) and a maximum of two into the four color-rows.</p>
<p>The cell you can cross is either:</p>
<ul>
<li>a penalty cell;</li>
<li>or the number corresponding to the sum of white dice, anywhere;</li>
<li>and/or the number corresponding to the sum of a white dice and a color dice in the row corresponding to the dice color.</li>
</ul>
<p>Numbers must be crossed out from left to right in each of the four color-rows. You do not have to begin with the number farthest to the left, but if you skip any numbers, they cannot be crossed out afterward.</p>
<p>If you wish to cross out the number at the extreme right end of a color-row you must have first
crossed out at least five numbers in that row. Crossing this last number will increment your number of cells crossed in a row by 1 and lock the line (which has effect on other players in a multiplayer version).
</p>
<p>The more you cross cells on a color-row, the more points you get, see below. A penalty cell costs 5 points.</p>
<table>
<thead>
<tr>
<th scope="colgroup" colspan="13">Score table</th>
</tr>
</thead>
<tbody>
<tr>
<td>X</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
<td>6</td>
<td>7</td>
<td>8</td>
<td>9</td>
<td>10</td>
<td>11</td>
<td>12</td>
</tr>
<tr>
<td>#</td>
<td>1</td>
<td>3</td>
<td>6</td>
<td>10</td>
<td>15</td>
<td>21</td>
<td>28</td>
<td>36</td>
<td>45</td>
<td>55</td>
<td>66</td>
<td>78</td>
</tr>
</tbody>
</table>
<h3>End of game</h3>
<p>The game ends if either:</p>
<ul>
<li>The four penalty cells are crossed;</li>
<li>Two lines are locked.</li>
</ul>
<p>Total score is calculted as follows: sum of points per color-row (see table above) minus penalty points if any.</p>
</section>
<footer>
<h3>Credits</h3>
<p>Qwixx is a boardgame created by Steffen Benndorf and illustrated by O. & S. Freudenreich.<br>
This flawed web version is the work of <a href="https://julietoral.ovh/" title="Julie's webdev blog">Julie Boissière-Vasseur</a>, as part of webdevelopment studies. <a href="https://github.com/jboisseur/qwixx" title="GitHub Project Repository">GitHub Project Repository <i class="fa-brands fa-github" aria-hidden="true"></i></a></p>
</footer>
</body>
</html>