feat: add LoL Stats plugin — op.gg-powered player lookup
New plugin for League of Legends stats tracking, similar to op.gg: - Search summoners by Riot ID (Name#Tag) + region - Profile overview: rank, tier, LP, win rate, ladder position - Top champions with KDA and win rates - Match history with KDA, CS, items, game duration - Expandable match details showing all 10 players - Recent searches persisted across restarts Uses op.gg MCP server (no API key needed, no 24h expiration). Backend: server/src/plugins/lolstats/ (3 files) Frontend: web/src/plugins/lolstats/ (2 files) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
24b4dadb0f
commit
40c596fbfa
7 changed files with 1568 additions and 0 deletions
336
server/src/plugins/lolstats/opgg-api.ts
Normal file
336
server/src/plugins/lolstats/opgg-api.ts
Normal file
|
|
@ -0,0 +1,336 @@
|
|||
/**
|
||||
* op.gg MCP Client — calls the public op.gg MCP server at https://mcp-api.op.gg/mcp
|
||||
* Uses JSON-RPC 2.0 over StreamableHTTP (MCP protocol).
|
||||
* No API key needed.
|
||||
*/
|
||||
|
||||
import type {
|
||||
RegionInfo, SummonerProfile, MatchEntry, MatchParticipant,
|
||||
MatchParticipantStats, TeamStat,
|
||||
} from './types.js';
|
||||
|
||||
const MCP_URL = 'https://mcp-api.op.gg/mcp';
|
||||
let rpcId = 1;
|
||||
|
||||
// ── Regions ──
|
||||
|
||||
export const REGIONS: RegionInfo[] = [
|
||||
{ code: 'EUW', label: 'EU West' },
|
||||
{ code: 'EUNE', label: 'EU Nordic & East' },
|
||||
{ code: 'NA', label: 'North America' },
|
||||
{ code: 'KR', label: 'Korea' },
|
||||
{ code: 'BR', label: 'Brazil' },
|
||||
{ code: 'LAN', label: 'Latin America North' },
|
||||
{ code: 'LAS', label: 'Latin America South' },
|
||||
{ code: 'TR', label: 'Turkey' },
|
||||
{ code: 'RU', label: 'Russia' },
|
||||
{ code: 'JP', label: 'Japan' },
|
||||
{ code: 'OCE', label: 'Oceania' },
|
||||
{ code: 'PH', label: 'Philippines' },
|
||||
{ code: 'SG', label: 'Singapore' },
|
||||
{ code: 'TH', label: 'Thailand' },
|
||||
{ code: 'TW', label: 'Taiwan' },
|
||||
{ code: 'VN', label: 'Vietnam' },
|
||||
];
|
||||
|
||||
// ── MCP call helper ──
|
||||
|
||||
async function mcpCall(toolName: string, args: Record<string, any>): Promise<any> {
|
||||
const id = rpcId++;
|
||||
const body = {
|
||||
jsonrpc: '2.0',
|
||||
id,
|
||||
method: 'tools/call',
|
||||
params: { name: toolName, arguments: args },
|
||||
};
|
||||
|
||||
const res = await fetch(MCP_URL, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json, text/event-stream',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
throw new Error(`MCP HTTP ${res.status}: ${res.statusText}`);
|
||||
}
|
||||
|
||||
const json = await res.json() as any;
|
||||
|
||||
if (json.error) {
|
||||
const msg = json.error.message ?? JSON.stringify(json.error);
|
||||
throw new Error(msg);
|
||||
}
|
||||
|
||||
// MCP tools return content as an array of content blocks
|
||||
const text = json.result?.content?.[0]?.text;
|
||||
if (!text) throw new Error('Empty MCP response');
|
||||
|
||||
return text;
|
||||
}
|
||||
|
||||
// ── Response parser ──
|
||||
// op.gg MCP returns a custom class-notation format, not pure JSON.
|
||||
// We need to parse the structured text into usable data.
|
||||
|
||||
function parseClassNotation(text: string): any {
|
||||
// The response format is like:
|
||||
// class ClassName: field1,field2
|
||||
// ClassName(Data(...))
|
||||
//
|
||||
// We need to extract the actual data. The data part after the class
|
||||
// definitions is a nested constructor-call format.
|
||||
// Strategy: find the last line that starts with the root class name
|
||||
// and parse the nested parentheses structure.
|
||||
|
||||
const lines = text.split('\n');
|
||||
|
||||
// Find class definitions and the data line
|
||||
const classDefs: Map<string, string[]> = new Map();
|
||||
let dataLine = '';
|
||||
|
||||
for (const line of lines) {
|
||||
const classMatch = line.match(/^class (\w+): (.+)$/);
|
||||
if (classMatch) {
|
||||
classDefs.set(classMatch[1], classMatch[2].split(','));
|
||||
continue;
|
||||
}
|
||||
if (line.trim()) {
|
||||
dataLine = line.trim();
|
||||
}
|
||||
}
|
||||
|
||||
if (!dataLine) throw new Error('No data in MCP response');
|
||||
|
||||
// Parse the constructor-call notation recursively
|
||||
return parseConstructor(dataLine, classDefs);
|
||||
}
|
||||
|
||||
function parseConstructor(input: string, classDefs: Map<string, string[]>): any {
|
||||
// Try to match ClassName(...)
|
||||
const ctorMatch = input.match(/^(\w+)\((.+)\)$/s);
|
||||
if (!ctorMatch) {
|
||||
// It's a primitive value
|
||||
return parsePrimitive(input);
|
||||
}
|
||||
|
||||
const className = ctorMatch[1];
|
||||
const innerContent = ctorMatch[2];
|
||||
const fields = classDefs.get(className);
|
||||
|
||||
if (!fields) {
|
||||
// Unknown class, try to parse inner as a value
|
||||
return parseConstructor(innerContent, classDefs);
|
||||
}
|
||||
|
||||
// Split the inner content by top-level commas (respecting nested parens/brackets/strings)
|
||||
const values = splitTopLevel(innerContent);
|
||||
|
||||
const obj: Record<string, any> = {};
|
||||
for (let i = 0; i < fields.length && i < values.length; i++) {
|
||||
const fieldName = fields[i].trim();
|
||||
const rawVal = values[i].trim();
|
||||
|
||||
if (fieldName.endsWith('[]')) {
|
||||
// Array field — clean field name
|
||||
const cleanName = fieldName.replace('[]', '');
|
||||
obj[cleanName] = parseArrayValue(rawVal, classDefs);
|
||||
} else {
|
||||
obj[fieldName] = parseValue(rawVal, classDefs);
|
||||
}
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
function parseArrayValue(input: string, classDefs: Map<string, string[]>): any[] {
|
||||
// Array notation: [item1,item2,...] or [ClassName(...),ClassName(...)]
|
||||
if (input === '[]' || input === 'null' || input === 'None') return [];
|
||||
|
||||
if (input.startsWith('[') && input.endsWith(']')) {
|
||||
const inner = input.slice(1, -1).trim();
|
||||
if (!inner) return [];
|
||||
const items = splitTopLevel(inner);
|
||||
return items.map(item => parseValue(item.trim(), classDefs));
|
||||
}
|
||||
|
||||
// Single item not wrapped in brackets
|
||||
return [parseValue(input, classDefs)];
|
||||
}
|
||||
|
||||
function parseValue(input: string, classDefs: Map<string, string[]>): any {
|
||||
if (!input) return null;
|
||||
|
||||
// Check if it's a constructor call
|
||||
const ctorMatch = input.match(/^(\w+)\(/);
|
||||
if (ctorMatch && classDefs.has(ctorMatch[1])) {
|
||||
return parseConstructor(input, classDefs);
|
||||
}
|
||||
|
||||
// Check if it's an array
|
||||
if (input.startsWith('[') && input.endsWith(']')) {
|
||||
return parseArrayValue(input, classDefs);
|
||||
}
|
||||
|
||||
return parsePrimitive(input);
|
||||
}
|
||||
|
||||
function parsePrimitive(input: string): any {
|
||||
if (input === 'null' || input === 'None') return null;
|
||||
if (input === 'true' || input === 'True') return true;
|
||||
if (input === 'false' || input === 'False') return false;
|
||||
|
||||
// Quoted string
|
||||
if ((input.startsWith('"') && input.endsWith('"')) ||
|
||||
(input.startsWith("'") && input.endsWith("'"))) {
|
||||
return input.slice(1, -1);
|
||||
}
|
||||
|
||||
// Number
|
||||
const num = Number(input);
|
||||
if (!isNaN(num) && input !== '') return num;
|
||||
|
||||
// Raw string
|
||||
return input;
|
||||
}
|
||||
|
||||
function splitTopLevel(input: string): string[] {
|
||||
const results: string[] = [];
|
||||
let depth = 0;
|
||||
let inString = false;
|
||||
let stringChar = '';
|
||||
let current = '';
|
||||
|
||||
for (let i = 0; i < input.length; i++) {
|
||||
const ch = input[i];
|
||||
const prev = i > 0 ? input[i - 1] : '';
|
||||
|
||||
if (inString) {
|
||||
current += ch;
|
||||
if (ch === stringChar && prev !== '\\') {
|
||||
inString = false;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch === '"' || ch === "'") {
|
||||
inString = true;
|
||||
stringChar = ch;
|
||||
current += ch;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch === '(' || ch === '[') {
|
||||
depth++;
|
||||
current += ch;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch === ')' || ch === ']') {
|
||||
depth--;
|
||||
current += ch;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ch === ',' && depth === 0) {
|
||||
results.push(current);
|
||||
current = '';
|
||||
continue;
|
||||
}
|
||||
|
||||
current += ch;
|
||||
}
|
||||
|
||||
if (current.trim()) results.push(current);
|
||||
return results;
|
||||
}
|
||||
|
||||
// ── Public API functions ──
|
||||
|
||||
export async function getProfile(
|
||||
gameName: string, tagLine: string, region: string,
|
||||
): Promise<SummonerProfile> {
|
||||
const raw = await mcpCall('lol_get_summoner_profile', {
|
||||
game_name: gameName,
|
||||
tag_line: tagLine,
|
||||
region,
|
||||
lang: 'en_US',
|
||||
desired_output_fields: [
|
||||
'data.summoner.game_name',
|
||||
'data.summoner.tagline',
|
||||
'data.summoner.level',
|
||||
'data.summoner.profile_image_url',
|
||||
'data.summoner.updated_at',
|
||||
'data.summoner.league_stats[].tier_info.{tier,division,lp,tier_image_url}',
|
||||
'data.summoner.league_stats[].{game_type,win,lose,is_hot_streak}',
|
||||
'data.summoner.ladder_rank.{rank,total}',
|
||||
'data.summoner.most_champions.champion_stats[].{champion_name,id,play,win,lose,kill,death,assist}',
|
||||
'data.summoner.most_champions.{game_type,play,win,lose}',
|
||||
],
|
||||
});
|
||||
|
||||
const parsed = parseClassNotation(raw);
|
||||
// Navigate to summoner data (may be nested in data.summoner or similar)
|
||||
const summoner = parsed?.summoner ?? parsed?.data?.summoner ?? parsed;
|
||||
|
||||
return {
|
||||
game_name: summoner.game_name ?? gameName,
|
||||
tagline: summoner.tagline ?? tagLine,
|
||||
level: summoner.level ?? 0,
|
||||
profile_image_url: summoner.profile_image_url ?? '',
|
||||
league_stats: Array.isArray(summoner.league_stats) ? summoner.league_stats : [],
|
||||
ladder_rank: summoner.ladder_rank ?? { rank: null, total: null },
|
||||
most_champions: summoner.most_champions ?? { game_type: '', play: 0, win: 0, lose: 0, champion_stats: [] },
|
||||
updated_at: summoner.updated_at ?? '',
|
||||
};
|
||||
}
|
||||
|
||||
export async function getMatches(
|
||||
gameName: string, tagLine: string, region: string, limit = 10,
|
||||
): Promise<MatchEntry[]> {
|
||||
const raw = await mcpCall('lol_list_summoner_matches', {
|
||||
game_name: gameName,
|
||||
tag_line: tagLine,
|
||||
region,
|
||||
lang: 'en_US',
|
||||
limit,
|
||||
desired_output_fields: [
|
||||
'data.game_history[].{id,created_at,game_length_second,game_type,game_map}',
|
||||
'data.game_history[].average_tier_info.{tier,division}',
|
||||
'data.game_history[].participants[].{champion_name,champion_id,items[],items_names[],position,team_key,spells[]}',
|
||||
'data.game_history[].participants[].stats.{kill,death,assist,minion_kill,neutral_minion_kill,gold_earned,total_damage_dealt_to_champions,total_damage_taken,ward_place,vision_wards_bought_in_game,champion_level,op_score,op_score_rank,result}',
|
||||
'data.game_history[].participants[].summoner.{game_name,tagline}',
|
||||
'data.game_history[].teams[].{key,banned_champions_names[]}',
|
||||
'data.game_history[].teams[].game_stat.{is_win,baron_kill,dragon_kill,tower_kill,champion_kill,gold_earned,inhibitor_kill,rift_herald_kill}',
|
||||
],
|
||||
});
|
||||
|
||||
const parsed = parseClassNotation(raw);
|
||||
const history = parsed?.game_history ?? parsed?.data?.game_history ?? [];
|
||||
return Array.isArray(history) ? history : [];
|
||||
}
|
||||
|
||||
export async function getMatchDetail(
|
||||
gameId: string, createdAt: string, region: string,
|
||||
): Promise<MatchEntry | null> {
|
||||
const raw = await mcpCall('lol_get_summoner_game_detail', {
|
||||
game_id: gameId,
|
||||
created_at: createdAt,
|
||||
region,
|
||||
lang: 'en_US',
|
||||
desired_output_fields: [
|
||||
'data.game_detail.{id,created_at,game_length_second,game_type,game_map}',
|
||||
'data.game_detail.average_tier_info.{tier,division}',
|
||||
'data.game_detail.participants[].{champion_name,champion_id,items[],items_names[],position,team_key,spells[]}',
|
||||
'data.game_detail.participants[].stats.{kill,death,assist,minion_kill,neutral_minion_kill,gold_earned,total_damage_dealt_to_champions,total_damage_taken,ward_place,vision_wards_bought_in_game,champion_level,op_score,op_score_rank,result}',
|
||||
'data.game_detail.participants[].summoner.{game_name,tagline}',
|
||||
'data.game_detail.teams[].{key,banned_champions_names[]}',
|
||||
'data.game_detail.teams[].game_stat.{is_win,baron_kill,dragon_kill,tower_kill,champion_kill,gold_earned,inhibitor_kill,rift_herald_kill}',
|
||||
],
|
||||
});
|
||||
|
||||
const parsed = parseClassNotation(raw);
|
||||
return parsed?.game_detail ?? parsed?.data?.game_detail ?? null;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue