<commit_before>/**
 * Simple ELO score rating system based on
 * https://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details
 */
library ELO {
    struct Score {
        uint score;
        string alias;
    }
    struct Scores {
        mapping(address => Score) scores;
    }

    /**
     * Records a game result for a game between two players.
     * Winner can be 0 to record a draw
     */
    function recordResult(Scores storage self,
                          address player1, address player2, address winner) {
        // Get current scores
        uint scoreA = getScore(self, player1);
        uint scoreB = getScore(self, player2);

        // Calculate result for player A
        int resultA = 1; // 0 = lose, 1 = draw, 2 = win
        if (winner == player1) {
            resultA = 2;
        }
        else if (winner == player2) {
            resultA = 0;
        }

        // Calculate new score
        var (changeA, changeB) = getScoreChange(int(scoreA)-int(scoreB), resultA);
        setScore(self, player1, uint(int(scoreA) + changeA));
        setScore(self, player2, uint(int(scoreB) + changeB));
    }

    /**
     * Table based expectation formula
     * E = 1 / ( 1 + 10**((difference)/400))
     * Table calculated based on inverse: difference = (400*log(1/E-1))/(log(10))
     * scoreChange = K * (result - E)
     * K = 20
     * Because curve is mirrored around 0, uses only one table for positive side
     * Returns (scoreChangeA, scoreChangeB)
     */
    function getScoreChange(int difference, int resultA) returns (int, int) {
        bool reverse = (difference > 0); // note if difference was positive
        uint diff = abs(difference); // take absolute to lookup in positive table
        // Score change lookup table
        int scoreChange = 10;
        if (diff >= 798) scoreChange = 20;
        else if (diff >= 511) scoreChange = 19;
        else if (diff >= 381) scoreChange = 18;
        else if (diff >= 301) scoreChange = 17;
        else if (diff >= 240) scoreChange = 16;
        else if (diff >= 190) scoreChange = 15;
        else if (diff >= 147) scoreChange = 14;
        else if (diff >= 107) scoreChange = 13;
        else if (diff >= 70) scoreChange = 12;
        else if (diff >= 34) scoreChange = 11;
        // Depending on result (win/draw/lose), calculate score changes
        if (resultA == 2) {
            return ((reverse ? 20-scoreChange : scoreChange ),
                    (reverse ? -scoreChange : -(20-scoreChange)));
        }<commit_msg>Use Elo score change table based on correct round numbers instead of integer truncation<commit_after>/**
 * Simple ELO score rating system based on
 * https://en.wikipedia.org/wiki/Elo_rating_system#Mathematical_details
 */
library ELO {
    struct Score {
        uint score;
        string alias;
    }
    struct Scores {
        mapping(address => Score) scores;
    }

    /**
     * Records a game result for a game between two players.
     * Winner can be 0 to record a draw
     */
    function recordResult(Scores storage self,
                          address player1, address player2, address winner) {
        // Get current scores
        uint scoreA = getScore(self, player1);
        uint scoreB = getScore(self, player2);

        // Calculate result for player A
        int resultA = 1; // 0 = lose, 1 = draw, 2 = win
        if (winner == player1) {
            resultA = 2;
        }
        else if (winner == player2) {
            resultA = 0;
        }

        // Calculate new score
        var (changeA, changeB) = getScoreChange(int(scoreA)-int(scoreB), resultA);
        setScore(self, player1, uint(int(scoreA) + changeA));
        setScore(self, player2, uint(int(scoreB) + changeB));
    }

    /**
     * Table based expectation formula
     * E = 1 / ( 1 + 10**((difference)/400))
     * Table calculated based on inverse: difference = (400*log(1/E-1))/(log(10))
     * scoreChange = Round( K * (result - E) )
     * K = 20
     * Because curve is mirrored around 0, uses only one table for positive side
     * Returns (scoreChangeA, scoreChangeB)
     */
    function getScoreChange(int difference, int resultA) returns (int, int) {
        bool reverse = (difference > 0); // note if difference was positive
        uint diff = abs(difference); // take absolute to lookup in positive table
        // Score change lookup table
        int scoreChange = 10;
        if (diff >= 635) scoreChange = 20;
        else if (diff >= 436) scoreChange = 19;
        else if (diff >= 337) scoreChange = 18;
        else if (diff >= 269) scoreChange = 17;
        else if (diff >= 214) scoreChange = 16;
        else if (diff >= 168) scoreChange = 15;
        else if (diff >= 126) scoreChange = 14;
        else if (diff >= 88) scoreChange = 13;
        else if (diff >= 52) scoreChange = 12;
        else if (diff >= 17) scoreChange = 11;
        // Depending on result (win/draw/lose), calculate score changes
        if (resultA == 2) {
            return ((reverse ? 20-scoreChange : scoreChange ),
                    (reverse ? -scoreChange : -(20-scoreChange)));
        }
{'fraction_non_alphanumeric': 0.1102883865939205, 'fraction_numerical': 0.03409976617303195, 'mean_word_length': 2.081032412965186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 27, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3343747', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1424, 'n_words': 512}