[Huawei Developers] Freemind R:
More information like this, you can visit HUAWEI Developer Forum​

Introduction

Huawei Game service provides a centralized place for you to manage game services and configure metadata for authorizing and authenticating your game. Using Huawei game service, developer can access range of capabilities to help develop your games more efficiently.

Features

1. Developers can promote their game efficiently and quickly.

2. Developers can easily build the foundation of game by implementing features like achievements and events.

3. Developers can perform in-depth operations tailored to their game and their users.

In this article, we will implement event feature provided by Huawei game service in Tic tac toe game.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610123533.43521430452790788470752065633932:50510730063004:2800:A58313DB34112D69526B1D6235A0A025CEEF95E3CBB8854C05D15F47AEA504EC.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610123533.43521430452790788470752065633932:50510730063004:2800:A58313DB34112D69526B1D6235A0A025CEEF95E3CBB8854C05D15F47AEA504EC.gif[/IMG]​

Prerequisites

1. Developer has created an application on App Gallery Connect. Follow Creating an App.

2. Integrating app gallery connect SDK. Please refer to AppGallery Connect Service Getting Started.

3. Developer has integrated Huawei Account kit. Follow this tutorial to integrate account kit.

4. HUAWEI mobile phone with HMS Core 4.0.0.300 or later installed.

Setup:

1. Enable Huawei Game service in Manage APIS. Please refer to Service Enabling.

2. Add appgallery connect plug-in in app-level build.gradle

apply plugin: 'com.huawei.agconnect'

3. Add following dependencies in app-level build.gradle and click on Sync Now and wait till synchronization is done.

dependencies {
implementation 'com.huawei.hms:base:4.0.4.301'
implementation 'com.huawei.hms:hwid:4.0.4.300'
implementation 'com.huawei.hms:iap:4.0.4.300'
implementation 'com.huawei.hms:game:4.0.3.301'
}

Initialization

Once Initial set up is done, let’s implement Huawei game service in Tic tac toe game

1. Add the following code in onCreate() method of Application class.

public class GameServiceApplication extends Application {

@Override
public void onCreate() {
super.onCreate();
HuaweiMobileServicesUtil.setApplication(this);
}

@Override
public void onTerminate() {
super.onTerminate();
}
}

2. To initialize the game

private void init() {

JosAppsClient appsClient = JosApps.getJosAppsClient(this, null);

appsClient.init();

Log.i(TAG, "initialization success");

}

Signin In

1. When app is launched, Huawei sign-in page is displayed.

2. User enters Huawei Huawei ID and password to sign in.

3. After a successful sign in, the app obtains player information corresponding to Huawei ID.

Please refer to this article for sign in implementation.

Game Events

The Game events allow developer to collect specific data generated by players and store them on Huawei game server for AppGallery connect. In this article, we will collect data pertaining to player win. Every time players win the match, we will store it in Huawei game server.

Creating an Event in AppGallery Connect

1. Sign in to AppGallery Connect and select My apps.

2. Select an app from the app list to create an event.

3. Click the Operate tab and go to Products > Game Event. Click Create.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610091159.92479277850775550831017835746512:50510730063004:2800:C04061601E13372BB9C2B0901E486A553D01B4FA9C44FABCAFD8714B01AE95F7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610091159.92479277850775550831017835746512:50510730063004:2800:C04061601E13372BB9C2B0901E486A553D01B4FA9C44FABCAFD8714B01AE95F7.png[/IMG]​

4. Configure the event information and click Save.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610091226.62139426554536698665373402293377:50510730063004:2800:31FA1B0D5B2CE41342987BDAEB0483E358D4624BD0A47131E4FBB1F372C93F96.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610091226.62139426554536698665373402293377:50510730063004:2800:31FA1B0D5B2CE41342987BDAEB0483E358D4624BD0A47131E4FBB1F372C93F96.png[/IMG]​

5. Check your event ID on the event list and properly save the ID for event development.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610091536.56101104108968727541350270365266:50510730063004:2800:4266246480C9291F5ABCE04F7BB19E9696CB12972403AAE454F5E99D6A07EEBB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610091536.56101104108968727541350270365266:50510730063004:2800:4266246480C9291F5ABCE04F7BB19E9696CB12972403AAE454F5E99D6A07EEBB.png[/IMG]​

Implementation of Event

1. To initialize EventsClient instance

EventsClient client = Games.getEventsClient(this, mAuthHuaweiId);

mAuthHuaweiId is obtained during sign in.

2. To submit the event

client.grow(eventId, growAmount);

eventId indicates the ID of the event which is generated while defining the event in AppGallery connect. In our app we will submit the event everytime players win the match.

The growAmount parameter specifies an increment amount of the event value.

To Obtain Events

Following code can be used to obtain the events.

Task<List<Event>> task = client.getEventList(forceReload);

forceReload is a Boolean type used to indicate whether to obtain data stored on the Huawei game server or locally cached data.

Following code shows how list of events are returned.

task.addOnSuccessListener(new OnSuccessListener<List<Event>>() {
@Override
public void onSuccess(List<Event> data) {
if (data == null) {
Log.w("Event", "event is null");
return;
}
for (Event event : data) {
Log.i("Event", "event id:" + event.getEventId());
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
if (e instanceof ApiException) {
String result = "errorCode:"
+ ((ApiException) e).getStatusCode();
Log.e("Event", result);
}
}
});

We will show the retrieved events list in recyclerview. Clicking on event list item will show details about the events.

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final Event event = eventList.get(position);
final String eventId = event.getEventId();

Glide.with(context).load(event.getThumbnailUri()).into(holder.eventImage);
holder.eventName.setText(event.getName());
holder.eventAmonut.setText("value："+event.getValue() + "#" + event.getLocaleValue());
holder.eventDes.setText(event.getDescription());

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBtnClickListener.onItemClick(position);
}
});

holder.eventReport.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mBtnClickListener.reportEvent(eventId);
}
});

}

Screenshots

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610120515.24842038316438728070275078304735:50510730063004:2800:8AF87F73F3F202534B6B9772B91BD544EC86AFA1F3E566A16F45C0B3471247FB.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610120515.24842038316438728070275078304735:50510730063004:2800:8AF87F73F3F202534B6B9772B91BD544EC86AFA1F3E566A16F45C0B3471247FB.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610120640.03197412557621362312709881931225:50510730063004:2800:5759162A562154B414FDB353B5F3D2C42B61476BB204A6C0126216090AE27B86.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610120640.03197412557621362312709881931225:50510730063004:2800:5759162A562154B414FDB353B5F3D2C42B61476BB204A6C0126216090AE27B86.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610120805.56019708603215835757412608910754:50510730063004:2800:30CDFE6B936408CF5E8BD83A45951A700ACBDA498313856365EA4EB4E0BF6E56.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/940/588/2640091000001940588.20200610120805.56019708603215835757412608910754:50510730063004:2800:30CDFE6B936408CF5E8BD83A45951A700ACBDA498313856365EA4EB4E0BF6E56.jpg[/IMG]
​

Summary

In this article, we understood initial setup and game event features of Huawei Game Service. In next article, we will discuss more features provided by Huawei Game service.

Source code can be downloaded from https://github.com/DTSE-India-Community/Huawei-Game-Service