#!/usr/bin/env php . // Assign badges based on RAC percentile. // Customize this to grant other types of badges require_once("../inc/util_ops.inc"); // thresholds for the various badges // (i.e. gold badge is for top 1% of active users/teams) // $badge_pctiles = array(1, 10, 25); $badge_images = array("gold.png", "silver.png", "bronze.png"); // get the records for percentile badges; create if needed // function get_pct_badges($badge_name_prefix, $badge_pctiles, $badge_images) { $badges = array(); for ($i=0; $i<3; $i++) { $badges[$i] = get_badge($badge_name_prefix."_".$i, "Top ".$badge_pctiles[$i]."%", $badge_images[$i]); } return $badges; } // get the RAC percentiles // function get_percentiles($is_user, $badge_pctiles) { $percentiles = array(); for ($i=0; $i<3; $i++) { if ($is_user) { $percentiles[$i] = BoincUser::percentile("expavg_credit", "expavg_credit>1", 100-$badge_pctiles[$i]); } else { $percentiles[$i] = BoincTeam::percentile("expavg_credit", "expavg_credit>1", 100-$badge_pctiles[$i]); } if ($percentiles[$i] === false) { die("Can't get percentiles\n"); } } return $percentiles; } function assign_pct_badge($is_user, $item, $percentiles, $badges) { for ($i=0; $i<3; $i++) { if ($item->expavg_credit >= $percentiles[$i]) { assign_badge($is_user, $item, $badges[$i]); unassign_badges($is_user, $item, $badges, $i); return; } } unassign_badges($is_user, $item, $badges, -1); } function assign_badges($is_user, $badge_pctiles, $badge_images) { $kind = $is_user?"user":"team"; $badges = get_pct_badges($kind."_pct", $badge_pctiles, $badge_images); $pctiles = get_percentiles($is_user, $badge_pctiles); echo "thresholds for $kind badges: $pctiles[0] $pctiles[1] $pctiles[2]\n"; $n = 0; $maxid = $is_user?BoincUser::max("id"):BoincTeam::max("id"); while ($n <= $maxid) { $m = $n + 1000; if ($is_user) { $items = BoincUser::enum_fields("id, expavg_credit", "id>=$n and id<$m and total_credit>0"); } else { $items = BoincTeam::enum_fields("id, expavg_credit", "id>=$n and id<$m and total_credit>0"); } foreach ($items as $item) { assign_pct_badge($is_user, $item, $pctiles, $badges); // ... assign other types of badges } $n = $m; } } assign_badges(true, $badge_pctiles, $badge_images); assign_badges(false, $badge_pctiles, $badge_images); ?>