diff --git a/constants/profile-name.ts b/constants/profile-name.ts
index 0cebfab..683b6ee 100644
--- a/constants/profile-name.ts
+++ b/constants/profile-name.ts
@@ -1,5 +1,6 @@
export const PROFILE_NAME_MIN_LENGTH = 2;
export const PROFILE_NAME_MAX_LENGTH = 16;
+export const PROFILE_NAME_TAG_DIGITS = 4;
export const PROFILE_NAME_BLOCKED_WORDS = [
'시발', '씨발', '씨팔', '시팔', '씨바', '씨벌', '씨부랄', '씹', '좆', '좃', '존나',
'병신', '븅신', '빙신', '개새끼', '새끼', '개새기', '개색기', '개색끼', '지랄', '염병',
diff --git a/src/client/lobby-features.ts b/src/client/lobby-features.ts
index f653796..808c872 100644
--- a/src/client/lobby-features.ts
+++ b/src/client/lobby-features.ts
@@ -2,7 +2,7 @@ import { COSMETIC_COLORS, COSMETIC_EQUIPMENT, EYEWEAR_IDS, HAIR_STYLES, SKIN_TON
import { RANKED_POLL_MS } from '../../constants/ranked.js';
import { PROFILE_NAME_MAX_LENGTH } from '../../constants/profile-name.js';
import { GAME_API_MESSAGES } from '../../constants/recording.js';
-import { profileNameError } from '../shared/profile-name.js';
+import { formatProfileName, profileNameError } from '../shared/profile-name.js';
import { appearanceKey, normalizeAppearance, type FighterAppearance } from '../shared/cosmetics.js';
import type { Leaderboard, PlayerProfile, RankedStatus } from '../shared/ranked-types.js';
import type { ActionId } from '../shared/types.js';
@@ -23,6 +23,8 @@ interface LobbyOptions {
export function createLobbyFeatures(options: LobbyOptions) {
const { api } = options;
const actions = document.querySelector('.home-actions')!;
+ const profileName = document.createElement('p'); profileName.id = 'profile-name'; profileName.hidden = true;
+ actions.after(profileName);
const rankedStart = document.createElement('button');
rankedStart.id = 'ranked-start'; rankedStart.className = 'primary'; rankedStart.disabled = true;
rankedStart.innerHTML = '랭킹 대전→';
@@ -93,6 +95,8 @@ export function createLobbyFeatures(options: LobbyOptions) {
function updateProfile(next: PlayerProfile) {
const appearanceChanged = !profile || appearanceKey(profile.appearance) !== appearanceKey(next.appearance);
profile = next;
+ profileName.textContent = next.displayName ? formatProfileName(next) : '';
+ profileName.hidden = !next.displayName;
if (appearanceChanged) {
options.onAppearance(next.appearance);
select(queue, '.queue-fighter').innerHTML = fighterArt(0, false, next.appearance);
@@ -190,7 +194,7 @@ export function createLobbyFeatures(options: LobbyOptions) {
} catch (error) { if (revision === profileRevision) profileNotice.textContent = error instanceof Error ? error.message : '프로필을 불러오지 못했습니다.'; }
};
leaderboardOpen.onclick = async () => {
- select(leaderboardDialog, '.ranking-self').textContent = profile ? `내 이름 · ${profile.displayName}` : '';
+ select(leaderboardDialog, '.ranking-self').textContent = profile ? `내 이름 · ${formatProfileName(profile)}` : '';
const content = select(leaderboardDialog, '.leaderboard-content'); content.textContent = '불러오는 중'; leaderboardDialog.showModal();
try {
const board = await api.request('/api/ranked/leaderboard');
@@ -203,7 +207,7 @@ export function createLobbyFeatures(options: LobbyOptions) {
for (const entry of rows) {
const row = document.createElement('tr');
if (entry.playerId === profile?.playerId) row.className = 'self-ranking';
- for (const text of [entry.rank, entry.displayName + (entry.playerId === profile?.playerId ? ' (나)' : ''), entry.best, entry.wins]) {
+ for (const text of [entry.rank, formatProfileName(entry) + (entry.playerId === profile?.playerId ? ' (나)' : ''), entry.best, entry.wins]) {
const cell = document.createElement('td'); cell.textContent = String(text); row.append(cell);
}
table.tBodies[0].append(row);
diff --git a/src/server/ranked-service.ts b/src/server/ranked-service.ts
index 7e6350e..87123a2 100644
--- a/src/server/ranked-service.ts
+++ b/src/server/ranked-service.ts
@@ -18,7 +18,7 @@ import { createEmotes } from '../shared/emotes.js';
import { profileNameError } from '../shared/profile-name.js';
interface ProfileDocument {
- _id: string; displayName: string; appearance: FighterAppearance; environment: string; createdAt: Date; updatedAt: Date;
+ _id: string; displayName: string; nameTag?: number; appearance: FighterAppearance; environment: string; createdAt: Date; updatedAt: Date;
}
interface TerminalMatch {
_id: string; playerIds: Pair; appearances: Pair; winnerIndex: 0 | 1 | null;
@@ -38,6 +38,10 @@ const emptyStats = (): ModeStats => ({ wins: 0, losses: 0, draws: 0, current: 0,
// One service owns the queue and all match clocks in the established single-process server.
export async function createRankedService(db: Db, clock: Clock = systemClock, environment = 'production', onError?: (code: string) => void) {
const profiles = db.collection('player_profiles');
+ const counters = db.collection<{ _id: string; value: number }>('profile_counters');
+ await profiles.createIndex({ nameTag: 1 }, { unique: true, sparse: true });
+ const highestTag = (await profiles.find({ nameTag: { $exists: true } }).sort({ nameTag: -1 }).limit(1).next())?.nameTag ?? 0;
+ await counters.updateOne({ _id: 'nameTag' }, { $max: { value: highestTag } }, { upsert: true });
const results = db.collection('ranked_matches');
await results.createIndex({ playerIds: 1, order: 1 });
let resultOrder = (await results.find().sort({ order: -1 }).limit(1).next())?.order ?? 0;
@@ -50,6 +54,14 @@ export async function createRankedService(db: Db, clock: Clock = systemClock, en
selves: Map; complete: boolean } | undefined;
let leaderboardRead: Promise | undefined;
let tail: Promise = Promise.resolve(), closed = false;
+ async function ensureNameTag(profile: ProfileDocument): Promise {
+ if (profile.nameTag !== undefined) return profile.nameTag;
+ const counter = (await counters.findOneAndUpdate({ _id: 'nameTag' }, { $inc: { value: 1 } }, { returnDocument: 'after' }))!;
+ // Concurrent reads may reserve unused numbers, but only the first assignment wins.
+ const assigned = await profiles.findOneAndUpdate({ _id: profile._id, nameTag: { $exists: false } },
+ { $set: { nameTag: counter.value } }, { returnDocument: 'after' });
+ return assigned?.nameTag ?? (await profiles.findOne({ _id: profile._id }))!.nameTag!;
+ }
function serial(work: () => Promise): Promise {
const next = tail.then(() => {
if (closed) throw new ApiError(503, 'RANKED_CLOSED');
@@ -118,9 +130,10 @@ export async function createRankedService(db: Db, clock: Clock = systemClock, en
for await (const result of results.find().sort({ order: 1, _id: 1 })) for (const id of result.playerIds) {
const stats = totals.get(id) ?? emptyStats(); addResult(stats, result, id); totals.set(id, stats);
}
- const entries = (await profiles.find({ _id: { $in: [...totals.keys()] } }).toArray()).map(profile => ({ playerId: profile._id,
+ const entries = (await Promise.all((await profiles.find({ _id: { $in: [...totals.keys()] } }).toArray()).map(async profile => ({ playerId: profile._id,
+ nameTag: await ensureNameTag(profile),
displayName: profileNameError(profile.displayName) ? '플레이어' : profile.displayName, wins: totals.get(profile._id)!.wins, best: totals.get(profile._id)!.best,
- })).sort((a, b) => b.best - a.best || b.wins - a.wins || (a.playerId < b.playerId ? -1 : a.playerId > b.playerId ? 1 : 0))
+ })))).sort((a, b) => b.best - a.best || b.wins - a.wins || (a.playerId < b.playerId ? -1 : a.playerId > b.playerId ? 1 : 0))
.map((entry, index) => ({ rank: index + 1, ...entry }));
// Return an overlapping snapshot without caching it; retrying here can starve
// under steady saves. The next request refreshes, independent of gameplay.
@@ -138,8 +151,9 @@ export async function createRankedService(db: Db, clock: Clock = systemClock, en
appearance: { ...DEFAULT_APPEARANCE }, environment, createdAt: now, updatedAt: now,
} }, { upsert: true });
const doc = (await profiles.findOne({ _id: playerId }))!;
+ const nameTag = await ensureNameTag(doc);
const stats = await readStats(playerId);
- return { playerId, displayName: profileNameError(doc.displayName) ? '' : doc.displayName, appearance: normalizeAppearance(doc.appearance), stats,
+ return { playerId, displayName: profileNameError(doc.displayName) ? '' : doc.displayName, nameTag, appearance: normalizeAppearance(doc.appearance), stats,
unlockedColors: availableColorIds(stats), unlockedEquipment: availableEquipmentIds(stats) };
}
async function persist(match: RankedMatch) {
diff --git a/src/shared/profile-name.ts b/src/shared/profile-name.ts
index 7160591..a5169c3 100644
--- a/src/shared/profile-name.ts
+++ b/src/shared/profile-name.ts
@@ -1,4 +1,8 @@
-import { PROFILE_NAME_BLOCKED_WORDS, PROFILE_NAME_MAX_LENGTH, PROFILE_NAME_MIN_LENGTH } from '../../constants/profile-name.js';
+import { PROFILE_NAME_BLOCKED_WORDS, PROFILE_NAME_MAX_LENGTH, PROFILE_NAME_MIN_LENGTH, PROFILE_NAME_TAG_DIGITS } from '../../constants/profile-name.js';
+
+export function formatProfileName(profile: { displayName: string; nameTag: number }): string {
+ return `${profile.displayName}#${String(profile.nameTag).padStart(PROFILE_NAME_TAG_DIGITS, '0')}`;
+}
// Compare decomposed Hangul too, so compatibility/combining jamo and separators cannot hide listed words.
const comparisonName = (name: string) => name.normalize('NFKD').toLowerCase().replace(/[^\p{L}]/gu, '');
diff --git a/src/shared/ranked-types.ts b/src/shared/ranked-types.ts
index 0df3cf3..1efd9c2 100644
--- a/src/shared/ranked-types.ts
+++ b/src/shared/ranked-types.ts
@@ -6,6 +6,7 @@ import type { EquipmentId } from '../../constants/cosmetics.js';
export interface PlayerProfile {
playerId: string;
displayName: string;
+ nameTag: number;
appearance: FighterAppearance;
stats: ModeStats;
unlockedColors: string[];
@@ -21,6 +22,7 @@ export interface LeaderboardEntry {
rank: number;
playerId: string;
displayName: string;
+ nameTag: number;
wins: number;
best: number;
}
diff --git a/tests/lobby-profile.test.ts b/tests/lobby-profile.test.ts
index aec5836..6e6fa75 100644
--- a/tests/lobby-profile.test.ts
+++ b/tests/lobby-profile.test.ts
@@ -44,7 +44,7 @@ test('first visit waits for a saved nickname and keeps the form open on rejectio
const document = documentSurface(), original = Object.getOwnPropertyDescriptor(globalThis, 'document');
Object.defineProperty(globalThis, 'document', { configurable: true, value: document });
t.after(() => { if (original) Object.defineProperty(globalThis, 'document', original); else Reflect.deleteProperty(globalThis, 'document'); });
- const profile: PlayerProfile = { playerId: 'a', displayName: '', appearance: normalizeAppearance(undefined),
+ const profile: PlayerProfile = { playerId: 'a', displayName: '', nameTag: 1, appearance: normalizeAppearance(undefined),
stats: { wins: 0, losses: 0, draws: 0, current: 0, best: 0 }, unlockedColors: ['white'], unlockedEquipment: [] };
let writes = 0, initialized = false, rejectSave = true;
const api = { async request(_path: string, method?: string, body?: { displayName: string }) {
@@ -70,6 +70,11 @@ test('first visit waits for a saved nickname and keeps the form open on rejectio
assert.equal(initialized, false); assert.equal(dialog.querySelector('p').textContent, '저장 실패');
rejectSave = false; await submit(); await loading;
assert.equal(initialized, true); assert.equal(dialog.open, false);
+ assert.equal(document.getElementById('profile-name').textContent, '손오공#0001');
+ await document.getElementById('nickname-open').onclick!();
+ assert.equal(dialog.querySelector('input').value, '손오공', 'the tag is not part of the editable nickname');
+ dialog.querySelector('input').value = '베지터'; await submit();
+ assert.equal(document.getElementById('profile-name').textContent, '베지터#0001');
});
test('ranked confirmation shows accept, waits for opponent and cancels by offer id', async t => {
@@ -77,7 +82,7 @@ test('ranked confirmation shows accept, waits for opponent and cancels by offer
Object.defineProperty(globalThis, 'document', { configurable: true, value: document });
t.after(() => { if (original) Object.defineProperty(globalThis, 'document', original); else Reflect.deleteProperty(globalThis, 'document'); });
t.mock.timers.enable({ apis: ['setTimeout'] });
- const profile: PlayerProfile = { playerId: 'a', displayName: 'A', appearance: normalizeAppearance(undefined),
+ const profile: PlayerProfile = { playerId: 'a', displayName: 'A', nameTag: 1, appearance: normalizeAppearance(undefined),
stats: { wins: 0, losses: 0, draws: 0, current: 0, best: 0 }, unlockedColors: ['white'], unlockedEquipment: [] };
let accepted = false, screen = '', cancelBody: unknown;
const api = { async request(path: string, method?: string, body?: unknown) {
@@ -104,7 +109,7 @@ test('a delayed accept response cannot overwrite a restored newer offer', async
Object.defineProperty(globalThis, 'document', { configurable: true, value: document });
t.after(() => { if (original) Object.defineProperty(globalThis, 'document', original); else Reflect.deleteProperty(globalThis, 'document'); });
t.mock.timers.enable({ apis: ['setTimeout'] });
- const profile: PlayerProfile = { playerId: 'a', displayName: 'A', appearance: normalizeAppearance(undefined),
+ const profile: PlayerProfile = { playerId: 'a', displayName: 'A', nameTag: 1, appearance: normalizeAppearance(undefined),
stats: { wins: 0, losses: 0, draws: 0, current: 0, best: 0 }, unlockedColors: ['white'], unlockedEquipment: [] };
let currentId = 'old', finishAccept!: (value: unknown) => void;
const pendingAccept = new Promise(resolve => { finishAccept = resolve; });
@@ -130,7 +135,7 @@ test('an old confirmation poll cannot restore the prompt after accepting starts
Object.defineProperty(globalThis, 'document', { configurable: true, value: document });
t.after(() => { if (original) Object.defineProperty(globalThis, 'document', original); else Reflect.deleteProperty(globalThis, 'document'); });
t.mock.timers.enable({ apis: ['setTimeout'] });
- const profile: PlayerProfile = { playerId: 'a', displayName: 'A', appearance: normalizeAppearance(undefined),
+ const profile: PlayerProfile = { playerId: 'a', displayName: 'A', nameTag: 1, appearance: normalizeAppearance(undefined),
stats: { wins: 0, losses: 0, draws: 0, current: 0, best: 0 }, unlockedColors: ['white'], unlockedEquipment: [] };
const confirming = { status: 'confirming', profile, offer: { matchId: 'offer-a', expiresAt: 10000, serverNow: 0, accepted: false } };
const engine = createMatchEngine({ matchId: 'offer-a', mode: 'friend', selfIndex: 0, clock: { now: () => 0 } });
@@ -166,7 +171,7 @@ for (const initiallyQueued of [false, true]) test(`ranked introduction follows s
const engine = createMatchEngine({ matchId: 'ranked-intro', mode: 'friend', selfIndex: 0, clock: { now: () => now } });
engine.start(matchIntroductionDuration(DEFAULT_GLOBAL_SETTINGS, true));
const profile: PlayerProfile = {
- playerId: 'player-a', displayName: 'Alpha', appearance: normalizeAppearance(undefined),
+ playerId: 'player-a', displayName: 'Alpha', nameTag: 1, appearance: normalizeAppearance(undefined),
stats: { wins: 0, losses: 0, draws: 0, current: 0, best: 0 }, unlockedColors: ['white'], unlockedEquipment: [],
};
const screens: string[] = [];
@@ -213,7 +218,7 @@ test('a profile read started before equip cannot undo the saved appearance', asy
else Reflect.deleteProperty(globalThis, 'document');
});
const profile = (skinTone: 'light' | 'tan'): PlayerProfile => ({
- playerId: 'player-a', displayName: 'Alpha', appearance: normalizeAppearance({ characterId: 0, skinTone, colorId: 'white' }),
+ playerId: 'player-a', displayName: 'Alpha', nameTag: 1, appearance: normalizeAppearance({ characterId: 0, skinTone, colorId: 'white' }),
stats: { wins: 0, losses: 0, draws: 0, current: 0, best: 0 }, unlockedColors: ['white'], unlockedEquipment: [],
});
let finishOldRead!: (value: PlayerProfile) => void;
@@ -267,7 +272,7 @@ for (const editedDuringLoad of [false, true]) {
const cached = normalizeAppearance({ characterId: 0, colorId: 'white' });
const remote = normalizeAppearance({ characterId: 1, colorId: 'white', equipment: ['shoes'] });
const profile = (appearance: FighterAppearance): PlayerProfile => ({
- playerId: 'player-a', displayName: 'Alpha', appearance,
+ playerId: 'player-a', displayName: 'Alpha', nameTag: 1, appearance,
stats: { wins: 5, losses: 0, draws: 0, current: 5, best: 5 },
unlockedColors: ['white'], unlockedEquipment: ['shoes', 'wristbands'],
});
diff --git a/tests/profile-name.test.ts b/tests/profile-name.test.ts
index 9065482..ffaf474 100644
--- a/tests/profile-name.test.ts
+++ b/tests/profile-name.test.ts
@@ -1,6 +1,13 @@
import assert from 'node:assert/strict';
import test from 'node:test';
-import { profileNameError } from '../src/shared/profile-name.js';
+import { formatProfileName, profileNameError } from '../src/shared/profile-name.js';
+
+test('name tags distinguish duplicate nicknames and keep all digits beyond four places', () => {
+ assert.equal(formatProfileName({ displayName: '손오공', nameTag: 1 }), '손오공#0001');
+ assert.equal(formatProfileName({ displayName: '손오공', nameTag: 2 }), '손오공#0002');
+ assert.equal(formatProfileName({ displayName: '손오공', nameTag: 10000 }), '손오공#10000');
+ assert.equal(profileNameError('손오공#0001'), 'INVALID_PROFILE_NAME');
+});
test('nickname validation accepts ordinary names and rejects profanity and separated variants', () => {
for (const name of ['손오공', '무도가', '구름-1234', 'ClassMaster', '별빛 전사']) assert.equal(profileNameError(name), undefined, name);
diff --git a/tests/ranked.test.ts b/tests/ranked.test.ts
index 70d3262..b2f835f 100644
--- a/tests/ranked.test.ts
+++ b/tests/ranked.test.ts
@@ -156,6 +156,27 @@ test('bot and friend wins never change ranked standings, profile stats or cosmet
} finally { await f.close(); }
});
+test('duplicate nicknames receive distinct persistent tags despite concurrent reads and client tag spoofing', integration, async () => {
+ const f = await fixture();
+ try {
+ const reads = await Promise.all(Array.from({ length: 8 }, () => f.service.profile('player-a')));
+ assert.ok(reads[0].nameTag > 0);
+ assert.equal(new Set(reads.map(profile => profile.nameTag)).size, 1);
+ const a = await f.service.updateProfile('player-a', { displayName: '손오공' });
+ const b = await f.service.updateProfile('player-b', { displayName: '손오공', nameTag: a.nameTag } as any);
+ assert.equal(a.displayName, b.displayName); assert.notEqual(a.nameTag, b.nameTag);
+ assert.equal((await f.service.updateProfile('player-a', { displayName: '베지터', nameTag: b.nameTag } as any)).nameTag, a.nameTag);
+ await assert.rejects(f.db.collection('player_profiles').updateOne({ _id: 'player-b' }, { $set: { nameTag: a.nameTag } }), { code: 11000 });
+ await f.service.close();
+ const restarted = await createRankedService(f.db, f.clock, 'test');
+ try {
+ assert.equal((await restarted.profile('player-a')).nameTag, a.nameTag);
+ assert.equal((await restarted.profile('player-b')).nameTag, b.nameTag);
+ assert.ok((await restarted.profile('player-c')).nameTag > Math.max(a.nameTag, b.nameTag));
+ } finally { await restarted.close(); }
+ } finally { await f.close(); }
+});
+
test('profiles save filtered names and validate unlocked appearances, persistence and pairing snapshots', integration, async () => {
const f = await fixture();
try {
@@ -200,6 +221,7 @@ test('startup preserves old names and hides invalid leaderboard names without re
const profiles = f.db.collection('player_profiles');
await profiles.updateOne({ _id: 'player-a' }, { $set: { displayName: '광고.example' }, $unset: { nameVersion: '' } });
await profiles.updateOne({ _id: 'player-b' }, { $set: { displayName: '무도가', nameVersion: 0 } });
+ await profiles.updateMany({}, { $unset: { nameTag: '' } });
const restarted = await createRankedService(f.db, f.clock, 'test');
let names: string[];
try {
@@ -207,6 +229,8 @@ test('startup preserves old names and hides invalid leaderboard names without re
const board = await restarted.leaderboard('new-viewer');
names = board.entries.map(entry => entry.displayName);
assert.equal(names.length, 2);
+ assert.ok(board.entries.every(entry => entry.nameTag > 0));
+ assert.equal(new Set(board.entries.map(entry => entry.nameTag)).size, 2);
assert.deepEqual(names, ['플레이어', '무도가']);
assert.equal(board.entries[0].wins, 1);
const profile = await restarted.profile('player-a');