@@ -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 ) {
0 commit comments