open static method
Future<void>
open({
- required CampaignModel campaign,
- required GamificationConfig config,
- required String chanceId,
- required GameSettle settle,
- required String? userId,
- required String? deviceId,
- required String projectId,
- required String? themeFontFamilyOf()?,
- required Future<
String?> loadSkinHtml(- GamificationConfig config
- required Future<
String?> downloadSkin(- GamificationConfig config
- required Future<
void> present(- GameOpenRequest request
- required void reportOpenFailed(
- GameOpenFailedEvent event,
- CEPTriggerPayload payload
- required void settleDirectFailure(
- CEPTriggerPayload payload,
- String reason
- void reportViewed(
- GameViewedEvent event,
- CEPTriggerPayload payload
- void reportClicked(
- GameClickedEvent event,
- CEPTriggerPayload payload
- void reportDismissed(
- GameDismissedEvent event,
- CEPTriggerPayload payload
- void reportCepImpressed(
- CEPTriggerPayload payload
- void reportCepClicked(
- CEPTriggerPayload payload,
- String elementId
- void reportCepDismissed(
- CEPTriggerPayload payload,
- String elementId
- void reportChanceEvents(
- List<
ChanceAnalyticsEvent> events, - CEPTriggerPayload payload
- List<
- bool isPresentationAlive()?,
- GameOpenTiming? timing,
- String tierOf()?,
The full open, as in iOS GameSurface.open: the busy check, the
rewards-only SDK recheck, the skin cache-or-download, the user id check,
the presentation-alive check, the single request build, and the handoff.
Every former open path (direct, entry tap, rewards chance) funnels
through here. Never throws.
The caller supplies the platform seams so this stays testable without
a full SDK: loadSkinHtml returns the cached bundle HTML or null,
downloadSkin fetches it on a miss, present pushes the host.
isPresentationAlive fails an in-flight open as cancelled when the
owning presentation settled first (a cancelled direct trigger).
timing records the open stages for ms_to_ready and the failure
stage; a fresh one is started when omitted.
Implementation
static Future<void> open({
required CampaignModel campaign,
required GamificationConfig config,
required String chanceId,
required GameSettle settle,
required NavigatorState? navigator,
required String? userId,
required String? deviceId,
required String projectId,
required String? Function()? themeFontFamilyOf,
required Future<String?> Function(GamificationConfig config) loadSkinHtml,
required Future<String?> Function(GamificationConfig config) downloadSkin,
required Future<void> Function(GameOpenRequest request) present,
required void Function(GameOpenFailedEvent event, CEPTriggerPayload payload)
reportOpenFailed,
required void Function(CEPTriggerPayload payload, String reason)
settleDirectFailure,
void Function(GameViewedEvent event, CEPTriggerPayload payload)?
reportViewed,
void Function(GameClickedEvent event, CEPTriggerPayload payload)?
reportClicked,
void Function(GameDismissedEvent event, CEPTriggerPayload payload)?
reportDismissed,
void Function(CEPTriggerPayload payload)? reportCepImpressed,
void Function(CEPTriggerPayload payload, String elementId)?
reportCepClicked,
void Function(CEPTriggerPayload payload, String reason)? reportCepDismissed,
void Function(List<ChanceAnalyticsEvent> events, CEPTriggerPayload payload)?
reportChanceEvents,
bool Function()? isPresentationAlive,
GameOpenTiming? timing,
String Function()? tierOf,
}) async {
final campaignKey = campaign.campaignKey;
final clock = timing ?? GameOpenTiming();
clock.start();
// 1. One game owns the surface: a second open fails `busy` and never
// overwrites the live game.
if (_busy) {
_log.w('Game not opened — another game is already open',
campaign: campaignKey);
fail(
settle: settle,
campaign: campaign,
config: config,
reason: 'busy',
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
);
return;
}
clock.mark(GameOpenStage.grant);
// 2. SDK compatibility recheck, rewards only: a presentation was
// checked at routing, so only the rewards widget rechecks here.
if (settle is GameSettleRewards) {
final required = config.minSdkVersion;
if (required != null &&
(GamificationConfig.isNewer(required, packageVersion) ?? false)) {
_log.w('Game not opened — needs a newer SDK (min=$required)',
campaign: campaignKey);
fail(
settle: settle,
campaign: campaign,
config: config,
reason: 'unsupported_sdk',
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
stage: clock.stage?.wire ?? '',
);
return;
}
if (!config.bridgeSupported) {
_log.w(
'Game not opened — needs a newer SDK '
'(bridgeVersion=${config.bridgeVersion ?? 0})',
campaign: campaignKey);
fail(
settle: settle,
campaign: campaign,
config: config,
reason: 'bridge_version_unsupported',
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
stage: clock.stage?.wire ?? '',
);
return;
}
}
// 3. Skin cache hit opens at once, else download-then-show.
var warm = false;
String? html;
try {
html = await loadSkinHtml(config);
} catch (_) {
html = null;
}
if (html != null && html.isNotEmpty) {
warm = true;
} else {
_log.d(
'Gamification skin cache miss — downloading before show '
'(skinId=${config.skinId}, skinVersion=${config.skinVersion}',
campaign: campaignKey);
try {
html = await downloadSkin(config);
} catch (_) {
html = null;
}
if (html == null || html.isEmpty) {
_log.w('Game not opened — skin download failed', campaign: campaignKey);
fail(
settle: settle,
campaign: campaign,
config: config,
reason: 'skin_unavailable',
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
stage: clock.stage?.wire ?? '',
);
return;
}
}
clock.mark(GameOpenStage.download);
// 4. No user, no game.
final resolvedUserId = userId?.trim() ?? '';
if (resolvedUserId.isEmpty) {
_log.d('Game not opened — no user id', campaign: campaignKey);
fail(
settle: settle,
campaign: campaign,
config: config,
reason: 'missing_config',
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
stage: clock.stage?.wire ?? '',
);
return;
}
if (navigator == null) {
fail(
settle: settle,
campaign: campaign,
config: config,
reason: 'missing_config',
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
stage: clock.stage?.wire ?? '',
);
return;
}
// 5. A cancelled presentation cancels its in-flight game: the CEP, the
// timeline and the live-test report see `cancelled`, never a phantom.
bool alive = true;
try {
alive = isPresentationAlive?.call() ?? true;
} catch (_) {
alive = true;
}
if (!alive) {
_log.d('Game not opened — presentation settled first',
campaign: campaignKey);
fail(
settle: settle,
campaign: campaign,
config: config,
reason: 'cancelled',
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
stage: clock.stage?.wire ?? '',
);
return;
}
// 6–7. Build the request once and hand it to the presenter.
String tier = '';
try {
tier = tierOf?.call() ?? '';
} catch (_) {
tier = '';
}
final request = makeGameOpenRequest(
entryHtml: html,
chanceId: chanceId,
config: config,
campaign: campaign,
payload: payloadOf(settle, campaign),
userId: storageIdentity(userId: resolvedUserId, deviceId: deviceId ?? ''),
projectId: projectId,
themeFontFamily: themeFontFamilyOf?.call(),
settle: settle,
warm: warm,
tier: tier,
timing: clock,
reportViewed: reportViewed,
reportClicked: reportClicked,
reportDismissed: reportDismissed,
reportOpenFailed: reportOpenFailed,
reportCepImpressed: reportCepImpressed,
reportCepClicked: reportCepClicked,
reportCepDismissed: reportCepDismissed,
settleDirectFailure: settleDirectFailure,
reportChanceEvents: reportChanceEvents,
);
clock.mark(GameOpenStage.load);
try {
await present(request);
} on GameOpenAborted catch (aborted) {
// The open already settled (timeout, supersede, cancel): report it
// once with the real reason, never a push and never a second failure.
_log.w('Game not opened — ${aborted.reason} before present',
campaign: campaignKey);
fail(
settle: settle,
campaign: campaign,
config: config,
reason: aborted.reason,
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
stage: clock.stage?.wire ?? '',
);
return;
} catch (error) {
_log.w('Game not opened — present failed ($error)',
campaign: campaignKey);
fail(
settle: settle,
campaign: campaign,
config: config,
reason: 'bundle_load_failed',
chanceId: chanceId,
reportOpenFailed: reportOpenFailed,
settleDirectFailure: settleDirectFailure,
stage: clock.stage?.wire ?? '',
);
return;
}
clock.mark(GameOpenStage.commit);
}