Skip to content

Instantly share code, notes, and snippets.

@fredbradley
Last active February 16, 2024 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fredbradley/c54a0af7b0966a5fa46d5d072ba56bfe to your computer and use it in GitHub Desktop.
Save fredbradley/c54a0af7b0966a5fa46d5d072ba56bfe to your computer and use it in GitHub Desktop.
<?php
namespace App\Actions\GuessWho;
use Illuminate\Support\Collection;
class GenerateGame
{
public Collection $answers;
public function __construct(public Collection $subjects)
{
$this->answers = new Collection();
}
private function addAnswer(): mixed
{
try {
$randomAnswer = $this->subjects->random();
if ($this->answers->contains($randomAnswer)) {
return $this->addAnswer();
}
$this->answers->push($randomAnswer);
return $randomAnswer;
} catch (\Exception $e) {
return $e->getMessage();
}
}
public function makeMultipleQuestions(
string $question,
string $questionKey,
string $answerKey,
int $numberOfQuestions
): Collection {
$questions = [];
for ($i = 0; $i < $numberOfQuestions; $i++) {
$questions[] = $this->makeQuestion($question, $questionKey, $answerKey);
}
return collect($questions);
}
public function makeQuestion(string $question, string $questionKey, string $answerKey): array
{
$answer = $this->addAnswer();
$wrongAnswers = $this->subjects->filter(function ($subject) use ($answer) {
return $subject !== $answer;
})->random(3);
return [
'question' => $question,
'key' => $answer->$questionKey,
'answer' => $answer->$answerKey,
'possibleAnswers' => $wrongAnswers->push($answer)->pluck($answerKey),
];
}
}
<?php
namespace App\Http\Controllers;
use App\Actions\GuessWho\GenerateGame;
use App\Models\GuessWhoGame;
use App\Models\House;
use App\Models\Pupil;
use App\Models\School;
use App\Models\Staff;
use App\Models\TeachingSet;
use App\Models\VisitingMusicTeacher;
use Illuminate\Http\Request;
class GuessWhoController extends Controller
{
public function leaderboard()
{
$scores = GuessWhoGame::select('user_id')
->selectRaw('max(highestStreak) as highestStreak, max(updated_at) as updated_at, max(created_at) as created_at')
->orderByDesc('highestStreak')
->groupBy('user_id')
->limit(10)
->get();
return view('guess-who.leaderboard', compact('scores'));
}
public function index()
{
return view('guess-who.index');
}
public function game(Request $request)
{
$selection = match ($request->get('quick-selection')) {
'tutees' => auth()->user()->staff->tutees,
'house' => House::find($request->get('house'))->pupils()->current()->get(),
'school' => School::find($request->get('school'))->pupils()->current()->get(),
'staff' => Staff::current()->whereHas('photograph')->get(),
'vmts' => Staff::current()->findMany(VisitingMusicTeacher::pluck('staff_id')),
default => Pupil::current()->whereHas('photograph')->get(),
};
if ($request->has('nc-year')) {
$selection = Pupil::current()->where('nc_year', $request->get('nc-year'))->whereHas('photograph')->get();
}
if ($request->has('academic-set')) {
$selection = TeachingSet::find($request->get('academic-set'))->pupils;
}
$questionLimit = 10;
$game = new GenerateGame($selection);
$questions = $game->makeMultipleQuestions(
'Who is this?',
'photograph',
'name',
$selection->count() > $questionLimit ? $questionLimit : $selection->count()
);
return view('guess-who.game', compact('questions'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment