前言
本文用到的一切数据均来自@px_poxiao 通过总结比赛等价分变动拟合的公式。仅供参考,不是官方计算公式,不代表真实等级分变动。
感谢@px_poxiao 无私开源运营洛谷档案馆 luogu.ac.cn 并公布等级分预测公式,同时,他也为插件的开发做了很多贡献。
计算原理
详细内容请移步 px_poxiao 的专栏。
总之就是 的计算包括历史权重 、赛前表现分 、本场表现分 和低场次惩罚组成,并被上限 截断。
收集已有比赛情况并加入数据库,对样本比赛 318072、318187、319865、320476 进行计算,排除 的选手后共收集了 条赛前场次超过 场的样本( 等数据参考性较高),P95 绝对误差为 。
考虑到比赛之间不是相互独立,而是相互影响的。
所以给数据库的所有数据加上 / 标签表示真实数据/预测数据,
1.每场比赛根据所有赛前的 / 数据计算 等,加入 队列
2.每更新一次比赛,官方计算等级分的,从 队列移除,并将该场比赛的 数据加入, 备份,重新计算所有的 比赛即可。
实现细节
- 通过 API 获取所有预测的等级分,获得历史比赛成绩;
- 生成表格的 html(由于原生页面是手写的绘图库且没有外部接口);
- 用生成的表格替换原页面上的即可。
源代码
// ==UserScript==
// @name 预测洛谷等级分趋势图
// @namespace http://tampermonkey.net/
// @version 2.0
// @description 预测你的等级分!
// @author a_small_OIer
// @match https://www.luogu.com.cn/user/*
// @grant GM_xmlhttpRequest
// @connect luogu.ac.cn
// @require https://cdn.jsdelivr.net/npm/echarts@5.4.3/dist/echarts.min.js
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
function waitForElement(selector, timeout = 5000){
return new Promise((resolve, reject) => {
const start = Date.now();
const check = () => {
const el = document.querySelector(selector);
if (el) resolve(el);
else if (Date.now() - start > timeout) reject(new Error(`元素 ${selector} 加载超时`));
else setTimeout(check, 200);
};
check();
});
}
function getUid(){
const match = window.location.pathname.match(/\/user\/(\d+)/);
return match ? match[1] : null;
}
function parseHistoryData(){
const contextEl = document.getElementById('lentille-context');
if (!contextEl) throw new Error('未找到 #lentille-context');
const raw = contextEl.textContent;
const parsed = JSON.parse(raw);
const elo = parsed?.data?.elo;
if (!Array.isArray(elo) || elo.length === 0) throw new Error('elo数据无效或为空');
return elo;
}
function fetchPredictions(uid){
return new Promise((resolve, reject) => {
const url = `https://luogu.ac.cn/api/v1/user/${uid}/rating-predictions`;
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function(response) {
try {
const json = JSON.parse(response.responseText);
resolve(json);
} catch (e) {
reject(new Error('解析预测API响应失败'));
}
},
onerror: function(err) {
reject(err);
}
});
});
}
function isoToUnix(iso) {
return new Date(iso).getTime() / 1000;
}
function buildFullData(history, predictions) {
const sortedHistory = history.slice().sort((a, b) => a.time - b.time);
const rawItems = predictions?.items || [];
const validItems = rawItems.filter(item => {
const warnings = item.warnings || [];
return !warnings.some(w => w.includes('赛前等级分不低于本场等级分阈值'));
});
const predData = validItems.map(item => ({
isPredicted: true,
rating: item.predicted_rating,
time: isoToUnix(item.end_time),
latest: false,
contest: {
id: item.contest_id,
startTime: isoToUnix(item.start_time),
endTime: isoToUnix(item.end_time),
name: item.contest_name
},
userCount: 0,
prevDiff: item.predicted_delta,
previous: {
rating: item.old_rating,
time: null,
latest: false,
contest: null,
userCount: 0,
prevDiff: null
}
})).sort((a, b) => a.time - b.time);
const full = [...sortedHistory, ...predData];
const lastHistory = sortedHistory[sortedHistory.length - 1];
if (predData.length > 0 && lastHistory) {
const placeholder = {
...lastHistory,
isPredicted: true,
isPlaceholder: true,
rating: lastHistory.rating,
time: lastHistory.time,
contest: { ...lastHistory.contest }
};
const idx = full.indexOf(predData[0]);
full.splice(idx, 0, placeholder);
}
return full;
}
function formatTime(ts) {
const d = new Date((ts + 8 * 3600) * 1000);
const year = d.getUTCFullYear();
const month = String(d.getUTCMonth() + 1).padStart(2, '0');
const day = String(d.getUTCDate()).padStart(2, '0');
const hours = String(d.getUTCHours()).padStart(2, '0');
const minutes = String(d.getUTCMinutes()).padStart(2, '0');
return `${year}-${month}-${day} ${hours}:${minutes}`;
}
function prepareContainer() {
let existingCard = document.querySelector('div.l-card:has(#rating-chart)');
if(!existingCard){
const cards = document.querySelectorAll('div.l-card');
for(const card of cards){
const header = card.querySelector('.header h3.lfe-h3');
if(header && header.textContent.trim() === '比赛等级分趋势图'){
existingCard = card;
break;
}
}
}
let container;
if(existingCard){
const header = existingCard.querySelector('.header');
while(existingCard.firstChild){
existingCard.removeChild(existingCard.firstChild);
}
if(header)
existingCard.appendChild(header);
const chartDiv = document.createElement('div');
chartDiv.id = 'rating-chart';
chartDiv.style.cssText = 'width: 100%; height: 320px;';
existingCard.appendChild(chartDiv);
container = chartDiv;
const footer = document.createElement('div');
footer.style.cssText = `
margin-top: 8px;
font-size: 12px;
color: #999;
text-align: right;
border-top: 1px solid #f0f0f0;
padding: 8px 20px 0 20px;
`;
footer.innerHTML = `
<span style="display:inline-block; background:#eee; border-radius:50%; width:16px; height:16px; line-height:16px; text-align:center; color:#666; font-weight:bold; margin-right:4px;">!</span>
虚线部分为预测,不代表最终等级分变动<br>数据来源 洛谷档案馆 luogu.ac.cn
`;
existingCard.appendChild(footer);
} else {
const wrapper = document.createElement('div');
wrapper.className = 'l-card';
wrapper.style.cssText = `
width: 100%;
max-width: 100%;
margin: 20px auto;
padding: 16px 0;
background: #fff;
border-radius: 8px;
box-shadow: 0 2px 12px rgba(0,0,0,0.08);
box-sizing: border-box;
`;
const header = document.createElement('div');
header.className = 'header';
header.style.cssText = `
display: flex;
justify-content: space-between;
align-items: center;
padding: 0 20px 12px 20px;
border-bottom: 1px solid #eee;
font-size: 16px;
font-weight: 600;
color: #333;
`;
header.innerHTML = `
<h3 class="lfe-h3" style="margin:0;font-size:16px;">比赛等级分趋势图</h3>
<span class="lfe-caption" style="font-size:13px;color:#888;font-weight:400;">点击数据点可跳转比赛</span>
`;
wrapper.appendChild(header);
const chartDiv = document.createElement('div');
chartDiv.id = 'rating-chart';
chartDiv.style.cssText = 'width: 100%; height: 320px;';
wrapper.appendChild(chartDiv);
const footer = document.createElement('div');
footer.style.cssText = `
margin-top: 8px;
font-size: 12px;
color: #999;
text-align: right;
border-top: 1px solid #f0f0f0;
padding: 8px 20px 0 20px;
`;
footer.innerHTML = `
<span style="display:inline-block; background:#eee; border-radius:50%; width:16px; height:16px; line-height:16px; text-align:center; color:#666; font-weight:bold; margin-right:4px;">!</span>
虚线部分为预测,不代表最终等级分变动
`;
wrapper.appendChild(footer);
const target = document.querySelector('.user-profile') || document.querySelector('.main');
if(target){
target.parentNode.insertBefore(wrapper, target.nextSibling);
}else{
document.body.appendChild(wrapper);
}
container = chartDiv;
}
return container;
}
function renderChart(container, data){
if(!container)
return;
const myChart = echarts.init(container);
const historical = data.filter(d => !d.isPredicted);
const predictedFull = data.filter(d => d.isPredicted);
const maxRating = Math.max(...data.map(d => d.rating));
const yMax = Math.ceil(maxRating / 400) * 400 || 400;
function buildSeriesItems(arr, isPredictedSeries = false) {
return arr.map(item => {
const base = {
value: [item.time * 1000, item.rating],
symbol: item.isPlaceholder ? 'none' : 'circle',
symbolSize: 8,
rawInfo: item
};
if(isPredictedSeries){
if(item.isPlaceholder){
base.itemStyle = { color: 'transparent', borderColor: 'transparent' };
}else{
base.itemStyle = {
color: 'rgba(255,255,255,0.4)',
borderColor: 'rgba(93,173,226,0.6)',
borderWidth: 1.5
};
}
}else{
base.itemStyle = {
color: '#fff',
borderColor: '#5dade2',
borderWidth: 2
};
}
if(item.rating === maxRating && !item.isPlaceholder){
if(isPredictedSeries){
base.itemStyle = {
color: 'rgba(255,255,255,0.4)',
borderColor: 'rgba(231,76,60,0.6)',
borderWidth: 1.5
};
}else{
base.itemStyle = {
color: '#fff',
borderColor: '#e74c3c',
borderWidth: 2
};
}
}
return base;
});
}
const option = {
grid: {
left: 0,
right: 15,
top: 25,
bottom: 20,
containLabel: true
},
xAxis: {
type: 'time',
axisLine: { lineStyle: { color: '#ccc' } },
axisLabel: {
color: '#555',
fontSize: 12,
formatter: function(value) {
const d = new Date(value);
return d.getFullYear() + '-' + String(d.getMonth()+1).padStart(2,'0');
}
},
splitLine: { show: false }
},
yAxis: {
type: 'value',
min: 0,
max: yMax,
interval: 400,
axisLine: { show: false },
axisLabel: { color: '#555', fontSize: 12 },
splitLine: { show: true, lineStyle: { color: '#e0e0e0', type: 'solid' } }
},
tooltip: {
trigger: 'item',
backgroundColor: 'rgba(42,42,42,0.95)',
borderColor: 'transparent',
borderRadius: 4,
padding: [12, 16],
textStyle: { color: '#fff' },
formatter: function(params){
const item = params.data.rawInfo;
if(!item || item.isPlaceholder)
return '';
const isPred = item.isPredicted || false;
const titlePrefix = isPred ? '[预测] ' : '';
const start = formatTime(item.contest.startTime);
const end = formatTime(item.contest.endTime);
let prevRating = 0, diff = 0;
if(item.previous && item.previous.rating !== undefined) {
prevRating = item.previous.rating;
diff = item.prevDiff || 0;
}else{
prevRating = item.rating - (item.prevDiff || 0);
diff = item.prevDiff || 0;
}
let diffColor, diffDisplay;
if(diff === 0){
diffColor = '#888';
diffDisplay = '±0';
}else if (diff > 0){
diffColor = '#4caf50';
diffDisplay = '+' + diff;
}else{
diffColor = '#e74c3c';
diffDisplay = diff;
}
return `
<div style="font-size:14px; font-weight:bold; margin-bottom:4px;">${titlePrefix}${item.contest.name}</div>
<div style="font-size:12px; color:#a0a0a0; margin-bottom:6px;">${start} ~ ${end}</div>
<div style="font-size:14px;">
等级分:<span style="color:#fff; font-weight:bold;">${prevRating}</span>
<span style="color:${diffColor}; font-weight:bold;"> ${diffDisplay}</span>
= <span style="color:#fff; font-weight:bold;">${item.rating}</span>
</div>
`;
}
},
series: [
{
name: '历史比赛',
type: 'line',
data: buildSeriesItems(historical, false),
smooth: false,
lineStyle: { type: 'solid', color: '#5dade2', width: 2.5 },
animationDuration: 1500,
animationEasing: 'cubicOut'
},
{
name: '预测比赛',
type: 'line',
data: buildSeriesItems(predictedFull, true),
smooth: false,
lineStyle: {
type: 'dashed',
color: '#5dade2',
width: 2.5,
dash: [8, 6],
opacity: 0.6
},
animationDuration: 1500,
animationEasing: 'cubicOut'
}
]
};
myChart.setOption(option);
myChart.on('click', function(params){
const item = params.data?.rawInfo;
if (!item || item.isPlaceholder) return;
const contestId = item.contest?.id;
if(contestId){
window.open(`https://www.luogu.com.cn/contest/${contestId}`, '_blank');
//感谢破晓 关注@px_poxiao 谢谢喵
}
});
const resizeHandler = () => myChart.resize();
window.addEventListener('resize', resizeHandler);
const observer = new ResizeObserver(resizeHandler);
observer.observe(container);
}
async function main(){
try{
const uid = getUid();
if(!uid){
console.warn('未找到用户ID');
return;
}
let history;
try{
history = parseHistoryData();
}catch (e){
console.error('解析历史数据失败', e);
return;
}
let predictions = null;
try{
predictions = await fetchPredictions(uid);
}catch (e){
console.warn('获取预测数据失败,只显示历史比赛数据', e);
predictions = { items: [] };
}
const fullData = buildFullData(history, predictions);
if (fullData.length === 0) {
console.warn('无数据可展示');
return;
}
try{
await waitForElement('.l-card .header h3.lfe-h3', 3000);
}catch (e){
console.log('未找到原有卡片,将新建');
}
const container = prepareContainer();
if(!container){
console.error('无法获取图表容器');
return;
}
await new Promise(resolve => setTimeout(resolve, 100));
renderChart(container, fullData);
}catch (error){
console.error('脚本运行出错:', error);
}
}
if(document.readyState === 'complete'){
main();
}else{
window.addEventListener('load', main);
}
})();AI 使用声明:作者曾使用 DeepSeek 参与开发,保证本人贡献不小于 DeepSeek。
