-
Notifications
You must be signed in to change notification settings - Fork 0
/
teamStats.php
executable file
·239 lines (205 loc) · 9.27 KB
/
teamStats.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php require 'header.php'; ?>
<?php
require_once __DIR__ . "/config.php";
session_start();
if(isset($_POST['fpl-team-id']) && is_numeric($_POST['fpl-team-id']) && strlen($_POST['fpl-team-id']) < 12){
$teamId = $_POST['fpl-team-id'];
$_SESSION['teamID'] = $_POST['fpl-team-id'];
$_SESSION['LAST_ACTIVITY'] = time();
}
else if(isset($_SESSION['teamID']))
$teamId = $_SESSION['teamID'];
else
header('Location: index.php');
// mysqli_query($conn, "INSERT INTO get_users_data (fpl_team_id, requested_on, status) values('" . $teamId . "', '". time() . "', '1')");
$lastInsertedDate = 0;
$query = mysqli_query($conn, "SELECT last_insert_on FROM fpl_user_data where fpl_user_data_id = " . $teamId);
$result = mysqli_fetch_array($query);
$lastInsertedDate = $result['last_insert_on'];
if($lastInsertedDate == 0 && (time() - $lastInsertedDate > 21600)) {
$now = time();
//21600
// $teamId = 109123;
$contents = json_decode(file_get_contents("https://fantasy.premierleague.com/drf/entry/" . $teamId . "/event/1"));
$current_event = $contents->ce;
mysqli_query($conn, "DELETE FROM fpl_user_data WHERE id = '" . $contents->entry->id . "'");
mysqli_query($conn, "INSERT INTO fpl_user_data values ('" . $contents->entry->id . "', '" . $contents->entry->player_first_name . "', '" . $contents->entry->player_last_name . "', '" . $contents->entry->player_region_name . "','" . $contents->entry->summary_overall_points . "','" . $contents->entry->summary_overall_rank . "','" . $contents->entry->summary_event_points . "','" . $contents->entry->summary_event_rank . "','" . $contents->entry->current_event . "','" . $contents->entry->total_transfers . "','" . $contents->entry->name . "', " . $now .")");
mysqli_query($conn, "INSERT INTO get_users_data (fpl_team_id, requested_on, status, gameweek_number) values('" . $teamId . "', '". time() . "', '1', '".$current_event."')");
//HISTORY
$history = json_decode(file_get_contents("https://fantasy.premierleague.com/drf/entry/" . $teamId . "/history"));
foreach($history->history as $value) {
$subQuery = mysqli_query($conn, "SELECT count(*) as count from users_gameweek_history where user_fpl_id = " . $teamId . " AND gameweek_number = " . $value->event);
$subResult = mysqli_fetch_array($subQuery);
if($subResult['count'] == 0)
mysqli_query($conn, "INSERT INTO users_gameweek_history (user_fpl_id, points, total_points, rank, overall_rank, gameweek_number, team_value) values('" . $teamId . "', '". $value->points . "', '" . $value->total_points . "', '" . $value->rank . "', '" . $value->overall_rank . "', '" . $value->event . "', '" . $value->value . "')");
}
mysqli_query($conn, "DELETE FROM classic_leagues WHERE team_id = " . $teamId);
foreach($history->leagues->classic as $value) {
mysqli_query($conn, "INSERT INTO classic_leagues (entry_rank, entry_last_rank, entry_movement, entry_change, name, league_type, team_id) values('" . $value->entry_rank . "', '". $value->entry_last_rank . "', '" . $value->entry_movement . "', '" . $value->entry_change . "', '" . $value->name . "', '" . $value->league_type . "', '" . $teamId . "')");
}
}
$queryCheck = mysqli_query($conn, "SELECT gameweek_number from user_gameweek_picks where user_fpl_id = " . $teamId . " AND gameweek_number=" . $current_event);
$resultCheck = mysqli_fetch_array($queryCheck);
if($resultCheck['gameweek_number'] != $current_event) {
header('Location: playerStats.php');
}
$query = mysqli_query($conn, "SELECT * FROM fpl_user_data where fpl_user_data_id = " . $teamId);
$teamData = mysqli_fetch_array($query);
$query = mysqli_query($conn, "select sum(points) totalPoints, p.web_name
from user_gameweek_picks ugp
join fpl_user_data fud on fud.fpl_user_data_id = ugp.user_fpl_id
join players p on p.id = ugp.player_code
where has_played = 1
and ugp.user_fpl_id = " . $teamId . "
group by web_name
order by sum(points) desc
limit 0, 10");
while($row = mysqli_fetch_array($query)) {
$topPerformers[$row['web_name']] = $row['totalPoints'];
}
$query = mysqli_query($conn, "select sum(points) totalPoints, p.web_name from user_gameweek_picks ugp
join fpl_user_data fud on fud.fpl_user_data_id = ugp.user_fpl_id
join players p on p.id = ugp.player_code
where has_played = 1
and ugp.user_fpl_id = " . $teamId . "
group by web_name
order by sum(points) ASC
limit 0, 10");
while($row = mysqli_fetch_array($query)) {
$lowPerformers[$row['web_name']] = $row['totalPoints'];
}
$query = mysqli_query($conn, "SELECT distinct player_code, sum(ugp.points) total_points, p.web_name, et.name as elementTypeName
from user_gameweek_picks ugp
join players p on p.id = ugp.player_code
join element_types et on et.id = p.element_type
where user_fpl_id = " . $teamId . "
group by ugp.player_code
order by sum(ugp.points) desc");
$table = '<table id="gameweekPlayers" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Total points</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Total points</th>
</tr>
</tfoot>
<tbody>';
while($row = mysqli_fetch_array($query)) {
$table .= "<tr>
<td>" . $row['web_name'] . "</td>
<td>" . $row['elementTypeName'] . "</td>
<td>" . $row['total_points'] . "</td>
</tr>";
}
$table .= "</tbody></table>";
$query = mysqli_query($conn, "SELECT distinct player_code, sum(ugp.points) total_points, p.web_name, et.name as elementTypeName
from user_gameweek_picks ugp
join players p on p.id = ugp.player_code
join element_types et on et.id = p.element_type
where user_fpl_id = " . $teamId . "
AND is_captain = 1
group by ugp.player_code
order by sum(ugp.points) desc");
$captainsTable = '<table id="gameweekCaptains" class="table table-striped table-bordered" cellspacing="0" width="100%">
<thead>
<tr>
<th>Name</th>
<th>Position</th>
<th>Total points</th>
</tr>
</thead>
<tfoot>
<tr>
<th>Name</th>
<th>Position</th>
<th>Total points</th>
</tr>
</tfoot>
<tbody>';
while($row = mysqli_fetch_array($query)) {
$captainsTable .= "<tr>
<td>" . $row['web_name'] . "</td>
<td>" . $row['elementTypeName'] . "</td>
<td>" . $row['total_points'] . "</td>
</tr>";
}
$captainsTable .= "</tbody></table>";
?>
<div class="row tile_count">
<div class="col-md-4 col-sm-4 col-xs-6 team_name">
<div class="count_top">Team Name</div>
<div class="count"><?php echo $teamData['team_name']; ?></div>
</div>
<div class="col-md-2 col-sm-4 col-xs-6 tile_stats_count bg_yellow">
<div class="count_top">Overall Points</div>
<div class="count"><?php echo number_format($teamData['summary_overall_points']); ?></div>
</div>
<div class="col-md-2 col-sm-4 col-xs-6 tile_stats_count bg_green">
<div class="count_top">Overall Rank</div>
<div class="count"><?php echo number_format($teamData['summary_overall_rank']); ?></div>
</div>
<div class="col-md-2 col-sm-4 col-xs-6 tile_stats_count bg_red">
<div class="count_top">Gameweek Points</div>
<div class="count green"><?php echo number_format($teamData['summary_event_points']); ?></div>
</div>
<div class="col-md-2 col-sm-4 col-xs-6 tile_stats_count bg_aqua">
<div class="count_top">Gameweek Rank</div>
<div class="count"><?php echo number_format($teamData['summary_event_rank']); ?></div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-md-6 col-sm-4 col-xs-6 gameweekPlayersTable">
<div class="count_top">Players You selected from Gameweek 1</div>
<div class="panel-body"><?php echo $table; ?></div>
</div>
<div class="col-md-6 col-sm-4 col-xs-6 gameweekPlayersTable">
<div class="count_top">Captains you selected from Gameweek 1</div>
<div class="panel-body"><?php echo $captainsTable; ?></div>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-xs-12 col-md-6 leftContainerRightPadding">
<div class="panel panel-default chart-container">
<div class="panel-heading">Your Top Performers</div>
<div class="panel-body">
</div>
<canvas id='top-performers-canvas' width='535' height='300'></canvas>
</div>
</div>
<div class="col-xs-12 col-md-6 RightContainerLeftPadding">
<div class="panel panel-default chart-container">
<div class="panel-heading">Your Low Performers</div>
<div class="panel-body">
</div>
<canvas id='low-performers-canvas' width='535' height='300'></canvas>
</div>
</div>
</div>
</div>
</div>
<footer>
<center>
<div class="devunit">
Made with <span class="love"><i class="glyphicon glyphicon-heart"></i></span> by <a href="//milanchheda.com" target="_BLANK">Milan Chheda</a>
</div>
</center>
</footer>
<script type="text/javascript">
$(document).ready(function() {
$('#gameweekPlayers, #gameweekCaptains').DataTable({
fixedHeader: true,
order: [[2, 'desc']],
});
});
generatePointsChart(<?php echo json_encode($topPerformers, JSON_NUMERIC_CHECK); ?>, 'top-performers-canvas', '#1ABB9C');
generatePointsChart(<?php echo json_encode($lowPerformers, JSON_NUMERIC_CHECK); ?>, 'low-performers-canvas', '#E74C3C')
</script>