{
    "Function": "_addResultPoints",
    "File": "src/RankedBattle.sol",
    "Parent Contracts": [],
    "High-Level Calls": [
        "Neuron",
        "StakeAtRisk",
        "MergingPool",
        "StakeAtRisk",
        "StakeAtRisk"
    ],
    "Internal Calls": [
        "_getStakingFactor"
    ],
    "Library Calls": [],
    "Low-Level Calls": [],
    "Code": "function _addResultPoints(\n        uint8 battleResult, \n        uint256 tokenId, \n        uint256 eloFactor, \n        uint256 mergingPortion,\n        address fighterOwner\n    ) \n        private \n    {\n        uint256 stakeAtRisk;\n        uint256 curStakeAtRisk;\n        uint256 points = 0;\n\n        /// Check how many NRNs the fighter has at risk\n        stakeAtRisk = _stakeAtRiskInstance.getStakeAtRisk(tokenId);\n\n        /// Calculate the staking factor if it has not already been calculated for this round \n        if (_calculatedStakingFactor[tokenId][roundId] == false) {\n            stakingFactor[tokenId] = _getStakingFactor(tokenId, stakeAtRisk);\n            _calculatedStakingFactor[tokenId][roundId] = true;\n        }\n\n        /// Potential amount of NRNs to put at risk or retrieve from the stake-at-risk contract\n        curStakeAtRisk = (bpsLostPerLoss * (amountStaked[tokenId] + stakeAtRisk)) / 10**4;\n        if (battleResult == 0) {\n            /// If the user won the match\n\n            /// If the user has no NRNs at risk, then they can earn points\n            if (stakeAtRisk == 0) {\n                points = stakingFactor[tokenId] * eloFactor;\n            }\n\n            /// Divert a portion of the points to the merging pool\n            uint256 mergingPoints = (points * mergingPortion) / 100;\n            points -= mergingPoints;\n            _mergingPoolInstance.addPoints(tokenId, mergingPoints);\n\n            /// Do not allow users to reclaim more NRNs than they have at risk\n            if (curStakeAtRisk > stakeAtRisk) {\n                curStakeAtRisk = stakeAtRisk;\n            }\n\n            /// If the user has stake-at-risk for their fighter, reclaim a portion\n            /// Reclaiming stake-at-risk puts the NRN back into their staking pool\n            if (curStakeAtRisk > 0) {\n                _stakeAtRiskInstance.reclaimNRN(curStakeAtRisk, tokenId, fighterOwner);\n                amountStaked[tokenId] += curStakeAtRisk;\n            }\n\n            /// Add points to the fighter for this round\n            accumulatedPointsPerFighter[tokenId][roundId] += points;\n            accumulatedPointsPerAddress[fighterOwner][roundId] += points;\n            totalAccumulatedPoints[roundId] += points;\n            if (points > 0) {\n                emit PointsChanged(tokenId, points, true);\n            }\n        } else if (battleResult == 2) {\n            /// If the user lost the match\n\n            /// Do not allow users to lose more NRNs than they have in their staking pool\n            if (curStakeAtRisk > amountStaked[tokenId]) {\n                curStakeAtRisk = amountStaked[tokenId];\n            }\n            if (accumulatedPointsPerFighter[tokenId][roundId] > 0) {\n                /// If the fighter has a positive point balance for this round, deduct points \n                points = stakingFactor[tokenId] * eloFactor;\n                if (points > accumulatedPointsPerFighter[tokenId][roundId]) {\n                    points = accumulatedPointsPerFighter[tokenId][roundId];\n                }\n                accumulatedPointsPerFighter[tokenId][roundId] -= points;\n                accumulatedPointsPerAddress[fighterOwner][roundId] -= points;\n                totalAccumulatedPoints[roundId] -= points;\n                if (points > 0) {\n                    emit PointsChanged(tokenId, points, false);\n                }\n            } else {\n                /// If the fighter does not have any points for this round, NRNs become at risk of being lost\n                bool success = _neuronInstance.transfer(_stakeAtRiskAddress, curStakeAtRisk);\n                if (success) {\n                    _stakeAtRiskInstance.updateAtRiskRecords(curStakeAtRisk, tokenId, fighterOwner);\n                    amountStaked[tokenId] -= curStakeAtRisk;\n                }\n            }\n        }\n    }"
}