public function updateLeague($week) { // called on updating results // get all scores for players // write results to Standings table $sql = 'SELECT U.id, U.username, P.pred, P.joker, M.id, M.score, M.gotw FROM users U JOIN predictions P ON U.id = P.user_id JOIN matches M ON P.match_id = M.id WHERE (U.games & 1) != 0 AND (M.game & 1) != 0 AND M.week_id = ?'; $db = $this->getDataSource(); $data = $db->fetchAll($sql, [$week]); $table = []; $goals = []; foreach ($data as $w) { $uid = $w['U']['id']; $mid = $w['M']['id']; @($table[$uid]['goals'] += array_sum(explode('-', $w['P']['pred']))); if (!is_null($w['M']['score'])) { $score = calc($w['P']['pred'], $w['M']['score'], $w['P']['joker'], $w['M']['gotw']); @($table[$uid]['score'] += $score); if (empty($goals[$mid])) { $goals[$mid] = array_sum(explode('-', $w['M']['score'])); } } else { @($table[$uid]['score'] += 0); } } $goalsum = array_sum($goals); if (!empty($table)) { foreach ($table as $k => $n) { $goal_diff[$k] = abs($goalsum - $n['goals']); } } $closest = getmin($goal_diff); foreach ($table as $k => &$n) { if (in_array($k, $closest)) { $n['score'] += 3; } } ksort($table); uasort($table, function ($x, $y) { return $x['score'] > $y['score'] ? -1 : 1; }); $this->Standing->deleteAll(['week_id' => $week]); $row = 0; // which row are we on $rank = 0; // what is the rank of the player $absrank = 0; // what is abs score $prevrank = 0; // rank of row n-1 foreach ($table as $k => &$n) { $absrank = $n['score']; if ($absrank == $prevrank) { $row++; } else { $rank = ++$row; } $prevrank = $absrank; $tosave = ['Standing' => ['week_id' => $week, 'user_id' => $k, 'points' => $n['score'], 'position' => $rank]]; $this->Standing->create(); if ($this->Standing->save($tosave)) { // TODO something } else { $this->log('Couldn\'t update standings', 'admin'); } } }
$long = $_POST['place_longitude']; $nom = $_POST['place_name']; if ($userlog == "diagnosol-master") { $userlog = "all"; } // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "SELECT * FROM station"; $min = array("", 10000000000); $reponse = mysqli_query($conn, $sql); while ($donnees = mysqli_fetch_array($reponse)) { $min = getmin($min, $lat, $long, $donnees['Latitude'], $donnees['Longitude'], $donnees['Nom']); } $sql3 = "SELECT * FROM site_relation WHERE name = '{$nom}' AND owner = '{$userlog}'"; $compteur = 0; $reponse3 = mysqli_query($conn, $sql3); while ($donnees = mysqli_fetch_array($reponse3)) { $compteur = $compteur + reccount($nom, $userlog, $compteur); } if ($compteur != 0) { $nom = $nom . " (" . $compteur . ")"; } $sql2 = "INSERT INTO site_relation (`name`, `latitude`, `longitude`, `station`, `owner`) VALUES ('{$nom}','{$lat}','{$long}','{$min['0']}','{$userlog}')"; //test pour voir si les data sont add comme il faut if (mysqli_query($conn, $sql2)) { // echo "New record created successfully"; } else {