Skip to content

Commit

Permalink
Movies can now be given to players. Just generic settings page left! …
Browse files Browse the repository at this point in the history
…(ticks #5)
  • Loading branch information
t2t2 committed Apr 6, 2013
1 parent 0081c0f commit 84ec37b
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 6 deletions.
1 change: 1 addition & 0 deletions app/config/draft.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/
"league_defaults" => array(
"mode" => "bid",
"money" => 100,
"units" => "",
"movies" => array(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),
)
Expand Down
82 changes: 82 additions & 0 deletions app/controllers/LeagueController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function getList() {
"description" => "required",
"url" => "url",

"money" => "required|integer",
"units" => "required|max:16",
);

Expand All @@ -39,6 +40,7 @@ public function postCreate() {
$league->url = Input::get("url");

$league->mode = Config::get("draft.league_defaults.mode");
$league->money = Input::get("money");
$league->units = Input::get("units");
$league->end_date = "2013-09-20";
if($league->save()) {
Expand Down Expand Up @@ -84,6 +86,9 @@ public function getView($leagueID, $leagueSlug = '') {
}

/* Admin functions */
// League settings

// Users
public function getAdminUsers($leagueID, $leagueSlug = '') {
if(!($league = League::with('users')->find($leagueID))) {
App::abort(404);
Expand Down Expand Up @@ -181,4 +186,81 @@ public function postAdminUsers($leagueID, $leagueSlug = '') {
// Fallback
return Redirect::action("LeagueController@getAdminUsers", array($league->id, $league->slug))->withInput();
}
// Movies
public function getAdminMovies($leagueID, $leagueSlug = '') {
if(!($league = League::with("users", "movies")->find($leagueID))) {
App::abort(404);
}
if(!$league->userIsAdmin(Auth::user())) {
App::abort(404);
}

$league->movies->load("users");
// Preformatted for your satisfaction
$players = array_merge(array(array("id" => 0, "username" => "- Nobody -")), $league->players->map(function($player) {
return array("id" => $player->id, "username" => $player->username);
}));

$this->layout->content = View::make("league.admin.movies", array(
"league" => $league, "players" => $players,
));
}
public function postAdminMovies($leagueID, $leagueSlug = '') {
if(!($league = League::with("users", "movies")->find($leagueID))) {
App::abort(404);
}
if(!$league->userIsAdmin(Auth::user())) {
App::abort(404);
}

$league->movies->load("users");
$input = Input::get("movies");

$errors = array();
$changes = 0;

foreach ($league->movies as $movie) {
// Bought for = $movie->pivot->price
$_price = $input[$movie->id]["price"] ?: 0;
if(filter_var($_price, FILTER_VALIDATE_INT) !== false) {
if($movie->pivot->price != $_price) {
$movie->pivot->price = $_price;
$movie->pivot->save();
$changes++;
}
} else {
$errors[] = 'Movie '.e($movie->name).' was bought for an invalid price.';
}
// User = $movie->users[0]
$_player = $input[$movie->id]["player"] ?: 0;
if($_player == 0 or $league->players->find($_player)) {
if(isset($movie->users[0])) {
$currowner = $movie->users[0]->id;
} else {
$currowner = 0;
}
if($_player != $currowner) {
if($currowner == 0) {
$movie->users()->attach($_player, array("league_id" => $league->id));
} else {
$query = DB::table('league_movie_user')->whereLeagueId($league->id)->whereMovieId($movie->id);
if($_player) {
$query->update(array("user_id" => $_player));
} else {
$query->delete();
}
}
$changes++;
}
} else {
$errors[] = 'Movie '.e($movie->name).' was bought by an UFO.';
}
}
Notification::success("{$changes} changes saved!");
if(count($errors) > 0) {
Notification::warning("The following errors occured, and were not processed:<ul><li>".implode("</li><li>", $errors)."</li></ul>");
}

return Redirect::action("LeagueController@getAdminMovies", array($league->id, $league->slug));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddLeagueBuyingMoney extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('leagues', function(Blueprint $table)
{
$table->integer('money')->default(Config::get("draft.league_defaults.money"))->after('mode');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('leagues', function($table)
{
$table->dropColumn('money');
});
}

}
1 change: 1 addition & 0 deletions app/database/seeds/LeagueSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function run() {
EOL;
$leagueData['url'] = 'http://draft.nsfwshow.com/';
$leagueData['mode'] = 'bid';
$leagueData['money'] = 100;
$leagueData['units'] = '';
$leagueData['end_date'] = '2013-09-20';

Expand Down
7 changes: 7 additions & 0 deletions app/models/Movie.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
<?php

class Movie extends Eloquent {
public function users() {
// Only if called via league
if(!$this->pivot or $this->pivot->getTable() != "league_movie") {
return $this->belongsToMany('User', 'league_movie_user');
}
return $this->belongsToMany('User', 'league_movie_user')->withPivot('league_id')->where('league_id', $this->pivot->league_id);
}

/* Accessors & Mutators (aka. fancy words for getters and setters) */

Expand Down
3 changes: 3 additions & 0 deletions app/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@
'uses' => 'LeagueController@postCreate'
));
Route::get('league/{id}-{slug?}', 'LeagueController@getView');

Route::get('league/{id}-{slug?}/admin/users', 'LeagueController@getAdminUsers');
Route::post('league/{id}-{slug?}/admin/users', 'LeagueController@postAdminUsers');
Route::get('league/{id}-{slug?}/admin/movies', 'LeagueController@getAdminMovies');
Route::post('league/{id}-{slug?}/admin/movies', 'LeagueController@postAdminMovies');

8 changes: 4 additions & 4 deletions app/views/layout/league.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<li{{ Route::currentRouteAction() == "LeagueController@getView" ? ' class="active"' : null }}><a href="{{ action("LeagueController@getView", array("id" => $league->id, "slug" => $league->slug)) }}">Home</a></li>

@if($league->userIsAdmin(Auth::user()))
<li class="dropdown pull-right">
<li class="dropdown pull-right{{ Request::is('league/*-*/admin/*') ? ' active' : null }}">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">Admin <b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="#">Settings</a></li>
<li><a href="{{ action("LeagueController@getAdminUsers", array("id" => $league->id, "slug" => $league->slug)) }}">Users</a></li>
<li><a href="#">Movies</a></li>
<li{{ Request::is('league/*-*/admin/settings') ? ' class="active"' : null }}><a href="#">Settings</a></li>
<li{{ Request::is('league/*-*/admin/users') ? ' class="active"' : null }}><a href="{{ action("LeagueController@getAdminUsers", array("id" => $league->id, "slug" => $league->slug)) }}">Users</a></li>
<li{{ Request::is('league/*-*/admin/movies') ? ' class="active"' : null }}><a href="{{ action("LeagueController@getAdminMovies", array("id" => $league->id, "slug" => $league->slug)) }}">Movies</a></li>
</ul>
</li>
@endif
Expand Down
28 changes: 28 additions & 0 deletions app/views/league/admin/movies.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@extends("layout.league")

@section("content")
<h2>Movies</h2>
{{ Former::open()->populate($league) }}
{{ Former::token() }}
<table class="table">
<thead>
<tr>
<th>Movie</th>
<th>Bought for</th>
<th>Player</th>
</tr>
</thead>
<tbody>
@foreach($league->movies as $i => $movie)
<tr>
<th>{{{ $movie->name }}}</th>
<td><div class="input-prepend"><span class="add-on">{{{ $league->units }}}</span>{{ Former::small_text("movies[{$movie->id}][price]")->raw()->value($movie->pivot->price)->pattern('[+-]?\d*\.?\d+') }}</div></td>
<td>{{ Former::select("movies[{$movie->id}][player]")->raw()->fromQuery($players, "username")->select(isset($movie->users[0]) ? $movie->users[0]->id : 0) }}</td>
</tr>
@endforeach
</tbody>
</table>
{{ Former::primary_submit("Save") }}
{{ Former::close() }}

@stop
1 change: 1 addition & 0 deletions app/views/league/create.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{{ Former::xxlarge_text("url") }}
{{ Former::legend("League Settings") }}
<p>Note: During alpha you can only copy rules of the NSFWshow Movie Draft..... Well and modify these settings:</p>
{{ Former::text("money")->value(Config::get("draft.league_defaults.money")) }}
{{ Former::text("units")->value(Config::get("draft.league_defaults.units")) }}
{{ Former::actions()->primary_submit('Submit')->reset('Reset') }}
{{ Former::close() }}
5 changes: 3 additions & 2 deletions app/views/league/view.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@foreach($league->players as $player)
<li class="player">
<h3>{{{ $player->displayname }}}</h3>
<span class="player-money pull-right">Total: $0 <small class="muted">for {{ e($league->units).' '.$player->buytotal }}</small></span>
<span class="player-money pull-right">Total: $0 <small class="muted">for {{ e($league->units).' '.$player->buytotal.' / '.$league->units.' '.$league->money }}</small></span>
@if(count($player->movies) > 0)
<?php
$movieNames = $player->movies->map(function($movie) use($league) {
Expand Down Expand Up @@ -42,12 +42,13 @@
@endif
<h3>League Settings</h3>
<p><span class="muted">End date:</span> {{{ $league->end_date->format("F j, Y") }}}</p>
<p><span class="muted">Buying money:</span> {{{ $league->units.' '.$league->money }}}</p>
<h3>Movies</h3>
<ul>
<?php $now = new DateTime('now'); $printedUpcoming = false; ?>
@foreach($league->movies as $movie)
<?php
if($now < $movie->release && !$printedUpcoming) {
if($now < $movie->release && !$printedUpcoming) {
echo '</ul><div class="upcomingSeparator"><div></div><span>Upcoming</span></div><ul>';
$printedUpcoming = true;
}
Expand Down

0 comments on commit 84ec37b

Please sign in to comment.