Expand Lumen stories and add island postcards
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import 'progress.dart';
|
||||
|
||||
/// An opportunity is not an impression. Only actual fullscreen displays count.
|
||||
class AdPacing {
|
||||
AdPacing(this.store, {DateTime Function()? clock})
|
||||
: now = clock ?? DateTime.now {
|
||||
started = now();
|
||||
}
|
||||
final Progress store;
|
||||
final DateTime Function() now;
|
||||
late final DateTime started;
|
||||
bool get allowed {
|
||||
final time = now();
|
||||
if (time.difference(started) < const Duration(minutes: 3)) return false;
|
||||
final last = DateTime.tryParse(store.text('ad_last_fullscreen'));
|
||||
if (last != null && time.difference(last) < const Duration(minutes: 3)) {
|
||||
return false;
|
||||
}
|
||||
final day = time.toIso8601String().substring(0, 10);
|
||||
return store.text('ad_cap_day') != day || store.number('ad_cap_count') < 6;
|
||||
}
|
||||
|
||||
void shown({required bool automatic}) {
|
||||
final time = now();
|
||||
store.set('ad_last_fullscreen', time.toIso8601String());
|
||||
if (!automatic) return;
|
||||
final day = time.toIso8601String().substring(0, 10);
|
||||
final count = store.text('ad_cap_day') == day
|
||||
? store.number('ad_cap_count')
|
||||
: 0;
|
||||
store.set('ad_cap_day', day);
|
||||
store.set('ad_cap_count', count + 1);
|
||||
}
|
||||
}
|
||||
+76
-9
@@ -3,9 +3,32 @@ import 'dart:async';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
|
||||
import 'ad_pacing.dart';
|
||||
import 'play_metrics.dart';
|
||||
|
||||
class Ads {
|
||||
Ads({this.enabled = true});
|
||||
Ads({this.enabled = true, this.pacing, this.metrics});
|
||||
final bool enabled;
|
||||
final AdPacing? pacing;
|
||||
final PlayMetrics? metrics;
|
||||
bool _showing = false;
|
||||
bool foreground = true;
|
||||
|
||||
void _paid(AdWithoutView ad, String format) {
|
||||
ad.onPaidEvent = (ad, value, precision, currency) {
|
||||
metrics?.paid(
|
||||
format: format,
|
||||
micros: value,
|
||||
currency: currency,
|
||||
precision: precision.name,
|
||||
test: ad.adUnitId.contains('3940256099942544'),
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
void _impression(Ad ad, String format) => metrics?.event(
|
||||
'${ad.adUnitId.contains('3940256099942544') ? 'test' : 'configured'}_${format}_impressions',
|
||||
);
|
||||
RewardedAd? _reward;
|
||||
InterstitialAd? _interstitial;
|
||||
bool _started = false,
|
||||
@@ -64,6 +87,7 @@ class Ads {
|
||||
return;
|
||||
}
|
||||
_reward = a;
|
||||
_paid(a, 'rewarded');
|
||||
_rewardAt = DateTime.now();
|
||||
},
|
||||
onAdFailedToLoad: (_) {
|
||||
@@ -90,6 +114,7 @@ class Ads {
|
||||
return;
|
||||
}
|
||||
_interstitial = a;
|
||||
_paid(a, 'interstitial');
|
||||
_interstitialAt = DateTime.now();
|
||||
},
|
||||
onAdFailedToLoad: (_) {
|
||||
@@ -101,9 +126,15 @@ class Ads {
|
||||
}
|
||||
|
||||
Future<bool> hint(VoidCallback earned) async {
|
||||
if (!supported || !await ConsentInformation.instance.canRequestAds()) {
|
||||
if (!supported ||
|
||||
_disposed ||
|
||||
_showing ||
|
||||
!foreground ||
|
||||
!await ConsentInformation.instance.canRequestAds()) {
|
||||
return false;
|
||||
}
|
||||
if (_disposed || _showing || !foreground) return false;
|
||||
metrics?.event('rewarded_opportunities');
|
||||
final a = _reward;
|
||||
_reward = null;
|
||||
if (a == null ||
|
||||
@@ -111,31 +142,58 @@ class Ads {
|
||||
DateTime.now().difference(_rewardAt!).inMinutes >= 58) {
|
||||
a?.dispose();
|
||||
unawaited(_load());
|
||||
metrics?.event('rewarded_unavailable');
|
||||
return false;
|
||||
}
|
||||
_showing = true;
|
||||
var granted = false;
|
||||
a.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdShowedFullScreenContent: (_) => pacing?.shown(automatic: false),
|
||||
onAdImpression: (ad) => _impression(ad, 'rewarded'),
|
||||
onAdDismissedFullScreenContent: (ad) {
|
||||
_showing = false;
|
||||
ad.dispose();
|
||||
_load();
|
||||
},
|
||||
onAdFailedToShowFullScreenContent: (ad, e) {
|
||||
_showing = false;
|
||||
ad.dispose();
|
||||
_load();
|
||||
},
|
||||
);
|
||||
a.show(
|
||||
onUserEarnedReward: (ad, reward) {
|
||||
earned();
|
||||
},
|
||||
);
|
||||
try {
|
||||
await a.show(
|
||||
onUserEarnedReward: (ad, reward) {
|
||||
if (granted) return;
|
||||
granted = true;
|
||||
metrics?.event('rewarded_earned');
|
||||
earned();
|
||||
},
|
||||
);
|
||||
} catch (_) {
|
||||
_showing = false;
|
||||
a.dispose();
|
||||
unawaited(_load());
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
Future<void> between(VoidCallback next) async {
|
||||
if (!supported || !await ConsentInformation.instance.canRequestAds()) {
|
||||
if (!supported ||
|
||||
_disposed ||
|
||||
_showing ||
|
||||
!foreground ||
|
||||
!(pacing?.allowed ?? true) ||
|
||||
!await ConsentInformation.instance.canRequestAds()) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
if (_disposed || _showing || !foreground) {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
metrics?.event('interstitial_opportunities');
|
||||
final a = _interstitial;
|
||||
_interstitial = null;
|
||||
if (a == null ||
|
||||
@@ -143,23 +201,32 @@ class Ads {
|
||||
DateTime.now().difference(_interstitialAt!).inMinutes >= 58) {
|
||||
a?.dispose();
|
||||
next();
|
||||
metrics?.event('interstitial_unavailable');
|
||||
unawaited(_load());
|
||||
return;
|
||||
}
|
||||
var routed = false;
|
||||
_showing = true;
|
||||
void finish() {
|
||||
if (routed) return;
|
||||
routed = true;
|
||||
_showing = false;
|
||||
a.dispose();
|
||||
next();
|
||||
_load();
|
||||
}
|
||||
|
||||
a.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdShowedFullScreenContent: (_) => pacing?.shown(automatic: true),
|
||||
onAdImpression: (ad) => _impression(ad, 'interstitial'),
|
||||
onAdDismissedFullScreenContent: (_) => finish(),
|
||||
onAdFailedToShowFullScreenContent: (_, e) => finish(),
|
||||
);
|
||||
a.show();
|
||||
try {
|
||||
await a.show();
|
||||
} catch (_) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> privacy() async {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'progress.dart';
|
||||
import '../domain/postcards.dart';
|
||||
|
||||
/// Local diagnostics only. No device identifier, upload or attribution claim.
|
||||
/// Keep currencies separate: an SDK USD receipt is never treated as EUR.
|
||||
class PlayMetrics {
|
||||
PlayMetrics(this.store, {DateTime Function()? clock})
|
||||
: now = clock ?? DateTime.now;
|
||||
final Progress store;
|
||||
final DateTime Function() now;
|
||||
Map<String, dynamic> get _data {
|
||||
final saved = store.text('play_metrics_v1');
|
||||
return saved.isEmpty
|
||||
? <String, dynamic>{}
|
||||
: Map<String, dynamic>.from(jsonDecode(saved) as Map);
|
||||
}
|
||||
|
||||
void _update(void Function(Map<String, dynamic>) edit) {
|
||||
final data = _data;
|
||||
data.putIfAbsent('firstObservedUtc', () => now().toUtc().toIso8601String());
|
||||
edit(data);
|
||||
store.set('play_metrics_v1', jsonEncode(data));
|
||||
}
|
||||
|
||||
void visit() => _update((data) {
|
||||
final stamp = now().toUtc();
|
||||
final last = DateTime.tryParse(data['lastVisitUtc'] as String? ?? '');
|
||||
if (last == null || stamp.difference(last).inMinutes >= 30) {
|
||||
data['sessions'] = (data['sessions'] as int? ?? 0) + 1;
|
||||
}
|
||||
data['lastVisitUtc'] = stamp.toIso8601String();
|
||||
final days = Map<String, dynamic>.from(data['days'] as Map? ?? {});
|
||||
days.putIfAbsent(
|
||||
stamp.toIso8601String().substring(0, 10),
|
||||
() => <String, dynamic>{},
|
||||
);
|
||||
data['days'] = days;
|
||||
});
|
||||
|
||||
void event(String name) => _update((data) {
|
||||
final days = Map<String, dynamic>.from(data['days'] as Map? ?? {});
|
||||
final day = now().toUtc().toIso8601String().substring(0, 10);
|
||||
final counts = Map<String, dynamic>.from(days[day] as Map? ?? {});
|
||||
counts[name] = (counts[name] as int? ?? 0) + 1;
|
||||
days[day] = counts;
|
||||
data['days'] = days;
|
||||
});
|
||||
|
||||
void endVisit() => _update((data) {
|
||||
data['lastVisitUtc'] = now().toUtc().toIso8601String();
|
||||
});
|
||||
|
||||
void paid({
|
||||
required String format,
|
||||
required double micros,
|
||||
required String currency,
|
||||
required String precision,
|
||||
required bool test,
|
||||
}) {
|
||||
if (!micros.isFinite || micros < 0 || currency.isEmpty) return;
|
||||
_update((data) {
|
||||
final revenue = Map<String, dynamic>.from(
|
||||
data['revenueMicros'] as Map? ?? {},
|
||||
);
|
||||
final key =
|
||||
'${test ? 'test' : 'configured'}:$format:$currency:$precision';
|
||||
revenue[key] = (revenue[key] as num? ?? 0) + micros;
|
||||
data['revenueMicros'] = revenue;
|
||||
final byDay = Map<String, dynamic>.from(
|
||||
data['dailyRevenueMicros'] as Map? ?? {},
|
||||
);
|
||||
final day = now().toUtc().toIso8601String().substring(0, 10);
|
||||
final daily = Map<String, dynamic>.from(byDay[day] as Map? ?? {});
|
||||
daily[key] = (daily[key] as num? ?? 0) + micros;
|
||||
byDay[day] = daily;
|
||||
data['dailyRevenueMicros'] = byDay;
|
||||
});
|
||||
event('${test ? 'test' : 'configured'}_${format}_paid_callbacks');
|
||||
}
|
||||
|
||||
String export() => const JsonEncoder.withIndent(' ').convert({
|
||||
'schema': 1,
|
||||
'scope': 'This installation only; observation starts with this app version. No campaign or country attribution. Configured units may still be SDK test devices. SDK revenue can be estimated or absent; reconcile with AdMob.',
|
||||
..._data,
|
||||
'patternsCompleted': store.number('solved'),
|
||||
'postcardsCompleted': islandPostcards
|
||||
.where(
|
||||
(card) => List.generate(
|
||||
3,
|
||||
(slot) => store.flag('done_${card.puzzleKey(slot)}'),
|
||||
).every((done) => done),
|
||||
)
|
||||
.length,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user