Finished the scorecard
All checks were successful
/ publish (push) Successful in 50s

This commit is contained in:
Leon Mika 2025-12-21 10:56:49 +11:00
parent 1f8394f23b
commit c5420c97eb
6 changed files with 154 additions and 40 deletions

View file

@ -7,22 +7,24 @@ const windDistributions = [
];
const scoreTokens = {
'm': 1,
'ps': 1,
'pt': 1,
'ph': 1,
'xps': 1,
'xpt': 1,
'xph': 1,
'ks': 1,
'kt': 1,
'kh': 1,
'xks': 1,
'xkt': 1,
'xkh': 1,
'pd': 1,
'pw': 1,
'b': 1,
'm': 20,
'ps': 4,
'pt': 8,
'ph': 8,
'xps': 2,
'xpt': 4,
'xph': 4,
'ks': 16,
'kt': 32,
'kh': 32,
'xks': 8,
'xkt': 16,
'xkh': 16,
'pd': 2,
'pw': 2,
'b': 4,
'p': 1,
'd': -1,
}
function parseTokens(str) {
@ -67,7 +69,7 @@ export class GameState {
this.players = players;
this.rounds = rounds;
this.bumpWind = 0;
this.prevaling = "E";
this.prevailing = "E";
}
static newGame(playerNames) {
@ -78,7 +80,21 @@ export class GameState {
}
let rounds = [];
return new GameState(players, rounds);
let gs = new GameState(players, rounds);
gs.save();
return gs;
}
static load() {
let json = localStorage.getItem('mahjong-scorecard');
if (!json) {
return null;
}
let o = JSON.parse(json);
let gs = new GameState(o.p, o.r);
gs.bumpWind = o.b;
gs.prevailing = o.w;
return gs;
}
determineNextRound(playerScoreExprs) {
@ -92,7 +108,7 @@ export class GameState {
}
let windsBump = roundWinner !== currentEast;
let nextPrevailing = this.prevaling;
let nextPrevailing = this.prevailing;
if (windsBump) {
nextPrevailing = windDistributions[3][((this.bumpWind + 1) / this.players.length)|0];
}
@ -133,14 +149,24 @@ export class GameState {
this.players[pi].wind = windDistribution[i];
}
this.prevaling = nr.nextPrevailing;
this.prevailing = nr.nextPrevailing;
if (nr.windsBump) {
this.bumpWind++;
}
this.save();
}
save() {
localStorage.setItem('mahjong-scorecard', JSON.stringify({
p: this.players,
r: this.rounds,
b: this.bumpWind,
w: this.prevailing,
}));
}
}
let gameState = new GameState();
let gameState = GameState.load();
export function getGameState() {
return gameState;