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
2.9 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. // Get list of current used colors
  58. $teams = DB::table('teams')->pluck('name')->toArray();
  59. $color_names = array_values(self::colors());
  60. $available = array_diff($color_names, $teams);
  61. if(count($available)) {
  62. $next = array_shift($available);
  63. $colors = array_flip(self::colors());
  64. $team = new \App\Team;
  65. $team->name = $next;
  66. $team->color = $colors[$next];
  67. $team->save();
  68. return $next;
  69. } else {
  70. return "";
  71. }
  72. }
  73. public function add_player(Request $request)
  74. {
  75. try {
  76. // Look up the user ID
  77. $profile = Twitter::getUsers(['screen_name' => trim($request->input('twitter'), '@')]);
  78. // Follow the specified user
  79. Twitter::postFollow(['user_id' => $profile->id_str]);
  80. $player = Player::where('twitter_user_id', $profile->id_str)->first();
  81. if(!$player) {
  82. $player = new Player;
  83. $player->twitter_user_id = $profile->id;
  84. }
  85. $player->team_id = $request->input('team');
  86. $player->name = $profile->name;
  87. $player->twitter = $profile->screen_name;
  88. $player->photo = $profile->profile_image_url_https;
  89. $player->save();
  90. return response()->json(['player'=>$player]);
  91. } catch(\Exception $e) {
  92. return response()->json(['error'=>$e->getMessage()]);
  93. }
  94. }
  95. public function remove_player()
  96. {
  97. }
  98. }