Last active
December 17, 2021 19:09
-
-
Save susanBuck/be1db9a113f542157d432772b53e4d4c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Controllers; | |
class AppController extends Controller | |
{ | |
/** | |
* | |
*/ | |
public function index() | |
{ | |
# Player Log variables flash data | |
$player_name = $this->app->old('player_name'); | |
$timein = $this->app->old('$timein'); | |
$player_saved = $this->app->old('player_saved'); | |
$competitor = $this->app->old('competitor'); | |
# If the game was just played, we'll have a "timestamp" in flash data | |
# If it exists, use it to look up the results frmo the database | |
$timestamp = $this->app->old('timestamp'); | |
if ($timestamp) { | |
$round = $this->app->db()->findByColumn('rounds', 'timestamp', '=', $timestamp); | |
$round = $round[0]; # Narrow it down to the first row returned | |
# Decode the turn "arrays" that were stored as strings in the db | |
$round['player_turns'] = $this->convertTurnsFromStringToArray($round['player_turns']); | |
$round['opponent_turns'] = $this->convertTurnsFromStringToArray($round['opponent_turns']); | |
} | |
# Show the round on the page | |
return $this->app->view('index', [ | |
'player_name' => $player_name, | |
'timein' => $timein, | |
'player_saved' => $player_saved, | |
'competitor' => $competitor, | |
'round' => $round ?? null, | |
]); | |
} | |
/** | |
* | |
*/ | |
public function game() | |
{ | |
# Intiailize variables | |
$goal = 25; | |
$player_sum = 0; | |
$player_turns = []; | |
$opponent_sum = 0; | |
$opponent_turns = []; | |
$timestamp = date('Y-m-d H:i:s'); | |
# Play the game | |
while ($player_sum <= $goal && $opponent_sum <= $goal) { | |
# Player | |
$points = $this->getPoints(); # Helper function | |
$player_sum += $points; # Accumulate points | |
$player_turns[] = [$points, $player_sum]; # Build an array of turns, storing both the points and their sum | |
# Opponent | |
$points = $this->getPoints(); # Helper function | |
$opponent_sum += $points; # Accumulate points | |
$opponent_turns[] = [$points, $opponent_sum]; # Build an array of turns, storing both the points and their sum | |
} | |
# Player or opponent has reached goal; determine winner | |
if ($player_sum >= $goal or $opponent_sum >= $goal) { | |
$winner = $player_sum >= $goal ? 'Player' : 'Winner'; | |
} | |
# Array of round data to insert into the database | |
$round = [ | |
'timestamp' => $timestamp, | |
'player_turns' => $this->convertTurnsFromArrayToString($player_turns), | |
'opponent_turns' => $this->convertTurnsFromArrayToString($opponent_turns), | |
'winner' => $winner | |
]; | |
# Insert the round data into the database | |
$this->app->db()->insert('rounds', $round); | |
# Redirect back to the homepage where we'll show the results | |
$this->app->redirect('/', ['timestamp' => $timestamp]); | |
} | |
/** | |
* | |
*/ | |
public function history() | |
{ | |
$rounds = $this->app->db()->all('rounds'); | |
return $this->app->view('history', ['rounds' => $rounds]); | |
} | |
/** | |
* | |
*/ | |
public function round() | |
{ | |
# Get the id from the URL/query string | |
$id = $this->app->param('id'); | |
# Retrieve the round data from the database | |
$round = $this->app->db()->findById('rounds', $id); | |
# Convert the turns data back to an array | |
$round['player_turns'] = $this->convertTurnsFromStringToArray($round['player_turns']); | |
$round['opponent_turns'] = $this->convertTurnsFromStringToArray($round['opponent_turns']); | |
# Load the view, including the round data | |
return $this->app->view('round', ['round' => $round]); | |
} | |
/** | |
* | |
*/ | |
public function playerlog() | |
{ | |
$this->app->validate([ | |
'player_name' => 'required|alphaNumericDash|minLength:10', | |
]); | |
$id = $this->app->param('id'); | |
$player_name = $this->app->input('player_name'); | |
$competitor = $this->app->input('competitor'); | |
$timein = date('Y-m-d H:i:s'); | |
$this->app->db()->insert('players', [ | |
'id' => $id, | |
'player_name' => $player_name, | |
'competitor' => $competitor, | |
'timein' => date('Y-m-d H:i:s') | |
]); | |
$player = $player_saved; | |
$players = $this->app->db()->all('players'); | |
$this->app->redirect('/', [ | |
'player_saved' => true, | |
'id' => $this->app-> param('id'), | |
'player_name' => $this->app->input('player_name'), | |
'competitor' => $this->app->input('competitor'), | |
'timein' => $this->app->input('timein') | |
]); | |
} | |
/** | |
* | |
*/ | |
private function convertTurnsFromArrayToString($turns) | |
{ | |
$results = ''; | |
foreach ($turns as $turn) { | |
$turnAsString = implode(",", $turn); # Convert each turn to a string | |
$results .= $turnAsString . "|"; | |
} | |
$results = trim($results, '|'); # Remove trailing | | |
return $results; | |
} | |
/** | |
* | |
*/ | |
private function convertTurnsFromStringToArray($turns) | |
{ | |
$results = []; | |
$turns = explode('|', $turns); | |
foreach ($turns as $turn) { | |
$turn = explode(',', $turn); | |
$results[] = $turn; | |
} | |
return $results; | |
} | |
/** | |
* | |
*/ | |
public function register() | |
{ | |
$player_saved = $this->app->old('player_saved'); | |
$player = $player_saved; | |
$players = $this->app->db()->all('players'); | |
return $this->app->view('register', ['players' => $players]); | |
} | |
/** | |
* | |
*/ | |
private function getPoints() | |
{ | |
# Create the points with conditions from the outcome of each "turn" | |
# Utilized in the while loop which accumulates each "turn" until "goal or more" | |
# Array of special numbers, keys=> are value that determine points, | |
# The =>value is the random number that begins each turn | |
$special_numbers = [ | |
11 => 5, # Magic | |
6 => 4, # Bonus | |
0 => 9, # Curse | |
1 => 2, # Mystic | |
15 => 7 # Wild | |
]; | |
$random_number = random_int(1, 10); | |
# Check if random number generated is "special" | |
$special_number = array_search($random_number, $special_numbers); | |
return ($special_number) ? $special_number : $random_number; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('templates.master') | |
@section('title') | |
Game History | |
@endsection | |
@section('content') | |
<h2 class='center margin'>Game History (Eachdraidh geama)</h2> | |
<div class='center'> | |
<ul class='left'> | |
@foreach ($rounds as $round) | |
<li><a href='/round?id={{ $round['id'] }}'>Round: {{ $round['id'] }} – {{ $round['timestamp'] }}</a> | |
</li> | |
@endforeach | |
</ul> | |
</div> | |
<p class='center'>← <a href='/' class='center'>Return to Game (Till air ais chun gheama)</a></p> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('templates.master') | |
{{-- @section('title') | |
Game of Gnomes | |
@endsection --}} | |
@section('content') | |
<h2 class='center margin'>Welcome (M'ath)</h2> | |
<form method='POST' action='/playerlog' id='playerlog' class='center'> | |
<input type='hidden' name='timein' value='{{ $timein }}'> | |
<div> | |
<p>Select which competitor you want to be.</p> | |
<input type='radio' name='competitor' value='player'> | |
<label for='player'>Player </label> | |
<input type='radio' name='competitor' value='opponent' checked> | |
<label for='competitor'>Opponent</label> | |
</div> | |
<div> | |
@if ($app->errorsExist()) | |
<ul test='name-bad-input' test='validation-errors-alert' class='error alert alert-danger'> | |
@foreach ($app->errors() as $error) | |
<li>{{ $error }}</li> | |
@endforeach | |
</ul> | |
@endif | |
@if ($player_saved) | |
<div test='validation-errors-alert' class='alert alert-success margin'> Thank you, | |
{{ $app->old('player_name') }} your name is registered.</div> | |
@endif | |
<label for='player_name'>Name</label> | |
<input test='name-input' type='text' name='player_name' id='player_name' placeholder=' * Your Name' autofocus> | |
<button test='submit-button' 'type=' submit' class='btn' id='enter' class='inline'>Enter</button> | |
<p class='detail'>*Name is required to play. | |
Min. length: 10 characters; <br>alpha-numeric, dashes, underscores only</p> | |
<p class='center'><a href='/register' class='center'> View Registered Players Log </a> →</p> | |
</div> | |
</form> | |
<div class='center'> | |
<form method='POST' action='/game' id='game'> | |
<button test='submit-play-button' type='submit' name='play' id='play' value='play' class='btn'>Play | |
Game!</button> | |
</form> | |
{{-- Display Output of Results from AppController --}} | |
@if ($round) | |
<div> | |
<dl> | |
<dt class='left'>Player turn:</dt> | |
<dd class='inline left'> | |
@foreach ($round['player_turns'] as $turn) | |
Roll: {{ $turn[0] }} → {{ $turn[1] }}<br> | |
@endforeach | |
</dd> | |
<dt class='left'>Opponent turn:</dt> | |
<dd class='inline left'> | |
@foreach ($round['opponent_turns'] as $turn) | |
Roll: {{ $turn[0] }} → {{ $turn[1] }}<br> | |
@endforeach | |
</dd> | |
<dt class='left'>Winner: {{ $round['winner'] }}</dt> | |
<dd class='left'>Round: {{ $round['id'] }}</dd> | |
<dd class='left'> {{ $round['timestamp'] }}</dd> | |
</dl> | |
</div> | |
@endif | |
<p><a href='/history' class='center'> View Game History </a> →</p> | |
<p class='details'>Faic eachdraidh geama</p> | |
</div> | |
<h3 class='left'>Game Instructions</h3> | |
<ul class='left'> | |
<li>Select Player or the Opponent</li> | |
<li>Start the game by pressing "Play" which initiates a random number from 1-10 for moves toward the "Goal" which is | |
25</li> | |
<li>The moves are recorded and accumulate sums for each turn</li> | |
<li>Some numbers generated will produce special moves (positive and negative!) | |
<ul> | |
<li>Number 5 is "Magic" moves 11 instead</li> | |
<li>Number 4 is "Bonus" moves 6 instead</li> | |
<li>Number 9 is "Curse" moves 0 instead</li> | |
<li>Number 2 is "Mystic" moves 1 instead</li> | |
<li>Number 7 is "Wild" moves 15 instead</li> | |
</ul> | |
<li>"Turns" continue until the score reaches or exceeds the "Goal".</li> | |
<li> Whomever reaches "Goal" first Wins!</li> | |
</ul> | |
@endsection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@extends('templates.master') | |
@section('title') | |
Round Details | |
@endsection | |
@section('content') | |
<h2 class="center">Round Details (Cuairt)</h2> | |
<dl> | |
<dt>Round: {{ $round['id'] }}</dt> | |
<dd>Timestamp: {{ $round['timestamp'] }}</dd> | |
<dt class='left'>Player turn:</dt> | |
<dd class='inline'> | |
@foreach ($round['player_turns'] as $turn) | |
Roll: {{ $turn[0] }} → {{ $turn[1] }}<br> | |
@endforeach | |
</dd> | |
<dt class='left'>Opponent turn:</dt> | |
<dd class='inline'> | |
@foreach ($round['opponent_turns'] as $turn) | |
Roll: {{ $turn[0] }} → {{ $turn[1] }}<br> | |
@endforeach | |
</dd> | |
<dt class='left'>Winner: {{ $round['winner'] }}</dt> | |
</dl> | |
<a href='/history' class='center'> ← Return to History (Till air ais gu eachdraidh) </a></p> | |
@endsection |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment