protected function dateSearch() { if (!isset($this->request->query['date'])) { $error = 'No date provided.'; } else { $query_params = $this->request->query; $start_time = strtotime($query_params['date']); $end_time = $start_time + 24 * 60 * 60; $conditions = array('game_time' => array('$gt' => $start_time, '$lt' => $end_time)); if (isset($query_params['league'])) { $conditions['league_id'] = new \MongoId($query_params['league']); } if (isset($query_params['fieldsite'])) { $conditions['fieldsite_id'] = new \MongoId($query_params['fieldsite']); } $games = Games::find('all', array('conditions' => $conditions, 'order' => array('game_time' => 'ASC'))); if (empty($error) and $games->count() == 0) { $error = 'No games found.'; } } return $this->render(array('data' => compact('error', 'games'), 'layout' => false)); }
public static function getImage() { $game = Games::find('first', array('conditions' => array('id' => Session::read('game_id')))); $word = Words::find('first', array('conditions' => array('id' => $game->word_id))); $img_number = self::MAX_IMAGE_COUNT - $game->max_wrong_tries + $game->wrong_tries; if ($game->max_wrong_tries == 5) { switch ($game->wrong_tries) { case 1: $img_number = 3; break; case 2: $img_number = 5; break; case 3: $img_number = 7; break; case 4: $img_number = 9; break; case 5: $img_number = 11; break; case 0: default: $img_number = 0; break; } } return self::HANG_IMAGE_PRE . $img_number . self::HANG_IMAGE_POST; }
public function trychar($char) { // do we have an active game session? if (!Session::check("game_id", array('name' => 'default'))) { return $this->redirect('games::index'); } // state: 0 = in-game, 1 = won, 2 = lost, 3 = already won, 4 = already lost $game = Games::find('first', array('conditions' => array('id' => Session::read('game_id')))); $word = Words::find('first', array('conditions' => array('id' => $game->word_id))); $resp = array("success" => 0, "wrong_tries_left" => $game->max_wrong_tries - $game->wrong_tries, "placeholders" => Games::getPlaceholders(), "image" => Games::getImage(), "message" => "", "state" => 0, "word" => ""); // game already won if ($game->state == 1) { $resp['message'] = "You already won!"; $resp['state'] = 3; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // game already lost } elseif ($game->state == 2) { $resp['message'] = "You already lost!"; $resp['state'] = 4; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); } // ist der gesendete buchstabe erlaubt? if (!Games::isValidChar($char)) { $resp['success'] = 0; $resp['message'] = "Non-valid char used."; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // falls der gesendete buchstabe schonmal gesendet wurde } elseif (stripos($game->input_text, $char) !== false) { $resp['success'] = 0; $resp['message'] = "Char was already used."; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // buchstabe ist valid und wurde nicht schon gesendet } else { $game->input_text .= $char; $game->save(); $resp['success'] = 1; $resp['placeholders'] = Games::getPlaceholders(); // buchstabe kommt in gesuchtem wort vor if (!(stripos($word->value, $char) === false) && stripos($word->value, $char) >= 0) { $resp['image'] = Games::getImage(); // alle zeichen erraten, gewonnen // WIN! if (stripos(Games::getPlaceholders(), self::PLACEHOLDER) === false) { $game->state = 1; // won $game->save(); $resp['state'] = 1; // won $resp['message'] = "You won! Play a new game?"; $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // buchstabe stimmt, aber noch nicht alles erraten } else { $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries; $resp['message'] = "Nice! Guess the next char!"; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); } // buchstabe kommt NICHT vor } else { $game->wrong_tries++; $game->save(); $resp['image'] = Games::getImage(); // hat der benutzer die maximale anzahl versuche überschritten? // LOOSE! if ($game->wrong_tries >= $game->max_wrong_tries) { $resp['wrong_tries_left'] = 0; $resp['message'] = "Out of tries. New Game?"; $resp['state'] = 2; // lost $game->state = 2; // lost $resp['word'] = $word->value; $game->save(); return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); // falscher buchstaben eingegeben, benutzer hat weiteren versuch } else { $resp['wrong_tries_left'] = $game->max_wrong_tries - $game->wrong_tries; $resp['message'] = "Nope. Try again!"; return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); } } } return $this->render(array('type' => 'json', 'data' => $resp, 'layout' => false)); }
public function reportScore() { $game = Games::find($this->request->id); if (!$game) { $redirectUrl = $this->request->env('HTTP_REFERER') ?: '/'; $this->flashMessage('Game not found.', array('alertType' => 'error')); return $this->redirect($redirectUrl); } if (!isset($this->CURRENT_USER) or !$game->canReport($this->CURRENT_USER)) { $redirectUrl = $this->request->env('HTTP_REFERER') ?: '/'; $this->flashMessage('You do not have permission to report scores for that game.', array('alertType' => 'error')); return $this->redirect($redirectUrl); } if ($this->request->data) { $doSave = true; if ($game->scores) { $old_score_report = $game->scores->export(); $old_score_report = $old_score_report['data']; } $team_ids = array(); // NOTE: This code also lives in the Games Controller (for editing games that already have scores) // This should probably be abstracted and put in the games model $new_score_report = array('reporter_id' => $this->CURRENT_USER->_id, 'report_time' => time(), 'reporter_ip' => $this->request->env('REMOTE_ADDR'), 'rainout' => false, 'forfeit' => false); if (isset($this->request->data['rainout'])) { $new_score_report['rainout'] = true; } else { if (isset($this->request->data['forfeit'])) { # make sure winner is in team list # save winner with forfeit score (wherever that is) $doSave = false; } else { $max_score = -1; $winner_id = null; foreach ($this->request->data['scores'] as $id => $score) { $score = abs(intval($score)); if ($score > $max_score) { $max_score = $score; $winner_id = $id; } else { if ($score == $max_score) { // This means we have a tie, somehow $winner_id = null; } } $new_score_report[$id] = $score; $team_ids[] = new \MongoId($id); } } } $game->scores = $new_score_report; $game->winner = $winner_id; if ($doSave) { if ($game->save()) { if (isset($old_score_report)) { // Log the previously-reported score $conditions = array('_id' => $game->_id); $query = array('$push' => array('old_scores' => $old_score_report)); Games::update($query, $conditions); } // Mark teams as needing an update $conditions = array('_id' => array('$in' => $team_ids)); $query = array('$set' => array('stats.needs_update' => true)); Teams::update($query, $conditions, array('multiple' => true)); $this->flashMessage('Your score has been successfully reported.', array('alertType' => 'success')); return $this->redirect($this->request->env('HTTP_REFERER') ?: '/'); } } } $league = $game->getLeague(); $teams = array(); foreach ($game->teams as $t_id) { $teams[(string) $t_id] = Teams::find((string) $t_id); } return compact('game', 'league', 'teams'); }