feat(m7a): integrate admob app id with test ad units
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flame/game.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'src/ads/ads_service.dart';
|
||||
import 'src/game/iron_arc_game.dart';
|
||||
import 'src/native/native_match.dart';
|
||||
import 'src/net/api_client.dart';
|
||||
@@ -39,6 +40,7 @@ class _MatchScreenState extends State<MatchScreen> {
|
||||
int _seed = 42;
|
||||
late final ApiClient _api = ApiClient(baseUrl: _baseUrl);
|
||||
late final OfflineQueue _queue = OfflineQueue();
|
||||
late final AdsService _ads = AdsService();
|
||||
late final MatchSession _session =
|
||||
MatchSession(client: _api, queue: _queue, deviceId: 'pending');
|
||||
late IronArcGame _game = _buildGame();
|
||||
@@ -47,10 +49,12 @@ class _MatchScreenState extends State<MatchScreen> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initSession();
|
||||
_ads.initialize();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_ads.dispose();
|
||||
_api.close();
|
||||
_session.dispose();
|
||||
super.dispose();
|
||||
@@ -84,6 +88,7 @@ class _MatchScreenState extends State<MatchScreen> {
|
||||
} else if (game.hud.value.isFinished) {
|
||||
_session.enqueueReplay(game.replayJson());
|
||||
}
|
||||
if (game.hud.value.isFinished) _ads.onMatchFinished();
|
||||
};
|
||||
return game;
|
||||
}
|
||||
@@ -112,6 +117,12 @@ class _MatchScreenState extends State<MatchScreen> {
|
||||
_toast('Replay läuft');
|
||||
}
|
||||
|
||||
Future<void> _showRewardedAd() async {
|
||||
final reward = await _ads.showRewarded();
|
||||
if (!mounted) return;
|
||||
_toast(reward == null ? 'Keine Anzeige verfügbar' : 'Belohnung: +$reward');
|
||||
}
|
||||
|
||||
Future<void> _flushQueue() async {
|
||||
final validated = await _session.flushQueue();
|
||||
if (mounted) _toast('$validated Replay(s) validiert');
|
||||
@@ -193,6 +204,7 @@ class _MatchScreenState extends State<MatchScreen> {
|
||||
onOnline: _startOnline,
|
||||
onReplay: _playReplay,
|
||||
onFlush: _flushQueue,
|
||||
onAd: _showRewardedAd,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:google_mobile_ads/google_mobile_ads.dart';
|
||||
|
||||
/// AdMob integration.
|
||||
///
|
||||
/// The **application id** lives in `AndroidManifest.xml`
|
||||
/// (`ca-app-pub-3909261635234286~8769927442`).
|
||||
///
|
||||
/// Until real ad units exist, Google's official **test units** are used on
|
||||
/// purpose: showing live ads during development produces invalid traffic and can
|
||||
/// get the AdMob account suspended.
|
||||
class AdsService {
|
||||
/// Official Google test unit for rewarded video.
|
||||
static const String rewardedUnitId =
|
||||
'ca-app-pub-3940256099942544/5224354917';
|
||||
|
||||
/// Official Google test unit for interstitials.
|
||||
static const String interstitialUnitId =
|
||||
'ca-app-pub-3940256099942544/1033173712';
|
||||
|
||||
/// Never show an interstitial during the first matches (retention).
|
||||
static const int skipFirstMatches = 3;
|
||||
|
||||
/// After that, at most every second match.
|
||||
static const int everyNthMatch = 2;
|
||||
|
||||
RewardedAd? _rewarded;
|
||||
InterstitialAd? _interstitial;
|
||||
bool _initialized = false;
|
||||
int _matchesFinished = 0;
|
||||
|
||||
bool get isInitialized => _initialized;
|
||||
bool get isRewardedReady => _rewarded != null;
|
||||
int get matchesFinished => _matchesFinished;
|
||||
|
||||
/// Initialises the SDK once and preloads both formats.
|
||||
Future<void> initialize() async {
|
||||
if (_initialized) return;
|
||||
await MobileAds.instance.initialize();
|
||||
_initialized = true;
|
||||
debugPrint('ads: SDK initialised');
|
||||
_loadRewarded();
|
||||
_loadInterstitial();
|
||||
}
|
||||
|
||||
void _loadRewarded() {
|
||||
RewardedAd.load(
|
||||
adUnitId: rewardedUnitId,
|
||||
request: const AdRequest(),
|
||||
rewardedAdLoadCallback: RewardedAdLoadCallback(
|
||||
onAdLoaded: (ad) {
|
||||
_rewarded = ad;
|
||||
debugPrint('ads: rewarded ready');
|
||||
},
|
||||
onAdFailedToLoad: (error) {
|
||||
_rewarded = null;
|
||||
debugPrint('ads: rewarded failed (${error.code}) ${error.message}');
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _loadInterstitial() {
|
||||
InterstitialAd.load(
|
||||
adUnitId: interstitialUnitId,
|
||||
request: const AdRequest(),
|
||||
adLoadCallback: InterstitialAdLoadCallback(
|
||||
onAdLoaded: (ad) {
|
||||
_interstitial = ad;
|
||||
debugPrint('ads: interstitial ready');
|
||||
},
|
||||
onAdFailedToLoad: (error) {
|
||||
_interstitial = null;
|
||||
debugPrint('ads: interstitial failed (${error.code}) ${error.message}');
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// Shows a rewarded video and returns the granted amount, or null if none was
|
||||
/// available (the caller must not grant a reward in that case).
|
||||
Future<int?> showRewarded() async {
|
||||
final ad = _rewarded;
|
||||
if (ad == null) {
|
||||
_loadRewarded();
|
||||
return null;
|
||||
}
|
||||
_rewarded = null;
|
||||
int? earned;
|
||||
ad.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdDismissedFullScreenContent: (ad) {
|
||||
ad.dispose();
|
||||
_loadRewarded();
|
||||
},
|
||||
onAdFailedToShowFullScreenContent: (ad, error) {
|
||||
debugPrint('ads: rewarded show failed ${error.code}');
|
||||
ad.dispose();
|
||||
_loadRewarded();
|
||||
},
|
||||
);
|
||||
await ad.show(onUserEarnedReward: (ad, reward) {
|
||||
earned = reward.amount.toInt();
|
||||
debugPrint('ads: reward ${reward.amount} ${reward.type}');
|
||||
});
|
||||
return earned;
|
||||
}
|
||||
|
||||
/// Called when a match ends; applies the frequency caps from the GDD.
|
||||
void onMatchFinished() {
|
||||
_matchesFinished++;
|
||||
if (_matchesFinished <= skipFirstMatches) return;
|
||||
if (_matchesFinished % everyNthMatch != 0) return;
|
||||
final ad = _interstitial;
|
||||
if (ad == null) {
|
||||
_loadInterstitial();
|
||||
return;
|
||||
}
|
||||
_interstitial = null;
|
||||
ad.fullScreenContentCallback = FullScreenContentCallback(
|
||||
onAdDismissedFullScreenContent: (ad) {
|
||||
ad.dispose();
|
||||
_loadInterstitial();
|
||||
},
|
||||
onAdFailedToShowFullScreenContent: (ad, error) {
|
||||
debugPrint('ads: interstitial show failed ${error.code}');
|
||||
ad.dispose();
|
||||
_loadInterstitial();
|
||||
},
|
||||
);
|
||||
ad.show();
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_rewarded?.dispose();
|
||||
_interstitial?.dispose();
|
||||
_rewarded = null;
|
||||
_interstitial = null;
|
||||
}
|
||||
}
|
||||
@@ -96,12 +96,14 @@ class Controls extends StatelessWidget {
|
||||
required this.onOnline,
|
||||
required this.onReplay,
|
||||
required this.onFlush,
|
||||
required this.onAd,
|
||||
});
|
||||
|
||||
final MatchSession session;
|
||||
final VoidCallback onOnline;
|
||||
final VoidCallback onReplay;
|
||||
final VoidCallback onFlush;
|
||||
final VoidCallback onAd;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -115,6 +117,7 @@ class Controls extends StatelessWidget {
|
||||
label: session.queued > 0 ? 'Senden (${session.queued})' : 'Senden',
|
||||
onTap: onFlush,
|
||||
),
|
||||
_Action(icon: Icons.ondemand_video, label: 'Ad', onTap: onAd),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user