File size: 4,945 Bytes
a59c3c4
 
d1f47f9
 
 
 
ca6e756
d1f47f9
 
 
 
 
 
 
 
a59c3c4
 
d1f47f9
a59c3c4
d1f47f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a59c3c4
 
d1f47f9
 
 
a59c3c4
d1f47f9
 
 
 
ca6e756
 
d1f47f9
ca6e756
 
d1f47f9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Pictures of one code, paired in the browser by their content's digests (a shared one of content.sha256s, or else
// content.sha256), with what changed between them. Nothing here is sent or stored.
import { join, pctText } from './util.js';
import { t } from './copy.js';
import { levelName } from './levels.js';
import { levelIndex } from './verdict.js';
import { ecUse } from './made.js';

let listOf = () => []; // the page's pictures (app.js)
export const pairWith = (fn) => { listOf = fn; };

const isFile = (p) => p.kind === 'graphic';
const scanned = (r) => (r.checks?.modules_from === 'scanner' ? r.checks.version : null);
/** Whether a content can be matched: by its digest, or, from a server without one, as an uncut text. */
const comparable = (c) => Boolean(c && (c.sha256 || (c.text != null && !c.cut)));
/** One content: a digest both share (a text that stands for two byte strings has two); from a server without them,
 *  the same digest; without one, only uncut, equal texts with equal byte counts. */
function same(a, b) {
  if (a.sha256s?.length && b.sha256s?.length) return a.sha256s.some((d) => b.sha256s.includes(d));
  if (a.sha256 && b.sha256) return a.sha256 === b.sha256;
  return a.text != null && b.text != null && !a.cut && !b.cut && a.bytes === b.bytes && a.text === b.text;
}

/** Who a read picture is compared with, among pictures of the same code (same content and version) first, then of
 *  the same content: a photo with the earliest original graphic, if there is one; otherwise a later picture with the
 *  earliest one. { q, versions } for the same content at another version; { none: true } when its content can't be
 *  matched; null when there's no one to compare with. */
export function pairOf(p) {
  const c = p.status === 'done' ? p.result?.content : null;
  if (!c) return null;
  const others = listOf().filter((q) => q !== p && q.status === 'done' && q.result?.content);
  if (!others.length) return null;
  if (!comparable(c)) return { none: true };
  const mates = others.filter((q) => comparable(q.result.content) && same(c, q.result.content)).sort((a, b) => a.n - b.n);
  const other = (q) => { const [a, b] = [scanned(p.result), scanned(q.result)]; return a != null && b != null && a !== b; };
  const pick = (list) => (!isFile(p) && list.find(isFile)) || list.find((q) => q.n < p.n) || null;
  const q = pick(mates.filter((m) => !other(m))) || pick(mates);
  if (!q) return null;
  return other(q) ? { q, versions: [scanned(p.result), scanned(q.result)] } : { q };
}

/** The pairing's lines for "Your picture": whom it matches, then what changed (the error correction used only
 *  between two files, levels only between two photos), or, when nothing compared differs, what was compared. */
export function pairLines(p) {
  const pair = pairOf(p);
  if (!pair) return [];
  if (pair.none) return [t('pair.not_compared')];
  const { q } = pair;
  const here = isFile(p) ? t(isFile(q) ? 'pair.in_this_file' : 'pair.in_file') : t('pair.here');
  const there = isFile(q) && !isFile(p) ? t('pair.in_file') : t('pair.in_picture', { n: q.n });
  if (pair.versions) return [t('pair.other_version', { a: pair.versions[0], la: here, b: pair.versions[1], lb: there })];
  const compared = []; // what both pictures have; only what differs, as shown, is listed
  const line = (key, f) => {
    const [a, b] = [f(p.result), f(q.result)];
    if (!a || !b) return '';
    compared.push(t(`pair.name.${key}`));
    return a === b ? '' : t(`pair.${key}`, { a, la: here, b, lb: there });
  };
  const changes = [
    line('margin', marginText),
    line('contrast', (r) => (r.checks?.contrast?.value != null ? pctText(Math.min(1, r.checks.contrast.value)) : '')),
    line('ppm', (r) => (r.checks?.ppm?.at1280 != null ? r.checks.ppm.at1280.toFixed(1) : '')),
    isFile(p) && isFile(q) ? line('uec', usedText) : '',
    !isFile(p) && !isFile(q) ? line('level', (r) => levelName(levelIndex(r))) : '',
  ].filter(Boolean);
  const unchanged = t(compared.length === 1 ? 'pair.unchanged_one' : 'pair.unchanged', { list: join(compared) });
  return [t('pair.same', { n: q.n }), ...(changes.length || !compared.length ? changes : [unchanged])];
}

/** A margin as the checks word it: cut by the picture's edge (a capped one too: its edge may be 3.5 squares out), 4
 *  squares or more, or how many. */
function marginText(r) {
  const m = r.checks?.quiet;
  if (!m || m.modules == null) return '';
  if (m.edge || m.capped) return t('pair.margin_edge');
  if (m.modules >= 4) return t('pair.margin_full');
  return t(m.modules === 1 ? 'checks.quiet.margin_one' : 'checks.quiet.margin_many', { amount: String(Number(m.modules.toFixed(2))) });
}

/** The error correction a file uses, as its own line judges it (made.js ecUse), for comparing two files; '' where it
 *  isn't judged. */
function usedText(r) {
  const u = ecUse(r);
  return typeof u !== 'number' ? '' : u > 0 ? pctText(u) : t('pair.uec_none');
}