Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

raidboss: improve translation of timeline netregex #5962

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions ui/raidboss/timeline_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import logDefinitions, { LogDefinitionTypes } from '../../resources/netlog_defs'
import { buildNetRegexForTrigger } from '../../resources/netregexes';
import { UnreachableCode } from '../../resources/not_reached';
import Regexes from '../../resources/regexes';
import { translateRegex, translateText } from '../../resources/translations';
import {
AnonNetRegexParams,
translateRegex,
translateRegexBuildParamAnon,
translateText,
} from '../../resources/translations';
import { NetParams } from '../../types/net_props';
import { LooseTimelineTrigger, TriggerAutoConfig } from '../../types/trigger';

Expand Down Expand Up @@ -80,7 +85,7 @@ export type Error = {

export type Sync = {
id: number;
origRegexStr: string;
origInput: string | AnonNetRegexParams;
regexType: 'parsed' | 'net';
regex: RegExp;
start: number;
Expand Down Expand Up @@ -500,13 +505,17 @@ export class TimelineParser {
});
return line;
}

const translatedParams = translateRegexBuildParamAnon(
params,
this.options.ParserLanguage,
this.replacements,
).params;
return this.buildRegexSync(
uniqueid,
'net',
`${netRegexType} ${syncCommand.netRegex}`,
// TODO: Use `translateRegexBuildParam` instead, store off params for use elsewhere.
// See https://github.com/quisquous/cactbot/pull/5939#discussion_r1399453530
Regexes.parse(this.GetReplacedSync(buildNetRegexForTrigger(netRegexType, params))),
params,
Regexes.parse(buildNetRegexForTrigger(netRegexType, translatedParams)),
syncCommand.args,
seconds,
lineNumber,
Expand Down Expand Up @@ -545,7 +554,7 @@ export class TimelineParser {
private buildRegexSync(
uniqueid: number,
regexType: 'parsed' | 'net',
regex: string,
origInput: string | AnonNetRegexParams,
parsedRegex: RegExp,
args: string | undefined,
seconds: number,
Expand All @@ -555,7 +564,7 @@ export class TimelineParser {
) {
const sync: Sync = {
id: uniqueid,
origRegexStr: regex,
origInput: origInput,
regexType: regexType,
regex: parsedRegex,
start: seconds - 2.5,
Expand Down Expand Up @@ -680,8 +689,18 @@ export class TimelineParser {
if (lineText)
line = line.replace(` "${lineText.name}"`, ` "${lineText.text}"`);
const lineSync = lineToSync[lineNumber];
if (lineSync)
line = line.replace(`sync /${lineSync.origRegexStr}/`, `sync /${lineSync.regex.source}/`);
if (lineSync) {
if (typeof lineSync.origInput === 'string') {
line = line.replace(`sync /${lineSync.origInput}/`, `sync /${lineSync.regex.source}/`);
} else {
const translatedParams = translateRegexBuildParamAnon(
lineSync.origInput,
timeline.options.ParserLanguage,
timeline.replacements,
).params;
line = line.replace(/{[^}]*}/, `{ ${JSON.stringify(translatedParams)} }`);
}
}

if (syncErrors?.[lineNumber])
line += ' #MISSINGSYNC';
Expand Down
36 changes: 26 additions & 10 deletions util/find_missing_timeline_translations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const findMissingTimeline = (
{
type: 'replaceSync',
items: new Set(
timeline.syncStarts.map((x) => ({ text: x.regex.source, line: x.lineNumber })),
timeline.syncStarts.map((x) => ({ text: x.origInput, line: x.lineNumber })),
),
replace: trans.replaceSync || {},
label: 'sync',
Expand Down Expand Up @@ -185,16 +185,32 @@ const findMissingTimeline = (

for (const testCase of testCases) {
for (const item of testCase.items) {
if (isIgnored(item.text))
continue;
let matched = false;
for (const regex in testCase.replace) {
if (item.text.match(Regexes.parse(regex))) {
matched = true;
break;
const origInput = item.text;
let errorStr: string | undefined;
if (typeof origInput === 'string') {
let matched = false;
if (isIgnored(origInput))
continue;
for (const regex in testCase.replace) {
if (!origInput.match(Regexes.parse(regex))) {
matched = true;
break;
}
}
if (!matched)
errorStr = `"${origInput}"`;
} else {
const result = translateRegexBuildParamAnon(origInput, locale, [trans]);
const missingFields = result.missingFields;
if (!result.wasTranslated && missingFields !== undefined) {
const outputObj: { [key: string]: AnonNetRegexParams['string'] } = {};
for (const field of missingFields)
outputObj[field] = origInput[field];
errorStr = JSON.stringify(outputObj);
}
}
if (!matched) {

if (errorStr !== undefined) {
// Because we handle syncs separately from texts, in order to
// sort them all properly together, create a key to be used with sort().
const sortKey = String(item.line).padStart(8, '0') + testCase.label;
Expand All @@ -203,7 +219,7 @@ const findMissingTimeline = (
item.line,
testCase.label,
locale,
`"${item.text}"`,
errorStr,
];
}
}
Expand Down