public static function all_game_scores($gameid) { $players = Game::games_players($gameid); $player_scores = array(); foreach ($players as $player) { $sql = "SELECT hole.hole_num, hole.par, score.scoreid, score.stroke, score.ob, score.legal FROM score\n JOIN hole ON score.holeid = hole.holeid\n WHERE score.gameid = :gameid AND score.playerid = :playerid\n ORDER BY hole.hole_num ASC"; $query = DB::connection()->prepare($sql); $query->execute(array('gameid' => $gameid, 'playerid' => $player->playerid)); $rows = $query->fetchAll(); $scores = array(); $total_score = 0; foreach ($rows as $row) { $score = new Score(array('hole_num' => $row['hole_num'], 'hole_par' => $row['par'], 'scoreid' => $row['scoreid'], 'stroke' => $row['stroke'], 'ob' => $row['ob'], 'legal' => $row['legal'])); $scores[] = $score; $total_score += (int) $row['stroke']; $total_score += (int) $row['ob']; } $player_scores['player' . $player->playerid] = $scores; } // Sort array by total score uasort($player_scores, function ($a, $b) { $a_total_score = 0; foreach ($a as $score) { $a_total_score += $score->stroke; $a_total_score += $score->ob; } $b_total_score = 0; foreach ($b as $score) { $b_total_score += $score->stroke; $b_total_score += $score->ob; } return $a_total_score - $b_total_score; }); return $player_scores; }
public static function all_game_scores($gameid) { $players = Game::games_players($gameid); $player_scores = array(); foreach ($players as $player) { $sql = "SELECT * FROM score WHERE gameid = :gameid AND playerid = :playerid"; $query = DB::connection()->prepare($sql); $query->execute(array('gameid' => $gameid, 'playerid' => $player->playerid)); $rows = $query->fetchAll(); $scores = array(); foreach ($rows as $row) { $score = new Score(array('scoreid' => $row['scoreid'], 'gameid' => $row['gameid'], 'holeid' => $row['holeid'], 'playerid' => $row['playerid'], 'stroke' => $row['stroke'], 'ob' => $row['ob'])); $score->prepare(); $scores[] = $score; } $player_scores[$player->firstname] = $scores; } return $player_scores; }
public static function update($gameid) { $attributes = $_POST; $rain = isset($_POST['rain']) && $_POST['rain'] ? "1" : "0"; // checked=1, unchecked=0 $wet_no_rain = isset($_POST['wet_no_rain']) && $_POST['wet_no_rain'] ? "1" : "0"; // checked=1, unchecked=0 $windy = isset($_POST['windy']) && $_POST['windy'] ? "1" : "0"; // checked=1, unchecked=0 $variant = isset($_POST['variant']) && $_POST['variant'] ? "1" : "0"; // checked=1, unchecked=0 $dark = isset($_POST['dark']) && $_POST['dark'] ? "1" : "0"; // checked=1, unchecked=0 $led = isset($_POST['led']) && $_POST['led'] ? "1" : "0"; // checked=1, unchecked=0 $snow = isset($_POST['snow']) && $_POST['snow'] ? "1" : "0"; // checked=1, unchecked=0 $doubles = isset($_POST['doubles']) && $_POST['doubles'] ? "1" : "0"; // checked=1, unchecked=0 $temp = $_POST['temp'] != "" ? $_POST['temp'] : null; // temperature can be null (or 0!) $date = $_POST['date']; $time = $_POST['time']; $comment = $_POST['comment']; $courseid = $_POST['courseid']; $gamedate = $date . " " . $time . ":00"; $game = new Game(array('gameid' => $gameid, 'courseid' => $courseid, 'gamedate' => $gamedate, 'comment' => $comment, 'rain' => $rain, 'wet_no_rain' => $wet_no_rain, 'windy' => $windy, 'variant' => $variant, 'dark' => $dark, 'led' => $led, 'snow' => $snow, 'doubles' => $doubles, 'temp' => $temp)); $errors = $game->errors(); $course = Course::find($courseid); $player_scores = Score::all_game_scores($gameid); // Cycle through players foreach ($player_scores as $playerid => $scores) { // playerid is i.e. 'player4' $legal_index = 'legal-' . $playerid; $legal = isset($_POST[$legal_index]) && $_POST[$legal_index] ? "1" : "0"; // checked=1, unchecked=0 foreach ($scores as $score) { // inputs are in format 'player1-hole1' $stroke = $_POST[$playerid . '-hole' . $score->hole_num]; $ob = $_POST[$playerid . '-obhole' . $score->hole_num]; $score->stroke = (int) $stroke; $score->ob = (int) $ob; $score->legal = $legal; $errors = array_merge($errors, $score->errors()); } } if (count($errors) == 0) { // Game and scores were all valid $gameid = $game->update(); foreach ($player_scores as $playerid => $scores) { foreach ($scores as $score) { $score->update(); } } // Clear cached pages Cache::clear(); Redirect::to('/game/' . $game->gameid, array('message' => 'Peli ja sen tulokset päivitetty.')); } else { $game->prepare(); View::make('game/edit.html', array('errors' => $errors, 'game' => $game, 'date' => $date, 'time' => $time, 'players' => Game::games_players($gameid), 'attributes' => $attributes, 'course' => $game->course)); } }