Aktualisiere Punktzahlen ...
";

////////////////////////////////////////////////////////////////////////////////

$multicnt = 0;
while (TRUE) {
    $multis = Query(<< 1
EOF
, RESOLVE_IDS, 'minid');
    if (!$multis)
        break;
    $multis = array_keys($multis);
    $multicnt += count($multis);
    foreach($multis as $bid)
        Query("DELETE FROM wm_bets WHERE id=$bid");
}
if ($multicnt) {
    echo "WARNING! $multicnt multi-bets found and eliminated!\n";
}

////////////////////////////////////////////////////////////////////////////////

error_reporting(error_reporting() & ~E_NOTICE);

$matches = Query("SELECT * FROM wm_matches", RESOLVE_IDS);
$teams = Query("SELECT * FROM wm_teams", RESOLVE_IDS);
$users = Query("SELECT * FROM wm_users");
$champ = GetChampion();

////////////////////////////////////////////////////////////////////////////////

$refs = array();
foreach ($matches as $mid => $match) {
    $ta = $teams[$match['teamA']];
    $tb = $teams[$match['teamB']];

    # enter ref ID of winning and losing team into ref table
    if (($match['goalsA'] !== NULL) && ($match['goalsB'] !== NULL)) {
        $dmatch = $match['goalsA'] - $match['goalsB'];
        if ($dmatch > 0) {
            $refs[$mid] = intval($ta['id']);
            $refs[$mid + 100] = intval($tb['id']);
        } else if ($dmatch < 0) {
            $refs[$mid] = intval($tb['id']);
            $refs[$mid + 100] = intval($ta['id']);
        }
    }

    # check whether a team ref can be entered
    if (!$ta['valid'] && ($ref = @$refs[$ta['refcode']])) {
        echo MatchName($match['class'], $match['number'], "X") . ", Team A = {$teams[$ref]['name']}\n";
        Query("UPDATE wm_matches SET teamA=$ref WHERE id=$mid");
    }
    if (!$tb['valid'] && ($ref = @$refs[$tb['refcode']])) {
        echo MatchName($match['class'], $match['number'], "X") . ", Team B = {$teams[$ref]['name']}\n";
        Query("UPDATE wm_matches SET teamB=$ref WHERE id=$mid");
    }
}

////////////////////////////////////////////////////////////////////////////////

foreach ($users as $user) {
    $uid = intval($user['id']);
    echo "$uid ({$user['name']} <{$user['email']}>):";
    flush();

    # iterate over valid bets
    $bets = Query("SELECT `match`, goalsA, goalsB FROM wm_bets WHERE user=$uid AND NOT(ISNULL(goalsA) OR ISNULL(goalsB))");
    $exact = 0;
    $diff = 0;
    $win = 0;
    $even = 0;
    $all = 0;
    $none = 0;
    foreach($bets as $bet) {
        $match = $matches[$bet['match']];
        if (!$match || ($match['goalsA'] === NULL) || ($match['goalsB'] === NULL))
            continue;  # no such match or match result is not known yet
        $dbet = $bet['goalsA'] - $bet['goalsB'];
        $dmatch = $match['goalsA'] - $match['goalsB'];
        if (($match['goalsA'] == $bet['goalsA']) && ($match['goalsB'] == $bet['goalsB']))
            $exact++;
        else if (($dbet == 0) && ($dmatch == 0))
            $even++;
        else if ($dbet == $dmatch)
            $diff++;
        else if (diffsgn($bet['goalsA'], $bet['goalsB']) == diffsgn($match['goalsA'], $match['goalsB']))
            $win++;
        else
            $none++;
        $all++;
    }
    
    # add bonuses
    $bonus = 0;
    if ($champ) {
        if ($user['bet0'] == $champ) $bonus += BonusBetScore(0);
        if ($user['bet8'] == $champ) $bonus += BonusBetScore(8);
        if ($user['bet4'] == $champ) $bonus += BonusBetScore(4);
        if ($user['bet2'] == $champ) $bonus += BonusBetScore(2);
    }

    $any = $all - $none;
    $score = $exact * ScoreExact + $diff * ScoreDiff + $win * ScoreWin + $even * ScoreEven + $bonus;
    echo " exact=$exact diff=$diff win=$win even=$even any=$any all=$all bonus=$bonus score=$score\n";
    Query("UPDATE wm_users SET score=$score, exact=$exact, any=$any WHERE id=$uid");
}

echo "
"; flush(); redirect("adm.php"); CloseDB(); ?>