Skip to content

Commit 65a36ba

Browse files
author
DavidQ
committed
BUILD PR: centralize high score helpers across games.
1 parent e1c142e commit 65a36ba

9 files changed

Lines changed: 111 additions & 78 deletions

File tree

docs/dev/CODEX_COMMANDS.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
MODEL: GPT-5.3-codex
22
REASONING: high
33
COMMAND:
4-
Execute docs/pr/BUILD_PR_SHARED_EXTRACTION_28_CLAMP_CORE_BATCH.md exactly.
5-
Edit only:
6-
- src/engine/utils/math.js (export fix only if needed)
7-
- listed game files
8-
Fail fast if clamp is not available.
4+
Execute docs/pr/BUILD_PR_SHARED_EXTRACTION_29_HIGHSCORE_HELPERS_BATCH.md exactly.
5+
Edit only listed files.
6+
Create shared helper file.
97
Do not expand scope.
10-
Package to <project folder>/tmp/BUILD_PR_SHARED_EXTRACTION_28_CLAMP_CORE_BATCH_delta.zip
8+
Package to <project folder>/tmp/BUILD_PR_SHARED_EXTRACTION_29_HIGHSCORE_HELPERS_BATCH_delta.zip

docs/dev/COMMIT_COMMENT.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
BUILD PR: centralize clamp(value,min,max) across core game batch.
1+
BUILD PR: centralize high score helpers across games.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
clamp core batch extraction
1+
high score helpers centralized
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
clamp centralized
1+
helpers moved to shared
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# BUILD_PR_SHARED_EXTRACTION_29_HIGHSCORE_HELPERS_BATCH
2+
3+
## Purpose
4+
Centralize duplicated high score helper functions across game implementations into a shared game utility.
5+
6+
## Single PR Purpose
7+
Normalize ONLY these helpers:
8+
9+
- sanitizeScore(value)
10+
- sanitizeInitials(value)
11+
- sanitizeRow(row)
12+
- sortRows(rows)
13+
14+
Target files:
15+
16+
1. games/Asteroids/systems/AsteroidsHighScoreService.js
17+
2. games/SpaceDuel/game/SpaceDuelHighScoreService.js
18+
3. games/SpaceInvaders/game/SpaceInvadersHighScoreService.js
19+
20+
## Exact Files Allowed
21+
22+
### New shared file (allowed in this PR)
23+
1. src/shared/utils/highScoreUtils.js
24+
25+
### Consumers
26+
2. games/Asteroids/systems/AsteroidsHighScoreService.js
27+
3. games/SpaceDuel/game/SpaceDuelHighScoreService.js
28+
4. games/SpaceInvaders/game/SpaceInvadersHighScoreService.js
29+
30+
## Shared Helper Creation
31+
Create:
32+
src/shared/utils/highScoreUtils.js
33+
34+
Export exactly:
35+
- sanitizeScore
36+
- sanitizeInitials
37+
- sanitizeRow
38+
- sortRows
39+
40+
Use ONE of the existing implementations as source-of-truth.
41+
Do NOT merge logic.
42+
Do NOT change behavior.
43+
44+
## Consumer Changes
45+
46+
For each consumer file:
47+
- remove local helper implementations for the 4 functions
48+
- import from src/shared/utils/highScoreUtils.js
49+
- keep all logic intact
50+
51+
## Constraints
52+
- no engine changes
53+
- no other helpers
54+
- no behavior change
55+
- exact batch only
56+
57+
## Validation
58+
- helpers exist only in shared file
59+
- consumers import correctly
60+
- behavior unchanged
61+
62+
## Non-Goals
63+
- no other score systems
64+
- no UI changes

games/Asteroids/systems/AsteroidsHighScoreService.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ David Quesenberry
55
AsteroidsHighScoreService.js
66
*/
77
import { StorageService } from '../../../src/engine/persistence/index.js';
8+
import {
9+
sanitizeScore,
10+
sanitizeInitials,
11+
sanitizeRow,
12+
sortRows,
13+
} from '../../../src/shared/utils/highScoreUtils.js';
814

915
const DEFAULT_KEY = 'toolboxaid:games:asteroids:high-score-table';
1016
const DEFAULT_ROWS = [
@@ -15,29 +21,6 @@ const DEFAULT_ROWS = [
1521
{ initials: 'BOT', score: 500 },
1622
];
1723

18-
function sanitizeScore(value) {
19-
if (!Number.isFinite(value)) {
20-
return 0;
21-
}
22-
return Math.max(0, Math.trunc(value));
23-
}
24-
25-
function sanitizeInitials(value) {
26-
const letters = String(value ?? '').toUpperCase().replace(/[^A-Z]/g, '');
27-
return (letters || 'AAA').slice(0, 3).padEnd(3, 'A');
28-
}
29-
30-
function sanitizeRow(row) {
31-
return {
32-
initials: sanitizeInitials(row?.initials),
33-
score: sanitizeScore(row?.score),
34-
};
35-
}
36-
37-
function sortRows(rows) {
38-
return [...rows].sort((a, b) => b.score - a.score);
39-
}
40-
4124
export default class AsteroidsHighScoreService {
4225
constructor({ key = DEFAULT_KEY, tableSize = 5, storage = null } = {}) {
4326
this.key = key;

games/SpaceDuel/game/SpaceDuelHighScoreService.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ David Quesenberry
55
SpaceDuelHighScoreService.js
66
*/
77
import { StorageService } from '../../../src/engine/persistence/index.js';
8+
import {
9+
sanitizeScore,
10+
sanitizeInitials,
11+
sanitizeRow,
12+
sortRows,
13+
} from '../../../src/shared/utils/highScoreUtils.js';
814

915
const DEFAULT_KEY = 'toolboxaid:games:space-duel:high-scores';
1016
const DEFAULT_TABLE = [
@@ -15,29 +21,6 @@ const DEFAULT_TABLE = [
1521
{ initials: 'BOT', score: 500 },
1622
];
1723

18-
function sanitizeInitials(value) {
19-
const letters = String(value ?? '').toUpperCase().replace(/[^A-Z]/g, '');
20-
return (letters || '???').slice(0, 3).padEnd(3, 'A');
21-
}
22-
23-
function sanitizeScore(value) {
24-
if (!Number.isFinite(value)) {
25-
return 0;
26-
}
27-
return Math.max(0, Math.trunc(value));
28-
}
29-
30-
function sanitizeRow(row) {
31-
return {
32-
initials: sanitizeInitials(row?.initials),
33-
score: sanitizeScore(row?.score),
34-
};
35-
}
36-
37-
function sortRows(rows) {
38-
return [...rows].sort((a, b) => b.score - a.score);
39-
}
40-
4124
export default class SpaceDuelHighScoreService {
4225
constructor({
4326
key = DEFAULT_KEY,

games/SpaceInvaders/game/SpaceInvadersHighScoreService.js

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ David Quesenberry
55
SpaceInvadersHighScoreService.js
66
*/
77
import { StorageService } from '../../../src/engine/persistence/index.js';
8+
import {
9+
sanitizeScore,
10+
sanitizeInitials,
11+
sanitizeRow,
12+
sortRows,
13+
} from '../../../src/shared/utils/highScoreUtils.js';
814

915
const DEFAULT_KEY = 'toolboxaid:games:space-invaders:high-score-table';
1016
const DEFAULT_ROWS = [
@@ -15,29 +21,6 @@ const DEFAULT_ROWS = [
1521
{ initials: 'BOT', score: 500 },
1622
];
1723

18-
function sanitizeScore(value) {
19-
if (!Number.isFinite(value)) {
20-
return 0;
21-
}
22-
return Math.max(0, Math.trunc(value));
23-
}
24-
25-
function sanitizeInitials(value) {
26-
const letters = String(value ?? '').toUpperCase().replace(/[^A-Z]/g, '');
27-
return (letters || 'AAA').slice(0, 3).padEnd(3, 'A');
28-
}
29-
30-
function sanitizeRow(row) {
31-
return {
32-
initials: sanitizeInitials(row?.initials),
33-
score: sanitizeScore(row?.score),
34-
};
35-
}
36-
37-
function sortRows(rows) {
38-
return [...rows].sort((a, b) => b.score - a.score);
39-
}
40-
4124
export default class SpaceInvadersHighScoreService {
4225
constructor({ key = DEFAULT_KEY, tableSize = 5, storage = null } = {}) {
4326
this.key = key;

src/shared/utils/highScoreUtils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export function sanitizeScore(value) {
2+
if (!Number.isFinite(value)) {
3+
return 0;
4+
}
5+
return Math.max(0, Math.trunc(value));
6+
}
7+
8+
export function sanitizeInitials(value) {
9+
const letters = String(value ?? "").toUpperCase().replace(/[^A-Z]/g, "");
10+
return (letters || "AAA").slice(0, 3).padEnd(3, "A");
11+
}
12+
13+
export function sanitizeRow(row) {
14+
return {
15+
initials: sanitizeInitials(row?.initials),
16+
score: sanitizeScore(row?.score),
17+
};
18+
}
19+
20+
export function sortRows(rows) {
21+
return [...rows].sort((a, b) => b.score - a.score);
22+
}

0 commit comments

Comments
 (0)