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 diff --git a/projects/rpsls-laravel-example/app/controllers/HomeController.php b/projects/rpsls-laravel-example/app/controllers/HomeController.php index 528108d..6ff7a8a 100644 --- a/projects/rpsls-laravel-example/app/controllers/HomeController.php +++ b/projects/rpsls-laravel-example/app/controllers/HomeController.php @@ -1,56 +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 showChooser() - { - return View::make('chooser'); - } + class HomeController extends BaseController { + + var $actionLog, $actionLogTransformer; + + public function __construct(ActionLogInterface $actionLog, TransformerInterface $actionLogTransformer) + { + $this->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 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' + ]); + + if ( $validator->passes() ) + { + // Represents the user + $judge = new \RPSLS\Referee(); + + // Represents the server + $player = new \RPSLS\Player(); - public function throwAction($action) - { - $validator = Validator::make(['action' => $action], [ - 'action' => 'required|in:rock,paper,scissors,lizard,spock' - ]); - if($validator->passes()) - { - $judge = new \RPSLS\Referee(); - $player = new \RPSLS\Player(); - $myAction = $player->chooseAction(); - $results = $judge->judgeResults($action, $myAction); - $record = $this->actionLog->create( - $this->actionLogTransformer->transform([ - 'user_action' => $action, - 'ai_action' => $myAction, - 'results' => $results, - ]) - ); - return View::make('chooser')->with( - ['results' => $results, 'yourChoice' => $action, 'myChoice' => $myAction] - ); - } - Session::flash('error', $validator->messages()); - return Redirect::route('chooser'); - } - -} + // 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, + 'ai_action' => $myAction, + '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..6f22580 100644 --- a/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php +++ b/projects/rpsls-laravel-example/app/lib/RPSLS/Player.php @@ -1,19 +1,23 @@ options[array_rand($this->options)]; - } -} \ No newline at end of file + + /** + * 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 8042454..6f6fcb2 100644 --- a/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php +++ b/projects/rpsls-laravel-example/app/lib/RPSLS/Referee.php @@ -1,48 +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', - ], - ]; - $result = $rules[$throw1][$throw2]; - return $result; - } -} \ No newline at end of file + class Referee { + + 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; + } + } diff --git a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php index f4a39e7..b98b978 100644 --- a/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php +++ b/projects/rpsls-laravel-example/app/lib/Repositories/ActionLogEloquent.php @@ -1,19 +1,33 @@ user_action = $data['user_action']; - return ActionLog::create($data); - } -} \ No newline at end of file + /** + * Return specific logged action + * @param $id integer + */ + public function find($id) + { + return ActionLog::find($id); + } + + /** + * 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 932120b..95557b6 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 @@