/** * Battle search - handles searching battle logs. */ import {FS, Utils, ProcessManager, Repl} from '../../lib'; import {checkRipgrepAvailability} from '../config-loader'; const BATTLESEARCH_PROCESS_TIMEOUT = 3 * 60 * 60 * 1000; // 3 hours interface BattleOutcome { lost: string; won: string; turns: string; } interface BattleSearchResults { totalBattles: number; /** Total battle outcomes. Null when only searching for one userid.*/ totalOutcomes: BattleOutcome[] | null; totalWins: {[k: string]: number}; totalLosses: {[k: string]: number}; totalTies: number; timesBattled: {[k: string]: number}; } const MAX_BATTLESEARCH_PROCESSES = 1; export async function runBattleSearch(userids: ID[], month: string, tierid: ID, turnLimit?: number) { const useRipgrep = await checkRipgrepAvailability(); const pathString = `logs/${month}/${tierid}/`; const results: {[k: string]: BattleSearchResults} = {}; let files = []; try { files = await FS(pathString).readdir(); } catch (err: any) { if (err.code === 'ENOENT') { return results; } throw err; } const [userid] = userids; files = files.filter(item => item.startsWith(month)).map(item => `logs/${month}/${tierid}/${item}`); if (useRipgrep) { // Matches non-word (including _ which counts as a word) characters between letters/numbers // in a user's name so the userid can case-insensitively be matched to the name. const regexString = userids.map(id => `(?=.*?("p(1|2)":"${[...id].join('[^a-zA-Z0-9]*')}[^a-zA-Z0-9]*"))`).join(''); let output; try { output = await ProcessManager.exec(['rg', '-i', regexString, '--no-line-number', '-P', '-tjson', ...files]); } catch { return results; } for (const line of output.stdout.split('\n').reverse()) { const [file, raw] = Utils.splitFirst(line, ':'); if (!raw || !line) continue; const data = JSON.parse(raw); const day = file.split('/')[3]; if (!results[day]) { results[day] = { totalBattles: 0, totalWins: {}, totalOutcomes: userids.length > 1 ? [] : null, totalLosses: {}, totalTies: 0, timesBattled: {}, }; } const p1id = toID(data.p1); const p2id = toID(data.p2); if (userids.length > 1) { // looking for specific userids, only register ones where those users are players if (userids.filter(item => [p1id, p2id].includes(item)).length < userids.length) continue; } else { if (!(p1id === userid || p2id === userid)) continue; } if (turnLimit && data.turns > turnLimit) continue; if (!results[day]) { results[day] = { totalBattles: 0, totalWins: {}, totalOutcomes: userids.length > 1 ? [] : null, totalLosses: {}, totalTies: 0, timesBattled: {}, }; } results[day].totalBattles++; const winnerid = toID(data.winner); const loser = winnerid === p1id ? p2id : p1id; if (userids.includes(winnerid)) { if (!results[day].totalWins[winnerid]) results[day].totalWins[winnerid] = 0; results[day].totalWins[winnerid]++; } else if (data.winner) { if (!results[day].totalLosses[loser]) results[day].totalLosses[loser] = 0; results[day].totalLosses[loser]++; } else { results[day].totalTies++; } // explicitly state 0 of stats if none for (const id of userids) { if (!results[day].totalLosses[id]) results[day].totalLosses[id] = 0; if (!results[day].totalWins[id]) results[day].totalWins[id] = 0; } const outcomes = results[day].totalOutcomes; if (outcomes) { outcomes.push({won: winnerid, lost: loser, turns: data.turns}); } // we only want foe data for single-userid searches const foe = userids.length > 1 ? null : userid === toID(data.p1) ? toID(data.p2) : toID(data.p1); if (foe) { if (!results[day].timesBattled[foe]) results[day].timesBattled[foe] = 0; results[day].timesBattled[foe]++; } } return results; } for (const file of files) { const subFiles = FS(`${file}`).readdirSync(); const day = file.split('/')[3]; for (const dayFile of subFiles) { const json = FS(`${file}/${dayFile}`).readIfExistsSync(); const data = JSON.parse(json); const p1id = toID(data.p1); const p2id = toID(data.p2); if (userids.length > 1) { // looking for specific userids, only register ones where those users are players if (userids.filter(item => item === p1id || item === p2id).length < userids.length) continue; } else { if (!(p1id === userid || p2id === userid)) continue; } if (turnLimit && data.turns > turnLimit) continue; if (!results[day]) { results[day] = { totalBattles: 0, totalWins: {}, totalOutcomes: [], totalLosses: {}, totalTies: 0, timesBattled: {}, }; } results[day].totalBattles++; const winnerid = toID(data.winner); const loser = winnerid === p1id ? p2id : p1id; if (userids.includes(winnerid)) { if (!results[day].totalWins[winnerid]) results[day].totalWins[winnerid] = 0; results[day].totalWins[winnerid]++; } else if (data.winner) { if (!results[day].totalLosses[loser]) results[day].totalLosses[loser] = 0; results[day].totalLosses[loser]++; } else { results[day].totalTies++; } // explicitly state 0 of stats if none for (const id of userids) { if (!results[day].totalLosses[id]) results[day].totalLosses[id] = 0; if (!results[day].totalWins[id]) results[day].totalWins[id] = 0; } const outcomes = results[day].totalOutcomes; if (outcomes) { outcomes.push({won: winnerid, lost: loser, turns: data.turns}); } // we don't want foe data if we're searching for 2 userids const foe = userids.length > 1 ? null : userid === p1id ? p2id : p1id; if (foe) { if (!results[day].timesBattled[foe]) results[day].timesBattled[foe] = 0; results[day].timesBattled[foe]++; } } } return results; } function buildResults( data: {[k: string]: BattleSearchResults}, userids: ID[], month: string, tierid: ID, turnLimit?: number ) { let buf = `>view-battlesearch-${userids.join('-')}--${turnLimit}--${month}--${tierid}--confirm\n|init|html\n|title|[Battle Search][${userids.join('-')}][${tierid}][${month}]\n`; buf += `|pagehtml|
`; buf += `${tierid} battles on ${month} where `; buf += userids.length > 1 ? `the users ${userids.join(', ')} were players` : `the user ${userids[0]} was a player`; buf += turnLimit ? ` and the battle lasted less than ${turnLimit} turn${Chat.plural(turnLimit)}` : ''; buf += `:
| Won | Lost | Turns |
|---|---|---|
| ${won} | ${lost} | ${turns} |
`; const {totalWins, totalLosses} = dayStats; buf += `
${day}`; buf += ` | |
|---|---|
| Category | Number |
| Total Battles | ${dayStats.totalBattles} |
| Total Wins${userids.length > 1 ? ` (${id}) ` : ''} | ${totalWins[id]} |
| Total Losses${userids.length > 1 ? ` (${id}) ` : ''} | ${totalLosses[id]} |
| Opponent | Times Battled |
| `; buf += `${foe}`; buf += ` | ${dayStats.timesBattled[foe]} |
Userid${Chat.plural(userids)}: ${userids.join(', ')}
`; if (turnLimit) { buf += `Maximum Turns: ${turnLimit}`; } buf += `
`; const months = Utils.sortBy( (await FS('logs/').readdir()).filter(f => f.length === 7 && f.includes('-')), name => ({reverse: name}) ); if (!month) { buf += `Please select a month:
Please select the tier to search:
Are you sure you want to run a battle search for for ${tierid} battles on ${month} `; buf += `where the ${userids.length > 1 ? `user(s) ${userids.join(', ')} were players` : `the user ${userid} was a player`}`; if (turnLimit) buf += ` and the battle lasted less than ${turnLimit} turn${Chat.plural(turnLimit)}`; buf += `?
`; return `${buf}`; } // Run search void getBattleSearch(connection, userids, month, tierid, turnLimit); return ( `` + `Searching for ${tierid} battles on ${month} where the ` + `${userids.length > 1 ? `user(s) ${userids.join(', ')} were players` : `the user ${userid} was a player`} ` + (turnLimit ? `and the battle lasted less than ${turnLimit} turn${Chat.plural(turnLimit)}.` : '') + `
Loading... (this will take a while)