Skip to content

Commit c1604b9

Browse files
committed
games: Force multiplicative bonuses to apply after additive ones
1 parent 7e5ae3a commit c1604b9

2 files changed

Lines changed: 15 additions & 7 deletions

File tree

src/ps/games/scrabble/index.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ export class Scrabble extends BaseGame<State> {
385385
return null;
386386
}
387387

388+
// TODO: Fix Discord embeds
388389
async renderEmbed(): Promise<EmbedBuilder | null> {
389390
const winners = this.winCtx && this.winCtx.type === 'win' ? this.winCtx.winnerIds : null;
390391
if (!winners) return null;
@@ -457,11 +458,14 @@ export class Scrabble extends BaseGame<State> {
457458
}
458459

459460
parseBonus(bonus: Bonus | null, tile: BoardTile): BonusReducer {
460-
return score => {
461-
if (!bonus) return score;
462-
const modifier = +bonus.charAt(0);
463-
const additive = bonus.charAt(1) === 'L';
464-
return additive ? score + (modifier - 1) * tile.points : score * modifier;
461+
const modifier = +(bonus?.charAt(0) ?? 0);
462+
const additive = bonus?.charAt(1) === 'L';
463+
return {
464+
apply: score => {
465+
if (!bonus) return score;
466+
return additive ? score + (modifier - 1) * tile.points : score * modifier;
467+
},
468+
weight: bonus ? (additive ? 1 : 2) : 0,
465469
};
466470
}
467471

@@ -475,7 +479,11 @@ export class Scrabble extends BaseGame<State> {
475479
const wordData = words.map<[string, number | null]>(word => {
476480
const scoring = this.checkWord(word.word);
477481
if (!scoring) return [word.word, null];
478-
return [word.word, word.bonuses.reduce((score, bonus) => bonus(score), word.baseScore) * scoring[0] + scoring[1]];
482+
return [
483+
word.word,
484+
word.bonuses.sortBy(bonus => bonus.weight, 'asc').reduce((score, bonus) => bonus.apply(score), word.baseScore) * scoring[0] +
485+
scoring[1],
486+
];
479487
});
480488
const invalidWords = wordData.filter(entry => entry[1] === null).map(entry => entry[0]);
481489
if (invalidWords.length > 0) {

src/ps/games/scrabble/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export type BoardTile = {
88
};
99

1010
export type Bonus = '3W' | '2W' | '3L' | '2L' | '2*';
11-
export type BonusReducer = (score: number) => number;
11+
export type BonusReducer = { apply: (score: number) => number; weight: number };
1212

1313
export type BaseBoard = (Bonus | null)[][];
1414
export type Board = (null | BoardTile)[][];

0 commit comments

Comments
 (0)