[Huawei Developers] Basavaraj.navi:
​

Introduction

Huawei Games is a group of APIs from Huawei to simplify some basic game features like leaderboards, achievements, events and online matches.

Gaming technologies are constantly evolving. Nevertheless, a lot of core gameplay elements have remained unchanged for decades. High scores, leaderboards, quests, achievements, and multiplayer support are examples. If you are developing a game for the Android platform, you don't have to implement any of those elements manually. You can simply use the Huawei Game services APIs instead.

Features of Huawei Game services

Huawei ID sign-in

Real-name authentication

Bulletins

Achievements

Events

Leaderboard

Saved games

Player statistics

Integration of Game Service

1. Configure the application on the AGC.

2. Client application development process.

Configure application on the AGC

Follow the steps.

Step 1: We need to register as a developer account in AppGallery Connect. If you are already developer ignore this step.

Step 2: Create an app by referring to Creating a Project and Creating an App in the Project. Select Game App.

Step 3: Set the data storage location based on the current location.

Step 4: Enabling Account Kit and Game Service. Open AppGallery connect, choose Manage API > Account kit and Game service.

Step 5: Generating a Signing Certificate Fingerprint.

Step 6: Configuring the Signing Certificate Fingerprint.

Step 7: Download your agconnect-services.json file, paste it into the app root directory.

Client application development process

Follow the steps.

Step 1: Create a flutter application in the Android studio (Any IDE which is your favorite).

Step 2: Add the App level Gradle dependencies. Choose inside project Android > app > build.gradle.

1

2	apply plugin: 'com.android.application'

apply plugin: 'com.huawei.agconnect'

Root level Gradle dependencies.

1

2	maven { url 'https://developer.huawei.com/repo/' }

classpath 'com.huawei.agconnect:agcp:1.4.1.300'

Step 3: Download the plugin Account kit and Game Kit.

Step 4: Add a downloaded file into the outside project directory. Declare plugin path in pubspec.yaml file under dependencies.

1

2

3

4

5

6

7	dependencies:

flutter:

sdk: flutter

huawei_account:

path: ../huawei_account/

huawei_gameservice:

path: ../huawei_gameservice/

Step 5: Build flutter application

In this example, I’m building a Tic Tac Toe game application using the Huawei Game service. And we will see the following feature in this article.

Sign In

Initialization

Getting player information

Saving player information

Sign In

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66	void signInWithHuaweiAccount() async {

HmsAuthParamHelper authParamHelper = new HmsAuthParamHelper();

authParamHelper

..setIdToken()

..setAuthorizationCode()

..setAccessToken()

..setProfile()

..setEmail()

..setScopeList([HmsScope.openId, HmsScope.email, HmsScope.profile])

..setRequestCode(8888);

try {

final HmsAuthHuaweiId accountInfo =

await HmsAuthService.signIn(authParamHelper: authParamHelper);

setState(() {

Navigator.push(

context,

MaterialPageRoute(

builder: (context) => HomePage(),

));

});

} on Exception catch (exception) {

print(exception.toString());

print("error: " + exception.toString());

}

}

void silentSignInHuaweiAccount() async {

HmsAuthParamHelper authParamHelper = new HmsAuthParamHelper();

try {

final HmsAuthHuaweiId accountInfo =

await HmsAuthService.silentSignIn(authParamHelper: authParamHelper);

if (accountInfo.unionId != null) {

print("Open Id: ${accountInfo.openId}");

print("Display name: ${accountInfo.displayName}");

print("Profile DP: ${accountInfo.avatarUriString}");

print("Email Id: ${accountInfo.email}");

Validator()

.showToast("Signed in successful as ${accountInfo.displayName}");

}

} on Exception catch (exception) {

print(exception.toString());

print('Login_provider:Can not SignIn silently');

Validator().showToast("SCan not SignIn silently ${exception.toString()}");

}

}

Future signOut() async {

final signOutResult = await HmsAuthService.signOut();

if (signOutResult) {

Validator().showToast("Signed out successful");

/* Route route = MaterialPageRoute(builder: (context) => SignInPage());

Navigator.pushReplacement(context, route);*/

} else {

print('Login_provider:signOut failed');

}

}

Future revokeAuthorization() async {

final bool revokeResult = await HmsAuthService.revokeAuthorization();

if (revokeResult) {

Validator().showToast("Revoked Auth Successfully");

} else {

Validator().showToast('Login_provider:Failed to Revoked Auth');

}

}

Users can sign in with Huawei Account. They can play games.

The above shows the sign-in with Huawei Account.

Initialization

Call the JosAppsClient.init method to initialize the game.

1

2

3	void init() async {

await JosAppsClient.init();

}

Getting player information

The below code gives the player information like playerId, openId, Player name etc. in which level user is playing.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15	Future<void> getPlayerInformation() async {

try {

Player player = await PlayersClient.getCurrentPlayer();

print("Player ID: " + player.playerId);

print("Open ID: " + player.openId);

print("Access Token: " + player.accessToken);

print("Player Name: " + player.displayName);

setState(() {

playerName = "Player Name: "+player.displayName;

savePlayerInformation(player.playerId, player.openId);

});

} on PlatformException catch (e) {

print("Error on getGamePlayer API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");

}

}

Saving player information

Player information can be updated using AppPlayerInfo. Player level, role, rank, etc. The following code shows the saving player information.

To save player information playerId and openId are mandatory.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15	void savePlayerInformation(String id, String openid) async {

try {

AppPlayerInfo info = new AppPlayerInfo(

rank: "1",

role: "beginner",

area: "2",

society: "Game",

playerId: id,

openId: openid);

await PlayersClient.savePlayerInfo(info);

} on PlatformException catch (e) {

print(

"Error on SavePlayer Info API, Error: ${e.code}, Error Description: ${GameServiceResultCodes.getStatusCodeMessage(e.code)}");

}

}

Result

Tips and Tricks

Download the latest HMS Flutter plugin.

Check dependencies downloaded properly.

Latest HMS Core APK is required.

Set minimum SDK 19 or later.

Conclusion

In this article, we have learnt the followings:

Creating an application on AGC

Integration of account kit and game service

Sign In

Initialization

Getting player information

Saving player information

In an upcoming article, I’ll come up with a new concept of game service.

Reference

Game service