583 lines
25 KiB
TypeScript
583 lines
25 KiB
TypeScript
import { BitmapFont, BlockInputEvents, Button, Label, LabelShadow, Color, Node, Sprite, SpriteFrame, UITransform, Vec2 } from 'cc';
|
|
import type { GameSession, GameState } from './GameSession';
|
|
import { SYMBOL_ART } from './OrchardAssets';
|
|
import { PAYLINES, PAYTABLE } from './SlotsMath';
|
|
import type { OrdinarySymbol } from './SlotsMath';
|
|
import { director } from 'cc';
|
|
import { GuestLegalLinks } from '../../login/scripts/GuestLegalLinks';
|
|
import { GIFT_REFERENCE, REFERENCE_IMAGES, REPEAT_OPTIONS, type DialogBox } from './DialogReferenceLayout';
|
|
import { authorCelebration } from './DialogCelebration';
|
|
import { DialogEffects } from './DialogEffects';
|
|
|
|
/**
|
|
* Coordinates are screen centres in the 1080 x 1920 reference layout (origin: top left).
|
|
* Every group stays at the Canvas origin; the composer converts leaf coordinates.
|
|
* Gold labels use the composer's bitmap font; all other labels use Arial.
|
|
*/
|
|
export interface DialogComposer {
|
|
group(name: string, parent?: Node): Node;
|
|
image(name: string, key: string, x: number, y: number, w: number, h: number, parent?: Node): Node;
|
|
text(name: string, value: string, x: number, y: number, w: number, h: number, size: number, parent?: Node, gold?: boolean): Node;
|
|
button(name: string, key: string, x: number, y: number, w: number, h: number, parent?: Node): Node;
|
|
shade(name: string, parent: Node): Node;
|
|
}
|
|
|
|
export type DialogKind = 'repeat' | 'preferences' | 'gift' | 'empty' | 'award' | 'summary' | 'guide';
|
|
type PreferenceKey = keyof GameState['settings'];
|
|
type VisibleDialog = DialogKind | 'error';
|
|
|
|
const GROUP_NAMES: Record<VisibleDialog, string> = {
|
|
repeat: 'RepeatDialog',
|
|
preferences: 'PreferencesDialog',
|
|
gift: 'GiftDialog',
|
|
empty: 'EmptyWalletDialog',
|
|
award: 'BonusAwardDialog',
|
|
summary: 'BonusSummaryDialog',
|
|
guide: 'GuideDialog',
|
|
error: 'ErrorDialog',
|
|
};
|
|
|
|
const AUTO_OPTIONS = REPEAT_OPTIONS;
|
|
|
|
const PREFERENCES: readonly { key: PreferenceKey; name: string; art: string; y: number }[] = [
|
|
{ key: 'music', name: 'Music', art: 'melody-row', y: 650.5 },
|
|
{ key: 'sound', name: 'Sound', art: 'effects-row', y: 816 },
|
|
{ key: 'vibration', name: 'Vibration', art: 'haptics-row', y: 981.5 },
|
|
];
|
|
|
|
const PAYING_SYMBOLS = Object.keys(PAYTABLE) as OrdinarySymbol[];
|
|
const SYMBOL_NAMES: Record<OrdinarySymbol, string> = {
|
|
seven: 'SEVEN', bell: 'BELL', bar: 'BAR', strawberry: 'STRAWBERRY',
|
|
blueberry: 'BLUEBERRY', plum: 'PLUM', cherry: 'CHERRY', watermelon: 'WATERMELON',
|
|
};
|
|
const GUIDE_TITLES = ['PAYTABLE 1 / 2', 'PAYTABLE 2 / 2', 'RULES & SYMBOLS', '20 PAYLINES', 'BONUS & DAILY COINS'];
|
|
const DAILY_COINS = 20_000;
|
|
const FAILURE = {
|
|
open: 'Unable to open this dialog. Please try again.',
|
|
repeat: 'Unable to start auto spin. Please try again.',
|
|
selection: 'Unable to update the selection. Please try again.',
|
|
preference: 'Unable to save your preference. Please try again. Your saved data has not been reset.',
|
|
gift: 'Unable to collect daily coins. Please try again. Your saved data has not been reset.',
|
|
clock: 'Unable to check the daily gift time. Please try again.',
|
|
award: 'Unable to start free spins. Please try again. Your saved progress has not been reset.',
|
|
summary: 'Unable to close the free-spin result. Please try again. Your saved progress has not been reset.',
|
|
guide: 'Unable to display this help page. Please try again.',
|
|
restore: 'Unable to restore the dialog. Please try again.',
|
|
};
|
|
|
|
function coins(value: number): string {
|
|
return value.toLocaleString('en-US');
|
|
}
|
|
|
|
/** Plain text buttons avoid reusing artwork that already contains an unrelated caption. */
|
|
function labelButton(ui: DialogComposer, name: string, value: string, x: number, y: number, w: number, parent: Node): Node {
|
|
const node = ui.text(name, value, x, y, w, 90, 36, parent);
|
|
const button = node.getComponent(Button) ?? node.addComponent(Button);
|
|
button.transition = Button.Transition.SCALE;
|
|
button.zoomScale = 0.96;
|
|
return node;
|
|
}
|
|
|
|
function body(ui: DialogComposer, name: string, value: string, y: number, h: number, parent: Node): Node {
|
|
const node = ui.text(name, value, 540, y, 820, h, 34, parent);
|
|
const label = node.getComponent(Label);
|
|
if (label) {
|
|
label.horizontalAlign = Label.HorizontalAlign.LEFT;
|
|
label.verticalAlign = Label.VerticalAlign.CENTER;
|
|
label.lineHeight = 48;
|
|
label.enableWrapText = true;
|
|
}
|
|
return node;
|
|
}
|
|
|
|
/**
|
|
* Authors ModalLayer/ Shade + eight independent dialog groups. No scene is saved here.
|
|
* Button and live-label names below are the shared authoring/runtime contract.
|
|
*/
|
|
export function authorDialogs(ui: DialogComposer, celebrationFont?: BitmapFont): void {
|
|
const layer = ui.group('ModalLayer');
|
|
layer.getComponent(DialogEffects) ?? layer.addComponent(DialogEffects);
|
|
const shade = ui.shade('Shade', layer);
|
|
if (!shade.getComponent(BlockInputEvents)) shade.addComponent(BlockInputEvents);
|
|
const groups = {} as Record<VisibleDialog, Node>;
|
|
for (const kind of Object.keys(GROUP_NAMES) as VisibleDialog[]) {
|
|
groups[kind] = ui.group(GROUP_NAMES[kind], layer);
|
|
groups[kind].active = false;
|
|
}
|
|
|
|
const picture = (name: string, parent: Node, button = false, box: DialogBox = REFERENCE_IMAGES[name]): Node => {
|
|
const [key, x, y, w, h] = box;
|
|
const node = button ? ui.button(name, key, x, y, w, h, parent) : ui.image(name, key, x, y, w, h, parent);
|
|
node.setSiblingIndex(parent.children.length - 1);
|
|
return node;
|
|
};
|
|
const white = (name: string, text: string, x: number, y: number, w: number, h: number, size: number, parent: Node): Node => {
|
|
const node = ui.text(name, text, x, y, w, h, size, parent);
|
|
const shadow = node.getComponent(LabelShadow) ?? node.addComponent(LabelShadow);
|
|
shadow.color = new Color(18, 0, 21, 190); shadow.offset = new Vec2(0, -3); shadow.blur = 2;
|
|
return node;
|
|
};
|
|
const repeat = groups.repeat;
|
|
picture('RepeatShell', repeat);
|
|
picture('RepeatGarnish', repeat);
|
|
picture('RepeatTitle', repeat);
|
|
white('RepeatHint', 'Choose number of spins', 540, 777, 740, 76, 48, repeat);
|
|
for (const option of AUTO_OPTIONS) {
|
|
const selected = option.count === 25;
|
|
ui.button(`RepeatCount${option.count}`, `${option.key}-${selected ? 'picked' : 'idle'}`,
|
|
option.x, option.y, selected ? 377 : 364, selected ? 178 : 172, repeat);
|
|
}
|
|
picture('RepeatStart', repeat, true);
|
|
white('RepeatFooter', 'Stop anytime', 540, 1414, 480, 60, 38, repeat);
|
|
picture('RepeatClose', repeat, true);
|
|
|
|
const preferences = groups.preferences;
|
|
preferences.getChildByName('PreferencesNote')?.destroy();
|
|
picture('PreferencesShell', preferences);
|
|
picture('PreferencesTitle', preferences);
|
|
picture('PreferencesFlourish', preferences);
|
|
for (const preference of PREFERENCES) {
|
|
picture(`Preferences${preference.name}Row`, preferences);
|
|
picture(`Preferences${preference.name}Toggle`, preferences, true);
|
|
}
|
|
picture('PreferencesGuide', preferences, true);
|
|
const privacy = labelButton(ui, 'PreferencesPrivacy', 'Privacy Policy', 335, 1281, 260, preferences);
|
|
const terms = labelButton(ui, 'PreferencesTerms', 'Terms of Service', 726, 1281, 310, preferences);
|
|
for (const link of [privacy, terms]) {
|
|
const text = link.getComponent(Label)!;
|
|
text.fontSize = 34; text.lineHeight = 42; text.isBold = false; text.isUnderline = true;
|
|
link.getComponent(UITransform)!.setContentSize(link === privacy ? 260 : 310, 62);
|
|
}
|
|
const notice = ui.text('PreferencesLegalNotice', '', 540, 1335, 760, 40, 23, preferences);
|
|
notice.active = false;
|
|
const legal = preferences.getComponent(GuestLegalLinks) ?? preferences.addComponent(GuestLegalLinks);
|
|
legal.privacyButton = privacy.getComponent(Button); legal.termsButton = terms.getComponent(Button); legal.notice = notice.getComponent(Label);
|
|
picture('PreferencesClose', preferences, true);
|
|
|
|
const gift = groups.gift;
|
|
for (const name of ['GiftShell', 'GiftTitle', 'GiftGarnish', 'GiftAmountPlate']) picture(name, gift, false, GIFT_REFERENCE.available[name]);
|
|
white('GiftAmount', `${coins(DAILY_COINS)} COINS`, 600, 1012.5, 550, 120, 82, gift);
|
|
picture('GiftDailyLabel', gift, false, GIFT_REFERENCE.available.GiftDailyLabel);
|
|
picture('GiftClaim', gift, true, GIFT_REFERENCE.available.GiftClaim);
|
|
picture('GiftCollected', gift, false, GIFT_REFERENCE.claimed.GiftCollected).active = false;
|
|
const footer = white('GiftCountdown', 'Available once per UTC day.', 540, 1446, 820, 60, 36, gift);
|
|
footer.getComponent(Label)!.isBold = false;
|
|
picture('GiftClose', gift, true, GIFT_REFERENCE.available.GiftClose);
|
|
|
|
const empty = groups.empty;
|
|
for (const name of ['EmptyWalletShell', 'EmptyWalletTitle', 'EmptyWalletGarnish', 'EmptyWalletNote']) picture(name, empty);
|
|
for (const name of ['EmptyWalletGift', 'EmptyWalletLater', 'EmptyWalletClose']) picture(name, empty, true);
|
|
|
|
const award = groups.award;
|
|
award.getChildByName('BonusAwardShell')?.destroy();
|
|
authorCelebration(ui, award);
|
|
for (const name of ['BonusAwardScatter1', 'BonusAwardScatter2', 'BonusAwardScatter3', 'BonusAwardTitle']) picture(name, award);
|
|
const count = ui.text('BonusAwardCount', '8', 534, 1116, 620, 350, 325, award, true);
|
|
count.getComponent(Label)!.lineHeight = 325;
|
|
if (celebrationFont) { count.getComponent(Label)!.font = celebrationFont; count.getComponent(Label)!.useSystemFont = false; }
|
|
picture('BonusAwardCaption', award);
|
|
picture('BonusAwardStart', award, true);
|
|
|
|
const summary = groups.summary;
|
|
for (const name of ['BonusSummaryShell', 'BonusSummaryGarnish', 'BonusSummaryTitle', 'BonusSummaryCaption', 'BonusSummaryAmountPlate']) picture(name, summary);
|
|
ui.text('BonusSummaryAmount', '0', 619, 1075.5, 480, 146, 126, summary, true);
|
|
picture('BonusSummaryContinue', summary, true);
|
|
|
|
const guide = groups.guide;
|
|
ui.image('GuideShell', 'preferences-shell', 540, 960, 980, 1550, guide);
|
|
ui.text('GuideTitle', GUIDE_TITLES[0], 540, 310, 760, 85, 44, guide);
|
|
ui.button('GuideClose', 'dismiss-orb', 960, 260, 90, 90, guide);
|
|
const pages = GUIDE_TITLES.map((_, index) => ui.group(`GuidePage${index}`, guide));
|
|
for (let page = 0; page < 2; page++) {
|
|
const parent = pages[page];
|
|
ui.text(`GuidePaytableHint${page}`, 'MATCHING SYMBOLS FROM THE LEFT', 540, 405, 820, 55, 32, parent);
|
|
for (let column = 0; column < 3; column++) {
|
|
ui.text(`GuidePaytableHeader${page}_${column}`, `${column + 3} MATCH`, 450 + column * 200, 490, 180, 55, 34, parent);
|
|
}
|
|
PAYING_SYMBOLS.slice(page * 4, page * 4 + 4).forEach((symbol, row) => {
|
|
const y = 625 + row * 225;
|
|
ui.image(`GuideSymbol_${symbol}`, SYMBOL_ART[symbol], 215, y, 145, 145, parent);
|
|
ui.text(`GuideSymbolName_${symbol}`, SYMBOL_NAMES[symbol], 215, y + 96, 230, 48, 32, parent);
|
|
PAYTABLE[symbol].forEach((multiplier, column) => {
|
|
ui.text(`GuideMultiplier_${symbol}_${column + 3}`, `${multiplier}x`, 450 + column * 200, y, 180, 80, 36, parent);
|
|
});
|
|
});
|
|
ui.text(`GuideLineBetCaption${page}`, 'LINE BET', 300, 1450, 280, 65, 34, parent);
|
|
ui.text(`GuideLineBet${page}`, '0', 685, 1450, 450, 70, 52, parent, true);
|
|
ui.text(`GuidePaytableNote${page}`, `Award = multiplier x line bet. All ${PAYLINES.length} lines play.`, 540, 1535, 850, 65, 32, parent);
|
|
}
|
|
|
|
ui.image('GuideWildSymbol', SYMBOL_ART.wild, 330, 510, 150, 150, pages[2]);
|
|
ui.image('GuideScatterSymbol', SYMBOL_ART.scatter, 750, 510, 150, 150, pages[2]);
|
|
ui.text('GuideWildCaption', 'WILD', 330, 615, 300, 55, 36, pages[2]);
|
|
ui.text('GuideScatterCaption', 'SCATTER', 750, 615, 300, 55, 36, pages[2]);
|
|
body(ui, 'GuideRules', [
|
|
`${PAYLINES.length} active lines. 5 reels, 3 rows.`,
|
|
'',
|
|
'Matching symbols pay from left to right.',
|
|
'Start on reel 1. Match 3, 4 or 5 symbols.',
|
|
'Only the best award pays on each line.',
|
|
'Wins on different lines are added.',
|
|
'',
|
|
'WILD replaces regular symbols.',
|
|
'It does not replace SCATTER.',
|
|
'',
|
|
'SCATTER counts anywhere on the reels.',
|
|
'3 / 4 / 5+ award 8 / 12 / 16 free spins.',
|
|
'SCATTER has no separate coin payout.',
|
|
].join('\n'), 1080, 800, pages[2]);
|
|
|
|
ui.text('GuideLineLegend', 'T = TOP M = MIDDLE B = BOTTOM', 540, 435, 850, 70, 32, pages[3]);
|
|
const linesPerColumn = Math.ceil(PAYLINES.length / 2);
|
|
PAYLINES.forEach((line, index) => {
|
|
const parent = ui.group(`GuidePayline${index + 1}`, pages[3]);
|
|
const x = 170 + Math.floor(index / linesPerColumn) * 420;
|
|
const y = 570 + index % linesPerColumn * 80;
|
|
ui.text(`GuidePaylineNumber${index + 1}`, String(index + 1).padStart(2, '0'), x, y, 65, 65, 36, parent);
|
|
// Individual fixed-width Arial cells preserve alignment without changing the font contract.
|
|
line.forEach((row, reel) => {
|
|
ui.text(`GuidePayline${index + 1}Reel${reel + 1}`, ['T', 'M', 'B'][row],
|
|
x + 85 + reel * 50, y, 45, 65, 36, parent);
|
|
});
|
|
});
|
|
ui.text('GuideLineDirection', 'Read each sequence from reel 1 to reel 5.', 540, 1470, 850, 100, 32, pages[3]);
|
|
|
|
ui.text('GuideFreeTitle', 'FREE SPINS', 540, 465, 820, 65, 36, pages[4]);
|
|
body(ui, 'GuideFreeRules', [
|
|
'3 / 4 / 5+ SCATTERs award 8 / 12 / 16.',
|
|
'Free spins keep the triggering bet.',
|
|
'Each spin makes one whole reel WILD;',
|
|
'SCATTERs stay unchanged.',
|
|
'Free spins do not retrigger.',
|
|
].join('\n'), 665, 290, pages[4]);
|
|
ui.text('GuideAutoTitle', 'AUTO SPIN', 540, 900, 820, 65, 36, pages[4]);
|
|
body(ui, 'GuideAutoRules', [
|
|
`Choose ${AUTO_OPTIONS.map(option => option.count).join(' / ')} spins.`,
|
|
'Only paid spins use this counter.',
|
|
'Opening a dialog stops auto spin.',
|
|
'Insufficient coins or a bonus stops it.',
|
|
].join('\n'), 1075, 240, pages[4]);
|
|
ui.text('GuideDailyTitle', 'DAILY COINS', 540, 1270, 820, 65, 36, pages[4]);
|
|
ui.text('GuideDailyAmount', coins(DAILY_COINS), 540, 1360, 600, 80, 60, pages[4], true);
|
|
body(ui, 'GuideDailyRules', 'Coins, once per UTC day.\nResets at 00:00 UTC.\nSaved locally for this guest account.', 1470, 160, pages[4]);
|
|
pages.forEach((page, index) => { page.active = index === 0; });
|
|
labelButton(ui, 'GuidePrevious', 'PREVIOUS', 260, 1640, 300, guide);
|
|
ui.text('GuidePageNumber', '1 / 5', 540, 1640, 180, 70, 34, guide);
|
|
labelButton(ui, 'GuideNext', 'NEXT', 820, 1640, 300, guide);
|
|
|
|
const error = groups.error;
|
|
ui.image('ErrorShell', 'repeat-shell', 540, 960, 870, 990, error);
|
|
ui.text('ErrorTitle', 'PLEASE TRY AGAIN', 540, 665, 740, 90, 44, error);
|
|
const message = body(ui, 'ErrorMessage', 'Unable to complete the action. Please try again.', 965, 390, error);
|
|
const messageLabel = message.getComponent(Label);
|
|
if (messageLabel) messageLabel.horizontalAlign = Label.HorizontalAlign.CENTER;
|
|
labelButton(ui, 'ErrorAcknowledge', 'OK', 540, 1300, 600, error);
|
|
layer.active = false;
|
|
}
|
|
|
|
function findNode(root: Node, name: string): Node | null {
|
|
if (root.name === name) return root;
|
|
for (const child of root.children) {
|
|
const found = findNode(child, name);
|
|
if (found) return found;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/** Owns only modal presentation; closing a dialog never resets the game phase. */
|
|
export class OrchardDialogs {
|
|
private readonly layer: Node;
|
|
private readonly groups: Record<VisibleDialog, Node>;
|
|
private readonly nodes = new Map<string, Node>();
|
|
private readonly listeners: { node: Node; callback: () => void }[] = [];
|
|
private visible: VisibleDialog | null = null;
|
|
private errorReturn: DialogKind | null = null;
|
|
private guideReturn: 'preferences' | null = null;
|
|
private selectedCount: number = 25;
|
|
private guidePage = 0;
|
|
private working = false;
|
|
private persistenceFailed = false;
|
|
private disposed = false;
|
|
|
|
/** root may be the Canvas, its scene ancestor, or ModalLayer itself. */
|
|
constructor(
|
|
root: Node,
|
|
private readonly session: GameSession,
|
|
private readonly art: (key: string) => SpriteFrame,
|
|
private readonly onAuto: (count: number) => void,
|
|
private readonly onResume: () => void,
|
|
private readonly onChange: () => void,
|
|
) {
|
|
const layer = findNode(root, 'ModalLayer');
|
|
if (!layer) throw new Error('Missing ModalLayer. Run authorDialogs before binding OrchardDialogs.');
|
|
this.layer = layer;
|
|
this.groups = {} as Record<VisibleDialog, Node>;
|
|
for (const kind of Object.keys(GROUP_NAMES) as VisibleDialog[]) {
|
|
this.groups[kind] = this.required(GROUP_NAMES[kind]);
|
|
}
|
|
const shade = this.required('Shade');
|
|
if (!shade.getComponent(BlockInputEvents)) shade.addComponent(BlockInputEvents);
|
|
shade.active = true;
|
|
this.bindControls();
|
|
this.close();
|
|
}
|
|
|
|
get opened(): boolean {
|
|
return !this.disposed && this.layer.isValid && this.layer.active;
|
|
}
|
|
|
|
show(kind: DialogKind): void {
|
|
if (this.disposed) return;
|
|
try {
|
|
if (kind === 'guide' && this.visible !== 'guide' && this.visible !== 'error') {
|
|
this.guideReturn = this.visible === 'preferences' ? 'preferences' : null;
|
|
this.guidePage = 0;
|
|
}
|
|
this.refresh(kind);
|
|
this.activate(kind);
|
|
} catch {
|
|
this.error(FAILURE.open);
|
|
}
|
|
}
|
|
|
|
close(): void {
|
|
if (this.disposed) return;
|
|
for (const group of Object.values(this.groups)) group.active = false;
|
|
this.layer.active = false;
|
|
this.visible = null;
|
|
this.errorReturn = null;
|
|
this.guideReturn = null;
|
|
}
|
|
|
|
/** Call from the view update loop; the session supplies the UTC clock. */
|
|
tick(): void {
|
|
if (!this.opened || this.visible !== 'gift') return;
|
|
try {
|
|
this.refreshGift();
|
|
} catch {
|
|
this.error(FAILURE.clock);
|
|
}
|
|
}
|
|
|
|
error(message: string, reload = false): void {
|
|
if (this.disposed) return;
|
|
if (reload) this.persistenceFailed = true;
|
|
if (this.visible !== 'error') this.errorReturn = this.visible;
|
|
this.label('ErrorMessage').string = message.trim().slice(0, 300) || FAILURE.open;
|
|
this.activate('error');
|
|
}
|
|
|
|
dispose(): void {
|
|
if (this.disposed) return;
|
|
for (const listener of this.listeners) {
|
|
if (listener.node.isValid) listener.node.off(Button.EventType.CLICK, listener.callback, this);
|
|
}
|
|
this.listeners.length = 0;
|
|
if (this.layer.isValid) this.close();
|
|
this.nodes.clear();
|
|
this.disposed = true;
|
|
}
|
|
|
|
private bindControls(): void {
|
|
for (const name of ['RepeatClose', 'PreferencesClose', 'GiftClose', 'EmptyWalletClose', 'EmptyWalletLater']) {
|
|
this.bind(name, () => this.close(), FAILURE.open);
|
|
}
|
|
for (const option of AUTO_OPTIONS) {
|
|
this.bind(`RepeatCount${option.count}`, () => {
|
|
this.selectedCount = option.count;
|
|
this.refreshRepeat();
|
|
}, FAILURE.selection);
|
|
}
|
|
this.bind('RepeatStart', () => {
|
|
if (this.session.state.phase !== 'idle') throw new Error('Auto spin requires the idle phase.');
|
|
this.close();
|
|
this.onAuto(this.selectedCount);
|
|
}, FAILURE.repeat);
|
|
for (const preference of PREFERENCES) {
|
|
this.bind(`Preferences${preference.name}Toggle`, () => {
|
|
this.session.setPreference(preference.key, !this.session.state.settings[preference.key]);
|
|
this.refreshPreferences();
|
|
this.onChange();
|
|
}, FAILURE.preference);
|
|
}
|
|
this.bind('PreferencesGuide', () => this.show('guide'), FAILURE.guide);
|
|
this.bind('EmptyWalletGift', () => this.show('gift'), FAILURE.gift);
|
|
this.bind('GiftClaim', () => {
|
|
const claimed = this.session.claimDaily();
|
|
this.refreshGift();
|
|
if (claimed) this.onChange();
|
|
}, FAILURE.gift);
|
|
// These are the only controls on the two mandatory free-spin dialogs.
|
|
this.bind('BonusAwardStart', () => {
|
|
this.session.startFree();
|
|
this.close();
|
|
this.onResume();
|
|
}, FAILURE.award);
|
|
this.bind('BonusSummaryContinue', () => {
|
|
this.session.closeFreeResult();
|
|
this.close();
|
|
this.onChange();
|
|
}, FAILURE.summary);
|
|
this.bind('GuidePrevious', () => this.turnGuide(-1), FAILURE.guide);
|
|
this.bind('GuideNext', () => this.turnGuide(1), FAILURE.guide);
|
|
this.bind('GuideClose', () => {
|
|
if (this.guideReturn === 'preferences') this.show('preferences');
|
|
else this.close();
|
|
}, FAILURE.guide);
|
|
this.bind('ErrorAcknowledge', () => {
|
|
if (this.persistenceFailed) {
|
|
// 旧会话不能盲目重放不确定提交,重新登录后从已保存阶段恢复。 / Reload the committed snapshot instead of replaying an uncertain mutation.
|
|
director.loadScene('Welcome');
|
|
return;
|
|
}
|
|
const next = this.recoverKind(this.errorReturn);
|
|
if (next === null) this.close();
|
|
else this.show(next);
|
|
}, FAILURE.restore);
|
|
}
|
|
|
|
private bind(name: string, action: () => void, failure: string): void {
|
|
const node = this.required(name);
|
|
const button = node.getComponent(Button);
|
|
if (!button) throw new Error(`Missing dialog Button: ${name}`);
|
|
const callback = (): void => {
|
|
if (this.disposed || this.working || !this.opened || !node.activeInHierarchy || !button.interactable) return;
|
|
const previous = this.visible;
|
|
this.working = true;
|
|
try {
|
|
action();
|
|
} catch {
|
|
if ([FAILURE.preference, FAILURE.gift, FAILURE.award, FAILURE.summary].includes(failure)) {
|
|
this.persistenceFailed = true;
|
|
this.error('Unable to confirm the saved change. Tap OK to return to login and safely restore your saved progress.');
|
|
return;
|
|
}
|
|
this.error(failure);
|
|
if (previous !== 'error') this.errorReturn = previous;
|
|
} finally {
|
|
this.working = false;
|
|
}
|
|
};
|
|
node.on(Button.EventType.CLICK, callback, this);
|
|
this.listeners.push({ node, callback });
|
|
}
|
|
|
|
private activate(kind: VisibleDialog): void {
|
|
for (const key of Object.keys(this.groups) as VisibleDialog[]) this.groups[key].active = key === kind;
|
|
this.visible = kind;
|
|
this.layer.active = true;
|
|
}
|
|
|
|
private refresh(kind: DialogKind): void {
|
|
switch (kind) {
|
|
case 'repeat': this.refreshRepeat(); break;
|
|
case 'preferences': this.refreshPreferences(); break;
|
|
case 'gift': this.refreshGift(); break;
|
|
case 'award': {
|
|
const state = this.session.state;
|
|
if (state.phase !== 'free-trigger' || state.free === null) throw new Error('No free-spin award is pending.');
|
|
this.label('BonusAwardCount').string = String(state.free.awarded);
|
|
break;
|
|
}
|
|
case 'summary': {
|
|
const state = this.session.state;
|
|
if (state.phase !== 'free-result' || state.free === null) throw new Error('No free-spin result is pending.');
|
|
this.label('BonusSummaryAmount').string = coins(state.free.total);
|
|
break;
|
|
}
|
|
case 'guide': this.refreshGuide(); break;
|
|
case 'empty': break;
|
|
}
|
|
}
|
|
|
|
private refreshRepeat(): void {
|
|
for (const option of AUTO_OPTIONS) {
|
|
const selected = option.count === this.selectedCount;
|
|
this.setArt(`RepeatCount${option.count}`, `${option.key}-${selected ? 'picked' : 'idle'}`);
|
|
this.required(`RepeatCount${option.count}`).getComponent(UITransform)!.setContentSize(selected ? 377 : 364, selected ? 178 : 172);
|
|
}
|
|
}
|
|
|
|
private refreshPreferences(): void {
|
|
const settings = this.session.state.settings;
|
|
for (const preference of PREFERENCES) {
|
|
this.setArt(`Preferences${preference.name}Toggle`, settings[preference.key] ? 'toggle-lit' : 'toggle-dim');
|
|
}
|
|
}
|
|
|
|
private refreshGift(): void {
|
|
const available = this.session.canClaimDaily();
|
|
for (const [name, [key, x, y, w, h]] of Object.entries(GIFT_REFERENCE[available ? 'available' : 'claimed'])) {
|
|
this.setArt(name, key);
|
|
this.required(name).setPosition(x - 540, 960 - y, 0);
|
|
this.required(name).getComponent(UITransform)!.setContentSize(w, h);
|
|
}
|
|
this.required('GiftClaim').active = available;
|
|
this.required('GiftCollected').active = !available;
|
|
const amount = this.label('GiftAmount');
|
|
amount.string = `${coins(DAILY_COINS)} COINS`;
|
|
amount.fontSize = available ? 82 : 96; amount.lineHeight = available ? 94 : 110;
|
|
amount.node.setPosition(available ? 60 : -5, available ? -52.5 : -75, 0);
|
|
amount.node.getComponent(UITransform)!.setContentSize(available ? 550 : 660, 125);
|
|
const seconds = Math.ceil(Math.max(0, this.session.nextResetMs()) / 1000);
|
|
const hours = Math.floor(seconds / 3600);
|
|
const minutes = Math.floor(seconds % 3600 / 60);
|
|
const remaining = seconds % 60;
|
|
const time = [hours, minutes, remaining].map(value => String(value).padStart(2, '0')).join(':');
|
|
const countdown = this.label('GiftCountdown');
|
|
countdown.string = available ? 'Available once per UTC day.' : `Next claim in ${time}`;
|
|
countdown.node.setPosition(0, available ? -486 : -459, 0);
|
|
countdown.fontSize = available ? 36 : 38;
|
|
countdown.isBold = !available;
|
|
}
|
|
|
|
private turnGuide(direction: number): void {
|
|
this.guidePage = Math.max(0, Math.min(GUIDE_TITLES.length - 1, this.guidePage + direction));
|
|
this.refreshGuide();
|
|
}
|
|
|
|
private refreshGuide(): void {
|
|
const state = this.session.state;
|
|
const lineBet = (state.free?.bet ?? state.bet) / PAYLINES.length;
|
|
for (let page = 0; page < GUIDE_TITLES.length; page++) {
|
|
this.required(`GuidePage${page}`).active = page === this.guidePage;
|
|
}
|
|
for (let page = 0; page < 2; page++) this.label(`GuideLineBet${page}`).string = coins(lineBet);
|
|
this.label('GuideTitle').string = this.guidePage === 3 ? `${PAYLINES.length} PAYLINES` : GUIDE_TITLES[this.guidePage];
|
|
this.label('GuidePageNumber').string = `${this.guidePage + 1} / ${GUIDE_TITLES.length}`;
|
|
this.component('GuidePrevious', Button).interactable = this.guidePage > 0;
|
|
this.component('GuideNext', Button).interactable = this.guidePage < GUIDE_TITLES.length - 1;
|
|
}
|
|
|
|
/** Restore pending confirmations after an error without replaying successful mutations. */
|
|
private recoverKind(previous: DialogKind | null): DialogKind | null {
|
|
const phase = this.session.state.phase;
|
|
if (phase === 'free-trigger') return 'award';
|
|
if (phase === 'free-result') return 'summary';
|
|
return previous === 'award' || previous === 'summary' ? null : previous;
|
|
}
|
|
|
|
private required(name: string): Node {
|
|
const cached = this.nodes.get(name);
|
|
if (cached) return cached;
|
|
const node = findNode(this.layer, name);
|
|
if (!node) throw new Error(`Missing authored dialog node: ${name}`);
|
|
this.nodes.set(name, node);
|
|
return node;
|
|
}
|
|
|
|
private component<T extends Label | Sprite | Button>(name: string, type: new () => T): T {
|
|
const component = this.required(name).getComponent(type);
|
|
if (!component) throw new Error(`Missing dialog component on: ${name}`);
|
|
return component;
|
|
}
|
|
|
|
private label(name: string): Label {
|
|
return this.component(name, Label);
|
|
}
|
|
|
|
private setArt(name: string, key: string): void {
|
|
this.component(name, Sprite).spriteFrame = this.art(key);
|
|
}
|
|
}
|