/* ZimiGames: Mahjong for four, the page. The rules, the scoring and the computers are in mahjong4-engine.js; the tile
faces are the ones Mahjong solitaire uses (mahjong-tiles.js). Each of the four seats is a computer or a person
(handoff.js): with two or more people the screen hides each hand behind a "Pass to" cover. Our own code, GPL-3.0. */
(function (root) {
'use strict';
if (typeof document === 'undefined' || !root.Mahjong4Engine || !root.MahjongTiles || !root.BoardUI) return;
var WK = root.WorldKit, BK = root.BoardKit, UI = root.BoardUI, T = UI.t, E = root.Mahjong4Engine, MT = root.MahjongTiles, $ = function (id) { return document.getElementById(id); };
var SEATS = E.SEATS, ROTATIONS = 4, COMPUTER_DELAY_MS = 600, CLAIM_DELAY_MS = 800, HINT_LEVEL = 'hard';
var G = WK.create({
id: 'mahjong4',
options: [
{ key: 'level', legend: 'mahjong4.computers', def: 'medium', choices: [['easy', 'board.level.easy'], ['medium', 'board.level.medium'], ['hard', 'board.level.hard']] },
{ key: 'min', legend: 'mahjong4.min', def: '3', choices: [['3', 'mahjong4.min3'], ['1', 'mahjong4.min1']] }
],
// The people at the table (Computer or Person for each seat) join the New game pane after the options.
extraOptions: function () { return [seats.block(null, { note: T('mahjong4.seat1Deals') })]; },
statKey: function (s) { return s.level + s.min; },
onNew: newGame, onHint: hint
});
var seats = newSeats();
var score = WK.scoreBar($('wk-score'), [{}, {}, {}, {}].map(function () { return { color: 'var(--m4-cloth)' }; }));
var els = { info: $('m4-info'), opps: $('m4-opps'), pool: $('m4-pool'), claim: $('m4-claim'), melds: $('m4-melds'), hand: $('m4-hand') };
var STATUS_MAX = 44; // the most a 390 px status line holds in one line
var game, t, epoch = 0, picked = -1, tip = -1, shown = [], event = ''; // event: what just happened, said while the computers answer a discard
// ── small helpers ──
function newSeats() { return root.ZimiSeats.create({ id: 'mahjong4', seats: SEATS, onChange: function () { if (t) render(); } }); }
function me() { return seats.view(); } // the seat whose hand is face up
function level() { return G.settings.level; }
function seatName(p) { return T('mahjong.wind.' + E.seatOf(t, p)); }
function seatInitial(p) { return T('mahjong4.initial.' + E.seatOf(t, p)); }
// Names go into markup built as strings: a person's typed name must not become markup.
function esc(x) { return String(x).replace(/&/g, '&').replace(//g, '>').replace(/"/g, '"'); }
function list(names) { return root.Intl && Intl.ListFormat ? new Intl.ListFormat(undefined, { type: 'conjunction' }).format(names) : names.join(', '); }
function nameOf(p) { return seats.person(p) ? seats.name(p) : seatName(p); }
function labelOf(p) { return seats.person(p) ? T('mahjong4.label', { name: seats.name(p), wind: seatName(p) }) : T('mahjong4.label.computer', { wind: seatName(p) }); }
// A sentence about a player: its "you" form when the player is the person at the screen (verbs agree).
function about(key, p, vars) { return T(key + (seats.isYou(p) ? '.you' : ''), Object.assign({ name: nameOf(p) }, vars)); }
function ownerOf(p) { return T(seats.isYou(p) ? 'mahjong4.owner.you' : 'mahjong4.owner', { name: nameOf(p) }); }
function meldName(type) { return T('mahjong4.meld.' + type); }
function tileHtml(k, cls, extra) { return '' + MT.faceMarkup(k) + ''; }
function backHtml(cls) { return ''; }
function meldTiles(m) {
if (m.type === 'chow') return [m.k, m.k + 1, m.k + 2];
return m.type === 'pung' ? [m.k, m.k, m.k] : [m.k, m.k, m.k, m.k];
}
function meldHtml(m, cls) {
var hidden = m.type === 'kong' && !m.open; // a concealed kong shows its two middle tiles face down
return '' + meldTiles(m).map(function (k, i) { return hidden && (i === 0 || i === 3) ? backHtml(cls) : tileHtml(k, cls); }).join('') + '';
}
function later(fn, ms) { var my = epoch; setTimeout(function () { if (my === epoch) fn(); }, ms); }
function say(text, busy) { G.say(text, busy); }
function human() { return t.phase === 'discard' && t.turn === me() && seats.person(t.turn) && !seats.covering && !t.result; }
// ── drawing the table ──
function render() {
var p, i;
els.info.textContent = T(game.repeats ? 'mahjong4.info.again' : 'mahjong4.info', { n: Math.min(ROTATIONS, game.rotations + 1), of: ROTATIONS, left: E.liveLeft(t) });
var names = [], vals = [];
for (p = 0; p < SEATS; p++) { names.push(nameOf(p) + ' (' + seatInitial(p) + ')'); vals.push(game.points[p]); }
score(names, vals, t.result ? -1 : t.turn);
// the three computers: tiles face down, melds face up
var opp = '';
for (var r = 1; r < SEATS; r++) {
p = (me() + r) % SEATS;
var backs = ''; for (i = 0; i < t.hands[p].length; i++) backs += backHtml('xs');
opp += '