Google Play games services, FINALLY!

This commit is contained in:
Ellpeck 2020-07-21 20:26:52 +02:00
parent 247912cbc9
commit 459ab78bee
9 changed files with 65 additions and 30 deletions

View file

@ -3,6 +3,8 @@ using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Gms.Ads;
using Android.Gms.Common;
using Android.Gms.Games;
using Android.OS;
using Android.Views;
using Android.Widget;
@ -27,6 +29,7 @@ namespace Android {
public class Activity1 : AndroidGameActivity {
private GameImpl game;
private AndroidPlatform platform;
private LinearLayout mainView;
protected override void OnCreate(Bundle bundle) {
@ -38,7 +41,8 @@ namespace Android {
// set up the game
TextInputWrapper.Current = new TextInputWrapper.Mobile();
this.game = new GameImpl(new AndroidPlatform(this, adLayout));
this.platform = new AndroidPlatform(this, adLayout);
this.game = new GameImpl(this.platform);
this.game.GraphicsDeviceManager.ResetWidthAndHeight(this.game.Window);
this.game.GraphicsDeviceManager.IsFullScreen = true;
this.game.OnLoadContent += game => {
@ -53,11 +57,11 @@ namespace Android {
this.game.GraphicsDeviceManager.PreferredBackBufferHeight = args.Bottom - args.Top;
this.game.GraphicsDeviceManager.ApplyChanges();
};
// don't render under notches
if (Build.VERSION.SdkInt >= BuildVersionCodes.P)
this.Window.Attributes.LayoutInDisplayCutoutMode = LayoutInDisplayCutoutMode.Never;
// total layout that is displayed
this.mainView = new LinearLayout(this) {Orientation = Orientation.Vertical};
this.mainView.LayoutParameters = new LinearLayout.LayoutParams(MatchParent, MatchParent);
@ -71,6 +75,13 @@ namespace Android {
this.game.Run();
}
protected override void OnActivityResult(int requestCode, Result result, Intent data) {
base.OnActivityResult(requestCode, result, data);
// Connect again after logging in to Google Play game services, but only if we haven't tried yet
if (requestCode == AndroidPlatform.GooglePlayLoginRequest && (int) result != GamesActivityResultCodes.ResultSignInFailed)
this.platform.GoogleApi.Connect();
}
public override void OnWindowFocusChanged(bool hasFocus) {
base.OnWindowFocusChanged(hasFocus);
// hide the status bar

View file

@ -82,6 +82,7 @@
<PackageReference Include="GameAnalytics.Xamarin.SDK" Version="4.1.1" />
<PackageReference Include="MonoGame.Content.Builder" Version="3.7.0.9" />
<PackageReference Include="MonoGame.Framework.Android" Version="3.7.1.189" />
<PackageReference Include="Xamarin.GooglePlayServices.Games" Version="29.0.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\TouchyTickets\TouchyTickets.csproj">

View file

@ -1,28 +1,42 @@
using System;
using System.Collections;
using System.Collections.Generic;
using Android.App;
using Android.Content;
using Android.Gms.Ads;
using Android.Net;
using Android.Gms.Common;
using Android.Gms.Common.Apis;
using Android.Gms.Extensions;
using Android.Gms.Games;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Coroutine;
using GameAnalyticsSDK;
using GameAnalyticsSDK.Utilities;
using TouchyTickets;
using Uri = Android.Net.Uri;
namespace Android {
public class AndroidPlatform : Platform {
public const int GooglePlayLoginRequest = 9001;
private readonly Activity activity;
private readonly LinearLayout adLayout;
public GoogleApiClient GoogleApi { get; private set; }
public AndroidPlatform(Activity activity, LinearLayout adLayout) {
this.activity = activity;
this.adLayout = adLayout;
}
public override void SetupAds() {
public override void SetupOnlineInteractions(Dictionary<string, object> analyticsJson) {
// Analytics
GameAnalytics.SetAutoDetectAppVersion(true);
GameAnalytics.Initialize(this.activity, GA_MiniJSON.JsonEncode(new Hashtable(analyticsJson)));
AndroidEnvironment.UnhandledExceptionRaiser += (o, args) => GameAnalytics.NewErrorEvent(GAErrorSeverity.Critical, args.Exception.ToString());
// Ads
var ad = new AdView(this.activity) {
AdUnitId = "ca-app-pub-5754829579653773/7841535920",
AdSize = AdSize.SmartBanner
@ -31,12 +45,20 @@ namespace Android {
.AddTestDevice("14B965C6457E17D2808061ADF7E34923")
.Build());
this.adLayout.AddView(ad);
}
public override void SetupAnalytics(Dictionary<string, object> json) {
GameAnalytics.SetAutoDetectAppVersion(true);
GameAnalytics.Initialize(this.activity, GA_MiniJSON.JsonEncode(new Hashtable(json)));
AndroidEnvironment.UnhandledExceptionRaiser += (o, args) => GameAnalytics.NewErrorEvent(GAErrorSeverity.Critical, args.Exception.ToString());
// Google Play game services
this.GoogleApi = new GoogleApiClient.Builder(this.activity)
.AddApi(GamesClass.API)
.AddScope(GamesClass.ScopeGames)
.AddOnConnectionFailedListener(res => {
if (res.HasResolution) {
res.StartResolutionForResult(this.activity, GooglePlayLoginRequest);
} else {
throw new GoogleApiClientConnectionException(res);
}
})
.Build();
this.GoogleApi.Connect();
}
public override void AddResourceEvent(bool sink, string currency, float amount, string itemType, string itemId) {

View file

@ -2,7 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="de.ellpeck.touchytickets" android:installLocation="auto"
android:versionCode="110" android:versionName="1.1.0">
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28"/>
<application android:label="Touchy Tickets" android:resizeableActivity="true"/>
<application android:label="Touchy Tickets" android:resizeableActivity="true">
<meta-data android:name="com.google.android.gms.games.APP_ID" android:value="\u003169609944700" />
</application>
<permission android:name="ACCESS_NETWORK_STATE"/>
<permission android:name="INTERNET"/>
</manifest>

View file

@ -112,3 +112,9 @@
/processor:FontDescriptionProcessor
/build:Fonts/Monospaced.spritefont
#begin Localization/Localization.fr.json
/copy:Localization/Localization.fr.json
#begin Localization/News.fr.json
/copy:Localization/News.fr.json

@ -1 +1 @@
Subproject commit 8428712d0a64e36334908cd25bec7dddd9abd66d
Subproject commit 99a5dcc832a1f789a0ccf4fe660568360650596d

View file

@ -42,17 +42,16 @@ namespace TouchyTickets {
}
private void LoadGame() {
// set up analytics
var settings = new Dictionary<string, object>();
settings["InfoLog"] = true;
settings["VerboseLog"] = true;
settings["ResourceCurrencies"] = new[] {"Tickets", "Stars"};
settings["ResourceItemTypes"] = new[] {"Attraction", "Restart", "Upgrade", "Modifier"};
// set up online stuff
var analytics = new Dictionary<string, object>();
analytics["InfoLog"] = true;
analytics["VerboseLog"] = true;
analytics["ResourceCurrencies"] = new[] {"Tickets", "Stars"};
analytics["ResourceItemTypes"] = new[] {"Attraction", "Restart", "Upgrade", "Modifier"};
// ios comes first, then android. For now they're the same
settings["GameKey"] = new[] {"cc18de06eebbc5d5e987c384fcd28000", "cc18de06eebbc5d5e987c384fcd28000"};
settings["SecretKey"] = new[] {"82ca1a930ee38e2383ffb02db7631e16033b511d", "82ca1a930ee38e2383ffb02db7631e16033b511d"};
this.Platform.SetupAnalytics(settings);
this.Platform.SetupAds();
analytics["GameKey"] = new[] {"cc18de06eebbc5d5e987c384fcd28000", "cc18de06eebbc5d5e987c384fcd28000"};
analytics["SecretKey"] = new[] {"82ca1a930ee38e2383ffb02db7631e16033b511d", "82ca1a930ee38e2383ffb02db7631e16033b511d"};
this.Platform.SetupOnlineInteractions(analytics);
this.Tutorial = new Tutorial();

View file

@ -3,9 +3,7 @@ using System.Collections.Generic;
namespace TouchyTickets {
public abstract class Platform {
public abstract void SetupAds();
public abstract void SetupAnalytics(Dictionary<string, object> json);
public abstract void SetupOnlineInteractions(Dictionary<string, object> analyticsJson);
public abstract void AddResourceEvent(bool sink, string currency, float amount, string itemType, string itemId);

View file

@ -4,11 +4,7 @@ using TouchyTickets;
namespace iOS {
public class IosPlatform : Platform {
public override void SetupAds() {
throw new System.NotImplementedException();
}
public override void SetupAnalytics(Dictionary<string, object> json) {
public override void SetupOnlineInteractions(Dictionary<string, object> analyticsJson) {
throw new System.NotImplementedException();
}