[Huawei Developers] shikkerimath:
Introduction

Hello everyone. This article is second part is Huawei Game Service blog series. In the first part, I’ve give some detail about Game Service and I will give information about achievements, and events.Also I've explain how to use it in the your mobile game app with the MVVM structure.

You can find first part of the Game Service blog series on the below.

How to Use Game Service with MVVM / Part 1 — Login

View attachment 5207385

What Is Achievement?

Achievements are a great way to increase player engagement within your game and to give players greater incentives to continue playing the game. An achievement can represent a player’s accomplishments (for example, number of defeated players or completed missions) or the skills the player has acquired. You can periodically add achievements to keep the game fresh and encourage players to continue to participate.

Achievement information must have been configured in AppGallery Connect. For details, please refer to Configuring Achievements.

Before calling achievement APIs, ensure that the player has signed in.

The device must run EMUI 10.0 or later, and have HUAWEI AppAssistant 10.1 or later installed, in order to support achievements display.

To use the achievement feature, users need to enable Game Services on HUAWEI AppGallery (10.3 or later). If a user who has not enabled Game Services triggers achievement API calling, the HMS Core SDK redirects the user to the Game Services switch page on HUAWEI AppGallery and instructs the user to enable Game Services. If the user does not enable Game Services, result code 7218 is returned. Your game needs to actively instruct users to go to Me > Settings > Game Services on AppGallery and enable Game Services, so the achievement feature will be available.

How To Create An Achievement?

Achievements are created on the console. For this, firstly log-in Huawei AGC Console.

Select “My Apps” -> Your App Name -> “Operate” -> “Achievements”

In this page, you can see your achievements and you can create a new achievement by clicking “Create” button.

View attachment 5207387

After clicked “Create” button, you will see detail page. In this page you should give some information for your achievement. So, an achievement can contain the following basic attributes:

ID: A unique string generated by AppGallery Connect to identify an achievement.

Name: A short name for the achievement that you define during achievement configuration (maximum of 100 characters).

Description: A concise description of the achievement. Usually, this instructs the player how to earn the achievement (maximum of 500 characters).

Icon: Displayed after an achievement is earned. The icon must be 512 x 512 px, and in PNG or JPG format, and must not contain any words in a specific language. The HMS Core SDK will automatically generate a grayscale version icon based on this icon and use it for unlocked achievements.

Steps: Achievements can be designated as standard or incremental. An incremental achievement involves a player completing a number of steps to unlock the achievement. The predefined number of steps is known as the number of achievement steps.

List order: The order in which the current achievement appears among all of the achievements. It is designated during achievement configuration.

State: An achievement can be in one of three different states in a game.

Hidden: A hidden achievement means that details about the achievement are hidden from the player. Such achievements are equipped with a generic placeholder description and icon while in a hidden state. If an achievement contains a spoiler about your game that you would like not to reveal, you may configure the achievement as hidden and reveal it to the payer after the game reaches a certain stage.

Revealed: A revealed achievement means that the player knows about the achievement, but has not yet earned it. If you wish to show the achievement to the player at the start of the game, you can configure it to this state.

Unlocked: An unlocked achievement means that the player has successfully earned the achievement. This state is unconfigurable and must be earned by the player. After the player achieves this state, a pop-up will be displayed at the top of the game page. The HMS Core SDK allows for an achievement to be unlocked offline. When a game comes back online, it synchronizes with Huawei game server to update the achievement’s unlocked state.

View attachment 5207389

After type all of the necessary information, click the “Save” button and save. After saving, you will be see again Achievement list. And you have to click “Release” button for start to using your achievements. Also, you can edit and see details of achievements in this page. But you must wait 1–2 days for the achievements to be approved. You can login the game with your developer account and test it until it is approved. But it must wait for approval before other users can view the achievements.

Listing Achievements

1.Create Achievement List Page

Firstly, create a Xml file, and add recyclerView to list all of the achievements. You can find my design in the below.

View attachment 5207391

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:id="@+id/relativeLay"
android:layout_marginBottom="50dp">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Achievements"
android:textSize="25dp"
android:textAllCaps="false"
android:gravity="center"
android:textColor="#9A9A9B"
android:fontFamily="@font/muli_regular"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp"/>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvFavorite"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="30dp"/>

</RelativeLayout>

2. Create AchievementsViewModel class

AchievementsViewModel class will receive data from View class, processes data and send again to View class.

Firstly create a LiveData list and ArrayList to set achievemets and send to View class. Create getLiveData() and setLiveData() methods.

Finally, create a client for achievements. And create a method to get all Achievements. Add all of the achievements into arrayList. After than, set this array to LiveData list.

Yo can see AchievementsViewModel class in the below.

class AchievementsViewModel(private val context: Context): ViewModel() {

var achievementLiveData: MutableLiveData<ArrayList<Achievement>>? = null
var achievementList: ArrayList<Achievement> = ArrayList<Achievement>()

fun init() {
achievementLiveData = MutableLiveData()
setLiveData();
achievementLiveData!!.setValue(achievementList);
}

fun getLiveData(): MutableLiveData<ArrayList<Achievement>>? {
return achievementLiveData
}
fun setLiveData() {
getAllAchievements()
}

fun getAllAchievements(){
var client: AchievementsClient = Games.getAchievementsClient(context as Activity)
var task: Task<List<Achievement>> = client.getAchievementList(true)

task.addOnSuccessListener { turnedList ->
turnedList?.let {
for(achievement in it){
Log.i(Constants.ACHIEVEMENT_VIEWMODEL_TAG, "turned Value : " + "${achievement.displayName}")
achievementList.add(achievement!!)
}
achievementLiveData!!.setValue(achievementList)
}
}.addOnFailureListener {
if(it is ApiException){
Log.i(Constants.ACHIEVEMENT_VIEWMODEL_TAG, "${(it as ApiException).statusCode}")
}
}
}
}

3. Create AchievementsViewModelFactory Class

Create a viewmodel factory class and set context as parameter. This class should be return ViewModel class.

class AchievementsViewModelFactory(private val context: Context): ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return AchievementsViewModel(context) as T
}
}

4. Create Adapter Class

To list the achievements, you must create an adapter class and a custom AchievementItem design. Here, you can make a design that suits your needs and create an adapter class.

5. Create AchievementsFragment

Firstly, ViewModel dependencies should be added on Xml file. We will use it as binding object. For this, open again your Xml file and add variable name as “viewmodel” and add type as your ViewModel class directory like that.

<data>
<variable
name="viewmodel"
type="com.xxx.xxx.viewmodel.AchievementsViewModel" />
</data>

Turn back AchievemetsFragment and add factory class, viewmodel class and binding.

private lateinit var binding: FragmentAchievementsBinding
private lateinit var viewModel: AchievementsViewModel
private lateinit var viewModelFactory: AchievementsViewModelFactory

Real full article

Tips & Tricks

Remember that each success and event has a different ID. So, you must set the ID value of the event or achievement you want to use.

You must wait until the approval process is complete before all users can use the achievements and events.

Conclusion

Thanks to this article, you can create event and achievement on the console. Also, thanks to this article, you can grow and list your events.

In the next article, I will explain Leaderboard, Saved Games and give an example. Please follow fourth article for develop your game app with clean architecture.

References

HMS Game Service

Read full article here