DEBUG: You asked to delete " . count( $playIDs ) . " records.
"; $set = "( "; for ( $i = 0; $i < count( $playIDs ); $i++ ) { if ( $i > 0 ) $set .= ", "; $set .= $playIDs[ $i ]; } $set .= " )"; echo "Calculating scores on games in $set.
"; displayRecords( $playIDs, false, false ); // display no id, no checkbox. // Collect averages and sums for each scenario/country. $sql = "SELECT TScenarioNames.title AS scenarioName, TCountryNames.name AS countryName, COUNT( TScores.score ) as playCount, SUM( TScores.score ) AS sumScore, AVG( TScores.score ) AS avgScore, SUM( TScores.margin ) AS sumMargin, AVG( TScores.margin ) AS avgMargin, STD( TScores.margin ) AS stdMargin FROM TScores, TPlays, TScenarioNames, TCountryNames WHERE ( TScores.idPlays IN $set ) AND ( TScores.idPlays = TPlays.id ) AND ( TPlays.idScenarioNames = TScenarioNames.id ) AND (TScores.idCountryNames = TCountryNames.id ) GROUP BY TPlays.idScenarioNames, TScores.idCountryNames ORDER BY scenarioName, countryName ASC"; $sqlResults = sqlQuery( $sql, $verbose ); $rowCount = 0; $delimiter = ":"; // Create an array keyed on scenario/country, each s/c contains group and player metrics. while ( $row = mysql_fetch_array( $sqlResults, MYSQL_ASSOC )) { $primaryKey = $row[ "scenarioName" ] . $delimiter . $row[ "countryName" ]; $metrics[ $primaryKey ][ "group" ][ "playCount" ] = $row[ "playCount" ]; $metrics[ $primaryKey ][ "group" ][ "sumScore" ] = $row[ "sumScore" ]; $metrics[ $primaryKey ][ "group" ][ "avgScore" ] = $row[ "avgScore" ]; $metrics[ $primaryKey ][ "group" ][ "sumMargin"] = $row[ "sumMargin" ]; $metrics[ $primaryKey ][ "group" ][ "avgMargin" ] = $row[ "avgMargin" ]; $metrics[ $primaryKey ][ "group" ][ "stdMargin" ] = $row[ "stdMargin" ]; $rowCount++; } // printArray( $metrics ); // Collect averages and sums for each scenario/country. $sql = "SELECT TScenarioNames.title AS scenarioName, TCountryNames.name AS countryName, COUNT( TScores.score ) as playCount, SUM( TScores.score ) AS sumScore, AVG( TScores.score ) AS avgScore, SUM( TScores.margin ) AS sumMargin, AVG( TScores.margin ) AS avgMargin, TPlayerNames.name AS playerName FROM TScores, TPlays, TScenarioNames, TCountryNames, TPlayerNames WHERE ( TScores.idPlays IN $set ) AND ( TScores.idPlays = TPlays.id ) AND ( TPlays.idScenarioNames = TScenarioNames.id ) AND (TScores.idCountryNames = TCountryNames.id ) AND (TScores.idPlayerNames = TPlayerNames.id ) GROUP BY TPlays.idScenarioNames, TScores.idCountryNames, playerName ORDER BY scenarioName, countryName, playerName ASC"; $sqlResults = sqlQuery( $sql, $verbose ); $rowCount = 0; while ( $row = mysql_fetch_array( $sqlResults, MYSQL_ASSOC )) { $primaryKey = $row[ "scenarioName" ] . $delimiter . $row[ "countryName" ]; $metrics[ $primaryKey ][ $row[ "playerName" ] ][ "playCount" ] = $row[ "playCount" ]; $metrics[ $primaryKey ][ $row[ "playerName" ] ][ "sumScore" ] = $row[ "sumScore" ]; $metrics[ $primaryKey ][ $row[ "playerName" ] ][ "avgScore" ] = $row[ "avgScore" ]; $metrics[ $primaryKey ][ $row[ "playerName" ] ][ "sumMargin"] = $row[ "sumMargin" ]; $metrics[ $primaryKey ][ $row[ "playerName" ] ][ "avgMargin" ] = $row[ "avgMargin" ]; // Standard score = ( score - avg ) / stdDev $stdDev = $metrics[ $primaryKey ][ "group" ][ "stdMargin" ]; if ( $stdDev < 0.000001 ) $stdDev = 1.0; // too few samples (e.g. one game ) $metrics[ $primaryKey ][ $row[ "playerName" ] ][ "zScore" ] = ( $row[ "avgMargin" ] - $metrics[ $primaryKey ][ "group" ][ "avgMargin" ] ) / $stdDev; $rowCount++; } // Calculate average zScores reset( $metrics ); foreach ($metrics as $metric) { foreach ($metric as $player => $values ) { if ( strcmp( $player, "group" ) != 0 ) { if ( is_null( $metrics[ "Total" ][ $player ][ "totalPlays" ] ) ) { $metrics[ "Total" ][ $player ][ "totalPlays" ] = 0; $metrics[ "Total" ][ $player ][ "totalZ" ] = 0; $metrics[ "Total" ][ $player ][ "avgZ" ] = 0; } $metrics[ "Total" ][ $player ][ "totalPlays" ] += $values[ "playCount" ]; for ( $i = 0; $i < $values[ "playCount" ]; $i++ ) $metrics[ "Total" ][ $player ][ "totalZ" ] += $values[ "zScore" ]; $metrics[ "Total" ][ $player ][ "avgZ" ] = $metrics[ "Total" ][ $player ][ "totalZ" ] / $metrics[ "Total" ][ $player ][ "totalPlays" ]; } } } // Calculate ordering by avgZ. reset( $metrics ); foreach ($metrics as $metric) { foreach ($metric as $player => $values ) { if ( strcmp( $player, "group" ) != 0 ) { $ordering[ $player ] = $metrics[ "Total" ][ $player ][ "avgZ" ]; } } } arsort( $ordering ); // reverse sort by value, maintain keys. $orderCount = 1; $previousZ = -1.0; $previousPosition = 1; foreach ($ordering as $player => $avgZ ) { if ( abs( $avgZ - $previousZ ) < 0.01 ) $metrics[ "Total" ][ $player ][ "order" ] = $previousPosition; // keep ties with same order else $metrics[ "Total" ][ $player ][ "order" ] = $orderCount; $previousZ = $avgZ; $previousPosition = $orderCount; $orderCount++; } // printArray( $metrics ); printRecords( $ordering, $metrics ); return $metrics;}function printRecords( $ordering, $metrics ) { echo "Scenario:Country | Group avgΔ/σ | \n"; reset( $ordering ); foreach ($ordering as $player => $order ) { echo "$player #/avgΔ/Z | "; } echo "|
---|---|---|---|
$key | "; echo "" . round( $metric[ "group" ][ "avgMargin"], 2 ) . " / " . round( $metric[ "group" ][ "stdMargin"], 2 ) . " | "; reset( $ordering ); foreach ($ordering as $player => $order ) { if ( is_null( $metric[ $player ] ) ) { echo ""; } else { echo " | " . $metric[ $player ][ "playCount" ] . " / " . round( $metric[ $player ][ "avgMargin" ], 2 ) . " / " . round( $metric[ $player ][ "zScore" ], 2 ) . " | "; } } echo "
Order / # / avgZ | "; echo ""; reset( $ordering ); foreach ($ordering as $player => $order ) { if ( is_null( $metric[ $player ] ) ) { echo " | "; } else { echo " | " . $metric[ $player ][ "order" ] ." / " . $metric[ $player ][ "totalPlays" ] . " / " . round( $metric[ $player ][ "avgZ" ], 2) . " | "; } } echo "
Purpose: Select records and perform metrics on the tournament records.
Note that if everyone in a (scenario,nation) group scores the same, the standard deviationwill be 0 and the normalized score will be 0 also.
Select via game date and selection boxes:
\n"; if (0 == strcmp( $command, "filter" )) { filterValues(); // echo "
DEBUG: fromDate=$fromDate, toDate=$toDate.
\n"; $playIDs = filterPlays( $fromDate, $toDate ); } else if (0 == strcmp( $command, "calculate" )) { // echo "DEBUG: command=delete.DEBUG: count(gameIDs)=" . count( $gameIDs ) . "
\n"; calculateRecords( $gameIDs ); unset( $gameIDs ); } } unset( $command );} // issetecho "Filter records here:
\n";filterForm();selectForm( $playIDs, "calculate", "Calculate Score" );?>