/
index.php
executable file
·211 lines (193 loc) · 7.97 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Created by PhpStorm.
* User: ale
* Date: 29/09/15
* Time: 20:05
*/
session_start();
require 'vendor/autoload.php';
require 'services/Service.php';
require 'utils/Database.php';
require 'utils/Helper.php';
require 'utils/Filter.php';
require 'utils/TournamentStatus.php';
$app = new \Slim\Slim(array(
'view' => new \Slim\Views\Twig()
));
$view = $app->view();
$view->parserOptions = array(
'debug' => true,
'cache' => dirname(__FILE__) . '/cache'
);
$app->get('/', function() use($app) {
if (isset($_SESSION['id'])) {
$app->redirectTo('my-tournaments');
} else {
$app->render('login.html.twig');
}
})
->name('login');
$app->post('/login', function() use($app) {
$name = $app->request->post('name');
if ($name) {
// Check if the username isn't already register
$user = Service::getUserByName($name);
if (!$user) {
$user_id = Service::createUser($name);
$_SESSION['id'] = $user_id;
} else {
$_SESSION['id'] = $user[0]['id'];
}
$app->redirectTo('my-tournaments');
} else {
$app->redirectTo('login');
}
});
$app->get('/my-tournaments', Filter::isRegisteredUser($app), function() use($app) {
$user_id = $_SESSION['id'];
$tournaments = Service::getMyTournaments($user_id);
foreach ($tournaments as $index => $tournament) {
$tournaments[$index]['registered'] = Service::isUserRegisteredInTournament($tournament['id'], $user_id);
$tournaments[$index]['num_people'] = Service::countUsersByTournamentId($tournament['id']);
$tournaments[$index]['date'] = Helper::getDateFormat($tournament['date_time']);
$tournaments[$index]['time'] = Helper::getTimeFormat($tournament['date_time']);
}
$app->render('tournaments.html.twig',
[
'tournaments' => $tournaments,
'user_id' => $user_id,
'page' => 'my-tournaments',
'title' => 'My tournaments'
]
);
})
->name('my-tournaments');
$app->get('/upcoming-tournaments', Filter::isRegisteredUser($app), function() use($app) {
$user_id = $_SESSION['id'];
$tournaments = Service::getUpcomingTournaments();
foreach ($tournaments as $index => $tournament) {
$tournaments[$index]['registered'] = Service::isUserRegisteredInTournament($tournament['id'], $user_id);
$tournaments[$index]['num_people'] = Service::countUsersByTournamentId($tournament['id']);
$tournaments[$index]['date'] = Helper::getDateFormat($tournament['date_time']);
$tournaments[$index]['time'] = Helper::getTimeFormat($tournament['date_time']);
}
$app->render('tournaments.html.twig',
[
'tournaments' => $tournaments,
'user_id' => $user_id,
'page' => 'upcoming',
'title' => 'Upcoming tournaments'
]
);
});
$app->get('/open-tournaments', Filter::isRegisteredUser($app), function() use($app) {
$user_id = $_SESSION['id'];
$tournaments = Service::getTournamentsByStatus(TournamentStatus::OPEN_STATUS);
foreach ($tournaments as $index => $tournament) {
$tournaments[$index]['registered'] = Service::isUserRegisteredInTournament($tournament['id'], $user_id);
$tournaments[$index]['num_people'] = Service::countUsersByTournamentId($tournament['id']);
$tournaments[$index]['date'] = Helper::getDateFormat($tournament['date_time']);
$tournaments[$index]['time'] = Helper::getTimeFormat($tournament['date_time']);
}
$app->render('tournaments.html.twig',
[
'tournaments' => $tournaments,
'user_id' => $user_id,
'page' => 'open',
'title' => 'Open tournaments'
]
);
});
$app->get('/all-tournaments', Filter::isRegisteredUser($app), function() use($app) {
$user_id = $_SESSION['id'];
$tournaments = Service::getTournaments();
foreach ($tournaments as $index => $tournament) {
$tournaments[$index]['registered'] = Service::isUserRegisteredInTournament($tournament['id'], $user_id);
$tournaments[$index]['num_people'] = Service::countUsersByTournamentId($tournament['id']);
$tournaments[$index]['date'] = Helper::getDateFormat($tournament['date_time']);
$tournaments[$index]['time'] = Helper::getTimeFormat($tournament['date_time']);
}
$app->render('tournaments.html.twig',
[
'tournaments' => $tournaments,
'user_id' => $user_id,
'page' => 'all',
'title' => 'All tournaments'
]
);
});
$app->get('/create-tournament', Filter::isRegisteredUser($app), function() use($app) {
$app->render('create_tournament.html.twig');
});
$app->get('/logout', function() use($app) {
unset($_SESSION['id']);
$app->redirectTo('login');
});
$app->post('/tournaments', Filter::isRegisteredUser($app), function() use($app) {
$name = $app->request->post('name');
$datetime = $app->request->post('datetime');
$status = TournamentStatus::OPEN_STATUS;
$promoter_id = $_SESSION['id'];
$tournament_id = Service::createTournament($name, $promoter_id, Helper::getDateTimeFormat($datetime), $status);
Service::registerUserInTournament($tournament_id, $promoter_id);
$app->redirectTo('my-tournaments');
});
$app->post('/tournaments/:id', Filter::isRegisteredUser($app), function($tournament_id) use($app) {
$user_id = $_SESSION['id'];
Service::registerUserInTournament($tournament_id, $user_id);
$app->redirectTo('participants', ['id' => $tournament_id]);
});
$app->post('/tournaments/:id/delete', Filter::isRegisteredUser($app), function($tournament_id) use($app) {
$user_id = $_SESSION['id'];
Service::deregisterUserFromTournament($tournament_id, $user_id);
$app->redirectTo('my-tournaments');
});
$app->get('/participants/:id', Filter::isRegisteredUser($app), function($tournament_id) use($app) {
$users = Service::getUsersByTournamentId($tournament_id);
$num_people = Service::countUsersByTournamentId($tournament_id);
$promoter = Service::getPromoterByTournamentId($tournament_id);
$tournament = Service::getTournamentByTournamentId($tournament_id);
$tournament['date'] = Helper::getDateFormat($tournament['date_time']);
$tournament['time'] = Helper::getTimeFormat($tournament['date_time']);
$app->render('participants.html.twig', ['users' => $users, 'num_people' => $num_people, 'promoter' => $promoter, 'tournament' => $tournament]);
})
->name('participants');
$app->post('/tournaments/:id/close', Filter::isRegisteredUser($app), function($tournament_id) use($app) {
Service::updateTournamentStatus($tournament_id, TournamentStatus::CLOSED_STATUS);
$users = Service::getUsersByTournamentIdOrderByRandom($tournament_id);
foreach ($users as $index => $user) {
Service::updatePosition($index+1, $tournament_id, $user['id']);
}
$app->redirectTo('rounds', ['id' => $tournament_id]);
});
$app->get('/rounds/:id', Filter::isRegisteredUser($app), function($tournament_id) use($app) {
$tournament = Service::getTournamentByTournamentId($tournament_id);
$tournament['date'] = Helper::getDateFormat($tournament['date_time']);
$tournament['time'] = Helper::getTimeFormat($tournament['date_time']);
// rounds
$users = Service::getUsersByTournamentIdOrderByPosition($tournament_id);
$rounds = [];
$round = [];
foreach ($users as $index => $user) {
$round[] = $user['name'];
if (count($round) == 2) {
$rounds[] = $round;
$round = [];
}
if ($index + 1 == count($users)) {
if (!Helper::isPowerOfTwo(count($users)) || count($round) == 1) {
if (count($round) == 1) {
$round[] = '';
$rounds[] = $round;
}
while (!Helper::isPowerOfTwo(count($rounds))) {
$rounds[] = ['', ''];
}
}
}
}
$app->render('rounds.html.twig', ['rounds' => $rounds, 'tournament' => $tournament]);
})
->name('rounds');
$app->run();