source: trunk/scripts/update-ratings @ 1184

Revision 1110, 3.8 KB checked in by savin.tiberiu@…, 2 years ago (diff)

The long awaited moment. *drumroll*

Split ia score.

Scores are now separated from the ratings. Also the rankings macro can take detail_round and detail_task parameters to add more columns.

REVIEW URL: http://reviewboard.infoarena.ro/r/64/

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
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
9require_once(dirname($argv[0]) . "/utilities.php");
10
11require_once(IA_ROOT_DIR."www/config.php");
12require_once(IA_ROOT_DIR."www/identity.php");
13require_once(IA_ROOT_DIR."common/db/round.php");
14require_once(IA_ROOT_DIR."common/db/score.php");
15require_once(IA_ROOT_DIR."common/db/parameter.php");
16require_once(IA_ROOT_DIR."common/db/user.php");
17require_once(IA_ROOT_DIR."common/round.php");
18require_once(IA_ROOT_DIR."common/score.php");
19require_once(IA_ROOT_DIR."common/rating.php");
20require_once(IA_ROOT_DIR."common/parameter.php");
21db_connect();
22
23// validate argv
24log_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);
29log_assert($round, "Invalid round identifier!");
30$params = round_get_parameters($round_id);
31log_assert(isset($params['rating_timestamp']),
32           "Round does not have parameter rating_timestamp!");
33$timestamp = parameter_decode('rating_timestamp', $params['rating_timestamp']);
34log_assert(isset($params['rating_update']),
35           "Round does not have parameter rating_update!");
36log_assert(parameter_decode('rating_update', $params['rating_update']),
37           "Round is not marked as rating_update!");
38log_print("Updating ratings for round ".$round_id."...\n");
39
40// read all registered user names
41log_print("Reading whole user list...");
42$user_list = user_get_list();
43$usercount = count($user_list);
44log_print(count($user_list) . " infoarena users");
45
46// read last user ratings, deviations & timestamps
47log_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);
53foreach ($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
67log_print("Merging data...");
68$users = rating_init($user_list, $current_scores);
69
70// read registered users
71$scores = array();
72log_print("Reading round registered users...");
73$round_users = round_get_registered_users_range($round['id'], 0, $usercount);
74log_print(count($round_users) . " users registered for this round.");
75foreach ($round_users as $row) {
76    log_assert(isset($row['username']));
77    $scores[$row['username']] = 0;
78}
79
80// read round scores
81log_print("Reading round scores...");
82$round_rows = score_get_rankings($round_id, null, 0, $usercount);
83log_print(count($round_rows) . " users were scored in this round.");
84foreach ($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
92rating_update($users, $scores, $timestamp);
93
94// update database
95log_print("Updating database...");
96foreach ($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?>
Note: See TracBrowser for help on using the repository browser.