You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.0 KiB

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB, Twitter;
use \App\Player, \App\Team;
class TeamController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
private static function colors() {
return [
'e6194b' => 'Red',
'3cb44b' => 'Green',
'ffe119' => 'Yellow',
'0082c8' => 'Blue',
'f58231' => 'Orange',
'911eb4' => 'Purple',
'46f0f0' => 'Cyan',
'f032e6' => 'Magenta',
'd2f53c' => 'Lime',
'fabebe' => 'Pink',
'008080' => 'Teal',
'e6beff' => 'Lavender',
'aa6e28' => 'Brown',
'fffac8' => 'Beige',
'800000' => 'Maroon',
'aaffc3' => 'Mint',
'808000' => 'Olive',
'ffd8b1' => 'Coral',
'000080' => 'Navy',
'808080' => 'Grey',
];
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$teams = Team::all();
$unassigned = Player::where('team_id', 0)->get();
return view('teams', [
'teams' => $teams,
'unassigned' => $unassigned,
]);
}
public function create_team()
{
$this->authorize('admin');
// Get list of current used colors
$teams = DB::table('teams')->pluck('name')->toArray();
$color_names = array_values(self::colors());
$available = array_diff($color_names, $teams);
if(count($available)) {
$next = array_shift($available);
$colors = array_flip(self::colors());
$team = new \App\Team;
$team->name = $next;
$team->slug = strtolower($next);
$team->color = $colors[$next];
$team->save();
return $next;
} else {
return "";
}
}
public function add_player(Request $request)
{
$this->authorize('admin');
try {
// Look up the user ID
$profile = Twitter::getUsers(['screen_name' => trim($request->input('twitter'), '@')]);
// Follow the specified user
Twitter::postFollow(['user_id' => $profile->id_str]);
$player = Player::where('twitter_user_id', $profile->id_str)->first();
if(!$player) {
$player = new Player;
$player->twitter_user_id = $profile->id;
}
$player->team_id = $request->input('team');
$player->name = $profile->name;
$player->twitter = $profile->screen_name;
$player->photo = $profile->profile_image_url_https;
$player->save();
return response()->json(['player'=>$player]);
} catch(\Exception $e) {
return response()->json(['error'=>$e->getMessage()]);
}
}
}