5180 fichiers 38.5 Mo 0 téléchargements 221 vues mis à jour il y a 5 heures ui
nitro-v36-with-renderer-v36 / src / api / notification / TreasureHuntNotifications.ts
TreasureHuntNotifications.ts 3.1 Ko Télécharger le fichier
/**
 * The treasure hunt texts of the official notifications controller (AIR 15, the controller that
 * registers TreasureHuntUpdate / TreasureHuntFail / TreasureHuntFirstWinner and turns each into a
 * `treasure_hunt` styled notification). Pure: the caller passes the localizer, which returns the
 * key itself for a missing text, like `LocalizeText`.
 */
export type TreasureHuntLocalizer = (key: string, parameters?: string[], replacements?: string[]) => string;

const FALLBACKS: Record<string, string> = {
    'treasure_hunt.progress.desc': 'Well done! You have completed %current%/%total% steps of the %hunt_name%!',
    'treasure_hunt.won.desc': 'Congratulations, you have completed the %hunt_name%!',
    'treasure_hunt.level_fail.desc': 'You need to be at least level %level% to complete this hunt, or level %level_paying% as a paying Habbo',
    'treasure_hunt.winner.desc': '%user_name% is the first Habbo to win the %hunt_name%!'
};

const fill = (text: string, parameters: string[], replacements: string[]): string =>
    parameters.reduce((value, parameter, index) => value.split(`%${parameter}%`).join(replacements[index] ?? ''), text);

const localizeOrFallback = (localize: TreasureHuntLocalizer, key: string, parameters: string[], replacements: string[]): string => {
    const value = localize(key, parameters, replacements);

    if (value && value !== key) return value;

    return fill(FALLBACKS[key] ?? '', parameters, replacements);
};

/** `getHuntName`: `treasure_hunt.<code>.name`, the hunt code itself when the text is missing. */
export const getTreasureHuntName = (huntCode: string, localize: TreasureHuntLocalizer): string => {
    const key = `treasure_hunt.${huntCode}.name`;
    const value = localize(key);

    return value && value !== key ? value : huntCode;
};

/** TreasureHuntUpdate: `won.desc` once the hunt is completed, `progress.desc` before. */
export const getTreasureHuntUpdateText = (
    huntCode: string,
    stepsCompleted: number,
    totalSteps: number,
    completed: boolean,
    localize: TreasureHuntLocalizer
): string => {
    const huntName = getTreasureHuntName(huntCode, localize);

    if (completed) return localizeOrFallback(localize, 'treasure_hunt.won.desc', ['hunt_name'], [huntName]);

    return localizeOrFallback(localize, 'treasure_hunt.progress.desc', ['current', 'total', 'hunt_name'], [String(stepsCompleted), String(totalSteps), huntName]);
};

/** TreasureHuntFail: the level the hunt asks for, regular and Habbo Club. */
export const getTreasureHuntFailText = (requiredLevel: number, requiredLevelPaying: number, localize: TreasureHuntLocalizer): string =>
    localizeOrFallback(localize, 'treasure_hunt.level_fail.desc', ['level', 'level_paying'], [String(requiredLevel), String(requiredLevelPaying)]);

/** TreasureHuntFirstWinner: the first Habbo who completed the hunt. */
export const getTreasureHuntWinnerText = (userName: string, huntCode: string, localize: TreasureHuntLocalizer): string =>
    localizeOrFallback(localize, 'treasure_hunt.winner.desc', ['user_name', 'hunt_name'], [userName, getTreasureHuntName(huntCode, localize)]);
15Sujets
1Dépôts
20Membres
2En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser