-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.php
48 lines (34 loc) · 1.05 KB
/
server.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
<?php
session_start();
require_once 'include/Participants/Player.php';
require_once 'include/TicTacToeGame.php';
if (!isset($_SESSION['playerId'])) {
$_SESSION['playerId'] = time();
}
$player = new \Participants\Player($_SESSION['playerId']);
if (isset($_SESSION['currentGame'])) {
try {
$player->loadExistingGame($_SESSION['currentGame']);
} catch (RuntimeException $e) {
session_destroy();
}
} else {
$player->createNewGame();
}
if (count($_POST)) {
if (!isset($_POST['x']) || !isset($_POST['y'])) {
throw new RuntimeException('Point is missing');
}
try {
$player->getCurrentGame()->markPoint($_POST['x'], $_POST['y']);
} catch (RuntimeException $e) {
die(json_encode(array('error' => $e->getMessage())));
}
}
//Save current game to session
$_SESSION['currentGame'] = $player->getCurrentGame()->toArray();
echo json_encode($player->getCurrentGame()->toArray()) . PHP_EOL;
//Create new game in case it is finished
if ($player->getCurrentGame()->isFinished()) {
session_destroy();
}