B4A Library Google Play Game Services

This is a wrapper for the Google Play Services related to games. This library was tested successfully with the API 37 of Google Play Services.

List of features:
  • Sign-in: supported;
  • Achievements: supported;
  • Leaderboards: supported;
  • Real-time multiplayer: supported (NEW!);
  • Turn-based multiplayer: supported;
  • Level and XP: supported;
  • Gifts and requests: supported;
  • Events and quests: not supported;
  • Saved games: supported;
  • Notifications: supported;
  • Player stats: supported;
  • Nearby connections: supported.
To compile, you need B4A v6+, JDK v7+ and android.jar v21+ (cf. Tools/Configure paths).

The library is provided with its Java source code and a few B4A modules:
  • ClsConnection: manages the sign-in, sign-out and connection events;
  • CodConverter: converts your data map to and from the byte array needed for the Saved Games service (this module requires three extra libraries: JSON, ByteConverter and RandomAccessFile) in case you don't own the DataCollection library;
  • CodTurnBasedMatch: helper module for turn-based matches.
Before using this library, you need to:
  • download the Google Play services SDK and Android Support Repository with the Android SDK manager;
  • copy the GooglePlayGameServices library (Jar+Xml) to your additional B4A libraries folder.
When you create a project requiring this library, you need to:
  • add this line in the project attributes of your Main module:
    B4X:
    #AdditionalJar: com.google.android.gms:play-services-games
  • add also this line if you want to access the Nearby Connections API:
    B4X:
    #AdditionalJar: com.google.android.gms:play-services-nearby
  • add these lines to your manifest:
    B4X:
    AddApplicationText(
    <meta-data android:name="com.google.android.gms.games.APP_ID"
        android:value="@string/app_id" />
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />)
  • add also these lines to the manifest if you want to access the Nearby Connections API:
    B4X:
    AddApplicationText(
    <meta-data android:name="com.google.android.gms.nearby.connection.SERVICE_ID"
        android:value="$PACKAGE$" />)
  • copy the games-ids.xml file generated by your Google Play developer console in the Objects\res\values folder.
To generate the games-ids.xml file, click on the link at the bottom of some screens (e.g. achievements) in your developer console (you need of course a game project properly configured to see it):
ressources.png


This file should look like this:
B4X:
<?xml version="1.0" encoding="utf-8"?>
<!--
Google Play game services IDs.
Save this file as res/values/games-ids.xml in your project.
-->
<resources>
  <string name="app_id">01234567890</string>
  <string name="achievement_win_on_a_very_large_map">CgkIuqeG8-kOEAIQAQ</string>
  <string name="achievement_win_on_a_large_map">CgkIuqeG8-kOEAIQAg</string>
  <string name="achievement_win_on_a_medium_map">CgkIuqeG8-kOEAIQAw</string>
  <string name="achievement_win_on_a_small_map">CgkIuqeG8-kOEAIQBA</string>
  <string name="achievement_win_on_a_very_small_map">CgkIuqeG8-kOEAIQBQ</string>
  <string name="leaderboard_main_score">CgkIuqeG8-kOEAIQBw</string>
</resources>

To set up a game in the Google Play developer console, please read this, then this.

To get details about a returned status code, please read this.
 

Attachments

  • Demos_37.zip
    71 KB · Views: 467
  • GooglePlayGameServices_372.zip
    241.3 KB · Views: 484
Last edited:

LucaMs

Expert
Licensed User
Longtime User

As you know I started (many centuries ago :D:() a turn based game and I'm having doubts about the complexity and I was considering whether to use Google services or perhaps imitate the "logic".

Given that all the logic is in single app, a hacker could ruin everything, modifying the app and publishing it on "parallel markets."

For this reason I think that a b4j server should be a better solution.
 
Last edited:

Informatix

Expert
Licensed User
Longtime User
As you know I started (many centuries ago :D:() a turn based game and I'm having doubts about the complexity and I was considering whether to use Google services or perhaps imitate the operation.

Given that all the logic is in single app, a hacker could ruin everything, changing the app and publishing it on "parallel markets."

For this reason I think that a b4j server should be a better solution.
If you delegate everything to a server (license checking, anti-cheating, etc.), then the hacker has just to hack the server to master the game (never easy, but possible and that happened very often in the game industry). If controls are done on the other players' devices (are the data valid? do they have the expected signature? are the player actions legal? etc.), I'd like to know how the hacker can defeat them. On the other side, with no server, it's more difficult to control that the player is legally registered and use a licensed copy. So maybe the good solution is a mix of both.
 

LucaMs

Expert
Licensed User
Longtime User
If you delegate everything to a server (license checking, anti-cheating, etc.), then the hacker has just to hack the server to master the game (never easy, but possible and that happened very often in the game industry). If controls are done on the other players' devices (are the data valid? do they have the expected signature? are the player actions legal? etc.), I'd like to know how the hacker can defeat them. On the other side, with no server, it's more difficult to control that the player is legally registered and use a licensed copy.
RIght.


So maybe the good solution is a mix of both.
Less right, because using a mix of both you can meet... both kind of hacker :)
 

Informatix

Expert
Licensed User
Longtime User
Less right, because using a mix of both you can meet... both kind of hacker :)
Not at all. To copy the game from one device to another or to publish it on an illegal site, there's no need to have a technical knowledge, you just have to copy the APK. You cannot avoid that, so you have to check that each client has an official license and doesn't use a "free" copy. Either you implement your own solution, with your server, or you use the Google licensing system. In both cases, you have to protect the client code against hacking.
To prevent cheating, the server-only solution is the weakest as the hacker has only to crack the server and it's the most accessible of all devices. On the contrary, in a mode where clients are able to verify the validity and legality of other players actions and data, the hacker has to be very smart and skilled to take control of the game (if your game has no big security holes). This solution is not used in a MMORPG because there are too many players involved and speed is important, but in a turn-based game, with a handful of players, it's an option to consider seriously.
 

LucaMs

Expert
Licensed User
Longtime User
you have to protect the client code against hacking
As you know, this is a hard job (I should say an impossible job).

To prevent cheating, the server-only solution is the weakest as the hacker has only to crack the server
Unlike the client, the hacker does not have the server executable code. He can attack in various ways the server or the communications, but I think this is for more skilled hackers.
 

Informatix

Expert
Licensed User
Longtime User
As you know, this is a hard job (I should say an impossible job).
That costs only 30 euros (-> ProBundle).

Unlike the client, the hacker does not have the server executable code. He can attack in various ways the server or the communications, but I think this is for more skilled hackers.
Yes, but the code he has on his device is of little interest as everything is checked by the other players, so any illegal action, any modified data will be detected by the other players. One server = one target and this target is easily accessible. Many clients = many targets and these targets are difficult to reach and hack.
 

LucaMs

Expert
Licensed User
Longtime User
That costs only 30 euros (-> ProBundle).
Yes, I know (vaguely) it, but I already have a pirated copy of your ProBundle.
As you know, I cannot write many words without kidding a little :D.

ProBundle can be very useful, I know; but I know also that sw [and Internet] cannot be 100% protected, not even Microsoft is able to protect its softwares.


Yes, but the code he has on his device is of little interest as everything is checked by the other players, so any illegal action, any modified data will be detected by the other players. One server = one target and this target is easily accessible. Many clients = many targets and these targets are difficult to reach and hack.
I know what you mean, of course: hit the operations center, it is more "efficient" :)
I have to think to how a client should be work to...etc.


That costs only 30 euros (-> ProBundle).
I would be willing to spend three times more for a b4j websocket server + B4A client, "just" the basic part, well done (OOP), allowing the management of a game like this (I refer only to the implementation of the handling of "data packages" and how to make sure that you will not have problems with clients' slow connections, Activity paused by players, and so on). Anyway, today my two neurons seems to work enough, then, if I do not fall asleep, I'll try even to... think :D


Thank you, @Informatix
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
Another thing to consider is that even if Google "speaks" of "peer-to-peer" between players this is not so, of course. Each message will go through Google's servers; this looks a lot like MQTT, it seems to me.

So I think it is better to use your own server, where you have more control and greater freedom of action.
 

LucaMs

Expert
Licensed User
Longtime User
It's true for turn-based games, not for the other modes.
Yes, I'm referring only to turn based games (selfishness, because it is what interests me :)).

And Google never pretended that there are peer-to-peer connections for turn-based games.
I read too much (and I produce too little :(). I read this phrase (P2P) in an article that was not published by Google but by an independent author who wrote about this argument.
 

LucaMs

Expert
Licensed User
Longtime User
I read too much (and I produce too little :(). I read this phrase (P2P) in an article that was not published by Google but by an independent author who wrote about this argument.
Forget it:

https://developers.google.com/games/services/common/concepts/realtimeMultiplayer

Room initialization

Internally, the room sets up a peer-to-peer mesh network between participants where clients can communicate directly with each other, rather than through the Google Play games services servers.


 

hanyelmehy

Active Member
Licensed User
Longtime User
How to get Current Player Id
something like
B4X:
mMyId = room.getParticipantId(Games.Players.getCurrentPlayerId(mGoogleApiClient));
 

hanyelmehy

Active Member
Licensed User
Longtime User
Did you look at the documentation?
Did you look at the provided demo and classes?
The answer is inside. Just search PlayerId.
Thank you for your answer ,i already was try but i was have confused between player id and player room id , now i get that player room id well created when connect to room and well be not like main player id ,i share code for reference
this code used in GPC_onRoomConnected event
B4X:
Dim GPGame As GPlayPlayers
If GPGame.IsInitialized=False Then GPGame.Initialize
Private CPlayer As GPlayPlayer       
CPlayer=GPGame.CurrentPlayer
MyRoomGameId=Room.GetParticipantId(CPlayer.PlayerId)
can you please add support for (Events)
 

hanyelmehy

Active Member
Licensed User
Longtime User
when try to use
Dim arrScores() As GPlayLeaderboardScore
it work in debug mode ,in release mode i get this error
B4X:
error: constructor LeaderboardScoreWrapper in class LeaderboardScoreWrapper cannot be applied to given types;
mostCurrent._arrscores[i0] = new flm.b4a.googleplay.LeaderboardScoreWrapper();
                             ^
  required: LeaderboardScore
  found: no arguments
  reason: actual and formal argument lists differ in length
 

Informatix

Expert
Licensed User
Longtime User
when try to use
Dim arrScores() As GPlayLeaderboardScore
it work in debug mode ,in release mode i get this error
B4X:
error: constructor LeaderboardScoreWrapper in class LeaderboardScoreWrapper cannot be applied to given types;
mostCurrent._arrscores[i0] = new flm.b4a.googleplay.LeaderboardScoreWrapper();
                             ^
  required: LeaderboardScore
  found: no arguments
  reason: actual and formal argument lists differ in length
You're not supposed to create new instances of LeaderboardScore yourself. If you really want to do that, the simplest solution is to add "= null" after your declaration.
 
Top