/** * A basic functional test example. * * @return void */ public function testViewingAClue() { $faker = Faker\Factory::create(); $user = factory(User::class)->create(['username' => 'jondoe1']); $name = $faker->sentence; $width = $faker->numberBetween(5, 20); $height = $faker->numberBetween(5, 20); $blackSquares = array(); for ($i = 1; $i < $faker->numberBetween(2, 20); $i++) { $blackSquares[] = $faker->numberBetween(1, $width) . '-' . $faker->numberBetween(1, $height); } $args = array('name' => $name, 'width' => $width, 'height' => $height, 'blackSquares' => $blackSquares, 'user_id' => $user->id); $puzzleTemplate = PuzzleTemplate::create($args); $user->puzzleTemplates()->save($puzzleTemplate); $name = $faker->sentence; $puzzle_squares = array(); $ordinal = 1; $clues = array(); for ($row = 1; $row <= $height; $row++) { for ($col = 1; $col <= $width; $col++) { if (!in_array($row . '-' . $col, $puzzleTemplate->blackSquares())) { $letter = substr($faker->word, 0, 1); $puzzle_squares[] = array('row' => $row, 'col' => $col, 'letter' => $letter, 'square_type' => 'white'); } if ($row == 1 || in_array($row - 1 . '-' . $col, $puzzleTemplate->blackSquares())) { //needs a down clue $clue = new Clue(); $clue->clue = $faker->sentence; $clue->ordinal = $ordinal; $clue->direction = 'down'; $clues[] = $clue; } if ($col == 1 || in_array($row . '-' . ($col - 1), $puzzleTemplate->blackSquares())) { //needs an across clue $clue = new Clue(); $clue->clue = $faker->sentence; $clue->ordinal = $ordinal; $clue->direction = 'across'; $clues[] = $clue; } if ($row == 1 || in_array($row - 1 . '-' . $col, $puzzleTemplate->blackSquares()) || $col == 1 || in_array($row . '-' . ($col - 1), $puzzleTemplate->blackSquares())) { $ordinal++; } } } $args = array('name' => $name, 'user_id' => $user->id, 'puzzle_template_id' => $puzzleTemplate->id, 'puzzle_squares' => $puzzle_squares, 'clues' => $clues); $puzzle = Puzzle::create($args); $user->puzzles()->save($puzzle); $clue_str = $faker->sentence; $args = array('puzzle_slug' => $puzzle->slug, 'clue' => $clue_str, 'ordinal' => 1, 'direction' => 'across'); $clue = Clue::replace($args); $this->assertEquals($clue->clue, $clue_str); $this->assertEquals($clue->puzzle_id, $puzzle->id); $this->visit('/puzzles/' . $puzzle->slug)->see($clue_str); }
public static function replace(array $args = array()) { $v = new Clue(); if ($v->validate($args)) { $puzzle = Puzzle::findBySlug($args['puzzle_slug']); if (!$puzzle) { return array('errors' => array('Puzzle not found')); } $c = self::where('puzzle_id', $puzzle->id)->where('ordinal', $args['ordinal'])->where('direction', $args['direction'])->first(); if (!$c) { $c = new Clue(); } $c->clue = $args['clue']; $c->puzzle_id = $puzzle->id; $c->ordinal = $args['ordinal']; $c->direction = $args['direction']; $c->save(); return $c; } else { return array('errors' => $v->errors); } }
public function postClue() { $user = Auth::user(); if (!$user) { $msg = 'Please log in'; $returnData = array('errors' => array($msg)); return response()->json($returnData, 401); } if (!Input::has('puzzle_slug')) { $msg = 'Invalid input: no puzzle slug'; $returnData = array('errors' => array($msg)); return response()->json($returnData, 400); } $slug = Input::get('puzzle_slug'); $p = Puzzle::findBySlug($slug); if ($user->id != $p->user_id) { $msg = 'This isn\'t your puzzle'; $returnData = array('errors' => array($msg)); return response()->json($returnData, 401); } $args = Input::all(); $c = Clue::replace($args); return $c; }
/** * A basic functional test example. * * @return void */ public function testGuessingTheLastCorrectSquare() { $faker = Faker\Factory::create(); $user_creator = factory(User::class)->create(['username' => 'trevorrosenthal']); $user_guesser = factory(User::class)->create(['username' => 'nickhundley']); $name = $faker->sentence; $width = 5; $height = 5; $blackSquares = array('1-1', '5-5'); $args = array('name' => $name, 'width' => $width, 'height' => $height, 'blackSquares' => $blackSquares, 'user_id' => $user_creator->id); $puzzleTemplate = PuzzleTemplate::create($args); $user_creator->puzzleTemplates()->save($puzzleTemplate); $name = $faker->sentence; $puzzleSquares = array(); $clues = array(); $args = array('name' => $name, 'puzzle_template_id' => $puzzleTemplate->id, 'puzzle_squares' => $puzzleSquares, 'clues' => $clues, 'user_id' => $user_creator->id); $puzzle = Puzzle::create($args); $firstword = "shop"; $secondword = "shove"; $thirdword = "wanes"; $fourthword = "arent"; $fifthword = "beds"; for ($k = 0; $k < strlen($firstword); $k++) { $args = array('row' => 1, 'col' => $k + 2, 'letter' => substr($firstword, $k, 1), 'puzzle_id' => $puzzle->id); PuzzleSquare::replace($args); } for ($k = 0; $k < strlen($secondword); $k++) { $args = array('row' => 2, 'col' => $k + 1, 'letter' => substr($secondword, $k, 1), 'puzzle_id' => $puzzle->id); PuzzleSquare::replace($args); } for ($k = 0; $k < strlen($thirdword); $k++) { $args = array('row' => 3, 'col' => $k + 1, 'letter' => substr($thirdword, $k, 1), 'puzzle_id' => $puzzle->id); PuzzleSquare::replace($args); } for ($k = 0; $k < strlen($fourthword); $k++) { $args = array('row' => 4, 'col' => $k + 1, 'letter' => substr($fourthword, $k, 1), 'puzzle_id' => $puzzle->id); PuzzleSquare::replace($args); } for ($k = 0; $k < strlen($fifthword); $k++) { $args = array('row' => 5, 'col' => $k + 1, 'letter' => substr($fifthword, $k, 1), 'puzzle_id' => $puzzle->id); PuzzleSquare::replace($args); } $clue_str = $faker->sentence; $across_clues = array(array('ordinal' => 1, 'clue' => 'Woodworking locale'), array('ordinal' => 5, 'clue' => 'Forcefully move'), array('ordinal' => 6, 'clue' => 'Lessens'), array('ordinal' => 7, 'clue' => '"I\'m right, _____ I?'), array('ordinal' => 8, 'clue' => 'Places for flowers')); $down_clues = array(array('ordinal' => 1, 'clue' => 'Unit of stock'), array('ordinal' => 2, 'clue' => 'Sharpened (as skills)'), array('ordinal' => 3, 'clue' => 'Baking appliances'), array('ordinal' => 4, 'clue' => 'Insect'), array('ordinal' => 5, 'clue' => 'Gently Brush')); foreach ($across_clues as $clue) { $args = array('puzzle_slug' => $puzzle->slug, 'clue' => $clue['clue'], 'ordinal' => $clue['ordinal'], 'direction' => 'across'); $clue = Clue::replace($args); } foreach ($down_clues as $clue) { $args = array('puzzle_slug' => $puzzle->slug, 'clue' => $clue['clue'], 'ordinal' => $clue['ordinal'], 'direction' => 'down'); $clue = Clue::replace($args); } $ret = $puzzle->activate(); for ($k = 0; $k < strlen($firstword); $k++) { $args = array('row' => 1, 'col' => $k + 2, 'letter' => substr($firstword, $k, 1), 'puzzle_slug' => $puzzle->slug, 'user_id' => $user_guesser->id); $pgs = PuzzleGuessSquare::replace($args); } $this->assertEquals($pgs['solved'], '0'); for ($k = 0; $k < strlen($secondword); $k++) { $args = array('row' => 2, 'col' => $k + 1, 'letter' => substr($secondword, $k, 1), 'puzzle_slug' => $puzzle->slug, 'user_id' => $user_guesser->id); $pgs = PuzzleGuessSquare::replace($args); } $this->assertEquals($pgs['solved'], '0'); for ($k = 0; $k < strlen($thirdword); $k++) { $args = array('row' => 3, 'col' => $k + 1, 'letter' => substr($thirdword, $k, 1), 'puzzle_slug' => $puzzle->slug, 'user_id' => $user_guesser->id); $pgs = PuzzleGuessSquare::replace($args); } $this->assertEquals($pgs['solved'], '0'); for ($k = 0; $k < strlen($fourthword); $k++) { $args = array('row' => 4, 'col' => $k + 1, 'letter' => substr($fourthword, $k, 1), 'puzzle_slug' => $puzzle->slug, 'user_id' => $user_guesser->id); $pgs = PuzzleGuessSquare::replace($args); } $this->assertEquals($pgs['solved'], '0'); for ($k = 0; $k < strlen($fifthword); $k++) { $args = array('row' => 5, 'col' => $k + 1, 'letter' => substr($fifthword, $k, 1), 'puzzle_slug' => $puzzle->slug, 'user_id' => $user_guesser->id); $pgs = PuzzleGuessSquare::replace($args); } $this->assertEquals($pgs['solved'], '1'); }