|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Support\Collection; |
|
4
|
|
|
class GraphMovie { |
|
|
|
|
|
|
5
|
|
|
public $earnings; |
|
6
|
|
|
private $earliest = NULL; |
|
7
|
|
|
public function __construct() { |
|
8
|
|
|
$this->earnings = new Collection(); |
|
9
|
|
|
} |
|
10
|
|
|
public function addEarning($date,$earning) { |
|
11
|
1 |
|
$this->earnings->put($date,$earning); |
|
12
|
1 |
|
if (($this->earliest != NULL && $date < $this->earliest) || $this->earliest == NULL) { |
|
13
|
|
|
$this->earliest = $date; |
|
14
|
|
|
} |
|
15
|
1 |
|
} |
|
16
|
1 |
|
public function sort() { |
|
17
|
1 |
|
$this->earnings->sortBy(function($role){return $role;}); |
|
18
|
|
|
} |
|
19
|
1 |
|
public function getEarningForDate($date) { |
|
20
|
|
|
//No data yet |
|
21
|
|
|
if(count($this->earnings)==0) { |
|
22
|
|
|
return 0; |
|
23
|
1 |
|
} |
|
24
|
1 |
|
//Data exists |
|
25
|
1 |
|
if($this->earnings->contains($date)) { |
|
26
|
1 |
|
return $this->earnings->get($date); |
|
27
|
1 |
|
} |
|
28
|
1 |
|
|
|
29
|
1 |
|
//Before earliest data |
|
30
|
1 |
|
//if ($this->earliest != NULL && $date < $this->earliest) { |
|
|
|
|
|
|
31
|
1 |
|
// return 0; |
|
32
|
1 |
|
//} |
|
33
|
|
|
|
|
34
|
1 |
|
//Gap somewhere. Iterate through earnings and find the |
|
35
|
1 |
|
//Assume sorted |
|
36
|
1 |
|
$retval = 0; |
|
37
|
1 |
|
foreach ($this->earnings->keys() as $checkdate) { |
|
38
|
1 |
|
if ($checkdate > $date) { |
|
39
|
1 |
|
return $retval; //Return the entry before this |
|
40
|
1 |
|
} |
|
41
|
1 |
|
$retval = $this->earnings->get($checkdate); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
//? |
|
45
|
1 |
|
return $retval; |
|
46
|
|
|
} |
|
47
|
1 |
|
} |
|
48
|
|
|
class LeagueController extends PageController { |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
1 |
|
|
|
51
|
1 |
|
/** |
|
52
|
1 |
|
* Leagues list page |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function index() { |
|
55
|
|
|
$search = Input::all(); |
|
56
|
|
|
|
|
57
|
1 |
|
// Generate seasons array, also default season. |
|
58
|
|
|
$seasons_list = []; |
|
59
|
|
|
$seasons = Config::get('draft.seasons'); |
|
60
|
1 |
|
$now = Carbon::now(); |
|
61
|
1 |
|
|
|
62
|
1 |
|
if (isset($search['season']) && ! array_key_exists($search['season'], $seasons) && $search['season'] != 0) { |
|
63
|
1 |
|
unset($search['season']); |
|
64
|
1 |
|
} |
|
65
|
1 |
|
|
|
66
|
1 |
|
foreach ($seasons as $key => $info) { |
|
67
|
1 |
|
$seasons_list[$key] = $info['name']; |
|
68
|
|
|
if (! isset($search['season'])) { |
|
69
|
|
|
$start = call_user_func_array(['Carbon', 'create'], $info['start']); |
|
70
|
|
|
$end = call_user_func_array(['Carbon', 'create'], $info['end']); |
|
71
|
|
|
if ($now->between($start, $end)) { |
|
72
|
3 |
|
$search['season'] = $key; |
|
73
|
|
|
} |
|
74
|
3 |
|
} |
|
75
|
|
|
} |
|
76
|
|
|
// Generate years array, also default year |
|
77
|
|
|
$years_list = range($now->year + 1, Config::get('draft.earliest_year'), -1); |
|
78
|
3 |
|
$years_list = array_combine($years_list, $years_list); |
|
79
|
3 |
|
if (! isset($search['year']) || ! isset($years_list[$search['year']])) { |
|
80
|
3 |
|
$search['year'] = $now->year; |
|
81
|
3 |
|
} |
|
82
|
3 |
|
if(! isset($search['inactive'])) { |
|
83
|
3 |
|
$search['inactive'] = false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
// Generate the query |
|
87
|
3 |
|
/** @type League|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $leagues_query */ |
|
88
|
3 |
|
$leagues_query = League::query(); |
|
89
|
3 |
|
|
|
90
|
3 |
|
$leagues_query->where('private', 0); |
|
|
|
|
|
|
91
|
3 |
|
|
|
92
|
|
|
// Season |
|
93
|
3 |
|
if ($search['season'] != 0) { |
|
94
|
3 |
|
$leagues_query->season($search['year'], $search['season']); |
|
|
|
|
|
|
95
|
3 |
|
} |
|
96
|
|
|
// Active |
|
97
|
|
|
$leagues_query->where('active', !$search['inactive']); |
|
|
|
|
|
|
98
|
3 |
|
|
|
99
|
3 |
|
|
|
100
|
3 |
|
$leagues = $leagues_query->paginate(10); |
|
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
// Output |
|
103
|
|
|
$this->layout->title = 'Leagues'; |
|
104
|
|
|
$this->layout->content = View::make('league.index', [ |
|
105
|
|
|
'leagues' => $leagues, |
|
106
|
|
|
'seasons' => $seasons_list, |
|
107
|
|
|
'years' => $years_list, |
|
108
|
|
|
'search' => $search, |
|
109
|
|
|
]); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Show leagues related to the current user |
|
114
|
|
|
*/ |
|
115
|
|
|
public function mine() { |
|
116
|
|
|
/** @type League|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $leagues_query */ |
|
117
|
|
|
$leagues_query = League::query(); |
|
118
|
|
|
|
|
119
|
|
|
// Where the user is a player |
|
120
|
|
|
$leagues_query->whereExists(function (\Illuminate\Database\Query\Builder $query) { |
|
|
|
|
|
|
121
|
|
|
$query->select([DB::raw(1)]) |
|
122
|
|
|
->from('league_teams') |
|
123
|
|
|
->join('league_team_user', 'league_teams.id', '=', 'league_team_user.league_team_id') |
|
124
|
|
|
->where('league_team_user.user_id', Auth::user()->id) |
|
125
|
|
|
->whereRaw('league_teams.league_id = leagues.id'); |
|
126
|
|
|
}); |
|
127
|
|
|
|
|
128
|
|
|
// Where the user is an admin |
|
129
|
|
|
$leagues_query->orWhereExists(function (\Illuminate\Database\Query\Builder $query) { |
|
|
|
|
|
|
130
|
|
|
$query->select([DB::raw(1)]) |
|
131
|
|
|
->from('league_admins') |
|
132
|
|
|
->where('league_admins.user_id', Auth::user()->id) |
|
133
|
|
|
->whereRaw('league_admins.league_id = leagues.id'); |
|
134
|
|
|
}); |
|
135
|
|
|
|
|
136
|
|
|
$leagues_query->orderBy('start_date', 'desc'); |
|
|
|
|
|
|
137
|
|
|
$leagues_query->select('leagues.*'); |
|
|
|
|
|
|
138
|
|
|
$leagues = $leagues_query->paginate(10); |
|
|
|
|
|
|
139
|
|
|
|
|
140
|
|
|
// Output |
|
141
|
|
|
$this->layout->title = 'My Leagues'; |
|
142
|
|
|
$this->layout->content = View::make('league.mine', compact('leagues')); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* New league validation rules |
|
147
|
|
|
* @var array |
|
148
|
|
|
*/ |
|
149
|
|
|
public $league_valid_rules = [ |
|
150
|
|
|
'name' => ['required', 'max:255'], |
|
151
|
|
|
'description' => ['required'], |
|
152
|
|
|
'url' => ['url'], |
|
153
|
|
|
'private' => ['boolean'], |
|
154
|
|
|
|
|
155
|
|
|
'money' => ['required', 'integer'], |
|
156
|
|
|
'units' => ['required', 'max:16'], |
|
157
|
|
|
'extra_weeks' => ['required', 'integer', 'between:1,12'], |
|
158
|
|
|
]; |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* League creation form |
|
162
|
|
|
*/ |
|
163
|
|
|
public function create() { |
|
164
|
|
|
|
|
165
|
|
|
$this->layout->title = 'Create league'; |
|
166
|
|
|
$this->layout->content = View::make('league.create', [ |
|
167
|
1 |
|
'validation_rules' => $this->league_valid_rules, |
|
168
|
|
|
]); |
|
169
|
|
|
} |
|
170
|
1 |
|
|
|
171
|
1 |
|
/** |
|
172
|
|
|
* League creation |
|
173
|
|
|
*/ |
|
174
|
1 |
|
public function store() { |
|
175
|
|
|
$validator = Validator::make(Input::all(), $this->league_valid_rules); |
|
176
|
1 |
|
if ($validator->fails()) { |
|
177
|
|
|
Notification::error('Whoops, something is wrong with your input. Check your errors and try again.'); |
|
178
|
1 |
|
|
|
179
|
1 |
|
return Redirect::route('league.create')->withInput()->withErrors($validator); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// Create the league |
|
183
|
1 |
|
$league = new League(Input::only([ |
|
184
|
|
|
'name', 'description', 'url', 'money', 'units' |
|
185
|
1 |
|
])); |
|
186
|
1 |
|
$league->private = Input::get('private') ? true : false; |
|
187
|
1 |
|
$league->mode = 'bid'; |
|
188
|
1 |
|
$league->extra_weeks = Input::get('extra_weeks'); |
|
189
|
1 |
|
$league->start_date = $league->end_date = Carbon::now()->addWeeks(Config::get('draft.maximum_weeks')); |
|
190
|
|
|
|
|
191
|
1 |
|
if ($league->save()) { |
|
192
|
1 |
|
// Attach current user as league admin |
|
193
|
1 |
|
$league->admins()->attach(Auth::user()); |
|
194
|
|
|
|
|
195
|
|
|
Notification::success('Your league has been created.'); |
|
196
|
|
|
|
|
197
|
|
|
return Redirect::route('league.show', ['league_slug' => $league->slug]); |
|
198
|
|
|
} else { |
|
199
|
|
|
Notification::error('Database error, please try again later.'); |
|
200
|
|
|
|
|
201
|
|
|
return Redirect::back()->withInput(); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* League page |
|
207
|
|
|
* |
|
208
|
|
|
* @param League $league |
|
209
|
|
|
*/ |
|
210
|
|
|
public function show(League $league) { |
|
211
|
|
|
$league->load(['teams', 'teams.users', 'teams.movies' => function($query) { |
|
212
|
|
|
/** @type LeagueMovie|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query */ |
|
213
|
|
|
$query->ordered(); |
|
|
|
|
|
|
214
|
|
|
}, 'teams.movies.movie', 'teams.movies.latestEarnings']); |
|
215
|
|
|
|
|
216
|
|
|
// Pre-do some work for teams and sort them by earnings |
|
217
|
|
|
$teams = new Collection(); |
|
218
|
|
|
|
|
219
|
|
|
foreach ($league->teams as $team) { |
|
220
|
|
|
$earnings = $team->movies->reduce(function ($total, LeagueMovie $movie) { |
|
221
|
|
|
if ($movie->latestEarnings) { |
|
222
|
|
|
return $total + $movie->latestEarnings->domestic; |
|
223
|
|
|
} else { |
|
224
|
|
|
return $total; |
|
225
|
|
|
} |
|
226
|
|
|
}, 0); |
|
227
|
|
|
$paid_for = $team->movies->reduce(function ($total, LeagueMovie $movie) { |
|
228
|
|
|
return $total + $movie->price; |
|
229
|
|
|
}, 0); |
|
230
|
|
|
$teams->push(compact('team', 'earnings', 'paid_for')); |
|
231
|
|
|
} |
|
232
|
|
|
$teams->sortByDesc('earnings'); |
|
233
|
|
|
|
|
234
|
|
|
$this->layout->content = View::make('league.show', compact('league', 'teams')); |
|
235
|
|
|
$this->layout->content->show_league_info = true; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
public function getChartData(League $league) { |
|
239
|
|
|
$complete = new Collection(); |
|
240
|
|
|
$possible_dates = new Collection(); |
|
241
|
|
|
$startdate = $league->start_date; |
|
|
|
|
|
|
242
|
|
|
$enddate = $league->end_date; |
|
243
|
|
|
$league->load(['teams.movies.movie.earnings' => function($query) { |
|
|
|
|
|
|
244
|
|
|
}])->where('movie_earnings.date','<=',$enddate); |
|
245
|
|
|
|
|
246
|
|
|
//Find all possible dates first |
|
247
|
|
|
foreach ($league->teams as $team) { |
|
248
|
|
|
foreach ($team->movies as $movie) { |
|
249
|
|
|
foreach ($movie->movie->earnings as $earning){ |
|
250
|
|
|
if($earning->date >= $league->start_date && $earning->date <= $league->end_date) { |
|
251
|
|
|
$possible_dates->push($earning->date->format("U")); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
$possible_dates = $possible_dates->unique(); |
|
257
|
|
|
$possible_dates->sortBy(function($role){return $role;}); |
|
258
|
|
|
|
|
259
|
|
|
//Go through the teams and populate the data |
|
260
|
|
|
foreach ($league->teams as $team) { |
|
261
|
|
|
$team_info = new Collection(); |
|
262
|
|
|
$team_earnings = new Collection(); |
|
263
|
|
|
$movies = new Collection(); |
|
264
|
|
|
$complete->push($team_info); |
|
265
|
|
|
$team_info->put("data",$team_earnings); |
|
266
|
|
|
$team_info->put("label",$team->name); |
|
267
|
|
|
$team_info->put("id",$team->id); |
|
268
|
|
|
|
|
269
|
|
|
//Fill actual data for movies now |
|
270
|
|
|
foreach ($team->movies as $movie) { |
|
271
|
|
|
foreach ($movie->movie->earnings as $earning){ |
|
272
|
|
|
$mydate = $earning->date->format("U"); |
|
273
|
|
|
if($possible_dates->contains($mydate)) { |
|
274
|
|
|
$movieinfo = $movies->get($movie->movie->id,new GraphMovie()); |
|
275
|
|
|
$movieinfo->addEarning($mydate,$earning->domestic); |
|
276
|
|
|
$movies->put($movie->movie->id,$movieinfo); |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
} |
|
280
|
|
|
//Sort movies so we can evaluate gaps |
|
281
|
|
|
foreach ($movies->values() as $movie) { |
|
282
|
|
|
$movie->sort(); |
|
283
|
|
|
} |
|
284
|
|
|
foreach ($possible_dates->values() as $date) { |
|
285
|
|
|
$item = new Collection(); |
|
286
|
|
|
$item->push($date * 1000); |
|
287
|
|
|
$movietotal = 0; |
|
288
|
|
|
foreach ($movies->values() as $movie) { |
|
289
|
|
|
$movietotal += $movie->getEarningForDate($date); |
|
290
|
|
|
} |
|
291
|
|
|
$item->push($movietotal); |
|
292
|
|
|
$team_earnings->push($item); |
|
293
|
|
|
} |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
return Response::json($complete); |
|
297
|
|
|
} |
|
298
|
|
|
|
|
299
|
|
|
/** |
|
300
|
|
|
* League display based by movies |
|
301
|
|
|
* |
|
302
|
|
|
* @param League $league |
|
303
|
|
|
*/ |
|
304
|
|
|
public function showMovies(League $league) { |
|
305
|
|
|
// Preload data |
|
306
|
|
|
$league->load(['movies' => function($query) { |
|
307
|
|
|
/** @type LeagueMovie|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query */ |
|
308
|
|
|
$query->ordered(); |
|
|
|
|
|
|
309
|
|
|
}, 'movies.movie', 'movies.latestEarnings', 'movies.teams']); |
|
310
|
|
|
|
|
311
|
|
|
$this->layout->content = View::make('league.show.movies', compact('league')); |
|
312
|
|
|
$this->layout->content->show_league_info = true; |
|
313
|
|
|
} |
|
314
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.