From 41ee8a24299235bf39179376d4d51db9fa2dfc57 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 21 Jan 2015 21:44:42 -0800 Subject: [PATCH 1/3] Ignore PHPStorm files --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index aa5997f..d415457 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ composer.phar vendor/ - +.idea* # Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file # composer.lock \ No newline at end of file From cb4c0d04b93e1eb45258d1b1fa8031a35d23fd3e Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Wed, 21 Jan 2015 21:44:57 -0800 Subject: [PATCH 2/3] Start Docs and comments because why not? --- .../app/controllers/HomeController.php | 24 +++++- .../app/lib/RPSLS/Player.php | 18 ++-- .../app/lib/RPSLS/Referee.php | 85 ++++++++++--------- .../lib/Repositories/ActionLogEloquent.php | 20 ++++- .../lib/Repositories/ActionLogInterface.php | 22 ++++- 5 files changed, 112 insertions(+), 57 deletions(-) diff --git a/projects/rpsls-laravel-example/app/controllers/HomeController.php b/projects/rpsls-laravel-example/app/controllers/HomeController.php index 528108d..c6c2578 100644 --- a/projects/rpsls-laravel-example/app/controllers/HomeController.php +++ b/projects/rpsls-laravel-example/app/controllers/HomeController.php @@ -24,20 +24,36 @@ public function __construct(ActionLogInterface $actionLog, TransformerInterface public function showChooser() { + // Load default view return View::make('chooser'); } public function throwAction($action) { + /* + * Make sure the action given is valid + * /throw/rock works... + * /throw/nothing doesn't... + */ $validator = Validator::make(['action' => $action], [ - 'action' => 'required|in:rock,paper,scissors,lizard,spock' - ]); + 'action' => 'required|in:rock,paper,scissors,lizard,spock' + ]); + if($validator->passes()) { + // Represents the user $judge = new \RPSLS\Referee(); + + // Represents the server $player = new \RPSLS\Player(); + + // Chose server action $myAction = $player->chooseAction(); + + // Who won? $results = $judge->judgeResults($action, $myAction); + + // Record results to persistance $record = $this->actionLog->create( $this->actionLogTransformer->transform([ 'user_action' => $action, @@ -45,10 +61,14 @@ public function throwAction($action) 'results' => $results, ]) ); + + // Display results to user return View::make('chooser')->with( ['results' => $results, 'yourChoice' => $action, 'myChoice' => $myAction] ); } + + // Validation failed...pass an error message to the user Session::flash('error', $validator->messages()); return Redirect::route('chooser'); } diff --git a/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php b/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php index cf40410..6b3356a 100644 --- a/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php +++ b/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php @@ -5,15 +5,19 @@ class Player { var $options = [ - 'rock', - 'paper', - 'scissors', - 'lizard', - 'spock', + 'rock', + 'paper', + 'scissors', + 'lizard', + 'spock', ]; - public function chooseAction() + + /** + * Get the Player's action + */ + public function chooseAction() { return $this->options[array_rand($this->options)]; } -} \ No newline at end of file +} diff --git a/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php b/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php index 8042454..f5a29fd 100644 --- a/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php +++ b/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php @@ -3,46 +3,51 @@ namespace RPSLS; class Referee { - public function judgeResults($throw1, $throw2) + + protected $rules = [ + 'rock' => [ + 'rock' => 'draw', + 'paper' => 'lose', + 'scissors' => 'win', + 'lizard' => 'win', + 'spock' => 'lose', + ], + 'paper' => [ + 'rock' => 'win', + 'paper' => 'draw', + 'scissors' => 'lose', + 'lizard' => 'lose', + 'spock' => 'win', + ], + 'scissors' => [ + 'rock' => 'lose', + 'paper' => 'win', + 'scissors' => 'draw', + 'lizard' => 'win', + 'spock' => 'lose', + ], + 'lizard' => [ + 'rock' => 'lose', + 'paper' => 'win', + 'scissors' => 'lose', + 'lizard' => 'tie', + 'spock' => 'win', + ], + 'spock' => [ + 'rock' => 'win', + 'paper' => 'lose', + 'scissors' => 'win', + 'lizard' => 'lose', + 'spock' => 'draw', + ], + ]; + + /** + * Return winner of match + */ + public function judgeResults($throw1, $throw2) { - $rules = [ - 'rock' => [ - 'rock' => 'draw', - 'paper' => 'lose', - 'scissors' => 'win', - 'lizard' => 'win', - 'spock' => 'lose', - ], - 'paper' => [ - 'rock' => 'win', - 'paper' => 'draw', - 'scissors' => 'lose', - 'lizard' => 'lose', - 'spock' => 'win', - ], - 'scissors' => [ - 'rock' => 'lose', - 'paper' => 'win', - 'scissors' => 'draw', - 'lizard' => 'win', - 'spock' => 'lose', - ], - 'lizard' => [ - 'rock' => 'lose', - 'paper' => 'win', - 'scissors' => 'lose', - 'lizard' => 'tie', - 'spock' => 'win', - ], - 'spock' => [ - 'rock' => 'win', - 'paper' => 'lose', - 'scissors' => 'win', - 'lizard' => 'lose', - 'spock' => 'draw', - ], - ]; - $result = $rules[$throw1][$throw2]; + $result = $this->rules[$throw1][$throw2]; return $result; } -} \ No newline at end of file +} diff --git a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php index f4a39e7..0471c10 100644 --- a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php +++ b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php @@ -2,18 +2,30 @@ class ActionLogEloquent implements ActionLogInterface { - public function all(){ + + /** + * Return all logged actions + */ + public function all(){ return ActionLog::all(); } - public function find($id){ + /** + * Return specific logged action + * @param $id integer + */ + public function find($id){ return ActionLog::find($id); } - public function create($data) + /** + * Create a logged action + * @param $data array + */ + public function create(array $data) { $record = new ActionLog; $record->user_action = $data['user_action']; return ActionLog::create($data); } -} \ No newline at end of file +} diff --git a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogInterface.php b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogInterface.php index 932120b..4fbe5eb 100644 --- a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogInterface.php +++ b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogInterface.php @@ -1,7 +1,21 @@ Date: Wed, 21 Jan 2015 21:58:06 -0800 Subject: [PATCH 3/3] Auto-formatting --- .../app/controllers/HomeController.php | 126 +++++++++--------- .../app/lib/RPSLS/Player.php | 34 ++--- .../app/lib/RPSLS/Referee.php | 97 +++++++------- .../lib/Repositories/ActionLogEloquent.php | 54 ++++---- .../lib/Repositories/ActionLogInterface.php | 32 ++--- 5 files changed, 174 insertions(+), 169 deletions(-) diff --git a/projects/rpsls-laravel-example/app/controllers/HomeController.php b/projects/rpsls-laravel-example/app/controllers/HomeController.php index c6c2578..6ff7a8a 100644 --- a/projects/rpsls-laravel-example/app/controllers/HomeController.php +++ b/projects/rpsls-laravel-example/app/controllers/HomeController.php @@ -1,76 +1,78 @@ actionLog = $actionLog; - $this->actionLogTransformer = $actionLogTransformer; - } - /* - |-------------------------------------------------------------------------- - | Default Home Controller - |-------------------------------------------------------------------------- - | - | You may wish to use controllers instead of, or in addition to, Closure - | based routes. That's great! Here is an example controller method to - | get you started. To route to this controller, just add the route: - | - | Route::get('/', 'HomeController@showWelcome'); - | - */ + public function __construct(ActionLogInterface $actionLog, TransformerInterface $actionLogTransformer) + { + $this->actionLog = $actionLog; + $this->actionLogTransformer = $actionLogTransformer; + } - public function showChooser() - { - // Load default view - return View::make('chooser'); - } - - public function throwAction($action) - { /* - * Make sure the action given is valid - * /throw/rock works... - * /throw/nothing doesn't... - */ - $validator = Validator::make(['action' => $action], [ - 'action' => 'required|in:rock,paper,scissors,lizard,spock' - ]); + |-------------------------------------------------------------------------- + | Default Home Controller + |-------------------------------------------------------------------------- + | + | You may wish to use controllers instead of, or in addition to, Closure + | based routes. That's great! Here is an example controller method to + | get you started. To route to this controller, just add the route: + | + | Route::get('/', 'HomeController@showWelcome'); + | + */ + + public function showChooser() + { + // Load default view + return View::make('chooser'); + } - if($validator->passes()) - { - // Represents the user - $judge = new \RPSLS\Referee(); + public function throwAction($action) + { + /* + * Make sure the action given is valid + * /throw/rock works... + * /throw/nothing doesn't... + */ + $validator = Validator::make(['action' => $action], [ + 'action' => 'required|in:rock,paper,scissors,lizard,spock' + ]); - // Represents the server - $player = new \RPSLS\Player(); + if ( $validator->passes() ) + { + // Represents the user + $judge = new \RPSLS\Referee(); - // Chose server action - $myAction = $player->chooseAction(); + // Represents the server + $player = new \RPSLS\Player(); - // Who won? - $results = $judge->judgeResults($action, $myAction); + // Chose server action + $myAction = $player->chooseAction(); - // Record results to persistance - $record = $this->actionLog->create( - $this->actionLogTransformer->transform([ - 'user_action' => $action, - 'ai_action' => $myAction, - 'results' => $results, - ]) - ); + // Who won? + $results = $judge->judgeResults($action, $myAction); - // Display results to user - return View::make('chooser')->with( - ['results' => $results, 'yourChoice' => $action, 'myChoice' => $myAction] - ); - } + // Record results to persistance + $record = $this->actionLog->create( + $this->actionLogTransformer->transform([ + 'user_action' => $action, + 'ai_action' => $myAction, + 'results' => $results, + ]) + ); - // Validation failed...pass an error message to the user - Session::flash('error', $validator->messages()); - return Redirect::route('chooser'); - } + // Display results to user + return View::make('chooser')->with( + ['results' => $results, 'yourChoice' => $action, 'myChoice' => $myAction] + ); + } -} + // Validation failed...pass an error message to the user + Session::flash('error', $validator->messages()); + + return Redirect::route('chooser'); + } + + } diff --git a/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php b/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php index 6b3356a..6f22580 100644 --- a/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php +++ b/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php @@ -1,23 +1,23 @@ options[array_rand($this->options)]; - } -} + /** + * Get the Player's action + */ + public function chooseAction() + { + return $this->options[array_rand($this->options)]; + } + } diff --git a/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php b/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php index f5a29fd..6f6fcb2 100644 --- a/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php +++ b/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php @@ -1,53 +1,54 @@ [ - 'rock' => 'draw', - 'paper' => 'lose', - 'scissors' => 'win', - 'lizard' => 'win', - 'spock' => 'lose', - ], - 'paper' => [ - 'rock' => 'win', - 'paper' => 'draw', - 'scissors' => 'lose', - 'lizard' => 'lose', - 'spock' => 'win', - ], - 'scissors' => [ - 'rock' => 'lose', - 'paper' => 'win', - 'scissors' => 'draw', - 'lizard' => 'win', - 'spock' => 'lose', - ], - 'lizard' => [ - 'rock' => 'lose', - 'paper' => 'win', - 'scissors' => 'lose', - 'lizard' => 'tie', - 'spock' => 'win', - ], - 'spock' => [ - 'rock' => 'win', - 'paper' => 'lose', - 'scissors' => 'win', - 'lizard' => 'lose', - 'spock' => 'draw', - ], - ]; + protected $rules = [ + 'rock' => [ + 'rock' => 'draw', + 'paper' => 'lose', + 'scissors' => 'win', + 'lizard' => 'win', + 'spock' => 'lose', + ], + 'paper' => [ + 'rock' => 'win', + 'paper' => 'draw', + 'scissors' => 'lose', + 'lizard' => 'lose', + 'spock' => 'win', + ], + 'scissors' => [ + 'rock' => 'lose', + 'paper' => 'win', + 'scissors' => 'draw', + 'lizard' => 'win', + 'spock' => 'lose', + ], + 'lizard' => [ + 'rock' => 'lose', + 'paper' => 'win', + 'scissors' => 'lose', + 'lizard' => 'tie', + 'spock' => 'win', + ], + 'spock' => [ + 'rock' => 'win', + 'paper' => 'lose', + 'scissors' => 'win', + 'lizard' => 'lose', + 'spock' => 'draw', + ], + ]; - /** - * Return winner of match - */ - public function judgeResults($throw1, $throw2) - { - $result = $this->rules[$throw1][$throw2]; - return $result; - } -} + /** + * Return winner of match + */ + public function judgeResults($throw1, $throw2) + { + $result = $this->rules[$throw1][$throw2]; + + return $result; + } + } diff --git a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php index 0471c10..b98b978 100644 --- a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php +++ b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php @@ -1,31 +1,33 @@ user_action = $data['user_action']; - return ActionLog::create($data); - } -} + /** + * Create a logged action + * @param $data array + */ + public function create(array $data) + { + $record = new ActionLog; + $record->user_action = $data['user_action']; + + return ActionLog::create($data); + } + } diff --git a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogInterface.php b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogInterface.php index 4fbe5eb..95557b6 100644 --- a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogInterface.php +++ b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogInterface.php @@ -1,21 +1,21 @@