-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
113 lines (104 loc) · 3.14 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
<?php
error_reporting(E_ALL);
session_start();
$start = microtime(true);
require 'functions.php';
require 'Card.php';
require 'GameState.php';
require 'Game.php';
require 'Player.php';
require 'Displayer.php';
unset($game);
// Stop game if requested
if (isset($_GET['stop_game'])) {
$_SESSION['game'] = null;
session_destroy();
}
// Load or create game
if (isset($_POST['new_game'])) {
$game = new Game();
} else if (isset($_SESSION['game'])) {
$game = unserialize($_SESSION['game']);
if (!($game instanceof Game)) {
session_destroy();
throw new Exception('Could not load the game! Please reload.');
}
} else {
// Offer to create a new game if we don't have any session.
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">
<input type="submit" style="margin: auto" value="Create new game" name="new_game" />
</form>';
exit;
}
$displayer = new Displayer($game);
$state = $game->getState();
if ($state === GameState::HAND_START) {
// If this method didn't change the game state, this would open a door to
// cheating, because cards would be distributed upon every reload.
$game->startNewHand();
$start_player = $game->getCurrentRoundStarter();
if ($start_player === Game::HUMAN_ID) {
$displayer->draw('Must start with the two of clubs', true);
}
else {
$msg = 'Player ' . ($start_player+1) . ' begins the hand.';
$displayer->draw($msg, false);
$game->playTillHuman();
}
save_game_to_session($game);
}
else if ($state === GameState::AWAITING_CLUBS) {
// Important to manually check here that we got the two of clubs
if (post_is_valid_card_format('card') && $_POST['card'] == Card::CLUBS . 2) {
$result = $game->processHumanMove($_POST['card']);
if ($result === Game::MOVE_OK) {
$nextRoundStarter = $game->playTillEnd();
$displayer->roundEndMessage($nextRoundStarter);
save_game_to_session($game);
} else {
// Throw an exception since we just checked that we got
// the two of clubs. We should never be in this clause!
throw new Exception('Game should have accepted two of Clubs!');
}
}
else {
$message = '<span class="error">Must start with the two of clubs!</span>';
$displayer->draw($message, true);
}
}
else if ($state === GameState::AWAITING_HUMAN) {
if (post_is_valid_card_format('card')) {
$result = $game->processHumanMove($_POST['card']);
if ($result === Game::MOVE_OK) {
$nextRoundStarter = $game->playTillEnd();
$displayer->roundEndMessage($nextRoundStarter);
save_game_to_session($game);
}
else {
$displayer->handleCardError($result);
}
} else {
$displayer->draw('Your turn.', true);
}
}
else if ($state === GameState::ROUND_END) {
$game->playTillHuman();
$displayer->draw('Your turn', true);
save_game_to_session($game);
}
else if ($state === GameState::GAME_END) {
$displayer->roundEndMessage(null);
}
else {
var_dump($game->getState());
throw new Exception("Encountered unknown game state");
}
$end = microtime(true);
$gen_time = round($end - $start, 3);
echo '<p class="footer">
Page generated in ' . $gen_time . ' s
<br /><a href="?stop_game">Stop game?</a>
</p>';
?>
</body>
</html>