@@ -8,122 +8,122 @@ |
||
| 8 | 8 | {
|
| 9 | 9 | |
| 10 | 10 | |
| 11 | - /** |
|
| 12 | - * @group OrmModels |
|
| 13 | - * @group PlayerOrm |
|
| 14 | - */ |
|
| 15 | - public function testPlayerOrmGetSet() |
|
| 16 | - {
|
|
| 17 | - $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller(); |
|
| 18 | - $playerM = $rndFiller->getPlayer(); |
|
| 19 | - $arrayPl = $playerM->toArray(); |
|
| 20 | - $playerO = \App\Lib\DsManager\Models\Orm\Player::create($arrayPl); |
|
| 21 | - $this->assertNotEmpty($playerO); |
|
| 11 | + /** |
|
| 12 | + * @group OrmModels |
|
| 13 | + * @group PlayerOrm |
|
| 14 | + */ |
|
| 15 | + public function testPlayerOrmGetSet() |
|
| 16 | + {
|
|
| 17 | + $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller(); |
|
| 18 | + $playerM = $rndFiller->getPlayer(); |
|
| 19 | + $arrayPl = $playerM->toArray(); |
|
| 20 | + $playerO = \App\Lib\DsManager\Models\Orm\Player::create($arrayPl); |
|
| 21 | + $this->assertNotEmpty($playerO); |
|
| 22 | 22 | |
| 23 | - $newPlayer = \App\Lib\DsManager\Models\Player::fromArray($playerO->toArray()); |
|
| 24 | - $this->assertNotEmpty($newPlayer); |
|
| 25 | - } |
|
| 23 | + $newPlayer = \App\Lib\DsManager\Models\Player::fromArray($playerO->toArray()); |
|
| 24 | + $this->assertNotEmpty($newPlayer); |
|
| 25 | + } |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * @group OrmModels |
|
| 29 | - * @group CoachOrm |
|
| 30 | - */ |
|
| 31 | - public function testCoachOrmGetSet() |
|
| 32 | - {
|
|
| 33 | - $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller(); |
|
| 34 | - $coach = $rndFiller->getCoach(); |
|
| 35 | - $arrayPl = $coach->toArray(); |
|
| 36 | - $coachO = \App\Lib\DsManager\Models\Orm\Coach::create($arrayPl); |
|
| 37 | - $this->assertNotEmpty($coachO); |
|
| 27 | + /** |
|
| 28 | + * @group OrmModels |
|
| 29 | + * @group CoachOrm |
|
| 30 | + */ |
|
| 31 | + public function testCoachOrmGetSet() |
|
| 32 | + {
|
|
| 33 | + $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller(); |
|
| 34 | + $coach = $rndFiller->getCoach(); |
|
| 35 | + $arrayPl = $coach->toArray(); |
|
| 36 | + $coachO = \App\Lib\DsManager\Models\Orm\Coach::create($arrayPl); |
|
| 37 | + $this->assertNotEmpty($coachO); |
|
| 38 | 38 | |
| 39 | - $newCoach = \App\Lib\DsManager\Models\Coach::fromArray($coachO->toArray()); |
|
| 40 | - $this->assertNotEmpty($newCoach); |
|
| 41 | - } |
|
| 39 | + $newCoach = \App\Lib\DsManager\Models\Coach::fromArray($coachO->toArray()); |
|
| 40 | + $this->assertNotEmpty($newCoach); |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * @group OrmModels |
|
| 45 | - * @group TeamOrm |
|
| 46 | - */ |
|
| 47 | - public function testTeamOrm() |
|
| 48 | - {
|
|
| 49 | - $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller(); |
|
| 50 | - $team = $rndFiller->getTeam($rndFiller->getLocale()); |
|
| 51 | - $teamArray = $team->toArray(); |
|
| 52 | - $this->assertNotEmpty($team); |
|
| 53 | - $teamO = \App\Lib\DsManager\Models\Orm\Team::create($teamArray); |
|
| 54 | - $this->assertNotEmpty($teamArray); |
|
| 55 | - $this->assertNotEmpty($teamArray['roster']); |
|
| 56 | - foreach ($teamArray['roster'] as $player) {
|
|
| 57 | - $player['team_id'] = $teamO->id; |
|
| 58 | - $playerO = \App\Lib\DsManager\Models\Orm\Player::create($player); |
|
| 59 | - $this->assertNotEmpty($playerO); |
|
| 60 | - } |
|
| 61 | - $teamArray['coach']['team_id'] = $teamO->id; |
|
| 62 | - $coachO = \App\Lib\DsManager\Models\Orm\Coach::create($teamArray['coach']); |
|
| 63 | - $this->assertNotEmpty($coachO); |
|
| 43 | + /** |
|
| 44 | + * @group OrmModels |
|
| 45 | + * @group TeamOrm |
|
| 46 | + */ |
|
| 47 | + public function testTeamOrm() |
|
| 48 | + {
|
|
| 49 | + $rndFiller = new \App\Lib\DsManager\Helpers\RandomFiller(); |
|
| 50 | + $team = $rndFiller->getTeam($rndFiller->getLocale()); |
|
| 51 | + $teamArray = $team->toArray(); |
|
| 52 | + $this->assertNotEmpty($team); |
|
| 53 | + $teamO = \App\Lib\DsManager\Models\Orm\Team::create($teamArray); |
|
| 54 | + $this->assertNotEmpty($teamArray); |
|
| 55 | + $this->assertNotEmpty($teamArray['roster']); |
|
| 56 | + foreach ($teamArray['roster'] as $player) {
|
|
| 57 | + $player['team_id'] = $teamO->id; |
|
| 58 | + $playerO = \App\Lib\DsManager\Models\Orm\Player::create($player); |
|
| 59 | + $this->assertNotEmpty($playerO); |
|
| 60 | + } |
|
| 61 | + $teamArray['coach']['team_id'] = $teamO->id; |
|
| 62 | + $coachO = \App\Lib\DsManager\Models\Orm\Coach::create($teamArray['coach']); |
|
| 63 | + $this->assertNotEmpty($coachO); |
|
| 64 | 64 | |
| 65 | - $this->assertNotEmpty( |
|
| 66 | - \App\Lib\DsManager\Models\Orm\Team::with( |
|
| 67 | - 'roster' |
|
| 68 | - )->with( |
|
| 69 | - 'coach' |
|
| 70 | - )->where( |
|
| 71 | - [ |
|
| 72 | - 'id' => $teamO->id |
|
| 73 | - ] |
|
| 74 | - )->get()->toArray() |
|
| 75 | - ); |
|
| 76 | - } |
|
| 65 | + $this->assertNotEmpty( |
|
| 66 | + \App\Lib\DsManager\Models\Orm\Team::with( |
|
| 67 | + 'roster' |
|
| 68 | + )->with( |
|
| 69 | + 'coach' |
|
| 70 | + )->where( |
|
| 71 | + [ |
|
| 72 | + 'id' => $teamO->id |
|
| 73 | + ] |
|
| 74 | + )->get()->toArray() |
|
| 75 | + ); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - /** |
|
| 79 | - * @group Match |
|
| 80 | - * @group createnewmatch |
|
| 81 | - */ |
|
| 82 | - public function testCreateNewMatch() |
|
| 83 | - {
|
|
| 84 | - $teams = \App\Lib\DsManager\Models\Orm\Team::with( |
|
| 85 | - 'roster', |
|
| 86 | - 'coach' |
|
| 87 | - )->get(); |
|
| 88 | - $this->assertNotNull($teams); |
|
| 89 | - $teams = $teams->toArray(); |
|
| 90 | - $homeIndex = array_rand($teams); |
|
| 91 | - $teamHome = $teams[$homeIndex]; |
|
| 92 | - unset($teams[$homeIndex]); |
|
| 93 | - $teamAway = $teams[array_rand($teams)]; |
|
| 94 | - $this->assertNotNull($teamHome); |
|
| 95 | - $this->assertNotNull($teamAway); |
|
| 96 | - $matchO = \App\Lib\DsManager\Models\Orm\Match::create( |
|
| 97 | - [ |
|
| 98 | - 'home_team_id' => $teamHome['id'], |
|
| 99 | - 'away_team_id' => $teamAway['id'] |
|
| 100 | - ] |
|
| 101 | - ); |
|
| 102 | - $this->assertNotNull($matchO); |
|
| 103 | - $matchNew = \App\Lib\DsManager\Models\Orm\Match::complete()->where('id', $matchO->id)->first();
|
|
| 104 | - $this->assertNotNull($matchNew); |
|
| 105 | - $match = \App\Lib\DsManager\Models\Match::fromArray($matchNew->toArray()); |
|
| 106 | - $this->assertNotNull($match); |
|
| 107 | - } |
|
| 78 | + /** |
|
| 79 | + * @group Match |
|
| 80 | + * @group createnewmatch |
|
| 81 | + */ |
|
| 82 | + public function testCreateNewMatch() |
|
| 83 | + {
|
|
| 84 | + $teams = \App\Lib\DsManager\Models\Orm\Team::with( |
|
| 85 | + 'roster', |
|
| 86 | + 'coach' |
|
| 87 | + )->get(); |
|
| 88 | + $this->assertNotNull($teams); |
|
| 89 | + $teams = $teams->toArray(); |
|
| 90 | + $homeIndex = array_rand($teams); |
|
| 91 | + $teamHome = $teams[$homeIndex]; |
|
| 92 | + unset($teams[$homeIndex]); |
|
| 93 | + $teamAway = $teams[array_rand($teams)]; |
|
| 94 | + $this->assertNotNull($teamHome); |
|
| 95 | + $this->assertNotNull($teamAway); |
|
| 96 | + $matchO = \App\Lib\DsManager\Models\Orm\Match::create( |
|
| 97 | + [ |
|
| 98 | + 'home_team_id' => $teamHome['id'], |
|
| 99 | + 'away_team_id' => $teamAway['id'] |
|
| 100 | + ] |
|
| 101 | + ); |
|
| 102 | + $this->assertNotNull($matchO); |
|
| 103 | + $matchNew = \App\Lib\DsManager\Models\Orm\Match::complete()->where('id', $matchO->id)->first();
|
|
| 104 | + $this->assertNotNull($matchNew); |
|
| 105 | + $match = \App\Lib\DsManager\Models\Match::fromArray($matchNew->toArray()); |
|
| 106 | + $this->assertNotNull($match); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - /** |
|
| 110 | - * @group Stats |
|
| 111 | - */ |
|
| 112 | - public function testGetStats() |
|
| 113 | - {
|
|
| 114 | - $match = \App\Lib\DsManager\Models\Orm\Match::where( |
|
| 115 | - [ |
|
| 116 | - 'simulated' => false |
|
| 117 | - ] |
|
| 118 | - )->whereNotNull( |
|
| 119 | - 'league_round_id' |
|
| 120 | - )->get()->random(1); |
|
| 121 | - $this->assertNotEmpty($match); |
|
| 122 | - $result = \App\Lib\DsManager\Helpers\MatchSimulator::simulateRound($match->league_round_id); |
|
| 123 | - $this->assertNotEmpty($result); |
|
| 124 | - $players = \App\Lib\DsManager\Models\Orm\Player::getBest(); |
|
| 125 | - $this->assertNotEmpty($players); |
|
| 126 | - $teams = \App\Lib\DsManager\Models\Orm\Team::getBest(); |
|
| 127 | - $this->assertNotEmpty($teams); |
|
| 128 | - } |
|
| 109 | + /** |
|
| 110 | + * @group Stats |
|
| 111 | + */ |
|
| 112 | + public function testGetStats() |
|
| 113 | + {
|
|
| 114 | + $match = \App\Lib\DsManager\Models\Orm\Match::where( |
|
| 115 | + [ |
|
| 116 | + 'simulated' => false |
|
| 117 | + ] |
|
| 118 | + )->whereNotNull( |
|
| 119 | + 'league_round_id' |
|
| 120 | + )->get()->random(1); |
|
| 121 | + $this->assertNotEmpty($match); |
|
| 122 | + $result = \App\Lib\DsManager\Helpers\MatchSimulator::simulateRound($match->league_round_id); |
|
| 123 | + $this->assertNotEmpty($result); |
|
| 124 | + $players = \App\Lib\DsManager\Models\Orm\Player::getBest(); |
|
| 125 | + $this->assertNotEmpty($players); |
|
| 126 | + $teams = \App\Lib\DsManager\Models\Orm\Team::getBest(); |
|
| 127 | + $this->assertNotEmpty($teams); |
|
| 128 | + } |
|
| 129 | 129 | } |
@@ -15,187 +15,187 @@ |
||
| 15 | 15 | use \App\Lib\DsManager\Models\Orm\Coach; |
| 16 | 16 | |
| 17 | 17 | $configuration = [ |
| 18 | - 'settings' => [ |
|
| 19 | - 'displayErrorDetails' => true, |
|
| 20 | - ], |
|
| 18 | + 'settings' => [ |
|
| 19 | + 'displayErrorDetails' => true, |
|
| 20 | + ], |
|
| 21 | 21 | ]; |
| 22 | 22 | $c = new \Slim\Container($configuration); |
| 23 | 23 | $api = new \Slim\App($c); |
| 24 | 24 | |
| 25 | 25 | $api->get('/ping', function ($request, $response, $args) {
|
| 26 | - $jsonResp = json_encode( |
|
| 27 | - [ |
|
| 28 | - "status" => "service up", |
|
| 29 | - "message" => "in a bottle" |
|
| 30 | - ] |
|
| 31 | - ); |
|
| 32 | - return Responder::getJsonResponse($jsonResp, $response); |
|
| 26 | + $jsonResp = json_encode( |
|
| 27 | + [ |
|
| 28 | + "status" => "service up", |
|
| 29 | + "message" => "in a bottle" |
|
| 30 | + ] |
|
| 31 | + ); |
|
| 32 | + return Responder::getJsonResponse($jsonResp, $response); |
|
| 33 | 33 | }); |
| 34 | 34 | |
| 35 | 35 | $api->get('/statistics', function ($request, $response, $args) {
|
| 36 | - return Responder::getJsonResponse( |
|
| 37 | - json_encode([ |
|
| 38 | - 'players' => Player::getBest(), |
|
| 39 | - 'teams' => Team::getBest() |
|
| 40 | - ],JSON_NUMERIC_CHECK), |
|
| 41 | - $response |
|
| 42 | - ); |
|
| 36 | + return Responder::getJsonResponse( |
|
| 37 | + json_encode([ |
|
| 38 | + 'players' => Player::getBest(), |
|
| 39 | + 'teams' => Team::getBest() |
|
| 40 | + ],JSON_NUMERIC_CHECK), |
|
| 41 | + $response |
|
| 42 | + ); |
|
| 43 | 43 | }); |
| 44 | 44 | |
| 45 | 45 | $api->get('/coaches', function ($request, $response, $args) {
|
| 46 | - return Responder::getJsonResponse( |
|
| 47 | - Coach::all(), |
|
| 48 | - $response |
|
| 49 | - ); |
|
| 46 | + return Responder::getJsonResponse( |
|
| 47 | + Coach::all(), |
|
| 48 | + $response |
|
| 49 | + ); |
|
| 50 | 50 | }); |
| 51 | 51 | |
| 52 | 52 | $api->get('/teams', function ($request, $response, $args) {
|
| 53 | - return Responder::getJsonResponse( |
|
| 54 | - Team::all(), |
|
| 55 | - $response |
|
| 56 | - ); |
|
| 53 | + return Responder::getJsonResponse( |
|
| 54 | + Team::all(), |
|
| 55 | + $response |
|
| 56 | + ); |
|
| 57 | 57 | }); |
| 58 | 58 | |
| 59 | 59 | $api->get('/teams/{id}', function ($request, $response, $args) {
|
| 60 | - return Responder::getJsonResponse( |
|
| 61 | - Team::complete() |
|
| 62 | - ->where( |
|
| 63 | - [ |
|
| 64 | - 'id' => $args['id'] |
|
| 65 | - ] |
|
| 66 | - )->get(), |
|
| 67 | - $response |
|
| 68 | - ); |
|
| 60 | + return Responder::getJsonResponse( |
|
| 61 | + Team::complete() |
|
| 62 | + ->where( |
|
| 63 | + [ |
|
| 64 | + 'id' => $args['id'] |
|
| 65 | + ] |
|
| 66 | + )->get(), |
|
| 67 | + $response |
|
| 68 | + ); |
|
| 69 | 69 | }); |
| 70 | 70 | |
| 71 | 71 | $api->get('/teams/{id}/players', function ($request, $response, $args) {
|
| 72 | - return Responder::getJsonResponse( |
|
| 73 | - Team::with( |
|
| 74 | - 'roster' |
|
| 75 | - )->where( |
|
| 76 | - [ |
|
| 77 | - 'id' => $args['id'] |
|
| 78 | - ] |
|
| 79 | - )->get(), |
|
| 80 | - $response |
|
| 81 | - ); |
|
| 72 | + return Responder::getJsonResponse( |
|
| 73 | + Team::with( |
|
| 74 | + 'roster' |
|
| 75 | + )->where( |
|
| 76 | + [ |
|
| 77 | + 'id' => $args['id'] |
|
| 78 | + ] |
|
| 79 | + )->get(), |
|
| 80 | + $response |
|
| 81 | + ); |
|
| 82 | 82 | }); |
| 83 | 83 | |
| 84 | 84 | $api->get('/teams/{id}/players/{playerId}', function ($request, $response, $args) {
|
| 85 | - return Responder::getJsonResponse( |
|
| 86 | - Player::statistics()->where( |
|
| 87 | - [ |
|
| 88 | - 'id' => $args['playerId'], |
|
| 89 | - 'team_id' => $args['id'] |
|
| 90 | - ] |
|
| 91 | - )->get(), |
|
| 92 | - $response |
|
| 93 | - ); |
|
| 85 | + return Responder::getJsonResponse( |
|
| 86 | + Player::statistics()->where( |
|
| 87 | + [ |
|
| 88 | + 'id' => $args['playerId'], |
|
| 89 | + 'team_id' => $args['id'] |
|
| 90 | + ] |
|
| 91 | + )->get(), |
|
| 92 | + $response |
|
| 93 | + ); |
|
| 94 | 94 | }); |
| 95 | 95 | |
| 96 | 96 | $api->get('/teams/{id}/coach', function ($request, $response, $args) {
|
| 97 | - return Responder::getJsonResponse( |
|
| 98 | - Team::with( |
|
| 99 | - 'coach' |
|
| 100 | - )->where( |
|
| 101 | - [ |
|
| 102 | - 'id' => $args['id'] |
|
| 103 | - ] |
|
| 104 | - )->get(), |
|
| 105 | - $response |
|
| 106 | - ); |
|
| 97 | + return Responder::getJsonResponse( |
|
| 98 | + Team::with( |
|
| 99 | + 'coach' |
|
| 100 | + )->where( |
|
| 101 | + [ |
|
| 102 | + 'id' => $args['id'] |
|
| 103 | + ] |
|
| 104 | + )->get(), |
|
| 105 | + $response |
|
| 106 | + ); |
|
| 107 | 107 | }); |
| 108 | 108 | |
| 109 | 109 | $api->get('/leagues', function ($request, $response, $args) {
|
| 110 | - return Responder::getJsonResponse( |
|
| 111 | - League::all(), |
|
| 112 | - $response |
|
| 113 | - ); |
|
| 110 | + return Responder::getJsonResponse( |
|
| 111 | + League::all(), |
|
| 112 | + $response |
|
| 113 | + ); |
|
| 114 | 114 | }); |
| 115 | 115 | |
| 116 | 116 | $api->get('/leagues/{id}', function ($request, $response, $args) {
|
| 117 | - return Responder::getJsonResponse( |
|
| 118 | - League::with('rounds')
|
|
| 119 | - ->where( |
|
| 120 | - [ |
|
| 121 | - 'id' => $args['id'] |
|
| 122 | - ] |
|
| 123 | - )->first(), |
|
| 124 | - $response |
|
| 125 | - ); |
|
| 117 | + return Responder::getJsonResponse( |
|
| 118 | + League::with('rounds')
|
|
| 119 | + ->where( |
|
| 120 | + [ |
|
| 121 | + 'id' => $args['id'] |
|
| 122 | + ] |
|
| 123 | + )->first(), |
|
| 124 | + $response |
|
| 125 | + ); |
|
| 126 | 126 | }); |
| 127 | 127 | |
| 128 | 128 | $api->get('/leagues/{id}/rounds/{roundId}', function ($request, $response, $args) {
|
| 129 | - return Responder::getJsonResponse( |
|
| 130 | - LeagueRound::complete() |
|
| 131 | - ->where( |
|
| 132 | - [ |
|
| 133 | - 'id' => $args['roundId'], |
|
| 134 | - ] |
|
| 135 | - )->first(), |
|
| 136 | - $response |
|
| 137 | - ); |
|
| 129 | + return Responder::getJsonResponse( |
|
| 130 | + LeagueRound::complete() |
|
| 131 | + ->where( |
|
| 132 | + [ |
|
| 133 | + 'id' => $args['roundId'], |
|
| 134 | + ] |
|
| 135 | + )->first(), |
|
| 136 | + $response |
|
| 137 | + ); |
|
| 138 | 138 | }); |
| 139 | 139 | |
| 140 | 140 | $api->put('/leagues/{id}/rounds/{roundId}/simulate', function ($request, $response, $args) {
|
| 141 | - return Responder::getJsonResponse( |
|
| 142 | - MatchSimulator::simulateRound( |
|
| 143 | - $args['roundId'] |
|
| 144 | - ), |
|
| 145 | - $response |
|
| 146 | - ); |
|
| 141 | + return Responder::getJsonResponse( |
|
| 142 | + MatchSimulator::simulateRound( |
|
| 143 | + $args['roundId'] |
|
| 144 | + ), |
|
| 145 | + $response |
|
| 146 | + ); |
|
| 147 | 147 | }); |
| 148 | 148 | |
| 149 | 149 | $api->get('/matches', function ($request, $response, $args) {
|
| 150 | - return Responder::getJsonResponse( |
|
| 151 | - Match::teams()->get(), |
|
| 152 | - $response |
|
| 153 | - ); |
|
| 150 | + return Responder::getJsonResponse( |
|
| 151 | + Match::teams()->get(), |
|
| 152 | + $response |
|
| 153 | + ); |
|
| 154 | 154 | }); |
| 155 | 155 | |
| 156 | 156 | $api->post('/matches', function ($request, $response, $args) {
|
| 157 | - $json = $request->getBody(); |
|
| 158 | - $json = json_decode($json, true); |
|
| 159 | - return Responder::getJsonResponse( |
|
| 160 | - Match::create( |
|
| 161 | - $json |
|
| 162 | - ), |
|
| 163 | - $response |
|
| 164 | - ); |
|
| 157 | + $json = $request->getBody(); |
|
| 158 | + $json = json_decode($json, true); |
|
| 159 | + return Responder::getJsonResponse( |
|
| 160 | + Match::create( |
|
| 161 | + $json |
|
| 162 | + ), |
|
| 163 | + $response |
|
| 164 | + ); |
|
| 165 | 165 | }); |
| 166 | 166 | |
| 167 | 167 | $api->get('/matches/{id}', function ($request, $response, $args) {
|
| 168 | - return Responder::getJsonResponse( |
|
| 169 | - Match::complete() |
|
| 170 | - ->where( |
|
| 171 | - [ |
|
| 172 | - 'id' => $args['id'] |
|
| 173 | - ] |
|
| 174 | - )->first(), |
|
| 175 | - $response |
|
| 176 | - ); |
|
| 168 | + return Responder::getJsonResponse( |
|
| 169 | + Match::complete() |
|
| 170 | + ->where( |
|
| 171 | + [ |
|
| 172 | + 'id' => $args['id'] |
|
| 173 | + ] |
|
| 174 | + )->first(), |
|
| 175 | + $response |
|
| 176 | + ); |
|
| 177 | 177 | }); |
| 178 | 178 | |
| 179 | 179 | $api->get('/matches/{id}/result', function ($request, $response, $args) {
|
| 180 | - $result = MatchResult::complete() |
|
| 181 | - ->where( |
|
| 182 | - [ |
|
| 183 | - 'id' => $args['id'] |
|
| 184 | - ] |
|
| 185 | - )->first(); |
|
| 180 | + $result = MatchResult::complete() |
|
| 181 | + ->where( |
|
| 182 | + [ |
|
| 183 | + 'id' => $args['id'] |
|
| 184 | + ] |
|
| 185 | + )->first(); |
|
| 186 | 186 | |
| 187 | - return Responder::getJsonResponse( |
|
| 188 | - $result, |
|
| 189 | - $response |
|
| 190 | - ); |
|
| 187 | + return Responder::getJsonResponse( |
|
| 188 | + $result, |
|
| 189 | + $response |
|
| 190 | + ); |
|
| 191 | 191 | }); |
| 192 | 192 | |
| 193 | 193 | $api->put('/matches/{id}/simulate', function ($request, $response, $args) {
|
| 194 | - return Responder::getJsonResponse( |
|
| 195 | - MatchSimulator::simulateCompleteResult( |
|
| 196 | - $args['id'] |
|
| 197 | - ), |
|
| 198 | - $response |
|
| 199 | - ); |
|
| 194 | + return Responder::getJsonResponse( |
|
| 195 | + MatchSimulator::simulateCompleteResult( |
|
| 196 | + $args['id'] |
|
| 197 | + ), |
|
| 198 | + $response |
|
| 199 | + ); |
|
| 200 | 200 | }); |
| 201 | 201 | $api->run(); |
| 202 | 202 | \ No newline at end of file |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | $c = new \Slim\Container($configuration); |
| 23 | 23 | $api = new \Slim\App($c); |
| 24 | 24 | |
| 25 | -$api->get('/ping', function ($request, $response, $args) {
|
|
| 25 | +$api->get('/ping', function($request, $response, $args) {
|
|
| 26 | 26 | $jsonResp = json_encode( |
| 27 | 27 | [ |
| 28 | 28 | "status" => "service up", |
@@ -32,31 +32,31 @@ discard block |
||
| 32 | 32 | return Responder::getJsonResponse($jsonResp, $response); |
| 33 | 33 | }); |
| 34 | 34 | |
| 35 | -$api->get('/statistics', function ($request, $response, $args) {
|
|
| 35 | +$api->get('/statistics', function($request, $response, $args) {
|
|
| 36 | 36 | return Responder::getJsonResponse( |
| 37 | 37 | json_encode([ |
| 38 | 38 | 'players' => Player::getBest(), |
| 39 | 39 | 'teams' => Team::getBest() |
| 40 | - ],JSON_NUMERIC_CHECK), |
|
| 40 | + ], JSON_NUMERIC_CHECK), |
|
| 41 | 41 | $response |
| 42 | 42 | ); |
| 43 | 43 | }); |
| 44 | 44 | |
| 45 | -$api->get('/coaches', function ($request, $response, $args) {
|
|
| 45 | +$api->get('/coaches', function($request, $response, $args) {
|
|
| 46 | 46 | return Responder::getJsonResponse( |
| 47 | 47 | Coach::all(), |
| 48 | 48 | $response |
| 49 | 49 | ); |
| 50 | 50 | }); |
| 51 | 51 | |
| 52 | -$api->get('/teams', function ($request, $response, $args) {
|
|
| 52 | +$api->get('/teams', function($request, $response, $args) {
|
|
| 53 | 53 | return Responder::getJsonResponse( |
| 54 | 54 | Team::all(), |
| 55 | 55 | $response |
| 56 | 56 | ); |
| 57 | 57 | }); |
| 58 | 58 | |
| 59 | -$api->get('/teams/{id}', function ($request, $response, $args) {
|
|
| 59 | +$api->get('/teams/{id}', function($request, $response, $args) {
|
|
| 60 | 60 | return Responder::getJsonResponse( |
| 61 | 61 | Team::complete() |
| 62 | 62 | ->where( |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | ); |
| 69 | 69 | }); |
| 70 | 70 | |
| 71 | -$api->get('/teams/{id}/players', function ($request, $response, $args) {
|
|
| 71 | +$api->get('/teams/{id}/players', function($request, $response, $args) {
|
|
| 72 | 72 | return Responder::getJsonResponse( |
| 73 | 73 | Team::with( |
| 74 | 74 | 'roster' |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | ); |
| 82 | 82 | }); |
| 83 | 83 | |
| 84 | -$api->get('/teams/{id}/players/{playerId}', function ($request, $response, $args) {
|
|
| 84 | +$api->get('/teams/{id}/players/{playerId}', function($request, $response, $args) {
|
|
| 85 | 85 | return Responder::getJsonResponse( |
| 86 | 86 | Player::statistics()->where( |
| 87 | 87 | [ |
@@ -93,7 +93,7 @@ discard block |
||
| 93 | 93 | ); |
| 94 | 94 | }); |
| 95 | 95 | |
| 96 | -$api->get('/teams/{id}/coach', function ($request, $response, $args) {
|
|
| 96 | +$api->get('/teams/{id}/coach', function($request, $response, $args) {
|
|
| 97 | 97 | return Responder::getJsonResponse( |
| 98 | 98 | Team::with( |
| 99 | 99 | 'coach' |
@@ -106,14 +106,14 @@ discard block |
||
| 106 | 106 | ); |
| 107 | 107 | }); |
| 108 | 108 | |
| 109 | -$api->get('/leagues', function ($request, $response, $args) {
|
|
| 109 | +$api->get('/leagues', function($request, $response, $args) {
|
|
| 110 | 110 | return Responder::getJsonResponse( |
| 111 | 111 | League::all(), |
| 112 | 112 | $response |
| 113 | 113 | ); |
| 114 | 114 | }); |
| 115 | 115 | |
| 116 | -$api->get('/leagues/{id}', function ($request, $response, $args) {
|
|
| 116 | +$api->get('/leagues/{id}', function($request, $response, $args) {
|
|
| 117 | 117 | return Responder::getJsonResponse( |
| 118 | 118 | League::with('rounds')
|
| 119 | 119 | ->where( |
@@ -125,7 +125,7 @@ discard block |
||
| 125 | 125 | ); |
| 126 | 126 | }); |
| 127 | 127 | |
| 128 | -$api->get('/leagues/{id}/rounds/{roundId}', function ($request, $response, $args) {
|
|
| 128 | +$api->get('/leagues/{id}/rounds/{roundId}', function($request, $response, $args) {
|
|
| 129 | 129 | return Responder::getJsonResponse( |
| 130 | 130 | LeagueRound::complete() |
| 131 | 131 | ->where( |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | ); |
| 138 | 138 | }); |
| 139 | 139 | |
| 140 | -$api->put('/leagues/{id}/rounds/{roundId}/simulate', function ($request, $response, $args) {
|
|
| 140 | +$api->put('/leagues/{id}/rounds/{roundId}/simulate', function($request, $response, $args) {
|
|
| 141 | 141 | return Responder::getJsonResponse( |
| 142 | 142 | MatchSimulator::simulateRound( |
| 143 | 143 | $args['roundId'] |
@@ -146,14 +146,14 @@ discard block |
||
| 146 | 146 | ); |
| 147 | 147 | }); |
| 148 | 148 | |
| 149 | -$api->get('/matches', function ($request, $response, $args) {
|
|
| 149 | +$api->get('/matches', function($request, $response, $args) {
|
|
| 150 | 150 | return Responder::getJsonResponse( |
| 151 | 151 | Match::teams()->get(), |
| 152 | 152 | $response |
| 153 | 153 | ); |
| 154 | 154 | }); |
| 155 | 155 | |
| 156 | -$api->post('/matches', function ($request, $response, $args) {
|
|
| 156 | +$api->post('/matches', function($request, $response, $args) {
|
|
| 157 | 157 | $json = $request->getBody(); |
| 158 | 158 | $json = json_decode($json, true); |
| 159 | 159 | return Responder::getJsonResponse( |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | ); |
| 165 | 165 | }); |
| 166 | 166 | |
| 167 | -$api->get('/matches/{id}', function ($request, $response, $args) {
|
|
| 167 | +$api->get('/matches/{id}', function($request, $response, $args) {
|
|
| 168 | 168 | return Responder::getJsonResponse( |
| 169 | 169 | Match::complete() |
| 170 | 170 | ->where( |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | ); |
| 177 | 177 | }); |
| 178 | 178 | |
| 179 | -$api->get('/matches/{id}/result', function ($request, $response, $args) {
|
|
| 179 | +$api->get('/matches/{id}/result', function($request, $response, $args) {
|
|
| 180 | 180 | $result = MatchResult::complete() |
| 181 | 181 | ->where( |
| 182 | 182 | [ |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | ); |
| 191 | 191 | }); |
| 192 | 192 | |
| 193 | -$api->put('/matches/{id}/simulate', function ($request, $response, $args) {
|
|
| 193 | +$api->put('/matches/{id}/simulate', function($request, $response, $args) {
|
|
| 194 | 194 | return Responder::getJsonResponse( |
| 195 | 195 | MatchSimulator::simulateCompleteResult( |
| 196 | 196 | $args['id'] |
@@ -8,138 +8,138 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Team extends DsManagerOrm |
| 10 | 10 | {
|
| 11 | - /** |
|
| 12 | - * |
|
| 13 | - */ |
|
| 14 | - const PLAYED_LIMIT = 5; |
|
| 15 | - /** |
|
| 16 | - * |
|
| 17 | - */ |
|
| 18 | - const FUTURE_LIMIT = 3; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * |
|
| 22 | - */ |
|
| 23 | - const TEAM_STATS_LIMIT = 5; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $table = 'teams'; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected $fillable = [ |
|
| 34 | - 'name', |
|
| 35 | - 'nationality' |
|
| 36 | - ]; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
| 40 | - */ |
|
| 41 | - public function roster() |
|
| 42 | - {
|
|
| 43 | - return $this->hasMany(Player::class); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
| 48 | - */ |
|
| 49 | - public function coach() |
|
| 50 | - {
|
|
| 51 | - return $this->hasOne(Coach::class); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - /** |
|
| 55 | - * @return mixed |
|
| 56 | - */ |
|
| 57 | - public function playedMatchesHome() |
|
| 58 | - {
|
|
| 59 | - return $this->hasMany( |
|
| 60 | - MatchResult::class, |
|
| 61 | - 'home_team_id' |
|
| 62 | - )->where('simulated', true)
|
|
| 63 | - ->orderBy('updated_at', 'DESC')
|
|
| 64 | - ->limit(self::PLAYED_LIMIT); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @return mixed |
|
| 69 | - */ |
|
| 70 | - public function futureMatchesHome() |
|
| 71 | - {
|
|
| 72 | - return $this->hasMany( |
|
| 73 | - MatchResult::class, |
|
| 74 | - 'home_team_id' |
|
| 75 | - )->where('simulated', false)
|
|
| 76 | - ->orderBy('updated_at', 'DESC')
|
|
| 77 | - ->limit(self::FUTURE_LIMIT); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @return mixed |
|
| 82 | - */ |
|
| 83 | - public function playedMatchesAway() |
|
| 84 | - {
|
|
| 85 | - return $this->hasMany( |
|
| 86 | - MatchResult::class, |
|
| 87 | - 'away_team_id' |
|
| 88 | - )->where('simulated', true)
|
|
| 89 | - ->orderBy('updated_at', 'DESC')
|
|
| 90 | - ->limit(self::PLAYED_LIMIT); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * @return mixed |
|
| 95 | - */ |
|
| 96 | - public function futureMatchesAway() |
|
| 97 | - {
|
|
| 98 | - return $this->hasMany( |
|
| 99 | - MatchResult::class, |
|
| 100 | - 'away_team_id' |
|
| 101 | - )->where('simulated', false)
|
|
| 102 | - ->orderBy('updated_at', 'DESC')
|
|
| 103 | - ->limit(self::FUTURE_LIMIT); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * @param $query |
|
| 109 | - * @return mixed |
|
| 110 | - */ |
|
| 111 | - public function scopeComplete($query) |
|
| 112 | - {
|
|
| 113 | - return $query->with( |
|
| 114 | - 'roster', |
|
| 115 | - 'coach', |
|
| 116 | - 'playedMatchesHome', |
|
| 117 | - 'playedMatchesHome.awayTeam', |
|
| 118 | - 'futureMatchesHome', |
|
| 119 | - 'futureMatchesHome.awayTeam', |
|
| 120 | - 'playedMatchesAway', |
|
| 121 | - 'playedMatchesAway.homeTeam', |
|
| 122 | - 'futureMatchesAway', |
|
| 123 | - 'futureMatchesAway.homeTeam' |
|
| 124 | - ); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * @return array |
|
| 129 | - */ |
|
| 130 | - public static function getBest() |
|
| 131 | - {
|
|
| 132 | - $result = Match::selectRaw('winner_id, COUNT(*) as won')
|
|
| 133 | - ->whereNotNull('winner_id')->where('winner_id', '!=', 0)
|
|
| 134 | - ->orderByRaw('COUNT(*) DESC')->groupBy('winner_id')
|
|
| 135 | - ->take(self::TEAM_STATS_LIMIT)->get()->keyBy('winner_id')->toArray();
|
|
| 136 | - $teams = Team::whereIn('id', array_keys($result))->get()->toArray();
|
|
| 137 | - $result = array_map(function ($team) use ($result) {
|
|
| 138 | - return array_merge($team, $result[$team['id']]); |
|
| 139 | - |
|
| 140 | - }, $teams); |
|
| 141 | - |
|
| 142 | - return $result; |
|
| 143 | - } |
|
| 11 | + /** |
|
| 12 | + * |
|
| 13 | + */ |
|
| 14 | + const PLAYED_LIMIT = 5; |
|
| 15 | + /** |
|
| 16 | + * |
|
| 17 | + */ |
|
| 18 | + const FUTURE_LIMIT = 3; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * |
|
| 22 | + */ |
|
| 23 | + const TEAM_STATS_LIMIT = 5; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $table = 'teams'; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected $fillable = [ |
|
| 34 | + 'name', |
|
| 35 | + 'nationality' |
|
| 36 | + ]; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
| 40 | + */ |
|
| 41 | + public function roster() |
|
| 42 | + {
|
|
| 43 | + return $this->hasMany(Player::class); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
| 48 | + */ |
|
| 49 | + public function coach() |
|
| 50 | + {
|
|
| 51 | + return $this->hasOne(Coach::class); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + /** |
|
| 55 | + * @return mixed |
|
| 56 | + */ |
|
| 57 | + public function playedMatchesHome() |
|
| 58 | + {
|
|
| 59 | + return $this->hasMany( |
|
| 60 | + MatchResult::class, |
|
| 61 | + 'home_team_id' |
|
| 62 | + )->where('simulated', true)
|
|
| 63 | + ->orderBy('updated_at', 'DESC')
|
|
| 64 | + ->limit(self::PLAYED_LIMIT); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @return mixed |
|
| 69 | + */ |
|
| 70 | + public function futureMatchesHome() |
|
| 71 | + {
|
|
| 72 | + return $this->hasMany( |
|
| 73 | + MatchResult::class, |
|
| 74 | + 'home_team_id' |
|
| 75 | + )->where('simulated', false)
|
|
| 76 | + ->orderBy('updated_at', 'DESC')
|
|
| 77 | + ->limit(self::FUTURE_LIMIT); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @return mixed |
|
| 82 | + */ |
|
| 83 | + public function playedMatchesAway() |
|
| 84 | + {
|
|
| 85 | + return $this->hasMany( |
|
| 86 | + MatchResult::class, |
|
| 87 | + 'away_team_id' |
|
| 88 | + )->where('simulated', true)
|
|
| 89 | + ->orderBy('updated_at', 'DESC')
|
|
| 90 | + ->limit(self::PLAYED_LIMIT); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * @return mixed |
|
| 95 | + */ |
|
| 96 | + public function futureMatchesAway() |
|
| 97 | + {
|
|
| 98 | + return $this->hasMany( |
|
| 99 | + MatchResult::class, |
|
| 100 | + 'away_team_id' |
|
| 101 | + )->where('simulated', false)
|
|
| 102 | + ->orderBy('updated_at', 'DESC')
|
|
| 103 | + ->limit(self::FUTURE_LIMIT); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * @param $query |
|
| 109 | + * @return mixed |
|
| 110 | + */ |
|
| 111 | + public function scopeComplete($query) |
|
| 112 | + {
|
|
| 113 | + return $query->with( |
|
| 114 | + 'roster', |
|
| 115 | + 'coach', |
|
| 116 | + 'playedMatchesHome', |
|
| 117 | + 'playedMatchesHome.awayTeam', |
|
| 118 | + 'futureMatchesHome', |
|
| 119 | + 'futureMatchesHome.awayTeam', |
|
| 120 | + 'playedMatchesAway', |
|
| 121 | + 'playedMatchesAway.homeTeam', |
|
| 122 | + 'futureMatchesAway', |
|
| 123 | + 'futureMatchesAway.homeTeam' |
|
| 124 | + ); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * @return array |
|
| 129 | + */ |
|
| 130 | + public static function getBest() |
|
| 131 | + {
|
|
| 132 | + $result = Match::selectRaw('winner_id, COUNT(*) as won')
|
|
| 133 | + ->whereNotNull('winner_id')->where('winner_id', '!=', 0)
|
|
| 134 | + ->orderByRaw('COUNT(*) DESC')->groupBy('winner_id')
|
|
| 135 | + ->take(self::TEAM_STATS_LIMIT)->get()->keyBy('winner_id')->toArray();
|
|
| 136 | + $teams = Team::whereIn('id', array_keys($result))->get()->toArray();
|
|
| 137 | + $result = array_map(function ($team) use ($result) {
|
|
| 138 | + return array_merge($team, $result[$team['id']]); |
|
| 139 | + |
|
| 140 | + }, $teams); |
|
| 141 | + |
|
| 142 | + return $result; |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | 145 | } |
| 146 | 146 | \ No newline at end of file |
@@ -8,109 +8,109 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | class Player extends DsManagerOrm |
| 10 | 10 | {
|
| 11 | - /** |
|
| 12 | - * |
|
| 13 | - */ |
|
| 14 | - const PLAYER_STATS_LIMIT = 5; |
|
| 11 | + /** |
|
| 12 | + * |
|
| 13 | + */ |
|
| 14 | + const PLAYER_STATS_LIMIT = 5; |
|
| 15 | 15 | |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @var string |
|
| 19 | - */ |
|
| 20 | - protected $table = 'players'; |
|
| 17 | + /** |
|
| 18 | + * @var string |
|
| 19 | + */ |
|
| 20 | + protected $table = 'players'; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @var array |
|
| 24 | - */ |
|
| 25 | - protected $fillable = [ |
|
| 26 | - 'name', |
|
| 27 | - 'surname', |
|
| 28 | - 'age', |
|
| 29 | - 'nationality', |
|
| 30 | - 'skillAvg', |
|
| 31 | - 'wageReq', |
|
| 32 | - 'val', |
|
| 33 | - 'role', |
|
| 34 | - 'team_id' |
|
| 35 | - ]; |
|
| 22 | + /** |
|
| 23 | + * @var array |
|
| 24 | + */ |
|
| 25 | + protected $fillable = [ |
|
| 26 | + 'name', |
|
| 27 | + 'surname', |
|
| 28 | + 'age', |
|
| 29 | + 'nationality', |
|
| 30 | + 'skillAvg', |
|
| 31 | + 'wageReq', |
|
| 32 | + 'val', |
|
| 33 | + 'role', |
|
| 34 | + 'team_id' |
|
| 35 | + ]; |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
| 39 | - */ |
|
| 40 | - public function team() |
|
| 41 | - {
|
|
| 42 | - return $this->belongsTo(Team::class); |
|
| 43 | - } |
|
| 37 | + /** |
|
| 38 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
| 39 | + */ |
|
| 40 | + public function team() |
|
| 41 | + {
|
|
| 42 | + return $this->belongsTo(Team::class); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * @return mixed |
|
| 47 | - */ |
|
| 48 | - public function lastMatches() |
|
| 49 | - {
|
|
| 50 | - return $this->hasMany(MatchPlayer::class) |
|
| 51 | - ->orderBy('updated_at', 'DESC')
|
|
| 52 | - ->limit(5); |
|
| 53 | - } |
|
| 45 | + /** |
|
| 46 | + * @return mixed |
|
| 47 | + */ |
|
| 48 | + public function lastMatches() |
|
| 49 | + {
|
|
| 50 | + return $this->hasMany(MatchPlayer::class) |
|
| 51 | + ->orderBy('updated_at', 'DESC')
|
|
| 52 | + ->limit(5); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * @return mixed |
|
| 57 | - */ |
|
| 58 | - public function goals() |
|
| 59 | - {
|
|
| 60 | - return $this->hasOne(MatchPlayer::class) |
|
| 61 | - ->selectRaw('player_id, sum(goals) as count')
|
|
| 62 | - ->groupBy('player_id');
|
|
| 63 | - } |
|
| 55 | + /** |
|
| 56 | + * @return mixed |
|
| 57 | + */ |
|
| 58 | + public function goals() |
|
| 59 | + {
|
|
| 60 | + return $this->hasOne(MatchPlayer::class) |
|
| 61 | + ->selectRaw('player_id, sum(goals) as count')
|
|
| 62 | + ->groupBy('player_id');
|
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * @return mixed |
|
| 67 | - */ |
|
| 68 | - public function appearances() |
|
| 69 | - {
|
|
| 70 | - return $this->hasOne(MatchPlayer::class) |
|
| 71 | - ->selectRaw('player_id, count(match_id) as count')
|
|
| 72 | - ->groupBy('player_id');
|
|
| 73 | - } |
|
| 65 | + /** |
|
| 66 | + * @return mixed |
|
| 67 | + */ |
|
| 68 | + public function appearances() |
|
| 69 | + {
|
|
| 70 | + return $this->hasOne(MatchPlayer::class) |
|
| 71 | + ->selectRaw('player_id, count(match_id) as count')
|
|
| 72 | + ->groupBy('player_id');
|
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - /** |
|
| 76 | - * @return mixed |
|
| 77 | - */ |
|
| 78 | - public function avg() |
|
| 79 | - {
|
|
| 80 | - return $this->hasOne(MatchPlayer::class) |
|
| 81 | - ->selectRaw('player_id, round(avg(vote),2) as avg')
|
|
| 82 | - ->groupBy('player_id');
|
|
| 83 | - } |
|
| 75 | + /** |
|
| 76 | + * @return mixed |
|
| 77 | + */ |
|
| 78 | + public function avg() |
|
| 79 | + {
|
|
| 80 | + return $this->hasOne(MatchPlayer::class) |
|
| 81 | + ->selectRaw('player_id, round(avg(vote),2) as avg')
|
|
| 82 | + ->groupBy('player_id');
|
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - /** |
|
| 86 | - * @param $query |
|
| 87 | - * @return mixed |
|
| 88 | - */ |
|
| 89 | - public function scopeStatistics($query) |
|
| 90 | - {
|
|
| 91 | - return $query->with( |
|
| 92 | - 'goals', |
|
| 93 | - 'appearances', |
|
| 94 | - 'avg', |
|
| 95 | - 'lastMatches', |
|
| 96 | - 'team' |
|
| 97 | - ); |
|
| 98 | - } |
|
| 85 | + /** |
|
| 86 | + * @param $query |
|
| 87 | + * @return mixed |
|
| 88 | + */ |
|
| 89 | + public function scopeStatistics($query) |
|
| 90 | + {
|
|
| 91 | + return $query->with( |
|
| 92 | + 'goals', |
|
| 93 | + 'appearances', |
|
| 94 | + 'avg', |
|
| 95 | + 'lastMatches', |
|
| 96 | + 'team' |
|
| 97 | + ); |
|
| 98 | + } |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * @return array |
|
| 102 | - */ |
|
| 103 | - public static function getBest() |
|
| 104 | - {
|
|
| 105 | - $result = MatchPlayer::selectRaw( |
|
| 106 | - 'player_id, COUNT(*) as appearances , AVG(vote) avg, SUM(goals) goals' |
|
| 107 | - )->where('goals', '>', 0)
|
|
| 108 | - ->orderByRaw('SUM(goals) DESC,COUNT(*) DESC')
|
|
| 109 | - ->groupBy('player_id')->take(self::PLAYER_STATS_LIMIT)->get()->keyBy('player_id')->toArray();
|
|
| 110 | - $players = Player::with('team')->whereIn('id', array_keys($result))->get()->toArray();
|
|
| 111 | - $result = array_map(function ($player) use ($result) {
|
|
| 112 | - return array_merge($player,$result[$player['id']]); |
|
| 113 | - }, $players); |
|
| 114 | - return $result; |
|
| 115 | - } |
|
| 100 | + /** |
|
| 101 | + * @return array |
|
| 102 | + */ |
|
| 103 | + public static function getBest() |
|
| 104 | + {
|
|
| 105 | + $result = MatchPlayer::selectRaw( |
|
| 106 | + 'player_id, COUNT(*) as appearances , AVG(vote) avg, SUM(goals) goals' |
|
| 107 | + )->where('goals', '>', 0)
|
|
| 108 | + ->orderByRaw('SUM(goals) DESC,COUNT(*) DESC')
|
|
| 109 | + ->groupBy('player_id')->take(self::PLAYER_STATS_LIMIT)->get()->keyBy('player_id')->toArray();
|
|
| 110 | + $players = Player::with('team')->whereIn('id', array_keys($result))->get()->toArray();
|
|
| 111 | + $result = array_map(function ($player) use ($result) {
|
|
| 112 | + return array_merge($player,$result[$player['id']]); |
|
| 113 | + }, $players); |
|
| 114 | + return $result; |
|
| 115 | + } |
|
| 116 | 116 | } |
| 117 | 117 | \ No newline at end of file |
@@ -108,8 +108,8 @@ |
||
| 108 | 108 | ->orderByRaw('SUM(goals) DESC,COUNT(*) DESC')
|
| 109 | 109 | ->groupBy('player_id')->take(self::PLAYER_STATS_LIMIT)->get()->keyBy('player_id')->toArray();
|
| 110 | 110 | $players = Player::with('team')->whereIn('id', array_keys($result))->get()->toArray();
|
| 111 | - $result = array_map(function ($player) use ($result) {
|
|
| 112 | - return array_merge($player,$result[$player['id']]); |
|
| 111 | + $result = array_map(function($player) use ($result) {
|
|
| 112 | + return array_merge($player, $result[$player['id']]); |
|
| 113 | 113 | }, $players); |
| 114 | 114 | return $result; |
| 115 | 115 | } |
@@ -13,118 +13,118 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class MatchResult extends DsManagerModel |
| 15 | 15 | {
|
| 16 | - /** |
|
| 17 | - * @var |
|
| 18 | - */ |
|
| 19 | - private $goalHome; |
|
| 20 | - /** |
|
| 21 | - * @var |
|
| 22 | - */ |
|
| 23 | - private $goalAway; |
|
| 24 | - /** |
|
| 25 | - * @var Team |
|
| 26 | - */ |
|
| 27 | - private $homeTeam; |
|
| 28 | - /** |
|
| 29 | - * @var Team |
|
| 30 | - */ |
|
| 31 | - private $awayTeam; |
|
| 16 | + /** |
|
| 17 | + * @var |
|
| 18 | + */ |
|
| 19 | + private $goalHome; |
|
| 20 | + /** |
|
| 21 | + * @var |
|
| 22 | + */ |
|
| 23 | + private $goalAway; |
|
| 24 | + /** |
|
| 25 | + * @var Team |
|
| 26 | + */ |
|
| 27 | + private $homeTeam; |
|
| 28 | + /** |
|
| 29 | + * @var Team |
|
| 30 | + */ |
|
| 31 | + private $awayTeam; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * MatchResult constructor. |
|
| 35 | - * @param $goalHome |
|
| 36 | - * @param $goalAway |
|
| 37 | - * @param Team $home |
|
| 38 | - * @param Team $away |
|
| 39 | - */ |
|
| 40 | - public function __construct($goalHome, $goalAway, Team $home, Team $away) |
|
| 41 | - {
|
|
| 42 | - $this->goalHome = $goalHome; |
|
| 43 | - $this->goalAway = $goalAway; |
|
| 44 | - $this->homeTeam = $home; |
|
| 45 | - $this->awayTeam = $away; |
|
| 46 | - } |
|
| 33 | + /** |
|
| 34 | + * MatchResult constructor. |
|
| 35 | + * @param $goalHome |
|
| 36 | + * @param $goalAway |
|
| 37 | + * @param Team $home |
|
| 38 | + * @param Team $away |
|
| 39 | + */ |
|
| 40 | + public function __construct($goalHome, $goalAway, Team $home, Team $away) |
|
| 41 | + {
|
|
| 42 | + $this->goalHome = $goalHome; |
|
| 43 | + $this->goalAway = $goalAway; |
|
| 44 | + $this->homeTeam = $home; |
|
| 45 | + $this->awayTeam = $away; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * @return array |
|
| 50 | - */ |
|
| 51 | - public function getWinnerLoser() |
|
| 52 | - {
|
|
| 53 | - $isDraw = false; |
|
| 54 | - $winner = $this->homeTeam; |
|
| 55 | - $loser = $this->awayTeam; |
|
| 56 | - if ($this->goalAway == $this->goalHome) {
|
|
| 57 | - $isDraw = true; |
|
| 58 | - } else if ($this->goalHome < $this->goalAway) {
|
|
| 59 | - $winner = $this->awayTeam; |
|
| 60 | - $loser = $this->homeTeam; |
|
| 61 | - } |
|
| 62 | - return [ |
|
| 63 | - 'is_draw' => $isDraw, |
|
| 64 | - 'winner_id' => $winner->id, |
|
| 65 | - 'loser_id' => $loser->id |
|
| 66 | - ]; |
|
| 67 | - } |
|
| 48 | + /** |
|
| 49 | + * @return array |
|
| 50 | + */ |
|
| 51 | + public function getWinnerLoser() |
|
| 52 | + {
|
|
| 53 | + $isDraw = false; |
|
| 54 | + $winner = $this->homeTeam; |
|
| 55 | + $loser = $this->awayTeam; |
|
| 56 | + if ($this->goalAway == $this->goalHome) {
|
|
| 57 | + $isDraw = true; |
|
| 58 | + } else if ($this->goalHome < $this->goalAway) {
|
|
| 59 | + $winner = $this->awayTeam; |
|
| 60 | + $loser = $this->homeTeam; |
|
| 61 | + } |
|
| 62 | + return [ |
|
| 63 | + 'is_draw' => $isDraw, |
|
| 64 | + 'winner_id' => $winner->id, |
|
| 65 | + 'loser_id' => $loser->id |
|
| 66 | + ]; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @return array |
|
| 71 | - */ |
|
| 72 | - public function toArray() |
|
| 73 | - {
|
|
| 74 | - $result = []; |
|
| 75 | - $result['home_team_id'] = $this->homeTeam->id; |
|
| 76 | - $result['away_team_id'] = $this->awayTeam->id; |
|
| 77 | - $result['goal_home'] = $this->goalHome; |
|
| 78 | - $result['goal_away'] = $this->goalAway; |
|
| 79 | - $result['info'] = $this->getWinnerLoser(); |
|
| 80 | - $result['info']['scorers'] = $this->getScorers(); |
|
| 81 | - $result['simulated'] = true; |
|
| 82 | - return $result; |
|
| 83 | - } |
|
| 84 | - /** |
|
| 85 | - * @return array |
|
| 86 | - */ |
|
| 87 | - private function getScorers() |
|
| 88 | - {
|
|
| 89 | - $scorers = [ |
|
| 90 | - 'home' => [], |
|
| 91 | - 'away' => [] |
|
| 92 | - ]; |
|
| 93 | - for ($i = 0; $i < $this->goalHome; $i++) {
|
|
| 94 | - $scorers['home'][] = $this->pickAScorer($this->homeTeam); |
|
| 95 | - } |
|
| 96 | - for ($i = 0; $i < $this->goalAway; $i++) {
|
|
| 97 | - $scorers['away'][] = $this->pickAScorer($this->awayTeam); |
|
| 98 | - } |
|
| 99 | - return $scorers; |
|
| 100 | - } |
|
| 69 | + /** |
|
| 70 | + * @return array |
|
| 71 | + */ |
|
| 72 | + public function toArray() |
|
| 73 | + {
|
|
| 74 | + $result = []; |
|
| 75 | + $result['home_team_id'] = $this->homeTeam->id; |
|
| 76 | + $result['away_team_id'] = $this->awayTeam->id; |
|
| 77 | + $result['goal_home'] = $this->goalHome; |
|
| 78 | + $result['goal_away'] = $this->goalAway; |
|
| 79 | + $result['info'] = $this->getWinnerLoser(); |
|
| 80 | + $result['info']['scorers'] = $this->getScorers(); |
|
| 81 | + $result['simulated'] = true; |
|
| 82 | + return $result; |
|
| 83 | + } |
|
| 84 | + /** |
|
| 85 | + * @return array |
|
| 86 | + */ |
|
| 87 | + private function getScorers() |
|
| 88 | + {
|
|
| 89 | + $scorers = [ |
|
| 90 | + 'home' => [], |
|
| 91 | + 'away' => [] |
|
| 92 | + ]; |
|
| 93 | + for ($i = 0; $i < $this->goalHome; $i++) {
|
|
| 94 | + $scorers['home'][] = $this->pickAScorer($this->homeTeam); |
|
| 95 | + } |
|
| 96 | + for ($i = 0; $i < $this->goalAway; $i++) {
|
|
| 97 | + $scorers['away'][] = $this->pickAScorer($this->awayTeam); |
|
| 98 | + } |
|
| 99 | + return $scorers; |
|
| 100 | + } |
|
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * @param Team $team |
|
| 104 | - * @return Player |
|
| 105 | - */ |
|
| 106 | - private function pickAScorer(Team $team) |
|
| 107 | - {
|
|
| 108 | - $player = null; |
|
| 109 | - if (Randomizer::boolOnPercentage(70)) {
|
|
| 110 | - $roles = Config::get('modules.roles');
|
|
| 111 | - $forwards = array_splice($roles, count($roles) / 2); |
|
| 112 | - $pos = array_rand($forwards); |
|
| 113 | - unset($forwards[$pos]); |
|
| 114 | - $player = $team->getBestPlayerForRole($pos); |
|
| 115 | - while (empty($player)) {
|
|
| 116 | - if (!empty($forwards)) {
|
|
| 117 | - $pos = array_rand($forwards); |
|
| 118 | - unset($forwards[$pos]); |
|
| 119 | - $player = $team->getBestPlayerForRole($pos); |
|
| 120 | - } else {
|
|
| 121 | - $player = $team->roster[array_rand($team->roster)]; |
|
| 122 | - } |
|
| 123 | - } |
|
| 124 | - } else {
|
|
| 125 | - $player = $team->roster[array_rand($team->roster)]; |
|
| 126 | - } |
|
| 127 | - return $player; |
|
| 128 | - } |
|
| 102 | + /** |
|
| 103 | + * @param Team $team |
|
| 104 | + * @return Player |
|
| 105 | + */ |
|
| 106 | + private function pickAScorer(Team $team) |
|
| 107 | + {
|
|
| 108 | + $player = null; |
|
| 109 | + if (Randomizer::boolOnPercentage(70)) {
|
|
| 110 | + $roles = Config::get('modules.roles');
|
|
| 111 | + $forwards = array_splice($roles, count($roles) / 2); |
|
| 112 | + $pos = array_rand($forwards); |
|
| 113 | + unset($forwards[$pos]); |
|
| 114 | + $player = $team->getBestPlayerForRole($pos); |
|
| 115 | + while (empty($player)) {
|
|
| 116 | + if (!empty($forwards)) {
|
|
| 117 | + $pos = array_rand($forwards); |
|
| 118 | + unset($forwards[$pos]); |
|
| 119 | + $player = $team->getBestPlayerForRole($pos); |
|
| 120 | + } else {
|
|
| 121 | + $player = $team->roster[array_rand($team->roster)]; |
|
| 122 | + } |
|
| 123 | + } |
|
| 124 | + } else {
|
|
| 125 | + $player = $team->roster[array_rand($team->roster)]; |
|
| 126 | + } |
|
| 127 | + return $player; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | } |
| 131 | 131 | \ No newline at end of file |