mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
feat(atlas): flight routes, travellers and badges on the server
- GET /api/addons/atlas/flights folds the user's flight bookings into routes, airports and totals for the globe's arcs (#1369, #2642). - GET /api/addons/atlas/people answers the Atlas side of other travellers on the instance: counts, visited countries and earned badges, never trips, places or dates. A user hides themselves with the atlas_hidden setting; the admin switch atlas_compare_enabled turns it off for all. - shared/src/atlas/atlas-badges.ts holds the badge rules, computed from the data a user already has, so nothing is stored. - MCP: list_atlas_flight_routes, list_atlas_badges, list_atlas_travellers.
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
import { computeBadges, crossesDateline, type BadgeInput } from './atlas-badges';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
const base: BadgeInput = {
|
||||
countries: [],
|
||||
continents: [],
|
||||
regions: [],
|
||||
flights: { count: 0, km: 0, longestKm: 0, crossesDateline: false },
|
||||
streak: 0,
|
||||
wishesReached: 0,
|
||||
};
|
||||
|
||||
const byId = (input: BadgeInput) => new Map(computeBadges(input).map((b) => [b.id, b]));
|
||||
|
||||
describe('atlas badges', () => {
|
||||
it('counts countries up the steps with the year the step was reached', () => {
|
||||
const countries = Array.from({ length: 12 }, (_, i) => ({ code: `C${i}`, year: 2010 + i }));
|
||||
const b = byId({ ...base, countries });
|
||||
expect(b.get('countries-1')).toMatchObject({ earned: true, year: 2010, country: 'C0' });
|
||||
expect(b.get('countries-5')).toMatchObject({ earned: true, year: 2014, country: 'C4' });
|
||||
expect(b.get('countries-10')).toMatchObject({ earned: true, year: 2019 });
|
||||
expect(b.get('countries-25')).toMatchObject({ earned: false, current: 12, target: 25 });
|
||||
});
|
||||
|
||||
it('needs six inhabited continents for the collector and Antarctica for all seven', () => {
|
||||
const six = ['Europe', 'Asia', 'Africa', 'North America', 'South America', 'Oceania'];
|
||||
expect(byId({ ...base, continents: six }).get('continents-6')?.earned).toBe(true);
|
||||
expect(byId({ ...base, continents: six }).get('continents-7')?.earned).toBe(false);
|
||||
expect(byId({ ...base, continents: [...six, 'Antarctica'] }).get('continents-7')?.earned).toBe(true);
|
||||
});
|
||||
|
||||
it('gives a country badge for half and for all of its regions', () => {
|
||||
const half = byId({ ...base, regions: [{ country: 'US', visited: 26, total: 50 }] });
|
||||
expect(half.get('regions-half-US')?.earned).toBe(true);
|
||||
expect(half.get('regions-all-US')).toBeUndefined();
|
||||
expect(half.get('regions-next-US')).toMatchObject({ earned: false, current: 26, target: 50 });
|
||||
const all = byId({ ...base, regions: [{ country: 'US', visited: 50, total: 50 }] });
|
||||
expect(all.get('regions-all-US')?.earned).toBe(true);
|
||||
// A country with fewer than five regions never has one.
|
||||
expect(byId({ ...base, regions: [{ country: 'LU', visited: 3, total: 3 }] }).get('regions-all-LU')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('measures flights against the Earth and the Moon', () => {
|
||||
const b = byId({ ...base, flights: { count: 30, km: 50000, longestKm: 9500, crossesDateline: true } });
|
||||
expect(b.get('flights-earth')?.earned).toBe(true);
|
||||
expect(b.get('flights-moon')).toMatchObject({ earned: false, current: 50000 });
|
||||
expect(b.get('flights-10')?.earned).toBe(true);
|
||||
expect(b.get('flights-50')).toMatchObject({ earned: false, current: 30 });
|
||||
expect(b.get('flights-longhaul')?.earned).toBe(true);
|
||||
expect(b.get('flights-dateline')?.earned).toBe(true);
|
||||
});
|
||||
|
||||
it('shows every goal from the start and earns the equator south of it', () => {
|
||||
const b = byId(base);
|
||||
for (const id of ['continents-7', 'flights-dateline', 'equator', 'polar']) {
|
||||
expect(b.get(id)).toMatchObject({ earned: false });
|
||||
expect(b.get(id)).not.toHaveProperty('secret');
|
||||
}
|
||||
const south = byId({ ...base, countries: [{ code: 'NZ' }] });
|
||||
expect(south.get('equator')?.earned).toBe(true);
|
||||
});
|
||||
|
||||
it('finds three new countries in one year', () => {
|
||||
const countries = [2019, 2019, 2019, 2020].map((year, i) => ({ code: `X${i}`, year }));
|
||||
expect(byId({ ...base, countries }).get('burst-3')).toMatchObject({ earned: true, year: 2019 });
|
||||
});
|
||||
|
||||
it('gives every badge a category and a way to earn it', () => {
|
||||
for (const b of computeBadges(base)) {
|
||||
expect(b.category).toBeTruthy();
|
||||
expect(b.descKey).toMatch(/^atlas\.badge\./);
|
||||
}
|
||||
});
|
||||
|
||||
it('crosses the date line only across 180 degrees', () => {
|
||||
expect(crossesDateline(170, -170)).toBe(true);
|
||||
expect(crossesDateline(10, 120)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,610 @@
|
||||
/**
|
||||
* Atlas badges. Every badge is a rule over Atlas data the user already has
|
||||
* (countries, regions, flights, years, wishes), so nothing is stored:
|
||||
* remove a mark and the badge goes with it, add a rule and every user is judged
|
||||
* by it at once. Server (profiles, assistant) and client (Passport) run this file.
|
||||
*/
|
||||
|
||||
export type BadgeTier = 'bronze' | 'silver' | 'gold' | 'aurora';
|
||||
|
||||
export type BadgeCategory = 'countries' | 'continents' | 'regions' | 'flights' | 'explorer' | 'collector';
|
||||
|
||||
export const BADGE_CATEGORIES: BadgeCategory[] = [
|
||||
'countries',
|
||||
'continents',
|
||||
'regions',
|
||||
'flights',
|
||||
'explorer',
|
||||
'collector',
|
||||
];
|
||||
|
||||
export type BadgeMotif =
|
||||
| 'first'
|
||||
| 'countries'
|
||||
| 'continents'
|
||||
| 'seven'
|
||||
| 'region'
|
||||
| 'earth'
|
||||
| 'moon'
|
||||
| 'flights'
|
||||
| 'longhaul'
|
||||
| 'airports'
|
||||
| 'dateline'
|
||||
| 'equator'
|
||||
| 'hemispheres'
|
||||
| 'tropics'
|
||||
| 'polar'
|
||||
| 'streak'
|
||||
| 'burst'
|
||||
| 'wish'
|
||||
| 'islands';
|
||||
|
||||
export interface BadgeInput {
|
||||
/** Visited countries by ISO A2, with the year of the first visit where known. */
|
||||
countries: Array<{ code: string; year?: number | null }>;
|
||||
/** Continent of each visited country (Antarctica counted as its own). */
|
||||
continents: string[];
|
||||
/** Regions per country: how many are visited and how many count. */
|
||||
regions: Array<{ country: string; visited: number; total: number }>;
|
||||
flights: { count: number; km: number; longestKm: number; crossesDateline: boolean; airports?: number };
|
||||
/** Years in a row with a trip, as the Atlas stats report it. */
|
||||
streak: number;
|
||||
/** Wishes on the bucket list that were reached. */
|
||||
wishesReached: number;
|
||||
}
|
||||
|
||||
export interface Badge {
|
||||
id: string;
|
||||
category: BadgeCategory;
|
||||
motif: BadgeMotif;
|
||||
tier: BadgeTier;
|
||||
/** Translation key of the title; params fill it. */
|
||||
titleKey: string;
|
||||
params?: Record<string, string | number>;
|
||||
/** Translation key saying how the badge is earned; shares params. */
|
||||
descKey: string;
|
||||
earned: boolean;
|
||||
current: number;
|
||||
target: number;
|
||||
/** Year the badge was reached, where the data says. */
|
||||
year?: number | null;
|
||||
/** The country that made it happen, where there is one (the n-th country, a region's country). */
|
||||
country?: string | null;
|
||||
}
|
||||
|
||||
const EARTH_KM = 40075;
|
||||
const MOON_KM = 384400;
|
||||
|
||||
const ARCTIC = new Set(['IS', 'NO', 'SE', 'FI', 'GL', 'SJ', 'RU', 'CA', 'FO']);
|
||||
const SOUTHERN = new Set([
|
||||
'AU',
|
||||
'NZ',
|
||||
'ZA',
|
||||
'AR',
|
||||
'CL',
|
||||
'BR',
|
||||
'PE',
|
||||
'BO',
|
||||
'PY',
|
||||
'UY',
|
||||
'NA',
|
||||
'BW',
|
||||
'ZW',
|
||||
'MZ',
|
||||
'MG',
|
||||
'AO',
|
||||
'ZM',
|
||||
'MW',
|
||||
'TZ',
|
||||
'ID',
|
||||
'FJ',
|
||||
'PG',
|
||||
'MU',
|
||||
'LS',
|
||||
'SZ',
|
||||
'RE',
|
||||
'NC',
|
||||
'PF',
|
||||
'WS',
|
||||
'TO',
|
||||
'VU',
|
||||
'SB',
|
||||
'TL',
|
||||
]);
|
||||
/** Countries wholly or largely west of Greenwich, for the two hemispheres. */
|
||||
const WESTERN = new Set([
|
||||
'US',
|
||||
'CA',
|
||||
'MX',
|
||||
'BR',
|
||||
'AR',
|
||||
'CL',
|
||||
'PE',
|
||||
'CO',
|
||||
'EC',
|
||||
'BO',
|
||||
'PY',
|
||||
'UY',
|
||||
'VE',
|
||||
'GY',
|
||||
'SR',
|
||||
'CR',
|
||||
'PA',
|
||||
'NI',
|
||||
'HN',
|
||||
'SV',
|
||||
'GT',
|
||||
'BZ',
|
||||
'CU',
|
||||
'JM',
|
||||
'HT',
|
||||
'DO',
|
||||
'PR',
|
||||
'BS',
|
||||
'BB',
|
||||
'TT',
|
||||
'IS',
|
||||
'PT',
|
||||
'IE',
|
||||
'MA',
|
||||
'SN',
|
||||
'GM',
|
||||
'GN',
|
||||
'SL',
|
||||
'LR',
|
||||
'CI',
|
||||
'ML',
|
||||
'MR',
|
||||
'CV',
|
||||
'GW',
|
||||
'GL',
|
||||
]);
|
||||
const TROPICS = new Set([
|
||||
'TH',
|
||||
'VN',
|
||||
'KH',
|
||||
'LA',
|
||||
'MY',
|
||||
'SG',
|
||||
'ID',
|
||||
'PH',
|
||||
'LK',
|
||||
'MV',
|
||||
'IN',
|
||||
'BR',
|
||||
'CO',
|
||||
'EC',
|
||||
'PE',
|
||||
'VE',
|
||||
'CR',
|
||||
'PA',
|
||||
'NI',
|
||||
'HN',
|
||||
'GT',
|
||||
'BZ',
|
||||
'CU',
|
||||
'JM',
|
||||
'DO',
|
||||
'HT',
|
||||
'BS',
|
||||
'KE',
|
||||
'TZ',
|
||||
'UG',
|
||||
'RW',
|
||||
'ET',
|
||||
'GH',
|
||||
'NG',
|
||||
'SN',
|
||||
'CM',
|
||||
'MG',
|
||||
'MU',
|
||||
'SC',
|
||||
'FJ',
|
||||
'PG',
|
||||
'SB',
|
||||
'VU',
|
||||
'WS',
|
||||
'TO',
|
||||
'BB',
|
||||
'TT',
|
||||
'MX',
|
||||
'AU',
|
||||
'BO',
|
||||
]);
|
||||
const ISLANDS = new Set([
|
||||
'IS',
|
||||
'IE',
|
||||
'GB',
|
||||
'MT',
|
||||
'CY',
|
||||
'JP',
|
||||
'PH',
|
||||
'ID',
|
||||
'LK',
|
||||
'MV',
|
||||
'MU',
|
||||
'SC',
|
||||
'MG',
|
||||
'NZ',
|
||||
'FJ',
|
||||
'CU',
|
||||
'JM',
|
||||
'BS',
|
||||
'BB',
|
||||
'TT',
|
||||
'DO',
|
||||
'HT',
|
||||
'TW',
|
||||
'SG',
|
||||
'BH',
|
||||
'CV',
|
||||
'ST',
|
||||
'KM',
|
||||
'WS',
|
||||
'TO',
|
||||
'VU',
|
||||
'SB',
|
||||
'PG',
|
||||
'TL',
|
||||
'BN',
|
||||
'NR',
|
||||
]);
|
||||
|
||||
const TIERS: BadgeTier[] = ['bronze', 'silver', 'gold'];
|
||||
|
||||
function steps(values: number[]): Array<{ target: number; tier: BadgeTier }> {
|
||||
return values.map((target, i) => ({
|
||||
target,
|
||||
tier: i === values.length - 1 && values.length > 3 ? 'aurora' : TIERS[Math.min(i, 2)],
|
||||
}));
|
||||
}
|
||||
|
||||
/** The countries in the order they were first reached (unknown years last, by code). */
|
||||
function byFirstVisit(countries: BadgeInput['countries']): BadgeInput['countries'] {
|
||||
return [...countries].sort((a, b) => (a.year ?? 9999) - (b.year ?? 9999) || a.code.localeCompare(b.code));
|
||||
}
|
||||
|
||||
function count(input: BadgeInput['countries'], set: Set<string>): number {
|
||||
return input.filter((c) => set.has(c.code)).length;
|
||||
}
|
||||
|
||||
export function computeBadges(input: BadgeInput): Badge[] {
|
||||
const out: Badge[] = [];
|
||||
const ordered = byFirstVisit(input.countries);
|
||||
const n = ordered.length;
|
||||
const push = (b: Omit<Badge, 'earned' | 'current'> & { current: number }) => {
|
||||
out.push({ ...b, current: Math.min(b.current, b.target), earned: b.current >= b.target });
|
||||
};
|
||||
|
||||
// ---- countries
|
||||
push({
|
||||
id: 'countries-1',
|
||||
category: 'countries',
|
||||
motif: 'first',
|
||||
tier: 'bronze',
|
||||
titleKey: 'atlas.badge.first',
|
||||
descKey: 'atlas.badge.firstDesc',
|
||||
current: n,
|
||||
target: 1,
|
||||
year: ordered[0]?.year ?? null,
|
||||
country: ordered[0]?.code ?? null,
|
||||
});
|
||||
for (const { target, tier } of steps([5, 10, 25, 50, 75, 100, 150])) {
|
||||
const nth = n >= target ? ordered[target - 1] : undefined;
|
||||
push({
|
||||
id: `countries-${target}`,
|
||||
category: 'countries',
|
||||
motif: 'countries',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.countries',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.countriesDesc',
|
||||
current: n,
|
||||
target,
|
||||
year: nth?.year ?? null,
|
||||
country: nth?.code ?? null,
|
||||
});
|
||||
}
|
||||
|
||||
// ---- continents
|
||||
const inhabited = new Set(input.continents.filter((c) => c !== 'Antarctica'));
|
||||
for (const { target, tier } of steps([2, 3, 4, 5])) {
|
||||
push({
|
||||
id: `continents-${target}`,
|
||||
category: 'continents',
|
||||
motif: 'continents',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.continentsN',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.continentsNDesc',
|
||||
current: inhabited.size,
|
||||
target,
|
||||
});
|
||||
}
|
||||
push({
|
||||
id: 'continents-6',
|
||||
category: 'continents',
|
||||
motif: 'continents',
|
||||
tier: 'gold',
|
||||
titleKey: 'atlas.badge.continents',
|
||||
descKey: 'atlas.badge.continentsDesc',
|
||||
current: inhabited.size,
|
||||
target: 6,
|
||||
});
|
||||
const all = new Set(input.continents);
|
||||
push({
|
||||
id: 'continents-7',
|
||||
category: 'continents',
|
||||
motif: 'seven',
|
||||
tier: 'aurora',
|
||||
titleKey: 'atlas.badge.seven',
|
||||
descKey: 'atlas.badge.sevenDesc',
|
||||
current: all.size,
|
||||
target: 7,
|
||||
});
|
||||
|
||||
// ---- regions: one badge per country with five regions or more, plus totals
|
||||
for (const r of input.regions) {
|
||||
if (r.total < 5) continue;
|
||||
const half = Math.ceil(r.total / 2);
|
||||
if (r.visited >= r.total) {
|
||||
push({
|
||||
id: `regions-all-${r.country}`,
|
||||
category: 'regions',
|
||||
motif: 'region',
|
||||
tier: 'aurora',
|
||||
titleKey: 'atlas.badge.regionsAll',
|
||||
params: { country: r.country },
|
||||
descKey: 'atlas.badge.regionsAllDesc',
|
||||
current: r.visited,
|
||||
target: r.total,
|
||||
country: r.country,
|
||||
});
|
||||
} else if (r.visited >= half) {
|
||||
push({
|
||||
id: `regions-half-${r.country}`,
|
||||
category: 'regions',
|
||||
motif: 'region',
|
||||
tier: 'silver',
|
||||
titleKey: 'atlas.badge.regionsHalf',
|
||||
params: { country: r.country },
|
||||
descKey: 'atlas.badge.regionsHalfDesc',
|
||||
current: r.visited,
|
||||
target: half,
|
||||
country: r.country,
|
||||
});
|
||||
}
|
||||
}
|
||||
const best = [...input.regions]
|
||||
.filter((r) => r.total >= 5 && r.visited < r.total)
|
||||
.sort((a, b) => b.visited / b.total - a.visited / a.total)[0];
|
||||
if (best) {
|
||||
push({
|
||||
id: `regions-next-${best.country}`,
|
||||
category: 'regions',
|
||||
motif: 'region',
|
||||
tier: 'aurora',
|
||||
titleKey: 'atlas.badge.regionsAll',
|
||||
params: { country: best.country },
|
||||
descKey: 'atlas.badge.regionsAllDesc',
|
||||
current: best.visited,
|
||||
target: best.total,
|
||||
country: best.country,
|
||||
});
|
||||
}
|
||||
const regionTotal = input.regions.reduce((s, r) => s + r.visited, 0);
|
||||
for (const { target, tier } of steps([10, 50, 100])) {
|
||||
push({
|
||||
id: `regions-${target}`,
|
||||
category: 'regions',
|
||||
motif: 'region',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.regionsN',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.regionsNDesc',
|
||||
current: regionTotal,
|
||||
target,
|
||||
});
|
||||
}
|
||||
|
||||
// ---- flights
|
||||
const f = input.flights;
|
||||
for (const { target, tier } of steps([1, 10, 50, 100])) {
|
||||
push({
|
||||
id: `flights-${target}`,
|
||||
category: 'flights',
|
||||
motif: 'flights',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.flights',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.flightsDesc',
|
||||
current: f.count,
|
||||
target,
|
||||
});
|
||||
}
|
||||
push({
|
||||
id: 'flights-earth',
|
||||
category: 'flights',
|
||||
motif: 'earth',
|
||||
tier: 'gold',
|
||||
titleKey: 'atlas.badge.earth',
|
||||
descKey: 'atlas.badge.earthDesc',
|
||||
current: Math.round(f.km),
|
||||
target: EARTH_KM,
|
||||
});
|
||||
push({
|
||||
id: 'flights-earth-5',
|
||||
category: 'flights',
|
||||
motif: 'earth',
|
||||
tier: 'gold',
|
||||
titleKey: 'atlas.badge.earth5',
|
||||
descKey: 'atlas.badge.earth5Desc',
|
||||
current: Math.round(f.km),
|
||||
target: EARTH_KM * 5,
|
||||
});
|
||||
push({
|
||||
id: 'flights-moon',
|
||||
category: 'flights',
|
||||
motif: 'moon',
|
||||
tier: 'aurora',
|
||||
titleKey: 'atlas.badge.moon',
|
||||
descKey: 'atlas.badge.moonDesc',
|
||||
current: Math.round(f.km),
|
||||
target: MOON_KM,
|
||||
});
|
||||
push({
|
||||
id: 'flights-longhaul',
|
||||
category: 'flights',
|
||||
motif: 'longhaul',
|
||||
tier: 'bronze',
|
||||
titleKey: 'atlas.badge.longhaul',
|
||||
descKey: 'atlas.badge.longhaulDesc',
|
||||
current: Math.round(f.longestKm),
|
||||
target: 9000,
|
||||
});
|
||||
push({
|
||||
id: 'flights-ultra',
|
||||
category: 'flights',
|
||||
motif: 'longhaul',
|
||||
tier: 'gold',
|
||||
titleKey: 'atlas.badge.ultra',
|
||||
descKey: 'atlas.badge.ultraDesc',
|
||||
current: Math.round(f.longestKm),
|
||||
target: 13000,
|
||||
});
|
||||
for (const { target, tier } of steps([10, 25])) {
|
||||
push({
|
||||
id: `airports-${target}`,
|
||||
category: 'flights',
|
||||
motif: 'airports',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.airports',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.airportsDesc',
|
||||
current: f.airports ?? 0,
|
||||
target,
|
||||
});
|
||||
}
|
||||
push({
|
||||
id: 'flights-dateline',
|
||||
category: 'flights',
|
||||
motif: 'dateline',
|
||||
tier: 'gold',
|
||||
titleKey: 'atlas.badge.dateline',
|
||||
descKey: 'atlas.badge.datelineDesc',
|
||||
current: f.crossesDateline ? 1 : 0,
|
||||
target: 1,
|
||||
});
|
||||
|
||||
// ---- explorer
|
||||
const south = count(input.countries, SOUTHERN);
|
||||
push({
|
||||
id: 'equator',
|
||||
category: 'explorer',
|
||||
motif: 'equator',
|
||||
tier: 'silver',
|
||||
titleKey: 'atlas.badge.equator',
|
||||
descKey: 'atlas.badge.equatorDesc',
|
||||
current: south ? 1 : 0,
|
||||
target: 1,
|
||||
});
|
||||
const west = count(input.countries, WESTERN);
|
||||
const both = west > 0 && n - west > 0 ? 1 : 0;
|
||||
push({
|
||||
id: 'hemispheres',
|
||||
category: 'explorer',
|
||||
motif: 'hemispheres',
|
||||
tier: 'silver',
|
||||
titleKey: 'atlas.badge.hemispheres',
|
||||
descKey: 'atlas.badge.hemispheresDesc',
|
||||
current: both,
|
||||
target: 1,
|
||||
});
|
||||
push({
|
||||
id: 'tropics',
|
||||
category: 'explorer',
|
||||
motif: 'tropics',
|
||||
tier: 'bronze',
|
||||
titleKey: 'atlas.badge.tropics',
|
||||
descKey: 'atlas.badge.tropicsDesc',
|
||||
current: count(input.countries, TROPICS) ? 1 : 0,
|
||||
target: 1,
|
||||
});
|
||||
push({
|
||||
id: 'polar',
|
||||
category: 'explorer',
|
||||
motif: 'polar',
|
||||
tier: 'aurora',
|
||||
titleKey: 'atlas.badge.polar',
|
||||
descKey: 'atlas.badge.polarDesc',
|
||||
current: count(input.countries, ARCTIC) ? 1 : 0,
|
||||
target: 1,
|
||||
});
|
||||
const islands = count(input.countries, ISLANDS);
|
||||
for (const { target, tier } of steps([5, 10])) {
|
||||
push({
|
||||
id: `islands-${target}`,
|
||||
category: 'explorer',
|
||||
motif: 'islands',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.islands',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.islandsDesc',
|
||||
current: islands,
|
||||
target,
|
||||
});
|
||||
}
|
||||
for (const { target, tier } of steps([3, 5, 10])) {
|
||||
push({
|
||||
id: `streak-${target}`,
|
||||
category: 'explorer',
|
||||
motif: 'streak',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.streak',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.streakDesc',
|
||||
current: input.streak,
|
||||
target,
|
||||
});
|
||||
}
|
||||
const perYear = new Map<number, number>();
|
||||
for (const c of input.countries) if (typeof c.year === 'number') perYear.set(c.year, (perYear.get(c.year) ?? 0) + 1);
|
||||
const bestYear = [...perYear.entries()].sort((a, b) => b[1] - a[1] || a[0] - b[0])[0];
|
||||
for (const { target, tier } of steps([3, 5])) {
|
||||
const hit = !!bestYear && bestYear[1] >= target;
|
||||
push({
|
||||
id: `burst-${target}`,
|
||||
category: 'explorer',
|
||||
motif: 'burst',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.burst',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.burstDesc',
|
||||
current: bestYear?.[1] ?? 0,
|
||||
target,
|
||||
year: hit ? bestYear[0] : null,
|
||||
});
|
||||
}
|
||||
|
||||
// ---- collector
|
||||
for (const { target, tier } of steps([1, 5, 10])) {
|
||||
push({
|
||||
id: `wishes-${target}`,
|
||||
category: 'collector',
|
||||
motif: 'wish',
|
||||
tier,
|
||||
titleKey: 'atlas.badge.wishes',
|
||||
params: { count: target },
|
||||
descKey: 'atlas.badge.wishesDesc',
|
||||
current: input.wishesReached,
|
||||
target,
|
||||
});
|
||||
}
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Whether a great-circle leg between two longitudes crosses the antimeridian. */
|
||||
export function crossesDateline(lng1: number, lng2: number): boolean {
|
||||
return Math.abs(lng2 - lng1) > 180;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import {
|
||||
atlasFlightsResponseSchema,
|
||||
atlasPeopleResponseSchema,
|
||||
atlasTravellersRequestSchema,
|
||||
} from './atlas-globe.schema';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
describe('atlas globe contract', () => {
|
||||
it('asks for 1 to 200 travellers', () => {
|
||||
expect(atlasTravellersRequestSchema.safeParse({ ids: [1, 2] }).success).toBe(true);
|
||||
expect(atlasTravellersRequestSchema.safeParse({ ids: [] }).success).toBe(false);
|
||||
expect(atlasTravellersRequestSchema.safeParse({ ids: Array.from({ length: 201 }, (_, i) => i + 1) }).success).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it('describes the flights answer', () => {
|
||||
const airport = { code: 'LIS', name: 'Lisbon', lat: 38.77, lng: -9.13 };
|
||||
const flights = {
|
||||
routes: [
|
||||
{ a: airport, b: { ...airport, code: null }, count: 2, km: 1500, firstDate: '2024-05-01', lastDate: null },
|
||||
],
|
||||
airports: [{ ...airport, count: 2 }],
|
||||
totals: { flights: 2, km: 3000, airports: 2, longestKm: 1500 },
|
||||
};
|
||||
expect(atlasFlightsResponseSchema.safeParse(flights).success).toBe(true);
|
||||
expect(atlasFlightsResponseSchema.safeParse({ ...flights, totals: {} }).success).toBe(false);
|
||||
});
|
||||
|
||||
it('describes the people answer', () => {
|
||||
const badge = { id: 'countries-5', motif: 'globe', tier: 'bronze', titleKey: 'atlas.badge.countries', target: 5 };
|
||||
const person = { id: 1, countries: 3, continents: 1, regions: 4, flightKm: 0, codes: ['PT'], badges: [badge] };
|
||||
expect(atlasPeopleResponseSchema.safeParse({ people: [person] }).success).toBe(true);
|
||||
expect(
|
||||
atlasPeopleResponseSchema.safeParse({ people: [{ ...person, badges: [{ ...badge, tier: 'platinum' }] }] })
|
||||
.success,
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
/** Atlas globe contract: the flight map and the comparison with other travellers. */
|
||||
|
||||
const atlasAirportSchema = z.object({
|
||||
code: z.string().nullable(),
|
||||
name: z.string(),
|
||||
lat: z.number(),
|
||||
lng: z.number(),
|
||||
});
|
||||
|
||||
/** GET /api/addons/atlas/flights — flown routes folded by airport pair. */
|
||||
export const atlasFlightsResponseSchema = z.object({
|
||||
routes: z.array(
|
||||
z.object({
|
||||
a: atlasAirportSchema,
|
||||
b: atlasAirportSchema,
|
||||
count: z.number(),
|
||||
km: z.number(),
|
||||
firstDate: z.string().nullable(),
|
||||
lastDate: z.string().nullable(),
|
||||
}),
|
||||
),
|
||||
airports: z.array(atlasAirportSchema.extend({ count: z.number() })),
|
||||
totals: z.object({ flights: z.number(), km: z.number(), airports: z.number(), longestKm: z.number() }),
|
||||
});
|
||||
export type AtlasFlightsResponse = z.infer<typeof atlasFlightsResponseSchema>;
|
||||
|
||||
export const atlasBadgeSchema = z.object({
|
||||
id: z.string(),
|
||||
motif: z.string(),
|
||||
tier: z.enum(['bronze', 'silver', 'gold', 'aurora']),
|
||||
titleKey: z.string(),
|
||||
params: z.record(z.string(), z.union([z.string(), z.number()])).optional(),
|
||||
year: z.number().nullable().optional(),
|
||||
target: z.number(),
|
||||
});
|
||||
|
||||
/** GET /api/addons/atlas/people — the travellers asked for, minus whoever hid themselves. */
|
||||
export const atlasPeopleResponseSchema = z.object({
|
||||
people: z.array(
|
||||
z.object({
|
||||
id: z.number(),
|
||||
countries: z.number(),
|
||||
continents: z.number(),
|
||||
regions: z.number(),
|
||||
flightKm: z.number(),
|
||||
codes: z.array(z.string()),
|
||||
badges: z.array(atlasBadgeSchema),
|
||||
}),
|
||||
),
|
||||
});
|
||||
export type AtlasPeopleResponse = z.infer<typeof atlasPeopleResponseSchema>;
|
||||
|
||||
/** The travellers to compare with, by user id (the MCP twin of `GET /people?ids=`). */
|
||||
export const atlasTravellersRequestSchema = z.object({
|
||||
ids: z.array(z.number().int().positive()).min(1).max(200),
|
||||
});
|
||||
export type AtlasTravellersRequest = z.infer<typeof atlasTravellersRequestSchema>;
|
||||
@@ -34,6 +34,8 @@ export * from './tag/trip-tag.schema';
|
||||
export * from './notification/notification.schema';
|
||||
export * from './memories/memories.schema';
|
||||
export * from './atlas/atlas.schema';
|
||||
export * from './atlas/atlas-badges';
|
||||
export * from './atlas/atlas-globe.schema';
|
||||
export * from './vacay/vacay.schema';
|
||||
export * from './packing/packing.schema';
|
||||
export * from './packing/packed-count';
|
||||
|
||||
Reference in New Issue
Block a user