PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/WebController.php

https://gitlab.com/AernBau/robot-battles
PHP | 126 lines | 107 code | 14 blank | 5 comment | 20 complexity | 4b20aad31187c83594073fde25a1a490 MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use Auth;
  6. class WebController extends Controller
  7. {
  8. public function home(){
  9. return view('home');
  10. }
  11. public function register(){
  12. return view('pages.register');
  13. }
  14. public function login(){
  15. return view('pages.login');
  16. }
  17. public function profile($id){
  18. $owner = false;
  19. if(Auth::check())
  20. if(Auth::user()->id == $id)
  21. $owner = true;
  22. $data = [];
  23. $opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));
  24. $context = stream_context_create($opts);
  25. $robots = file_get_contents(
  26. "http://".$_SERVER['HTTP_HOST']."/data/usrbt/".Auth::user()->id, false, $context);
  27. $robots = json_decode($robots);
  28. $data['robots'] = $robots;
  29. $data['owner'] = $owner;
  30. if(!$owner){
  31. $user = json_decode(file_get_contents(
  32. "http://".$_SERVER['HTTP_HOST']."/data/users/".$id));
  33. if (json_last_error() == JSON_ERROR_NONE) {
  34. $data['user'] = $user;
  35. }
  36. }
  37. return view('pages.profile', $data);
  38. }
  39. public function battle(){
  40. // Retrieving user's robots and checking how many fights they have done today.
  41. $opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));
  42. $context = stream_context_create($opts);
  43. $robots = file_get_contents(
  44. "http://".$_SERVER['HTTP_HOST']."/data/usrbt/".Auth::user()->id, false, $context);
  45. $robots = json_decode($robots);
  46. if (json_last_error() == JSON_ERROR_NONE) {
  47. foreach($robots as $robot){
  48. $previous_fights = file_get_contents(
  49. "http://".$_SERVER['HTTP_HOST']."/data/fights/attack/".$robot->id);
  50. $fights_in_past_day = 0;
  51. json_decode($previous_fights);
  52. if (json_last_error() == JSON_ERROR_NONE) {
  53. foreach(json_decode($previous_fights) as $fight){
  54. if(strtotime($fight->created_at) > strtotime('-1 day', strtotime(date('Y-m-d H:i:s')))){
  55. $fights_in_past_day++;
  56. }
  57. }
  58. $robot->fights_today = $fights_in_past_day;
  59. }
  60. }
  61. $data['player_robots'] = $robots;
  62. }
  63. // If less than or 5 enemy robots exist, all are shown.
  64. // If there are more than 5 enemy robots, 5 are randomly selected.
  65. // An improvement would be filtering win percentages to be closer to yours.
  66. $enemy_robots = json_decode(file_get_contents(
  67. "http://".$_SERVER['HTTP_HOST']."/data/enrbt/".Auth::user()->id, false, $context));
  68. $amount_of_random = 5;
  69. if(count($enemy_robots) < 5){
  70. $amount_of_random = count($enemy_robots);
  71. }
  72. if($amount_of_random == 0){
  73. return view('pages.battle', $data);
  74. }
  75. $rand_keys = array_rand($enemy_robots, $amount_of_random);
  76. $enemy_ext = [];
  77. if($amount_of_random > 1){
  78. foreach($rand_keys as $key){
  79. $player = json_decode(file_get_contents(
  80. "http://".$_SERVER['HTTP_HOST']."/data/users/".$enemy_robots[$key]->owner_id));
  81. $enemy_robots[$key]->username = $player->username;
  82. $enemy_ext[] = $enemy_robots[$key];
  83. }
  84. } else if($amount_of_random == 1) {
  85. $player = json_decode(file_get_contents(
  86. "http://".$_SERVER['HTTP_HOST']."/data/users/".$enemy_robots[$rand_keys]->owner_id));
  87. $enemy_robots[0]->username = $player->username;
  88. $enemy_ext[] = $enemy_robots[0];
  89. }
  90. $data['enemy_robots'] = $enemy_ext;
  91. return view('pages.battle', $data);
  92. }
  93. public function fights(){
  94. // IN PROGRESS
  95. $data['recent_10'] = json_decode(file_get_contents(
  96. "http://".$_SERVER['HTTP_HOST']."/data/fights/recent"));
  97. $data['top_robots'] = json_decode(file_get_contents(
  98. "http://".$_SERVER['HTTP_HOST']."/data/robots/top"));
  99. return view('pages.fights', $data);
  100. }
  101. public function robots(){
  102. $opts = array('http' => array('header'=> 'Cookie: ' . $_SERVER['HTTP_COOKIE']."\r\n"));
  103. $context = stream_context_create($opts);
  104. $robots = file_get_contents(
  105. "http://".$_SERVER['HTTP_HOST']."/data/usrbt/".Auth::user()->id, false, $context);
  106. $data = [];
  107. json_decode($robots);
  108. if (json_last_error() == JSON_ERROR_NONE)
  109. $data['robots'] = json_decode($robots);
  110. return view('pages.robots', $data);
  111. }
  112. }