public function getNextGamesDate($entity) { $focus_game = Games::first(array('conditions' => array('fieldsite_id' => $entity->_id, 'game_time' => array('$gt' => time())), 'order' => array('game_time' => 'DESC'))); if (!$focus_game) { $focus_game = Games::first(array('conditions' => array('fieldsite_id' => $entity->_id), 'order' => array('game_time' => 'DESC'))); } if ($focus_game) { $focus_time = $focus_game->game_time->sec; } else { $focus_time = time(); } return date('Y-m-d', $focus_time); }
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'); }
public function getGames($entity) { $conditions = array('teams' => $entity->_id); $order = array('game_time' => 1); return Games::all(compact('conditions', 'order')); }
protected function updateLeagueStandings() { // Compile a list of all teams needing an update across all leagues $conditions = array('needs_standings_update' => true); $fields = array('_id'); $leagues_needing_update = Leagues::all(compact('conditions', 'fields')); foreach ($leagues_needing_update as $l) { $team_list = array(); $conditions = array('league_id' => $l->_id); $leage_teams = Teams::all(compact('conditions')); foreach ($leage_teams as $t) { $team_list[] = $t; } usort($team_list, function ($a, $b) { // One of the team has no reported scores, other team wins by default. if (is_object($a->stats) and !is_object($b->stats)) { return 1; } else { if (is_object($a->stats) and !is_object($b->stats)) { return -1; } else { if (!is_object($a->stats) and !is_object($b->stats)) { return 0; } } } // Criteria #1: winning percentage $a_pct = $a->stats->wins / ($a->stats->wins + $a->stats->losses); $b_pct = $b->stats->wins / ($b->stats->wins + $b->stats->losses); if ($a_pct != $b_pct) { // Needs to return an int, these percentages won't be ints. return intval(($a_pct - $b_pct) * 100); } // Criteria #2: Number of wins if ($a->stats->wins != $b->stats->wins) { return $a->stats->wins - $b->stats->wins; } // Criteria #3: Head-to-head wins / Criteria #4: Head-to-head point-diff $conditions = array('teams' => array($a->_id, $b->_id), 'scores' => array('$exists' => true)); $joint_games = Games::all(compact('conditions')); $head_to_head_wins = 0; $head_to_head_pnts = 0; foreach ($joint_games as $g) { // Note: Lithium does not support $and yet, so we can't do this optimally if ($a->_id != $g->teams[0] and $a->_id != $g->teams[1] or $b->_id != $g->teams[0] and $b->_id != $g->teams[1]) { // This game isn't head-to-head, doesn't count; continue; } if ($g->winner == $a->_id) { $head_to_head_wins++; $head_to_head_pnts += $g->getScoreDiff(); } else { if ($g->winner == $b->_id) { $head_to_head_wins--; $head_to_head_pnts -= $g->getScoreDiff(); } } } if ($head_to_head_wins != 0) { return $head_to_head_wins; } if ($head_to_head_pnts != 0) { return $head_to_head_pnts; } // Criteria #5: Head-to-head point-diff if ($a->stats->point_differential != $b->stats->point_differential) { return $a->stats->point_differential - $b->stats->point_differential; } // Still tied? $a->stats->tied = true; $b->stats->tied = true; return 0; }); // Assign their ranks, handle ties correctly: $rank = 0; $tied_rank = null; foreach (array_reverse($team_list) as $t) { $rank++; if (!isset($tied_rank) and $t->stats->tied == true) { $tied_rank = $rank; } else { if ($t->stats->tied == false) { $tied_rank = null; } } $display_rank = $tied_rank ?: $rank; $t->save(array('stats.rank' => $display_rank)); } } }