Skip to content

Commit

Permalink
[S] Update for new ranking system
Browse files Browse the repository at this point in the history
  • Loading branch information
Benau committed Aug 28, 2020
1 parent 5adfb78 commit 15dcebd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 15 deletions.
11 changes: 10 additions & 1 deletion api/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,9 @@
$output->writeAttribute('scores', $ranking['scores']);
$output->writeAttribute('max-scores', $ranking['max_scores']);
$output->writeAttribute('num-races-done', $ranking['num_races_done']);
$output->writeAttribute('raw-scores', $ranking['raw_scores']);
$output->writeAttribute('rating-deviation', $ranking['rating_deviation']);
$output->writeAttribute('disconnects', $ranking['disconnects']);
$output->writeAttribute('rank', $ranking['rank']);
$output->endElement();
}
Expand Down Expand Up @@ -552,6 +555,9 @@
$output->writeAttribute('scores', $player['scores']);
$output->writeAttribute('max-scores', $player['max_scores']);
$output->writeAttribute('num-races-done', $player['num_races_done']);
$output->writeAttribute('raw-scores', $player['raw_scores']);
$output->writeAttribute('rating-deviation', $player['rating_deviation']);
$output->writeAttribute('disconnects', $player['disconnects']);
$output->writeAttribute('rank', $player['rank']);
$output->endElement();
}
Expand Down Expand Up @@ -584,7 +590,10 @@
$new_scores = isset($_POST['scores']) ? $_POST['scores'] : null;
$new_max_scores = isset($_POST['max-scores']) ? $_POST['max-scores'] : null;
$new_num_races_done = isset($_POST['num-races-done']) ? (int)$_POST['num-races-done'] : null;
Ranking::submitRanking($permission, $id_for_ranked, $new_scores, $new_max_scores, $new_num_races_done);
$new_raw_scores = isset($_POST['raw-scores']) ? $_POST['raw-scores'] : null;
$new_rating_deviation = isset($_POST['rating-deviation']) ? $_POST['rating-deviation'] : null;
$new_disconnects = isset($_POST['disconnects']) ? $_POST['disconnects'] : null;
Ranking::submitRanking($permission, $id_for_ranked, $new_scores, $new_max_scores, $new_num_races_done, $new_raw_scores, $new_rating_deviation, $new_disconnects);

$output->startElement('submit-ranking');
$output->writeAttribute('success', 'yes');
Expand Down
11 changes: 11 additions & 0 deletions cron/src/DailyCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,16 @@ public static function run(): void
{
echo "ERROR: Verification::cron \n" . $e->getMessage();
}

try
{
Ranking::cron();
echo "SUCCESS: Ranking::cron \n";
}
catch (RankingException $e)
{
echo "ERROR: RankingException::cron \n" . $e->getMessage();
}

}
}
65 changes: 56 additions & 9 deletions include/Ranking.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public static function getTopPlayersFromRanking($topn)
user_id,
username,
@score:=s.scores scores, max_scores,
num_races_done
num_races_done,
raw_scores,
rating_deviation,
disconnects
FROM `{DB_VERSION}_rankings` s
INNER JOIN `{DB_VERSION}_users` ON user_id = `{DB_VERSION}_users`.id,
(SELECT @score:=0, @rank:=0) r
Expand Down Expand Up @@ -70,9 +73,12 @@ public static function getInitialRanking()
return
[
'rank' => -1,
'scores' => 2000.0,
'max_scores' => 2000.0,
'num_races_done' => 0
'scores' => 1300.0,
'max_scores' => 1300.0,
'num_races_done' => 0,
'raw_scores' => 4000.0,
'rating_deviation' => 1000.0,
'disconnects' => 0
];
}

Expand All @@ -93,6 +99,9 @@ public static function getRanking($user_id)
a.scores,
a.max_scores,
a.num_races_done,
a.raw_scores,
a.rating_deviation,
a.disconnects,
(SELECT
COUNT(DISTINCT scores)
FROM `{DB_VERSION}_rankings` b
Expand Down Expand Up @@ -146,6 +155,9 @@ public static function resetRanking($user_permissions)
* @param double $new_scores
* @param double $new_max_scores
* @param int $new_num_races_done
* @param double $new_raw_scores
* @param double $new_rating_deviation
* @param $new_disconnects
*
* @throws RankingException
*/
Expand All @@ -154,7 +166,10 @@ public static function submitRanking(
$id_for_ranked,
$new_scores,
$new_max_scores,
$new_num_races_done
$new_num_races_done,
$new_raw_scores,
$new_rating_deviation,
$new_disconnects
) {
if (!in_array(AccessControl::PERM_SUMBIT_RANKINGS, $user_permissions))
{
Expand All @@ -164,21 +179,29 @@ public static function submitRanking(
{
DBConnection::get()->query(
"INSERT INTO `{DB_VERSION}_rankings` (user_id, scores,
max_scores, num_races_done) VALUES (:user_id, :scores, :max_scores, :num_races_done)
max_scores, num_races_done, raw_scores, rating_deviation, disconnects)
VALUES (:user_id, :scores, :max_scores, :num_races_done, :raw_scores, :rating_deviation, :disconnects)
ON DUPLICATE KEY UPDATE `scores` = :scores,
`max_scores` = :max_scores, `num_races_done`= :num_races_done",
`max_scores` = :max_scores, `num_races_done`= :num_races_done,
`raw_scores` = :raw_scores, `rating_deviation` = :rating_deviation, `disconnects`= :disconnects",
DBConnection::NOTHING,
[
':user_id' => $id_for_ranked,
':scores' => $new_scores,
':max_scores' => $new_max_scores,
':num_races_done' => $new_num_races_done
':num_races_done' => $new_num_races_done,
':raw_scores' => $new_raw_scores,
':rating_deviation' => $new_rating_deviation,
':disconnects' => $new_disconnects,
],
[
':user_id' => DBConnection::PARAM_INT,
':scores' => DBConnection::PARAM_STR,
':max_scores' => DBConnection::PARAM_STR,
':num_races_done' => DBConnection::PARAM_INT
':num_races_done' => DBConnection::PARAM_INT,
':raw_scores' => DBConnection::PARAM_STR,
':rating_deviation' => DBConnection::PARAM_STR,
':disconnects' => DBConnection::PARAM_STR, // Do not use PARAM_INT for 64bit unsigned integer
]
);
}
Expand All @@ -187,4 +210,28 @@ public static function submitRanking(
throw new RankingException(exception_message_db(_('sumbit ranking')));
}
}

/**
* Increase rating deviation of all records daily.
* Accounts playing ranked rarely will have a rising rating uncertainty
* Reduce the score accordingly to keep the scores in sync with raw scores and rating deviation
* (300 is 3 times the minimum rating deviation, we can't fetch the C++ code constant here).
* The formula increases a rating deviation of 100 to 400 in 500 days and from 400 to 1000 in 500 more days.
*
* @throws RankingException
*/
public static function cron()
{
try
{
DBConnection::get()->query(
"UPDATE `{DB_VERSION}_rankings`
SET rating_deviation = LEAST(1000.0, (rating_deviation + 0.277) * 1.001388),
scores = raw_scores - 3.0 * rating_deviation + 300.0");
}
catch (DBException $e)
{
throw new RankingException(exception_message_db('cron'));
}
}
}
11 changes: 7 additions & 4 deletions install/install.sql
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,13 @@ CREATE TABLE IF NOT EXISTS `v3_ipv4_mapping` (
-- Table structure for player ranking scores `v3_rankings`
--
CREATE TABLE IF NOT EXISTS `v3_rankings` (
`user_id` INT UNSIGNED NOT NULL,
`scores` DOUBLE NOT NULL,
`max_scores` DOUBLE NOT NULL,
`num_races_done` INT UNSIGNED NOT NULL,
`user_id` INT UNSIGNED NOT NULL,
`scores` DOUBLE NOT NULL,
`max_scores` DOUBLE NOT NULL,
`num_races_done` INT UNSIGNED NOT NULL,
`raw_scores` DOUBLE NOT NULL,
`rating_deviation` DOUBLE NOT NULL,
`disconnects` BIGINT UNSIGNED NOT NULL,
PRIMARY KEY (`user_id`),
KEY `scores` (`scores`),
CONSTRAINT `v3_rankings_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `v3_users` (`id`)
Expand Down
7 changes: 6 additions & 1 deletion rankings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@

$tpl->assignTitle(_h('Player Rankings'));

// disconnects is a 64bit bitflag,
// divide it with min(num_races_done, 64) for races done < 64 to have correct value
$query_rankings = <<<SQL
SELECT
IF (@score=s.scores, @rank:=@rank, @rank:=@rank+1)
`Rank`,
username `Username`,
ROUND(@score:=s.scores,2) `Scores`,
ROUND(max_scores, 2) `Maximum scores obtained`,
num_races_done `Races done`
num_races_done `Races done`,
ROUND(rating_deviation, 2) `Rating Deviation`,
concat(ROUND(BIT_COUNT(disconnects) /
CAST(LEAST(num_races_done, 64) AS DOUBLE) * 100.0 , 2), '%') `Disconnection rate`
FROM `{DB_VERSION}_rankings` s
INNER JOIN `{DB_VERSION}_users` ON user_id = `{DB_VERSION}_users`.id,
(SELECT @score:=0, @rank:=0) r
Expand Down

0 comments on commit 15dcebd

Please sign in to comment.