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

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use DB, Twitter;
  5. use \App\Player, \App\Team;
  6. class TeamController extends Controller
  7. {
  8. /**
  9. * Create a new controller instance.
  10. *
  11. * @return void
  12. */
  13. public function __construct()
  14. {
  15. $this->middleware('auth');
  16. }
  17. private static function colors() {
  18. return [
  19. 'e6194b' => 'Red',
  20. '3cb44b' => 'Green',
  21. 'ffe119' => 'Yellow',
  22. '0082c8' => 'Blue',
  23. 'f58231' => 'Orange',
  24. '911eb4' => 'Purple',
  25. '46f0f0' => 'Cyan',
  26. 'f032e6' => 'Magenta',
  27. 'd2f53c' => 'Lime',
  28. 'fabebe' => 'Pink',
  29. '008080' => 'Teal',
  30. 'e6beff' => 'Lavender',
  31. 'aa6e28' => 'Brown',
  32. 'fffac8' => 'Beige',
  33. '800000' => 'Maroon',
  34. 'aaffc3' => 'Mint',
  35. '808000' => 'Olive',
  36. 'ffd8b1' => 'Coral',
  37. '000080' => 'Navy',
  38. '808080' => 'Grey',
  39. ];
  40. }
  41. /**
  42. * Show the application dashboard.
  43. *
  44. * @return \Illuminate\Http\Response
  45. */
  46. public function index()
  47. {
  48. $teams = Team::all();
  49. $unassigned = Player::where('team_id', 0)->get();
  50. return view('teams', [
  51. 'teams' => $teams,
  52. 'unassigned' => $unassigned,
  53. ]);
  54. }
  55. public function create_team()
  56. {
  57. $this->authorize('admin');
  58. // Get list of current used colors
  59. $teams = DB::table('teams')->pluck('name')->toArray();
  60. $color_names = array_values(self::colors());
  61. $available = array_diff($color_names, $teams);
  62. if(count($available)) {
  63. $next = array_shift($available);
  64. $colors = array_flip(self::colors());
  65. $team = new \App\Team;
  66. $team->name = $next;
  67. $team->slug = strtolower($next);
  68. $team->color = $colors[$next];
  69. $team->save();
  70. return $next;
  71. } else {
  72. return "";
  73. }
  74. }
  75. public function add_player(Request $request)
  76. {
  77. $this->authorize('admin');
  78. try {
  79. // Look up the user ID
  80. $profile = Twitter::getUsers(['screen_name' => trim($request->input('twitter'), '@')]);
  81. // Follow the specified user
  82. Twitter::postFollow(['user_id' => $profile->id_str]);
  83. $player = Player::where('twitter_user_id', $profile->id_str)->first();
  84. if(!$player) {
  85. $player = new Player;
  86. $player->twitter_user_id = $profile->id;
  87. }
  88. $player->team_id = $request->input('team');
  89. $player->name = $profile->name;
  90. $player->twitter = $profile->screen_name;
  91. $player->photo = $profile->profile_image_url_https;
  92. $player->save();
  93. return response()->json(['player'=>$player]);
  94. } catch(\Exception $e) {
  95. return response()->json(['error'=>$e->getMessage()]);
  96. }
  97. }
  98. }