| 1 | #! /usr/bin/env php |
|---|
| 2 | <?php |
|---|
| 3 | |
|---|
| 4 | // update infoarena ratings FOR A SINGLE round |
|---|
| 5 | // expects round_id as argv 1 |
|---|
| 6 | // |
|---|
| 7 | // WARNING: Ratings should be updated in chronological order! |
|---|
| 8 | |
|---|
| 9 | require_once(dirname($argv[0]) . "/utilities.php"); |
|---|
| 10 | |
|---|
| 11 | require_once(IA_ROOT_DIR."www/config.php"); |
|---|
| 12 | require_once(IA_ROOT_DIR."www/identity.php"); |
|---|
| 13 | require_once(IA_ROOT_DIR."common/db/round.php"); |
|---|
| 14 | require_once(IA_ROOT_DIR."common/db/score.php"); |
|---|
| 15 | require_once(IA_ROOT_DIR."common/db/parameter.php"); |
|---|
| 16 | require_once(IA_ROOT_DIR."common/db/user.php"); |
|---|
| 17 | require_once(IA_ROOT_DIR."common/round.php"); |
|---|
| 18 | require_once(IA_ROOT_DIR."common/score.php"); |
|---|
| 19 | require_once(IA_ROOT_DIR."common/rating.php"); |
|---|
| 20 | require_once(IA_ROOT_DIR."common/parameter.php"); |
|---|
| 21 | db_connect(); |
|---|
| 22 | |
|---|
| 23 | // validate argv |
|---|
| 24 | log_assert(2 == $argc, "Expecting a single argument: round_id!"); |
|---|
| 25 | $round_id = $argv[1]; |
|---|
| 26 | |
|---|
| 27 | // validate round id |
|---|
| 28 | $round = round_get($round_id); |
|---|
| 29 | log_assert($round, "Invalid round identifier!"); |
|---|
| 30 | $params = round_get_parameters($round_id); |
|---|
| 31 | log_assert(isset($params['rating_timestamp']), |
|---|
| 32 | "Round does not have parameter rating_timestamp!"); |
|---|
| 33 | $timestamp = parameter_decode('rating_timestamp', $params['rating_timestamp']); |
|---|
| 34 | log_assert(isset($params['rating_update']), |
|---|
| 35 | "Round does not have parameter rating_update!"); |
|---|
| 36 | log_assert(parameter_decode('rating_update', $params['rating_update']), |
|---|
| 37 | "Round is not marked as rating_update!"); |
|---|
| 38 | log_print("Updating ratings for round ".$round_id."...\n"); |
|---|
| 39 | |
|---|
| 40 | // read all registered user names |
|---|
| 41 | log_print("Reading whole user list..."); |
|---|
| 42 | $user_list = user_get_list(); |
|---|
| 43 | $usercount = count($user_list); |
|---|
| 44 | log_print(count($user_list) . " infoarena users"); |
|---|
| 45 | |
|---|
| 46 | // read last user ratings, deviations & timestamps |
|---|
| 47 | log_print("Reading last user ratings, deviations and timestamps..."); |
|---|
| 48 | $current_scores = rating_last_scores(); |
|---|
| 49 | |
|---|
| 50 | // do some quick testing: check if current rating cache corresponds |
|---|
| 51 | // with what rating_last_scores() says |
|---|
| 52 | $ucheck = user_get_list(true); |
|---|
| 53 | foreach ($ucheck as $row) { |
|---|
| 54 | if (!(float)$row['rating_cache']) { |
|---|
| 55 | continue; |
|---|
| 56 | } |
|---|
| 57 | log_assert(isset($current_scores[$row['username']])); |
|---|
| 58 | if ($current_scores[$row['username']]['rating'] != $row['rating_cache']) { |
|---|
| 59 | echo $row['username']." -> ".$current_scores[$row['username']]['rating']." != ".$row['rating_cache']."\n"; |
|---|
| 60 | log_print('rating_last_scores() fails to match rating_cache!!!'); |
|---|
| 61 | // read_bool('This is serious. Have you seen the warning?'); |
|---|
| 62 | // log_error('rating_last_scores() fails to match rating_cache!!!'); |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | // merge data |
|---|
| 67 | log_print("Merging data..."); |
|---|
| 68 | $users = rating_init($user_list, $current_scores); |
|---|
| 69 | |
|---|
| 70 | // read registered users |
|---|
| 71 | $scores = array(); |
|---|
| 72 | log_print("Reading round registered users..."); |
|---|
| 73 | $round_users = round_get_registered_users_range($round['id'], 0, $usercount); |
|---|
| 74 | log_print(count($round_users) . " users registered for this round."); |
|---|
| 75 | foreach ($round_users as $row) { |
|---|
| 76 | log_assert(isset($row['username'])); |
|---|
| 77 | $scores[$row['username']] = 0; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | // read round scores |
|---|
| 81 | log_print("Reading round scores..."); |
|---|
| 82 | $round_rows = score_get_rankings($round_id, null, 0, $usercount); |
|---|
| 83 | log_print(count($round_rows) . " users were scored in this round."); |
|---|
| 84 | foreach ($round_rows as $row) { |
|---|
| 85 | log_assert(isset($row['user_name']) && isset($row['score'])); |
|---|
| 86 | if (isset($scores[$row['user_name']])) { |
|---|
| 87 | $scores[$row['user_name']] = $row['score']; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | // Computing new ratings |
|---|
| 92 | rating_update($users, $scores, $timestamp); |
|---|
| 93 | |
|---|
| 94 | // update database |
|---|
| 95 | log_print("Updating database..."); |
|---|
| 96 | foreach ($round_users as $row) { |
|---|
| 97 | $username = $row['username']; |
|---|
| 98 | $user_id = $row['user_id']; |
|---|
| 99 | // for this round (history) |
|---|
| 100 | score_update_rating($user_id, $round_id, $users[$username]['deviation'], $users[$username]['rating']); |
|---|
| 101 | |
|---|
| 102 | // cache rating |
|---|
| 103 | $user = $ucheck[$username]; |
|---|
| 104 | $user['rating_cache'] = $users[$username]['rating']; |
|---|
| 105 | user_update($user); |
|---|
| 106 | |
|---|
| 107 | } |
|---|
| 108 | ?> |
|---|