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
|
|
|
$this->earnings->put($date,$earning); |
12
|
|
|
if (($this->earliest != NULL && $date < $this->earliest) || $this->earliest == NULL) { |
13
|
|
|
$this->earliest = $date; |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
public function sort() { |
17
|
|
|
$this->earnings->sortBy(function($role){return $role;}); |
18
|
|
|
} |
19
|
|
|
public function getEarningForDate($date) { |
20
|
|
|
//No data yet |
21
|
|
|
if(count($this->earnings)==0) { |
22
|
|
|
return 0; |
23
|
|
|
} |
24
|
|
|
//Data exists |
25
|
|
|
if($this->earnings->contains($date)) { |
26
|
|
|
return $this->earnings->get($date); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
//Before earliest data |
30
|
|
|
//if ($this->earliest != NULL && $date < $this->earliest) { |
|
|
|
|
31
|
|
|
// return 0; |
32
|
|
|
//} |
33
|
|
|
|
34
|
|
|
//Gap somewhere. Iterate through earnings and find the |
35
|
|
|
//Assume sorted |
36
|
|
|
$retval = 0; |
37
|
|
|
foreach ($this->earnings->keys() as $checkdate) { |
38
|
|
|
if ($checkdate > $date) { |
39
|
|
|
return $retval; //Return the entry before this |
40
|
|
|
} |
41
|
|
|
$retval = $this->earnings->get($checkdate); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
//? |
45
|
|
|
return $retval; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
class LeagueController extends PageController { |
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Leagues list page |
53
|
|
|
*/ |
54
|
1 |
|
public function index() { |
55
|
1 |
|
$search = Input::all(); |
56
|
|
|
|
57
|
|
|
// Generate seasons array, also default season. |
58
|
1 |
|
$seasons_list = []; |
59
|
1 |
|
$seasons = Config::get('draft.seasons'); |
60
|
1 |
|
$now = Carbon::now(); |
61
|
|
|
|
62
|
1 |
|
if (isset($search['season']) && ! array_key_exists($search['season'], $seasons) && $search['season'] != 0) { |
63
|
|
|
unset($search['season']); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
foreach ($seasons as $key => $info) { |
67
|
1 |
|
$seasons_list[$key] = $info['name']; |
68
|
1 |
|
if (! isset($search['season'])) { |
69
|
1 |
|
$start = call_user_func_array(['Carbon', 'create'], $info['start']); |
70
|
1 |
|
$end = call_user_func_array(['Carbon', 'create'], $info['end']); |
71
|
1 |
|
if ($now->between($start, $end)) { |
72
|
1 |
|
$search['season'] = $key; |
73
|
1 |
|
} |
74
|
1 |
|
} |
75
|
1 |
|
} |
76
|
|
|
// Generate years array, also default year |
77
|
1 |
|
$years_list = range($now->year + 1, Config::get('draft.earliest_year'), -1); |
78
|
1 |
|
$years_list = array_combine($years_list, $years_list); |
79
|
1 |
|
if (! isset($search['year']) || ! isset($years_list[$search['year']])) { |
80
|
1 |
|
$search['year'] = $now->year; |
81
|
1 |
|
} |
82
|
1 |
|
if(! isset($search['inactive'])) { |
83
|
1 |
|
$search['inactive'] = false; |
84
|
1 |
|
} |
85
|
|
|
|
86
|
|
|
// Generate the query |
87
|
|
|
/** @type League|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $leagues_query */ |
88
|
1 |
|
$leagues_query = League::query(); |
89
|
|
|
|
90
|
1 |
|
$leagues_query->where('private', 0); |
|
|
|
|
91
|
|
|
|
92
|
|
|
// Season |
93
|
1 |
|
if ($search['season'] != 0) { |
94
|
1 |
|
$leagues_query->season($search['year'], $search['season']); |
|
|
|
|
95
|
1 |
|
} |
96
|
|
|
// Active |
97
|
1 |
|
$leagues_query->where('active', !$search['inactive']); |
|
|
|
|
98
|
|
|
|
99
|
|
|
|
100
|
1 |
|
$leagues = $leagues_query->paginate(10); |
|
|
|
|
101
|
|
|
|
102
|
|
|
// Output |
103
|
1 |
|
$this->layout->title = 'Leagues'; |
104
|
1 |
|
$this->layout->content = View::make('league.index', [ |
105
|
1 |
|
'leagues' => $leagues, |
106
|
1 |
|
'seasons' => $seasons_list, |
107
|
1 |
|
'years' => $years_list, |
108
|
1 |
|
'search' => $search, |
109
|
1 |
|
]); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Show leagues related to the current user |
114
|
|
|
*/ |
115
|
3 |
|
public function mine() { |
116
|
|
|
/** @type League|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $leagues_query */ |
117
|
3 |
|
$leagues_query = League::query(); |
118
|
|
|
|
119
|
|
|
// Where the user is a player |
120
|
|
|
$leagues_query->whereExists(function (\Illuminate\Database\Query\Builder $query) { |
|
|
|
|
121
|
3 |
|
$query->select([DB::raw(1)]) |
122
|
3 |
|
->from('league_teams') |
123
|
3 |
|
->join('league_team_user', 'league_teams.id', '=', 'league_team_user.league_team_id') |
124
|
3 |
|
->where('league_team_user.user_id', Auth::user()->id) |
125
|
3 |
|
->whereRaw('league_teams.league_id = leagues.id'); |
126
|
3 |
|
}); |
127
|
|
|
|
128
|
|
|
// Where the user is an admin |
129
|
|
|
$leagues_query->orWhereExists(function (\Illuminate\Database\Query\Builder $query) { |
|
|
|
|
130
|
3 |
|
$query->select([DB::raw(1)]) |
131
|
3 |
|
->from('league_admins') |
132
|
3 |
|
->where('league_admins.user_id', Auth::user()->id) |
133
|
3 |
|
->whereRaw('league_admins.league_id = leagues.id'); |
134
|
3 |
|
}); |
135
|
|
|
|
136
|
3 |
|
$leagues_query->orderBy('start_date', 'desc'); |
|
|
|
|
137
|
3 |
|
$leagues_query->select('leagues.*'); |
|
|
|
|
138
|
3 |
|
$leagues = $leagues_query->paginate(10); |
|
|
|
|
139
|
|
|
|
140
|
|
|
// Output |
141
|
3 |
|
$this->layout->title = 'My Leagues'; |
142
|
3 |
|
$this->layout->content = View::make('league.mine', compact('leagues')); |
143
|
3 |
|
} |
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
|
|
|
'validation_rules' => $this->league_valid_rules, |
168
|
|
|
]); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* League creation |
173
|
|
|
*/ |
174
|
|
|
public function store() { |
175
|
|
|
$validator = Validator::make(Input::all(), $this->league_valid_rules); |
176
|
|
|
if ($validator->fails()) { |
177
|
|
|
Notification::error('Whoops, something is wrong with your input. Check your errors and try again.'); |
178
|
|
|
|
179
|
|
|
return Redirect::route('league.create')->withInput()->withErrors($validator); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// Create the league |
183
|
|
|
$league = new League(Input::only([ |
184
|
|
|
'name', 'description', 'url', 'money', 'units' |
185
|
|
|
])); |
186
|
|
|
$league->private = Input::get('private') ? true : false; |
187
|
|
|
$league->mode = 'bid'; |
188
|
|
|
$league->extra_weeks = Input::get('extra_weeks'); |
189
|
|
|
$league->start_date = $league->end_date = Carbon::now()->addWeeks(Config::get('draft.maximum_weeks')); |
190
|
|
|
|
191
|
|
|
if ($league->save()) { |
192
|
|
|
// Attach current user as league admin |
193
|
|
|
$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
|
1 |
|
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
|
1 |
|
$query->ordered(); |
|
|
|
|
214
|
1 |
|
}, 'teams.movies.movie', 'teams.movies.latestEarnings']); |
215
|
|
|
|
216
|
|
|
// Pre-do some work for teams and sort them by earnings |
217
|
1 |
|
$teams = new Collection(); |
218
|
|
|
|
219
|
1 |
|
foreach ($league->teams as $team) { |
220
|
|
|
$earnings = $team->movies->reduce(function ($total, LeagueMovie $movie) { |
221
|
1 |
|
if ($movie->latestEarnings) { |
222
|
1 |
|
return $total + $movie->latestEarnings->domestic; |
223
|
|
|
} else { |
224
|
|
|
return $total; |
225
|
|
|
} |
226
|
1 |
|
}, 0); |
227
|
|
|
$paid_for = $team->movies->reduce(function ($total, LeagueMovie $movie) { |
228
|
1 |
|
return $total + $movie->price; |
229
|
1 |
|
}, 0); |
230
|
1 |
|
$teams->push(compact('team', 'earnings', 'paid_for')); |
231
|
1 |
|
} |
232
|
1 |
|
$teams->sortByDesc('earnings'); |
233
|
|
|
|
234
|
1 |
|
$this->layout->content = View::make('league.show', compact('league', 'teams')); |
235
|
1 |
|
$this->layout->content->show_league_info = true; |
236
|
1 |
|
} |
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.