From 5e17c6c7cc47a8c43c78d2aada46eadd5bb293ad Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Thu, 30 Jul 2026 14:01:18 +0300 Subject: [PATCH] fix --- app/db/seed/core/SplatoonFaker.ts | 41 ++++++++++++++++---------- app/db/seed/factories/BuildFactory.ts | 6 +++- app/features/user-page/in-game-name.ts | 2 +- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/app/db/seed/core/SplatoonFaker.ts b/app/db/seed/core/SplatoonFaker.ts index 59e7e0e90..5e947c4a6 100644 --- a/app/db/seed/core/SplatoonFaker.ts +++ b/app/db/seed/core/SplatoonFaker.ts @@ -1,22 +1,26 @@ +import { + IN_GAME_NAME, + sanitizeInGameName, +} from "~/features/user-page/in-game-name"; import { abilities } from "~/modules/in-game-lists/abilities"; import { clothesGearIds, headGearIds, shoesGearIds, } from "~/modules/in-game-lists/gear-ids"; -import { modesShort, rankedModesShort } from "~/modules/in-game-lists/modes"; +import { rankedModesShort } from "~/modules/in-game-lists/modes"; import { stageIds } from "~/modules/in-game-lists/stage-ids"; import type { Ability, BuildAbilitiesTuple, MainWeaponId, - ModeShort, ModeWithStage, } from "~/modules/in-game-lists/types"; import { canonicalWeaponSplId, mainWeaponIds, } from "~/modules/in-game-lists/weapon-ids"; +import invariant from "~/utils/invariant"; import { faker } from "./faker"; const STACKABLE_ABILITIES = abilities @@ -46,23 +50,20 @@ export function gear() { }; } -// xxx: prolly inline -/** A non-empty subset of the modes, e.g. the ones a build is made for. */ -export function modes(): ModeShort[] { - return faker.helpers.arrayElements(modesShort, { min: 1, max: 3 }); -} - -const IN_GAME_NAME_MAX_LENGTH = 10; - -// xxx: match the actual rules for this -/** An in-game name with its discriminator, e.g. `Agent 4#1859`. */ +/** + * An in-game name with its discriminator, e.g. `Agent 4#1859`. `name` is sanitized and + * truncated the way the real thing is, so the result always passes `inGameNameIsValid`. + */ export function inGameName(name = faker.person.firstName()): string { - const tag = faker.string.alphanumeric({ - length: faker.helpers.arrayElement([4, 5]), + const discriminator = faker.string.alphanumeric({ + length: { + min: IN_GAME_NAME.DISCRIMINATOR_MIN_LENGTH, + max: IN_GAME_NAME.DISCRIMINATOR_MAX_LENGTH, + }, casing: "lower", }); - return `${[...name].slice(0, IN_GAME_NAME_MAX_LENGTH).join("")}#${tag}`; + return `${sanitizedName(name)}#${discriminator}`; } /** @@ -86,6 +87,16 @@ export function buildAbilities(): BuildAbilitiesTuple { return [gearAbilities(), gearAbilities(), gearAbilities()]; } +function sanitizedName(name: string): string { + const characters = [...sanitizeInGameName(name)].slice( + 0, + IN_GAME_NAME.NAME_MAX_LENGTH, + ); + invariant(characters.length > 0, `No valid in-game name characters: ${name}`); + + return characters.join(""); +} + function gearAbilities(): [Ability, Ability, Ability, Ability] { const draw = () => faker.helpers.arrayElement(STACKABLE_ABILITIES); diff --git a/app/db/seed/factories/BuildFactory.ts b/app/db/seed/factories/BuildFactory.ts index 89c5132eb..8ca68aa6c 100644 --- a/app/db/seed/factories/BuildFactory.ts +++ b/app/db/seed/factories/BuildFactory.ts @@ -1,4 +1,5 @@ import * as BuildRepository from "~/features/builds/BuildRepository.server"; +import { modesShort } from "~/modules/in-game-lists/modes"; import { defineFactory } from "../core/defineFactory"; import { faker } from "../core/faker"; import * as SplatoonFaker from "../core/SplatoonFaker"; @@ -18,7 +19,10 @@ export const { create, createMany } = defineFactory({ defaults: () => ({ title: faker.lorem.words(3), description: faker.number.float(1) < 0.4 ? faker.lorem.paragraph() : null, - modes: faker.number.float(1) < 0.7 ? SplatoonFaker.modes() : null, + modes: + faker.number.float(1) < 0.7 + ? faker.helpers.arrayElements(modesShort, { min: 1, max: 3 }) + : null, ...(faker.number.float(1) < 0.85 ? SplatoonFaker.gear() : NO_GEAR), weaponSplIds: SplatoonFaker.mainWeapons( faker.helpers.arrayElement([1, 1, 1, 1, 2, 2, 3, 4, 5]), diff --git a/app/features/user-page/in-game-name.ts b/app/features/user-page/in-game-name.ts index 181b6b09f..f2d93f494 100644 --- a/app/features/user-page/in-game-name.ts +++ b/app/features/user-page/in-game-name.ts @@ -1,6 +1,6 @@ import type { FormsTranslationKey } from "~/form/types"; -const IN_GAME_NAME = { +export const IN_GAME_NAME = { NAME_MAX_LENGTH: 10, DISCRIMINATOR_MIN_LENGTH: 4, DISCRIMINATOR_MAX_LENGTH: 5,