Beispiel #1
0
 public static function create(array $args = array())
 {
     $v = new Word();
     if ($v->validate($args)) {
         $w = new Word();
         $word = preg_replace("[^a-zA-Z]", "", $args['word']);
         $w->word = $word;
         $w->length = strlen($args['word']);
         $w->user_id = $args['user_id'];
         $w->timestamp_utc = time();
         $w->save();
         for ($i = 0; $i < strlen($word); $i++) {
             $l = new Letter();
             $l->letter = substr($word, $i, 1);
             $l->word_id = $w->id;
             $l->ordinal = $i + 1;
             $l->save();
         }
         return $w;
     } else {
         return array('errors' => $v->errors);
     }
 }
Beispiel #2
0
 public function testAWordCanBeCreated()
 {
     $faker = Faker\Factory::create();
     $user = factory(User::class)->create(['username' => 'janedoe2']);
     $word_str = $faker->word;
     $args = array('word' => $word_str, 'user_id' => $user->id);
     $word = Word::create($args);
     $first_letter = Letter::where('word_id', $word->id)->where('ordinal', '1')->first();
     $this->assertEquals($word->word, $word_str);
     $this->assertEquals($first_letter->letter, substr($word_str, 0, 1));
     if (strlen($word_str) > 1) {
         $second_letter = Letter::where('word_id', $word->id)->where('ordinal', 2)->first();
         $this->assertEquals($second_letter->letter, substr($word_str, 1, 1));
     }
 }
 public function distribute_letters($game_id)
 {
     $games = \App\Models\Game_User::where('game_id', $game_id)->where('user_id', Auth::user()->id)->first();
     if ($games['has_left']) {
         return [];
     }
     $letters_user = \App\Models\Letter_User::where('game_id', '=', $game_id)->where('user_id', Auth::user()->id)->get(['letter_id'])->toArray();
     $flattened_array = array();
     array_walk_recursive($letters_user, function ($a) use(&$flattened_array) {
         $flattened_array[] = $a;
     });
     $user_letters = \App\Models\Letter::whereIn('id', $flattened_array)->get(['letter', 'points']);
     if (!$user_letters) {
         $user_letters = collect();
     }
     if ($user_letters->count() <= 6) {
         return $this->first_inventory($game_id, $user_letters);
     }
     return $user_letters;
 }
 public static function validate_word($game_id)
 {
     $letters = \Input::get('letters');
     if (self::has_winner($game_id)) {
         $list = \App\Models\Game_User::where('game_id', $game_id)->where('has_won', 1)->first();
         $user_win = \App\Models\User::where('id', '=', $list->user_id)->first();
         $msg = 'Ya existe un ganador y es ' . $user_win->first_name . '.';
         return response()->json(['errors' => true, 'msg' => $msg]);
     }
     if (self::has_ended($game_id)) {
         return response()->json(['errors' => true, 'msg' => 'El juego termino']);
     }
     if (self::have_turn($game_id)) {
         return response()->json(['errors' => true, 'msg' => 'NO es su turno.']);
     }
     if (self::have_give_up($game_id)) {
         return response()->json(['errors' => true, 'msg' => 'Usted ya se rindió.']);
     }
     $game_users = \App\Models\Game_User::where('game_id', '=', $game_id)->get();
     $turn = 0;
     foreach ($game_users as $game_user) {
         $turn += \App\Models\Game_Board_User::where('game_user_id', '=', $game_user->id)->count();
     }
     $board = self::board_generate($game_id);
     foreach ($letters as $letter) {
         $board[$letter['x']][$letter['y']]['letter'] = $letter['letter'];
     }
     $word = '';
     foreach ($letters as $letter) {
         $word .= $letter['letter'];
     }
     if ($turn == 0) {
         if ($letters[0]['x'] != 8 || $letters[0]['y'] != 8) {
             return response()->json(['errors' => true, 'msg' => 'La posición es incorrecta']);
         } elseif (self::check_word($word)) {
             $points = 0;
             $word_mult = 0;
             foreach ($letters as $letter) {
                 $letter_id = \App\Models\Letter::where('letter', $letter['letter'])->get();
                 $game_user_id = \App\Models\Game_User::where('game_id', $game_id)->where('user_id', Auth::user()->id)->first();
                 \App\Models\Game_Board_User::create(['x' => $letter['x'], 'y' => $letter['y'], 'letter_id' => $letter_id[0]['id'], 'game_user_id' => $game_user_id['id']]);
                 if ($board[$letter['x']][$letter['y']]['type'] == 1) {
                     $points += $letter_id[0]['points'] * $board[$letter['x']][$letter['y']]['multiplier'];
                 } elseif ($board[$letter['x']][$letter['y']]['type'] == 2) {
                     $word_mult += $board[$letter['x']][$letter['y']]['multiplier'];
                     $points += $letter_id[0]['points'];
                 } else {
                     $points += $letter_id[0]['points'];
                 }
                 $inventory = \App\Models\Letter_User::where('game_id', $game_id)->where('letter_id', $letter_id[0]['id'])->first();
                 $inventory->delete();
             }
             $game_user_id = \App\Models\Game_User::where('game_id', $game_id)->where('user_id', Auth::user()->id)->first();
             if ($word_mult > 0) {
                 $points = $points * $word_mult;
             }
             $game_user_id['points'] += $points;
             $game_user_id->save();
             self::cleanConsTurn($game_id);
             self::turn_pass($game_id);
             self::cleanConsTurn($game_id);
             return response()->json(['errors' => false, 'msg' => 'La palabra es correcta']);
         } else {
             return response()->json(['errors' => true, 'msg' => 'La palabra no se encuentra en el diccionario']);
         }
     } else {
         $is_ok = true;
         for ($i = 1; $i < 16; $i++) {
             $word = '';
             for ($j = 1; $j < 16; $j++) {
                 if (is_string($board[$i][$j]['letter'])) {
                     $word .= $board[$i][$j]['letter'];
                 } elseif (is_string($board[$i][$j]['letter']['letter'])) {
                     $word .= $board[$i][$j]['letter']['letter'];
                 } elseif (strlen($word) > 1) {
                     if (!self::check_word($word)) {
                         $is_ok = false;
                     }
                 } else {
                     $word = '';
                 }
             }
             if (is_string($board[$i][$j - 1]['letter'])) {
                 if (strlen($word) > 1) {
                     if (!self::check_word($word)) {
                         $is_ok = false;
                     }
                 }
             } elseif (is_string($board[$i][$j - 1]['letter']['letter'])) {
                 if (strlen($word) > 1) {
                     if (!self::check_word($word)) {
                         $is_ok = false;
                     }
                 }
             }
         }
         for ($i = 1; $i < 16; $i++) {
             $word = '';
             for ($j = 1; $j < 16; $j++) {
                 if (is_string($board[$j][$i]['letter'])) {
                     $word .= $board[$j][$i]['letter'];
                 } elseif (is_string($board[$j][$i]['letter']['letter'])) {
                     $word .= $board[$j][$i]['letter']['letter'];
                 } elseif (strlen($word) > 1) {
                     if (!self::check_word($word)) {
                         $is_ok = false;
                     }
                 } else {
                     $word = '';
                 }
             }
             if (is_string($board[$j - 1][$i]['letter'])) {
                 if (strlen($word) > 1) {
                     if (!self::check_word($word)) {
                         $is_ok = false;
                     }
                 }
             } elseif (is_string($board[$j - 1][$i]['letter']['letter'])) {
                 if (strlen($word) > 1) {
                     if (!self::check_word($word)) {
                         $is_ok = false;
                     }
                 }
             }
         }
         if ($is_ok) {
             $points = 0;
             $word_mult = 0;
             foreach ($letters as $letter) {
                 $letter_id = \App\Models\Letter::where('letter', $letter['letter'])->get();
                 $game_user_id = \App\Models\Game_User::where('game_id', $game_id)->where('user_id', Auth::user()->id)->first();
                 \App\Models\Game_Board_User::create(['x' => $letter['x'], 'y' => $letter['y'], 'letter_id' => $letter_id[0]['id'], 'game_user_id' => $game_user_id['id']]);
                 if ($board[$letter['x']][$letter['y']]['type'] == 1) {
                     $points += $letter_id[0]['points'] * $board[$letter['x']][$letter['y']]['multiplier'];
                 } elseif ($board[$letter['x']][$letter['y']]['type'] == 2) {
                     $word_mult += $board[$letter['x']][$letter['y']]['multiplier'];
                     $points += $letter_id[0]['points'];
                 } else {
                     $points += $letter_id[0]['points'];
                 }
                 $inventory = \App\Models\Letter_User::where('game_id', $game_id)->where('letter_id', $letter_id[0]['id'])->first();
                 $inventory->delete();
             }
             $game_user_id = \App\Models\Game_User::where('game_id', $game_id)->where('user_id', Auth::user()->id)->first();
             if ($word_mult > 0) {
                 $points = $points * $word_mult;
             }
             $game_user_id['points'] += $points;
             $game_user_id->save();
             self::cleanConsTurn($game_id);
             self::turn_pass($game_id);
             self::cleanConsTurn($game_id);
             return response()->json(['errors' => false, 'msg' => 'La palabra es correcta']);
         } else {
             return response()->json(['errors' => true, 'msg' => 'La palabra no se encuentra en el diccionario']);
         }
     }
 }
Beispiel #5
0
 public function activate($user, $sure = false)
 {
     $missing_clues = array();
     $missing_clues_for_one_letter_words = array();
     $missing_letters = array();
     $words = array();
     $pt = $this->puzzle_template;
     $pt->blackSquares = $pt->blackSquares();
     $squares = $this->puzzle_squares(true);
     $clues = $this->clues;
     $ordinal = 1;
     for ($row = 1; $row <= $pt->height; $row++) {
         for ($col = 1; $col <= $pt->width; $col++) {
             if (!in_array($row . '-' . $col, $pt->blackSquares) && $squares[$row . '-' . $col]['square_type'] != 'black' && (!@$squares[$row . '-' . $col]['letter'] || $squares[$row . '-' . $col]['letter'] == "")) {
                 $missing_letters[] = $row . '-' . $col;
             }
             $square_should_have_across_clue = false;
             $square_should_have_down_clue = false;
             if ($col != $pt->width && $squares[$row . '-' . $col]['square_type'] != 'black' && ($col == 1 || $squares[$row . '-' . ($col - 1)]['square_type'] == 'black')) {
                 $square_should_have_across_clue = true;
             }
             if ($row != $pt->height && $squares[$row . '-' . $col]['square_type'] != 'black' && ($row == 1 || $squares[$row - 1 . '-' . $col]['square_type'] == 'black')) {
                 $square_should_have_down_clue = true;
             }
             if ($square_should_have_down_clue) {
                 $found_clue = false;
                 foreach ($clues as $clue) {
                     if ($clue->ordinal == $ordinal && $clue->direction == 'down') {
                         $found_clue = $clue;
                     }
                 }
                 if (!$found_clue) {
                     if ($row == $pt->height || $squares[$row + 1 . '-' . $col]['square_type'] == 'black') {
                         $missing_clues_for_one_letter_words[] = $ordinal . ' down';
                     } else {
                         $missing_clues[] = $ordinal . ' down';
                     }
                 }
                 //find word
                 $word = "";
                 for ($word_row = $row; $word_row <= $pt->height && $squares[$word_row . '-' . $col]['square_type'] != 'black'; $word_row++) {
                     $word .= $squares[$word_row . '-' . $col]['letter'];
                 }
                 $word_db = Word::where('word', $word)->first();
                 if (!$word_db) {
                     $word_db = new Word();
                     $word_db->word = $word;
                     $word_db->length = strlen($word);
                     $word_db->user_id = $user->id;
                     $word_db->timestamp_utc = time();
                     $word_db->save();
                     for ($i = 0; $i < strlen($word); $i++) {
                         $letter = new Letter();
                         $letter->letter = substr($word, $i, 1);
                         $letter->word_id = $word_db->id;
                         $letter->ordinal = $i + 1;
                         $letter->save();
                     }
                 }
                 if ($found_clue) {
                     $clue_available = ClueAvailable::where('word_id', $word_db->id)->where('clue', $found_clue->clue)->first();
                     if (!$clue_available) {
                         $clue_available = new ClueAvailable();
                         $clue_available->word_id = $word_db->id;
                         $clue_available->clue = $found_clue->clue;
                         $clue_available->timestamp_utc = time();
                         $clue_available->save();
                     }
                 }
             }
             if ($square_should_have_across_clue) {
                 $found_clue = false;
                 foreach ($clues as $clue) {
                     if ($clue->ordinal == $ordinal && $clue->direction == 'across') {
                         $found_clue = $clue;
                     }
                 }
                 if (!$found_clue) {
                     if ($col == $pt->width || $squares[$row . '-' . ($col + 1)]['square_type'] == 'black') {
                         $missing_clues_for_one_letter_words[] = $ordinal . ' across';
                     } else {
                         $missing_clues[] = $ordinal . ' across';
                     }
                 }
             }
             if ($square_should_have_across_clue || $square_should_have_down_clue) {
                 $ordinal++;
             }
         }
     }
     if (count($missing_clues) > 0 || count($missing_letters) > 0 || count($missing_clues_for_one_letter_words) > 0 && !$sure) {
         return array('errors' => compact('missing_clues', 'missing_letters', 'missing_clues_for_one_letter_words'));
     } else {
         $this->active = 1;
         $this->timestamp_utc = time();
         $this->save();
         return array('success' => 1);
     }
 }